Skip to content
This repository has been archived by the owner on Feb 5, 2025. It is now read-only.

Commit

Permalink
allow disable tray icon while floating window is open
Browse files Browse the repository at this point in the history
  • Loading branch information
pompurin404 committed Oct 6, 2024
1 parent 1c68538 commit a5d187b
Show file tree
Hide file tree
Showing 9 changed files with 67 additions and 11 deletions.
4 changes: 4 additions & 0 deletions changelog.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
### New Features

- 允许在开启悬浮窗的情况下禁用托盘图标

### Bug Fixes

- 修复开启轻量模式后 TrafficMonitor 重复启动的问题
Expand Down
6 changes: 4 additions & 2 deletions src/main/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -150,13 +150,15 @@ app.whenReady().then(async () => {
app.on('browser-window-created', (_, window) => {
optimizer.watchWindowShortcuts(window)
})
const { showFloatingWindow: showFloating = false } = await getAppConfig()
const { showFloatingWindow: showFloating = false, disableTray = false } = await getAppConfig()
registerIpcMainHandlers()
await createWindow()
if (showFloating) {
showFloatingWindow()
}
await createTray()
if (!disableTray) {
await createTray()
}
await initShortcut()
app.on('activate', function () {
// On macOS it's common to re-create a window in the app when the
Expand Down
12 changes: 7 additions & 5 deletions src/main/resolve/floatingWindow.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import windowStateKeeper from 'electron-window-state'
import { join } from 'path'
import { getAppConfig, patchAppConfig } from '../config'
import { applyTheme } from './theme'
import { buildContextMenu } from './tray'
import { buildContextMenu, showTrayIcon } from './tray'

export let floatingWindow: BrowserWindow | null = null

Expand Down Expand Up @@ -55,7 +55,7 @@ async function createFloatingWindow(): Promise<void> {
}
}

export function showFloatingWindow(): void {
export async function showFloatingWindow(): Promise<void> {
if (floatingWindow) {
floatingWindow.show()
} else {
Expand All @@ -66,19 +66,21 @@ export function showFloatingWindow(): void {
export async function triggerFloatingWindow(): Promise<void> {
if (floatingWindow?.isVisible()) {
await patchAppConfig({ showFloatingWindow: false })
closeFloatingWindow()
await closeFloatingWindow()
} else {
await patchAppConfig({ showFloatingWindow: true })
showFloatingWindow()
await showFloatingWindow()
}
}

export function closeFloatingWindow(): void {
export async function closeFloatingWindow(): Promise<void> {
if (floatingWindow) {
floatingWindow.close()
floatingWindow.destroy()
floatingWindow = null
}
await showTrayIcon()
await patchAppConfig({ disableTray: false })
}

export async function showContextMenu(): Promise<void> {
Expand Down
13 changes: 13 additions & 0 deletions src/main/resolve/tray.ts
Original file line number Diff line number Diff line change
Expand Up @@ -365,3 +365,16 @@ export async function copyEnv(type: 'bash' | 'cmd' | 'powershell'): Promise<void
}
}
}

export async function showTrayIcon(): Promise<void> {
if (!tray) {
await createTray()
}
}

export async function closeTrayIcon(): Promise<void> {
if (tray) {
tray.destroy()
}
tray = null
}
7 changes: 6 additions & 1 deletion src/main/utils/init.ts
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,9 @@ async function migration(): Promise<void> {
],
appTheme = 'system',
envType = [process.platform === 'win32' ? 'powershell' : 'bash'],
useSubStore = true
useSubStore = true,
showFloatingWindow = false,
disableTray = false
} = await getAppConfig()
const {
'external-controller-pipe': externalControllerPipe,
Expand Down Expand Up @@ -208,6 +210,9 @@ async function migration(): Promise<void> {
if (externalController === undefined) {
await patchControledMihomoConfig({ 'external-controller': '' })
}
if (!showFloatingWindow && disableTray) {
await patchAppConfig({ disableTray: false })
}
}

function initDeeplink(): void {
Expand Down
8 changes: 5 additions & 3 deletions src/main/utils/ipc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ import {
import { getRuntimeConfig, getRuntimeConfigStr } from '../core/factory'
import { listWebdavBackups, webdavBackup, webdavDelete, webdavRestore } from '../resolve/backup'
import { getInterfaces } from '../sys/interface'
import { copyEnv } from '../resolve/tray'
import { closeTrayIcon, copyEnv, showTrayIcon } from '../resolve/tray'
import { registerShortcut } from '../resolve/shortcut'
import { closeMainWindow, mainWindow, showMainWindow, triggerMainWindow } from '..'
import {
Expand Down Expand Up @@ -210,11 +210,13 @@ export function registerIpcMainHandlers(): void {
ipcMain.handle('isAlwaysOnTop', () => {
return mainWindow?.isAlwaysOnTop()
})
ipcMain.handle('showTrayIcon', () => ipcErrorWrapper(showTrayIcon)())
ipcMain.handle('closeTrayIcon', () => ipcErrorWrapper(closeTrayIcon)())
ipcMain.handle('showMainWindow', showMainWindow)
ipcMain.handle('closeMainWindow', closeMainWindow)
ipcMain.handle('triggerMainWindow', triggerMainWindow)
ipcMain.handle('showFloatingWindow', showFloatingWindow)
ipcMain.handle('closeFloatingWindow', closeFloatingWindow)
ipcMain.handle('showFloatingWindow', () => ipcErrorWrapper(showFloatingWindow)())
ipcMain.handle('closeFloatingWindow', () => ipcErrorWrapper(closeFloatingWindow)())
ipcMain.handle('showContextMenu', () => ipcErrorWrapper(showContextMenu)())
ipcMain.handle('openFile', (_e, type, id, ext) => openFile(type, id, ext))
ipcMain.handle('openDevTools', () => {
Expand Down
19 changes: 19 additions & 0 deletions src/renderer/src/components/settings/general-config.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
applyTheme,
checkAutoRun,
closeFloatingWindow,
closeTrayIcon,
copyEnv,
disableAutoRun,
enableAutoRun,
Expand All @@ -17,6 +18,7 @@ import {
relaunchApp,
resolveThemes,
showFloatingWindow,
showTrayIcon,
startMonitor,
writeTheme
} from '@renderer/utils/ipc'
Expand All @@ -39,6 +41,7 @@ const GeneralConfig: React.FC = () => {
useDockIcon = true,
showTraffic = true,
proxyInTray = true,
disableTray = false,
showFloatingWindow: showFloating = false,
useWindowFrame = false,
autoQuitWithoutCore = false,
Expand Down Expand Up @@ -192,6 +195,22 @@ const GeneralConfig: React.FC = () => {
}}
/>
</SettingItem>
{showFloating && (
<SettingItem title="禁用托盘图标" divider>
<Switch
size="sm"
isSelected={disableTray}
onValueChange={async (v) => {
await patchAppConfig({ disableTray: v })
if (v) {
closeTrayIcon()
} else {
showTrayIcon()
}
}}
/>
</SettingItem>
)}
{platform !== 'linux' && (
<>
<SettingItem title="托盘菜单显示节点信息" divider>
Expand Down
8 changes: 8 additions & 0 deletions src/renderer/src/utils/ipc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -327,6 +327,14 @@ export async function subStoreCollections(): Promise<ISubStoreSub[]> {
return ipcErrorWrapper(await window.electron.ipcRenderer.invoke('subStoreCollections'))
}

export async function showTrayIcon(): Promise<void> {
return ipcErrorWrapper(await window.electron.ipcRenderer.invoke('showTrayIcon'))
}

export async function closeTrayIcon(): Promise<void> {
return ipcErrorWrapper(await window.electron.ipcRenderer.invoke('closeTrayIcon'))
}

export async function showMainWindow(): Promise<void> {
return ipcErrorWrapper(await window.electron.ipcRenderer.invoke('showMainWindow'))
}
Expand Down
1 change: 1 addition & 0 deletions src/shared/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,7 @@ interface IAppConfig {
proxyCols: 'auto' | '1' | '2' | '3' | '4'
connectionDirection: 'asc' | 'desc'
connectionOrderBy: 'time' | 'upload' | 'download' | 'uploadSpeed' | 'downloadSpeed'
disableTray?: boolean
showFloatingWindow?: boolean
connectionCardStatus?: CardStatus
dnsCardStatus?: CardStatus
Expand Down

0 comments on commit a5d187b

Please sign in to comment.