IYUUPlus-Windows/windows/app.js

102 lines
2.2 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')
const url = require('url').format({
protocol: 'http',
hostname: '127.0.0.1',
port: 8787
})
/** @type {BrowserWindow | null} */
let mainWindow = null
const createWindow = () => {
if (mainWindow === null) {
log.info('Create main window')
mainWindow = new BrowserWindow({
width: 800,
height: 600,
webPreferences: {
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()
mainWindow.hide()
})
} 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')
if (!mainWindow) {
createWindow()
}
if (visibleWindows()) {
mainWindow.hide()
}
}
const closeWindows = () => {
log.info('Close main window')
if (visibleWindows()) {
mainWindow.close()
mainWindow = null
}
}
const refreshWindows = () => {
log.info('Refresh main window')
hideWindows()
if (visibleWindows()) {
mainWindow.reload()
}
}
const refreshUrl = () => {
log.info('Refresh main window')
if (visibleWindows()) {
mainWindow.loadURL(url)
}
}
const rebootWindows = () => {
log.info('Reboot main window')
hideWindows()
if (visibleWindows()) {
mainWindow.reload()
}
}
const visibleWindows = () => {
return mainWindow && mainWindow.isVisible()
}
module.exports = { hideWindows, showWindows, createWindow, closeWindows, refreshWindows, rebootWindows, visibleWindows, refreshUrl };