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.
This commit is contained in:
Junjia 2025-03-22 23:35:35 +08:00 committed by GitHub
parent d763e93984
commit 36746074da
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 26 additions and 3 deletions

View File

@ -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<void> {
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)

View File

@ -309,7 +309,7 @@ export async function createTray(): Promise<void> {
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<void> {
}
tray = null
}
export async function showDockIcon(): Promise<void> {
if (process.platform === 'darwin' && !app.dock.isVisible()) {
await app.dock.show()
}
}
export async function hideDockIcon(): Promise<void> {
if (process.platform === 'darwin' && app.dock.isVisible()) {
app.dock.hide()
}
}