This commit is contained in:
萌森 2024-07-01 16:50:17 +08:00
parent 7080571a6f
commit b5d30c67b9
5 changed files with 62 additions and 29 deletions

22
main.js
View File

@ -1,15 +1,10 @@
const { app, BrowserWindow } = require('electron') const { app } = require('electron')
const path = require('node:path') const path = require('node:path')
const server = require('./server/server'); const server = require('./server/server');
const log = require('electron-log') const log = require('electron-log')
const mainWin = require('./windows/app'); const mainWin = require('./windows/app');
if (require('electron-squirrel-startup')) {
app.quit()
return
}
app.whenReady().then(() => { app.whenReady().then(() => {
log.transports.file.encoding = 'utf8'; log.transports.file.encoding = 'utf8';
log.transports.file.file = path.join(app.getPath('userData'), 'logs/iyuu.log'); 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', () => { app.on('before-quit', () => {
mainWin.hideWindows() mainWin.closeWindows()
log.info('App Windows are closing...');
})
app.on('will-quit', () => {
server.stopServer() server.stopServer()
log.info('App is quitting...'); log.info('App is quitting...');
}) })
app.on('quit', () => { app.on('quit', () => {
app.quit() mainWin.closeWindows()
server.stopServer()
log.info('App is quitted'); log.info('App is quitted');
}) })
if (require('electron-squirrel-startup')) {
server.stopServer()
app.quit()
return
}

View File

