IYUUPlus-Windows/windows/app.js

111 lines
2.5 KiB
JavaScript
Raw Normal View History

2024-07-01 14:08:06 +08:00
const { BrowserWindow } = require('electron')
const path = require('node:path')
const log = require('electron-log')
2024-07-01 16:50:17 +08:00
const server = require('../server/server');
2024-07-01 14:08:06 +08:00
const url = require('url').format({
protocol: 'http',
hostname: '127.0.0.1',
port: 8787
})
/** @type {BrowserWindow | null} */
let mainWindow = null
const createWindow = () => {
2024-07-01 16:50:17 +08:00
const iconPath = path.join(__dirname, '..', 'iyuu.ico')
2024-07-01 14:08:06 +08:00
if (mainWindow === null) {
log.info('Create main window')
mainWindow = new BrowserWindow({
width: 800,
height: 600,
2024-07-01 16:50:17 +08:00
icon: iconPath,
2024-07-01 14:08:06 +08:00
webPreferences: {
2024-07-02 16:26:48 +08:00
webSecurity: false,
2024-07-01 14:08:06 +08:00
preload: path.join(__dirname, '..', 'preload.js')
}
})
// mainWindow.webContents.openDevTools()
require("../menu/menu")
mainWindow.webContents.loadURL(url)
// mainWindow.loadURL(url)
mainWindow.on('minimize', (event) => {
event.preventDefault()
mainWindow.hide()
})
mainWindow.on('close', (event) => {
event.preventDefault()
2024-07-01 16:50:17 +08:00
if (mainWindow) {
mainWindow.hide()
}
2024-07-01 14:08:06 +08:00
})
} else {
log.info('Main window already created')
}
}
const showWindows = () => {
log.info('Show main window')
if (!mainWindow) {
createWindow()
return
}
if (!mainWindow.isVisible()) {
mainWindow.show()
}
}
const hideWindows = () => {
log.info('Hide main window')
2024-07-01 16:50:17 +08:00
if (mainWindow) {
if (mainWindow.isDestroyed()) {
return
}
if (mainWindow.isVisible()) {
mainWindow.hide()
}
2024-07-01 14:08:06 +08:00
}
}
const closeWindows = () => {
log.info('Close main window')
2024-07-01 16:50:17 +08:00
if (mainWindow) {
BrowserWindow.getAllWindows().forEach(window => {
if (window.id !== mainWindow.id) {
window.close()
}
})
mainWindow.destroy()
2024-07-01 14:08:06 +08:00
}
}
const refreshWindows = () => {
log.info('Refresh main window')
hideWindows()
2024-07-01 16:50:17 +08:00
if (mainWindow) {
2024-07-01 14:08:06 +08:00
mainWindow.reload()
}
}
const refreshUrl = () => {
log.info('Refresh main window')
2024-07-01 16:50:17 +08:00
if (mainWindow) {
2024-07-01 14:08:06 +08:00
mainWindow.loadURL(url)
}
}
const rebootWindows = () => {
log.info('Reboot main window')
2024-07-01 16:50:17 +08:00
server.restartServer()
2024-07-01 14:08:06 +08:00
}
const visibleWindows = () => {
return mainWindow && mainWindow.isVisible()
}
module.exports = { hideWindows, showWindows, createWindow, closeWindows, refreshWindows, rebootWindows, visibleWindows, refreshUrl };