electron實現相似QQ來新消息時的閃爍與任務欄窗口提醒

公司項目有一款帶即時聊天、羣組功能的APP,由於要給客服人員使用,須要開發PC版本。以前使用C#開發過一個PC版本,可是C#的UI這一塊支持的不太好,並且升級比較麻煩,我就牽頭基於Electron去實現了一個PC版本。git

 

遇到了客服那邊提過來的需求,當有新消息過來的時候,若是聊天窗口最小化了,須要有提醒,系統托盤也要像QQ同樣有新消息過來的提醒與閃爍。github

 

查了一個資料,兩個功能都實現了。api

image

image image

 

先看任務欄的提醒樣式如何實現 app

const path = require('path');
const electron = require('electron');
const {
    app,
    BrowserWindow,
    Menu,
    ipcMain,
    Tray
} = electron;

let mainWnd = null;

mainWnd = new BrowserWindow({
    minWidth: 1200,
    minHeight: 750,
    resizable: true,
    icon: 'icon.ico',
    skipTaskbar: false
});
// 開始或中止顯示窗口來得到用戶的關注
mainWnd.flashFrame(true);

 

閃爍的原理就是,用定時器更換托盤圖標的icon,一張正常、一張透明,切換(像眨眼睛同樣)。electron

let appIcon = new Tray(iconPath);

const contextMenu = Menu.buildFromTemplate([{
    label: '移除',
    click: function() {
        event.sender.send('tray-removed');
    }
}, {
    type: 'separator'
}, {
    label: 'Item1',
    type: 'radio'
}, {
    type: 'separator'
},{
    label: 'MenuItem2',
    type: 'checkbox',
    checked: true
}]);

// Make a change to the context menu
contextMenu.items[2].checked = false;

appIcon.setToolTip('在托盤中的 Electron 示例.');

appIcon.setContextMenu(contextMenu);


var count = 0;
setInterval(function() {
    if (count++ % 2 == 0) {
        appIcon.setImage(path.join(__dirname, '../img/tray/tray_icon_2.png'));
    } else {
        appIcon.setImage(path.join(__dirname, '../img/tray/tray_icon.png'));
    }
}, 400);

 

上面兩個功能並不複雜,主要是對API方法的調用。ui

 

 

參考:atom

[1] https://github.com/electron/electron/tree/master/docs-translations/zh-CN/apispa

[2] https://github.com/electron/electron/blob/master/docs-translations/zh-CN/api/browser-window.mdcode

[3] https://github.com/electron/electron/blob/master/docs-translations/zh-CN/api/tray.mdblog

[4] https://github.com/demopark/electron-api-demos-Zh_CN

[5] https://electron.atom.io/docs/

[6] https://www.w3cschool.cn/electronmanual/

相關文章
相關標籤/搜索