From b5d30c67b960afb18e6d80e0a5702bd22799a246 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=90=8C=E6=A3=AE?= Date: Mon, 1 Jul 2024 16:50:17 +0800 Subject: [PATCH] v1.0.0 --- main.js | 24 +++++++++++++++--------- menu/menu.js | 7 ++++++- menu/tray.js | 20 +++++++++++++++++--- server/server.js | 2 +- windows/app.js | 38 +++++++++++++++++++++++--------------- 5 files changed, 62 insertions(+), 29 deletions(-) diff --git a/main.js b/main.js index 7e8511d..85c7d81 100644 --- a/main.js +++ b/main.js @@ -1,15 +1,10 @@ -const { app, BrowserWindow } = require('electron') +const { app } = require('electron') const path = require('node:path') const server = require('./server/server'); const log = require('electron-log') const mainWin = require('./windows/app'); -if (require('electron-squirrel-startup')) { - app.quit() - return -} - app.whenReady().then(() => { log.transports.file.encoding = 'utf8'; log.transports.file.file = path.join(app.getPath('userData'), 'logs/iyuu.log'); @@ -32,12 +27,23 @@ app.on('window-all-closed', () => { }) app.on('before-quit', () => { - mainWin.hideWindows() + mainWin.closeWindows() + log.info('App Windows are closing...'); +}) + +app.on('will-quit', () => { server.stopServer() log.info('App is quitting...'); }) app.on('quit', () => { - app.quit() + mainWin.closeWindows() + server.stopServer() log.info('App is quitted'); -}) \ No newline at end of file +}) + +if (require('electron-squirrel-startup')) { + server.stopServer() + app.quit() + return +} \ No newline at end of file diff --git a/menu/menu.js b/menu/menu.js index 28b0490..0462e13 100644 --- a/menu/menu.js +++ b/menu/menu.js @@ -1,7 +1,10 @@ const { Menu, BrowserWindow } = require("electron") +const path = require('node:path') const server = require('../server/server'); const mainWin = require('../windows/app'); +const iconPath = path.join(__dirname, '..', 'iyuu.ico') + let template = [ { label: "IYUU", @@ -15,7 +18,6 @@ let template = [ { label: "重启服务", click: () => { - mainWin.closeWindows() server.restartServer() } @@ -26,6 +28,7 @@ let template = [ let win = new BrowserWindow({ width: 800, height: 600, + icon: iconPath, webPreferences: { nodeIntegration: true, contextIsolation: false, @@ -48,6 +51,7 @@ let template = [ let win = new BrowserWindow({ width: 800, height: 600, + icon: iconPath, webPreferences: { nodeIntegration: true, contextIsolation: false, @@ -65,6 +69,7 @@ let template = [ let win = new BrowserWindow({ width: 800, height: 600, + icon: iconPath, webPreferences: { nodeIntegration: true, contextIsolation: false, diff --git a/menu/tray.js b/menu/tray.js index 0913b93..c7e8a77 100644 --- a/menu/tray.js +++ b/menu/tray.js @@ -1,9 +1,11 @@ const { app, Menu, Tray } = require("electron") const path = require('node:path') +const log = require('electron-log') const server = require('../server/server'); const mainWin = require('../windows/app'); -const tray = new Tray('iyuu.ico'); +const iconPath = path.join(__dirname, '..', 'iyuu.ico') +const tray = new Tray(iconPath); const contextMenu = Menu.buildFromTemplate([ { @@ -12,12 +14,24 @@ const contextMenu = Menu.buildFromTemplate([ mainWin.showWindows() } }, + { + label: '通过浏览器打开', + click: () => { + require('electron').shell.openExternal('http://127.0.0.1:8787') + } + }, { label: '重启服务', click: () => { server.restartServer() } }, + { + label: '强制退出', + click: () => { + app.exit(414) + } + }, { label: '退出', click: () => { @@ -31,8 +45,8 @@ tray.setContextMenu(contextMenu); tray.on('double-click', () => { if (mainWin.visibleWindows()) { - mainWin.showWindows() - } else { mainWin.hideWindows() + } else { + mainWin.showWindows() } }); \ No newline at end of file diff --git a/server/server.js b/server/server.js index b134b36..f7545d2 100644 --- a/server/server.js +++ b/server/server.js @@ -79,7 +79,7 @@ function startServer() { function stopServer() { if (serverProcess) { - mainWin.closeWindows() + mainWin.hideWindows() log.info("Killing server process with PID:", serverProcess.pid); try { process.kill(-serverProcess.pid, 'SIGTERM'); diff --git a/windows/app.js b/windows/app.js index 46eaad2..518d629 100644 --- a/windows/app.js +++ b/windows/app.js @@ -1,6 +1,7 @@ const { BrowserWindow } = require('electron') const path = require('node:path') const log = require('electron-log') +const server = require('../server/server'); const url = require('url').format({ protocol: 'http', @@ -12,11 +13,13 @@ const url = require('url').format({ let mainWindow = null const createWindow = () => { + const iconPath = path.join(__dirname, '..', 'iyuu.ico') if (mainWindow === null) { log.info('Create main window') mainWindow = new BrowserWindow({ width: 800, height: 600, + icon: iconPath, webPreferences: { preload: path.join(__dirname, '..', 'preload.js') } @@ -35,7 +38,9 @@ const createWindow = () => { }) mainWindow.on('close', (event) => { event.preventDefault() - mainWindow.hide() + if (mainWindow) { + mainWindow.hide() + } }) } else { log.info('Main window already created') @@ -55,43 +60,46 @@ const showWindows = () => { const hideWindows = () => { log.info('Hide main window') - if (!mainWindow) { - createWindow() - } - if (visibleWindows()) { - mainWindow.hide() + if (mainWindow) { + if (mainWindow.isDestroyed()) { + return + } + if (mainWindow.isVisible()) { + mainWindow.hide() + } } } const closeWindows = () => { log.info('Close main window') - if (visibleWindows()) { - mainWindow.close() - mainWindow = null + if (mainWindow) { + BrowserWindow.getAllWindows().forEach(window => { + if (window.id !== mainWindow.id) { + window.close() + } + }) + mainWindow.destroy() } } const refreshWindows = () => { log.info('Refresh main window') hideWindows() - if (visibleWindows()) { + if (mainWindow) { mainWindow.reload() } } const refreshUrl = () => { log.info('Refresh main window') - if (visibleWindows()) { + if (mainWindow) { mainWindow.loadURL(url) } } const rebootWindows = () => { log.info('Reboot main window') - hideWindows() - if (visibleWindows()) { - mainWindow.reload() - } + server.restartServer() } const visibleWindows = () => {