From 36746074da9301e49c5c4f82f50b07dc6872aa8e Mon Sep 17 00:00:00 2001 From: Junjia Date: Sat, 22 Mar 2025 23:35:35 +0800 Subject: [PATCH] feat: Add dynamic Dock icon visibility for macOS (#594) Implemented `showDockIcon` and `hideDockIcon` to toggle the Dock icon visibility based on user preferences. Adjusted event handlers to respect the `useDockIcon` configuration setting, enhancing macOS-specific behavior and user experience. --- src/main/index.ts | 15 +++++++++++++-- src/main/resolve/tray.ts | 14 +++++++++++++- 2 files changed, 26 insertions(+), 3 deletions(-) diff --git a/src/main/index.ts b/src/main/index.ts index ed5fced..e7287a6 100644 --- a/src/main/index.ts +++ b/src/main/index.ts @@ -6,7 +6,7 @@ import { addProfileItem, getAppConfig, patchAppConfig } from './config' import { quitWithoutCore, startCore, stopCore } from './core/manager' import { triggerSysProxy } from './sys/sysproxy' import icon from '../../resources/icon.png?asset' -import { createTray } from './resolve/tray' +import { createTray, hideDockIcon, showDockIcon } from './resolve/tray' import { init } from './utils/init' import { join } from 'path' import { initShortcut } from './resolve/shortcut' @@ -269,10 +269,21 @@ export async function createWindow(): Promise { mainWindow?.webContents.reload() }) + mainWindow.on('show', () => { + showDockIcon() + }) + mainWindow.on('close', async (event) => { event.preventDefault() mainWindow?.hide() - const { autoQuitWithoutCore = false, autoQuitWithoutCoreDelay = 60 } = await getAppConfig() + const { + autoQuitWithoutCore = false, + autoQuitWithoutCoreDelay = 60, + useDockIcon = true + } = await getAppConfig() + if (!useDockIcon) { + hideDockIcon() + } if (autoQuitWithoutCore) { if (quitTimeout) { clearTimeout(quitTimeout) diff --git a/src/main/resolve/tray.ts b/src/main/resolve/tray.ts index fbee02a..f9410d9 100644 --- a/src/main/resolve/tray.ts +++ b/src/main/resolve/tray.ts @@ -309,7 +309,7 @@ export async function createTray(): Promise { tray?.setIgnoreDoubleClickEvents(true) if (process.platform === 'darwin') { if (!useDockIcon) { - app.dock.hide() + hideDockIcon() } ipcMain.on('trayIconUpdate', async (_, png: string) => { const image = nativeImage.createFromDataURL(png).resize({ height: 16 }) @@ -387,3 +387,15 @@ export async function closeTrayIcon(): Promise { } tray = null } + +export async function showDockIcon(): Promise { + if (process.platform === 'darwin' && !app.dock.isVisible()) { + await app.dock.show() + } +} + +export async function hideDockIcon(): Promise { + if (process.platform === 'darwin' && app.dock.isVisible()) { + app.dock.hide() + } +}