菜单自定义

This commit is contained in:
yj 2025-01-24 11:11:43 +08:00
parent c71ea10a4b
commit 7fd79873f3
1 changed files with 41 additions and 23 deletions

64
main.js
View File

@ -29,6 +29,7 @@ let regKey;
let connection = null;
let envStr;
let startNumber = 0;
let isShare = true;
powerSaveBlocker.start('prevent-display-sleep')
const id = powerSaveBlocker.start('prevent-display-sleep')
powerSaveBlocker.stop(id)
@ -66,33 +67,12 @@ class AppWindow extends BrowserWindow {
function quit() {
app.quit()
}
let tray;
function createTray() {
const iconPath = `${__dirname}/src/assets/${updateJs.getIcon(envStr)}.png`;
const trayIcon = nativeImage.createFromPath(iconPath);
const tray = new Tray(trayIcon);
const contextMenu = Menu.buildFromTemplate([
{
label: '打开', click: () => {
mainWindow.webContents.send('isOpenWindows');
},
// icon: iconPath,
},
{
label: '最小化到系统托盘', click: () => {
mainWindow.hide();
},
// icon: iconPath,
},
{
label: '退出', click: async () => {
quit()
},
// icon: iconPath,
},
]);
tray = new Tray(trayIcon);
tray.setToolTip(updateJs.getTitle(envStr));
tray.setContextMenu(contextMenu);
tray.on('click', () => {
mainWindow.webContents.send('isOpenWindows');
});
@ -101,6 +81,13 @@ function createTray() {
function createWindow() {
mainWindow = new AppWindow();
mainWindow.focus();
mainWindow.hookWindowMessage(278, function (e) {
mainWindow.setEnabled(false);//窗口禁用
setTimeout(() => {
mainWindow.setEnabled(true);//窗口启用
}, 100);
return true;
})
}
const additionalData = { myKey: 'myValue' }
app.on('ready', () => {
@ -513,10 +500,34 @@ app.on('ready', () => {
// 设置桌面应用基础属性
ipcMain.handle('setMainWindowSize', (event, config) => {
if (config.width === 250) {
const contextMenu = Menu.buildFromTemplate([
{
label: '退出',
click: () => quit(),
},
]);
tray.setContextMenu(contextMenu);
mainWindow.setSkipTaskbar(true)
mainWindow.setResizable(false)
mainWindow.setAlwaysOnTop(true, 'screen-saver')
} else {
const contextMenu = Menu.buildFromTemplate([
{
label: '打开',
click: () => mainWindow.webContents.send('isOpenWindows'),
},
{
label: '最小化到系统托盘',
click: () => mainWindow.hide(),
},
{
label: '退出',
click: () => quit(),
},
]);
tray.setContextMenu(contextMenu);
}
console.log(isShare);
// 设置最小窗口尺寸
mainWindow.setMinimumSize(config.width, config.height);
// 设置最大尺寸
@ -580,6 +591,13 @@ app.on('ready', () => {
// 测试 | 生产
child.loadURL(`file://${path.join(__dirname, './dist/index.html')}#/${config.key}`);
}
child.hookWindowMessage(278, function (e) {
child.setEnabled(false);//窗口禁用
setTimeout(() => {
child.setEnabled(true);//窗口启用
}, 100);
return true;
})
childWindow[config.key] = child
child.once('ready-to-show', () => {
if (config.show) {