You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
68 lines
1.9 KiB
68 lines
1.9 KiB
import { app, shell, BrowserWindow } from 'electron'
|
|
import { join } from 'path'
|
|
import { electronApp, optimizer, is } from '@electron-toolkit/utils'
|
|
import icon from '../../resources/icon.png?asset'
|
|
import { registerIpRouter } from './ipcRouter'
|
|
import log from 'electron-log'
|
|
import autoUpdaterManager from './autoUpdaterManager'
|
|
// 开发环境日志写到项目根目录 logs 文件夹
|
|
if (is.dev) {
|
|
log.transports.file.resolvePathFn = () => `${process.cwd()}/logs/main.log`
|
|
}
|
|
function createWindow() {
|
|
const mainWindow = new BrowserWindow({
|
|
width: 1440,
|
|
height: 1000,
|
|
show: false,
|
|
autoHideMenuBar: true,
|
|
...(process.platform === 'linux' ? { icon } : {}),
|
|
webPreferences: {
|
|
preload: join(__dirname, '../preload/index.js'),
|
|
sandbox: false
|
|
}
|
|
})
|
|
mainWindow.setMenu(null)
|
|
mainWindow.on('ready-to-show', () => {
|
|
mainWindow.show()
|
|
})
|
|
|
|
mainWindow.webContents.setWindowOpenHandler((details) => {
|
|
shell.openExternal(details.url)
|
|
return { action: 'deny' }
|
|
})
|
|
|
|
if (is.dev && process.env['ELECTRON_RENDERER_URL']) {
|
|
mainWindow.loadURL(process.env['ELECTRON_RENDERER_URL'])
|
|
} else {
|
|
mainWindow.loadFile(join(__dirname, '../renderer/index.html'))
|
|
}
|
|
|
|
return mainWindow
|
|
}
|
|
|
|
app.whenReady().then(() => {
|
|
log.info('Electron 应用已启动')
|
|
electronApp.setAppUserModelId('com.freesun')
|
|
|
|
app.on('browser-window-created', (_, window) => {
|
|
optimizer.watchWindowShortcuts(window)
|
|
})
|
|
// IPC处理函数注册
|
|
registerIpRouter()
|
|
const mainWindow = createWindow()
|
|
|
|
// 初始化自动更新管理器
|
|
autoUpdaterManager.initialize(mainWindow)
|
|
app.on('activate', function () {
|
|
if (BrowserWindow.getAllWindows().length === 0) createWindow()
|
|
})
|
|
})
|
|
|
|
app.on('window-all-closed', () => {
|
|
// 清理自动更新管理器资源
|
|
autoUpdaterManager.cleanup()
|
|
|
|
if (process.platform !== 'darwin') {
|
|
app.quit()
|
|
}
|
|
})
|
|
|