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

24
main.js
View File

@ -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');
})
})
if (require('electron-squirrel-startup')) {
server.stopServer()
app.quit()
return
}

View File

@ -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,

View File

@ -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()
}
});

View File

@ -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');

View File

@ -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 = () => {