系统托盘

This commit is contained in:
萌森 2024-06-29 00:41:06 +08:00
parent 7d2e569fd8
commit 0982fdd16e
6 changed files with 126 additions and 33 deletions

View File

@ -4,30 +4,38 @@ const { FuseV1Options, FuseVersion } = require('@electron/fuses');
module.exports = {
packagerConfig: {
asar: true,
icon: 'iyuu.png',
icon: 'iyuu',
extraResource: [
'run/',
'iyuu/',
"run.bat"
]
},
rebuildConfig: {},
makers: [
{
name: '@electron-forge/maker-squirrel',
config: {},
config: {
setupIcon: 'iyuu.ico',
},
},
{
name: '@electron-forge/maker-zip',
platforms: ['darwin'],
config: {
icon: 'iyuu.icns',
}
},
{
name: '@electron-forge/maker-deb',
config: {},
config: {
icon: 'iyuu.png',
},
},
{
name: '@electron-forge/maker-rpm',
config: {},
config: {
icon: 'iyuu.png',
},
},
],
plugins: [

BIN
iyuu.ico Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 47 KiB

49
main.js
View File

@ -15,13 +15,34 @@ const createWindow = () => {
webPreferences: {
nodeIntegration: true,
contextIsolation: false,
devTools: false,
webSecurity: false,
allowRunningInsecureContent: true,
preload: path.join(__dirname, 'preload.js')
}
})
require("./menu/menu.js")
require("./menu/tray.js")
win.loadURL('http://127.0.0.1:8787')
const url = require('url').format({
protocol: 'http',
slashes: true,
hostname: '127.0.0.1',
port: 8787
})
win.loadURL(url)
win.on('minimize', (event) => {
event.preventDefault()
win.hide()
})
win.on('close', (event) => {
event.preventDefault()
win.hide()
})
}
app.whenReady().then(() => {
@ -35,17 +56,31 @@ app.whenReady().then(() => {
setTimeout(() => {
createWindow()
app.on('activate', () => {
if (BrowserWindow.getAllWindows().length === 0) createWindow()
})
}, 5000)
})
app.on('window-all-closed', () => {
server.stopServer()
if (process.platform !== 'darwin') app.quit()
app.on('activate', () => {
if (BrowserWindow.getAllWindows().length === 0) {
createWindow()
} else {
app.show()
BrowserWindow.getAllWindows().forEach(win => {
win.show()
})
}
})
app.on('window-all-closed', () => {
BrowserWindow.getAllWindows().forEach(win => {
win.hide()
})
app.hide()
})
app.on('before-quit', () => {
server.stopServer()
log.info('App is quitting...');
})
process.on('uncaughtException', (error) => {
log.error('Uncaught Exception:', error);

View File

@ -5,6 +5,14 @@ let template = [
{
label: "IYUU",
submenu: [
{
label: "刷新",
click: () => {
BrowserWindow.getAllWindows().forEach(win => {
win.reload()
})
}
},
{
label: "重启服务",
click: () => {
@ -13,7 +21,7 @@ let template = [
BrowserWindow.getAllWindows().forEach(win => {
win.reload()
})
}, 1000)
}, 2000)
}
},
@ -39,6 +47,23 @@ let template = [
{
label: "帮助",
submenu: [
{
label: "问题反馈",
click: () => {
let win = new BrowserWindow({
width: 800,
height: 600,
webPreferences: {
nodeIntegration: true,
contextIsolation: false,
}
});
win.loadURL("https://gitee.com/qyg2297248353/iyuuplus-windows/issues");
win.on("closed", () => {
win = null;
});
}
},
{
label: "新疆萌森软件开发工作室提供技术支持",
click: () => {
@ -57,21 +82,7 @@ let template = [
}
},
{
label: "问题反馈",
click: () => {
let win = new BrowserWindow({
width: 800,
height: 600,
webPreferences: {
nodeIntegration: true,
contextIsolation: false,
}
});
win.loadURL("https://gitee.com/qyg2297248353/iyuuplus-windows/issues");
win.on("closed", () => {
win = null;
});
}
label: "关于"
}
]
}

43
menu/tray.js Normal file
View File

@ -0,0 +1,43 @@
const { app, Menu, Tray, BrowserWindow } = require("electron")
const path = require('node:path')
const server = require('../server/server');
const tray = new Tray('iyuu.ico');
const contextMenu = Menu.buildFromTemplate([{
label: '打开主窗口',
click: () => {
BrowserWindow.getAllWindows().forEach(win => {
win.show()
})
}
}, {
label: '重启服务',
click: () => {
BrowserWindow.getAllWindows().forEach(win => {
win.hide()
})
server.restartServer()
setTimeout(() => {
BrowserWindow.getAllWindows().forEach(win => {
win.show()
win.reload()
})
}, 1000)
}
},
{
label: '退出',
click: () => {
app.quit()
}
}
]);
tray.setToolTip('IYUU');
tray.setContextMenu(contextMenu);
tray.on('double-click', () => {
win.isVisible() ? win.hide() : win.show()
win.isVisible() ? win.setSkipTaskbar(false) : win.setSkipTaskbar(true);
});

View File

@ -28,10 +28,7 @@ function startServer() {
env.Path = `${phpDir};${env.Path}`;
const workingDir = path.resolve(path.join(resourcePath, 'iyuu'));
const batFile = path.join('windows.bat');
// batFile 父目录
log.info(`[IYUU] 工作目录: ${workingDir}`);
// serverProcess = exec(`cmd /c ${batPath}`, {
@ -93,7 +90,6 @@ function startServer() {
serverProcess.on("close", function (code) {
log.info("[IYUU] 服务退出:" + code);
restartServer();
});
}