Emperor_Shun_Reading/electron/addon/tray/index.js

85 lines
2.1 KiB
JavaScript
Raw Normal View History

2024-08-22 01:03:01 +00:00
const { Tray, Menu, app ,BrowserWindow} = require('electron');
const path = require('path');
const Ps = require('ee-core/ps');
const Log = require('ee-core/log');
const Electron = require('ee-core/electron');
const CoreWindow = require('ee-core/electron/window');
const Conf = require('ee-core/config');
const EE = require('ee-core/ee');
/**
* 托盘插件
* @class
*/
class TrayAddon {
constructor() {
this.tray = null;
}
/**
* 创建托盘
*/
create () {
// 开发环境,代码热更新开启时,会导致托盘中有残影
if (Ps.isDev() && Ps.isHotReload()) return;
Log.info('[addon:tray] load');
const { CoreApp } = EE;
const cfg = Conf.getValue('addons.tray');
const mainWindow = CoreWindow.getMainWindow();
// 托盘图标
let iconPath = path.join(Ps.getHomeDir(), cfg.icon);
// 托盘菜单功能列表
let trayMenuTemplate = [
{
label: '显示',
click: function () {
mainWindow.show();
}
},
{
label: '退出',
click: function () {
console.log('exit click 事件')
// CoreApp.appQuit();
app.quit()
}
}
]
// 点击关闭,最小化到托盘 这里要是阻止关闭就完全无法关闭了
mainWindow.on('close', (event) => {
console.log('close 事件')
console.log(event.sender)
// if (Electron.extra.closeWindow == true) {
// return;
// }
// mainWindow.hide();
// 禁止关闭
// event.preventDefault();
//todo: 这个变量控制的方法来区分是否退出 还是不退出
if(global.isUserExit == false){
event.preventDefault();
}else{
app.quit()
}
});
// 实例化托盘
this.tray = new Tray(iconPath);
this.tray.setToolTip(cfg.title);
const contextMenu = Menu.buildFromTemplate(trayMenuTemplate);
this.tray.setContextMenu(contextMenu);
// 左键单击的时候能够显示主窗口
this.tray.on('click', () => {
mainWindow.show()
})
}
}
TrayAddon.toString = () => '[class TrayAddon]';
module.exports = TrayAddon;