refactor(ipc): simplify launch on boot handling and improve WindowService logic

- Updated the IPC handler for setting launch on boot to directly use the boolean parameter for openAtLogin.
- Cleaned up the WindowService logic to enhance readability and maintainability, including minor adjustments to the tray behavior on macOS.
This commit is contained in:
kangfenmao 2025-03-23 13:22:54 +08:00
parent f9941a6858
commit 640ca19cba
2 changed files with 10 additions and 12 deletions

View File

@ -70,19 +70,11 @@ export function registerIpc(mainWindow: BrowserWindow, app: Electron.App) {
}) })
// launch on boot // launch on boot
ipcMain.handle('app:set-launch-on-boot', (_, isActive: boolean) => { ipcMain.handle('app:set-launch-on-boot', (_, openAtLogin: boolean) => {
// Set login item settings for windows and mac // Set login item settings for windows and mac
// linux is not supported because it requires more file operations // linux is not supported because it requires more file operations
if (isWin || isMac) { if (isWin || isMac) {
if (isActive) { app.setLoginItemSettings({ openAtLogin })
app.setLoginItemSettings({
openAtLogin: true
})
} else {
app.setLoginItemSettings({
openAtLogin: false
})
}
} }
}) })

View File

@ -261,6 +261,7 @@ export class WindowService {
// 托盘及关闭行为设置 // 托盘及关闭行为设置
const isShowTray = configManager.getTray() const isShowTray = configManager.getTray()
const isTrayOnClose = configManager.getTrayOnClose() const isTrayOnClose = configManager.getTrayOnClose()
// 没有开启托盘,或者开启了托盘,但设置了直接关闭,应执行直接退出 // 没有开启托盘,或者开启了托盘,但设置了直接关闭,应执行直接退出
if (!isShowTray || (isShowTray && !isTrayOnClose)) { if (!isShowTray || (isShowTray && !isTrayOnClose)) {
// 如果是Windows或Linux直接退出 // 如果是Windows或Linux直接退出
@ -269,8 +270,8 @@ export class WindowService {
return app.quit() return app.quit()
} }
} }
//上述逻辑以下,是“开启托盘+设置关闭时最小化到托盘”的情况
//上述逻辑以下,是“开启托盘+设置关闭时最小化到托盘”的情况
// 如果是Windows或Linux且处于全屏状态则退出应用 // 如果是Windows或Linux且处于全屏状态则退出应用
if (this.wasFullScreen) { if (this.wasFullScreen) {
if (isWin || isLinux) { if (isWin || isLinux) {
@ -281,9 +282,13 @@ export class WindowService {
return return
} }
} }
event.preventDefault() event.preventDefault()
mainWindow.hide() mainWindow.hide()
if (isMac && isTrayOnClose) {
app.dock?.hide() //for mac to hide to tray app.dock?.hide() //for mac to hide to tray
}
}) })
mainWindow.on('closed', () => { mainWindow.on('closed', () => {
@ -312,6 +317,7 @@ export class WindowService {
this.mainWindow = this.createMainWindow() this.mainWindow = this.createMainWindow()
this.mainWindow.focus() this.mainWindow.focus()
} }
//for mac users, when window is shown, should show dock icon (dock may be set to hide when launch) //for mac users, when window is shown, should show dock icon (dock may be set to hide when launch)
app.dock?.show() app.dock?.show()
} }