@ -1,7 +1,10 @@
const { Menu, BrowserWindow } = require("electron") const { Menu, BrowserWindow } = require("electron")
const path = require('node:path')
const server = require('../server/server'); const server = require('../server/server');
const mainWin = require('../windows/app'); const mainWin = require('../windows/app');
const iconPath = path.join(__dirname, '..', 'iyuu.ico')
let template = [ let template = [
{ {
label: "IYUU", label: "IYUU",
@ -15,7 +18,6 @@ let template = [
{ {
label: "重启服务", label: "重启服务",
click: () => { click: () => {
mainWin.closeWindows()
server.restartServer() server.restartServer()
} }
@ -26,6 +28,7 @@ let template = [
let win = new BrowserWindow({ let win = new BrowserWindow({
width: 800, width: 800,
height: 600, height: 600,
icon: iconPath,
webPreferences: { webPreferences: {
nodeIntegration: true, nodeIntegration: true,
contextIsolation: false, contextIsolation: false,
@ -48,6 +51,7 @@ let template = [
let win = new BrowserWindow({ let win = new BrowserWindow({
width: 800, width: 800,
height: 600, height: 600,
icon: iconPath,
webPreferences: { webPreferences: {
nodeIntegration: true, nodeIntegration: true,
contextIsolation: false, contextIsolation: false,
@ -65,6 +69,7 @@ let template = [
let win = new BrowserWindow({ let win = new BrowserWindow({
width: 800, width: 800,
height: 600, height: 600,
icon: iconPath,
webPreferences: { webPreferences: {
nodeIntegration: true, nodeIntegration: true,
contextIsolation: false, contextIsolation: false,

View File

@ -1,9 +1,11 @@
const { app, Menu, Tray } = require("electron") const { app, Menu, Tray } = require("electron")
const path = require('node:path') const path = require('node:path')
const log = require('electron-log')
const server = require('../server/server'); const server = require('../server/server');
const mainWin = require('../windows/app'); 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([ const contextMenu = Menu.buildFromTemplate([
{ {
@ -12,12 +14,24 @@ const contextMenu = Menu.buildFromTemplate([
mainWin.showWindows() mainWin.showWindows()
} }
}, },
{
label: '通过浏览器打开',
click: () => {
require('electron').shell.openExternal('http://127.0.0.1:8787')
}
},
{ {
label: '重启服务', label: '重启服务',
click: () => { click: () => {
server.restartServer() server.restartServer()
} }
}, },
{
label: '强制退出',
click: () => {
app.exit(414)
}
},
{ {
label: '退出', label: '退出',
click: () => { click: () => {
@ -31,8 +45,8 @@ tray.setContextMenu(contextMenu);
tray.on('double-click', () => { tray.on('double-click', () => {
if (mainWin.visibleWindows()) { if (mainWin.visibleWindows()) {
mainWin.showWindows()
} else {
mainWin.hideWindows() mainWin.hideWindows()
} else {
mainWin.showWindows()
} }
}); });

View File

@ -79,7 +79,7 @@ function startServer() {
function stopServer() { function stopServer() {
if (serverProcess) { if (serverProcess) {
mainWin.closeWindows() mainWin.hideWindows()
log.info("Killing server process with PID:", serverProcess.pid); log.info("Killing server process with PID:", serverProcess.pid);
try { try {
process.kill(-serverProcess.pid, 'SIGTERM'); process.kill(-serverProcess.pid, 'SIGTERM');

View File

@ -1,6 +1,7 @@
const { BrowserWindow } = require('electron') const { BrowserWindow } = require('electron')
const path = require('node:path') const path = require('node:path')
const log = require('electron-log') const log = require('electron-log')
const server = require('../server/server');
const url = require('url').format({ const url = require('url').format({
protocol: 'http', protocol: 'http',
@ -12,11 +13,13 @@ const url = require('url').format({
let mainWindow = null let mainWindow = null
const createWindow = () => { const createWindow = () => {
const iconPath = path.join(__dirname, '..', 'iyuu.ico')
if (mainWindow === null) { if (mainWindow === null) {
log.info('Create main window') log.info('Create main window')
mainWindow = new BrowserWindow({ mainWindow = new BrowserWindow({
width: 800, width: 800,
height: 600, height: 600,
icon: iconPath,
webPreferences: { webPreferences: {
preload: path.join(__dirname, '..', 'preload.js') preload: path.join(__dirname, '..', 'preload.js')
} }
@ -35,7 +38,9 @@ const createWindow = () => {
}) })
mainWindow.on('close', (event) => { mainWindow.on('close', (event) => {
event.preventDefault() event.preventDefault()
if (mainWindow) {
mainWindow.hide() mainWindow.hide()
}
}) })
} else { } else {
log.info('Main window already created') log.info('Main window already created')
@ -55,43 +60,46 @@ const showWindows = () => {
const hideWindows = () => { const hideWindows = () => {
log.info('Hide main window') log.info('Hide main window')
if (!mainWindow) { if (mainWindow) {
createWindow() if (mainWindow.isDestroyed()) {
return
} }
if (visibleWindows()) { if (mainWindow.isVisible()) {
mainWindow.hide() mainWindow.hide()
} }
}
} }
const closeWindows = () => { const closeWindows = () => {
log.info('Close main window') log.info('Close main window')
if (visibleWindows()) { if (mainWindow) {
mainWindow.close() BrowserWindow.getAllWindows().forEach(window => {
mainWindow = null if (window.id !== mainWindow.id) {
window.close()
}
})
mainWindow.destroy()
} }
} }
const refreshWindows = () => { const refreshWindows = () => {
log.info('Refresh main window') log.info('Refresh main window')
hideWindows() hideWindows()
if (visibleWindows()) { if (mainWindow) {
mainWindow.reload() mainWindow.reload()
} }
} }
const refreshUrl = () => { const refreshUrl = () => {
log.info('Refresh main window') log.info('Refresh main window')
if (visibleWindows()) { if (mainWindow) {
mainWindow.loadURL(url) mainWindow.loadURL(url)
} }
} }
const rebootWindows = () => { const rebootWindows = () => {
log.info('Reboot main window') log.info('Reboot main window')
hideWindows() server.restartServer()
if (visibleWindows()) {
mainWindow.reload()
}
} }
const visibleWindows = () => { const visibleWindows = () => {