Skip to content

Commit

Permalink
show proxy in tray menu
Browse files Browse the repository at this point in the history
  • Loading branch information
pompurin404 committed Aug 19, 2024
1 parent 87b7ed4 commit 206255d
Show file tree
Hide file tree
Showing 9 changed files with 77 additions and 33 deletions.
1 change: 1 addition & 0 deletions changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@

- 添加覆写脚本执行日志功能
- 支持查看局域网网络信息
- 支持托盘菜单显示节点信息
82 changes: 57 additions & 25 deletions src/main/resolve/tray.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,62 @@ import {
import icoIcon from '../../../resources/icon.ico?asset'
import pngIcon from '../../../resources/icon.png?asset'
import templateIcon from '../../../resources/iconTemplate.png?asset'
import { patchMihomoConfig } from '../core/mihomoApi'
import {
mihomoChangeProxy,
mihomoCloseAllConnections,
mihomoGroups,
patchMihomoConfig
} from '../core/mihomoApi'
import { mainWindow, showMainWindow } from '..'
import { app, ipcMain, Menu, nativeImage, shell, Tray } from 'electron'
import { app, Menu, nativeImage, shell, Tray } from 'electron'
import { dataDir, logDir, mihomoCoreDir, mihomoWorkDir } from '../utils/dirs'
import { triggerSysProxy } from '../sys/sysproxy'

export let tray: Tray | null = null

const buildContextMenu = async (): Promise<Menu> => {
const { mode, tun } = await getControledMihomoConfig()
const { sysProxy } = await getAppConfig()
const { sysProxy, autoCloseConnection, proxyInTray = true } = await getAppConfig()
let groupsMenu: Electron.MenuItemConstructorOptions[] = []
if (proxyInTray) {
try {
const groups = await mihomoGroups()
groupsMenu = groups.map((group) => {
return {
id: group.name,
label: group.name,
type: 'submenu',
submenu: group.all.map((proxy) => {
const delay = proxy.history.length ? proxy.history[proxy.history.length - 1].delay : -1
let displayDelay = `(${delay}ms)`
if (delay === -1) {
displayDelay = ''
}
if (delay === 0) {
displayDelay = '(Timeout)'
}
return {
id: proxy.name,
label: `${proxy.name} ${displayDelay}`,
type: 'radio',
checked: proxy.name === group.now,
click: async (): Promise<void> => {
await mihomoChangeProxy(group.name, proxy.name)
if (autoCloseConnection) {
await mihomoCloseAllConnections()
}
}
}
})
}
})
groupsMenu.unshift({ type: 'separator' })
} catch (e) {
// ignore
// 避免出错时无法创建托盘菜单
}
}

const contextMenu = [
{
id: 'show',
Expand All @@ -36,7 +81,6 @@ const buildContextMenu = async (): Promise<Menu> => {
await patchControledMihomoConfig({ mode: 'rule' })
await patchMihomoConfig({ mode: 'rule' })
mainWindow?.webContents.send('controledMihomoConfigUpdated')
await updateTrayMenu()
}
},
{
Expand All @@ -48,7 +92,6 @@ const buildContextMenu = async (): Promise<Menu> => {
await patchControledMihomoConfig({ mode: 'global' })
await patchMihomoConfig({ mode: 'global' })
mainWindow?.webContents.send('controledMihomoConfigUpdated')
await updateTrayMenu()
}
},
{
Expand All @@ -60,7 +103,6 @@ const buildContextMenu = async (): Promise<Menu> => {
await patchControledMihomoConfig({ mode: 'direct' })
await patchMihomoConfig({ mode: 'direct' })
mainWindow?.webContents.send('controledMihomoConfigUpdated')
await updateTrayMenu()
}
},
{ type: 'separator' },
Expand All @@ -77,7 +119,6 @@ const buildContextMenu = async (): Promise<Menu> => {
await patchAppConfig({ sysProxy: { enable: !enable } })
} finally {
mainWindow?.webContents.send('appConfigUpdated')
await updateTrayMenu()
}
}
},
Expand All @@ -94,9 +135,9 @@ const buildContextMenu = async (): Promise<Menu> => {
}
await patchMihomoConfig({ tun: { enable } })
mainWindow?.webContents.send('controledMihomoConfigUpdated')
await updateTrayMenu()
}
},
...groupsMenu,
{ type: 'separator' },
{
type: 'submenu',
Expand Down Expand Up @@ -152,31 +193,22 @@ export async function createTray(): Promise<void> {
if (process.platform === 'win32') {
tray = new Tray(icoIcon)
}
const menu = await buildContextMenu()

ipcMain.on('controledMihomoConfigUpdated', async () => {
await updateTrayMenu()
})
ipcMain.on('appConfigUpdated', async () => {
await updateTrayMenu()
})

tray?.setToolTip('Mihomo Party')
tray?.setContextMenu(menu)
tray?.setIgnoreDoubleClickEvents(true)
if (process.platform === 'darwin') {
if (!useDockIcon) {
app.dock.hide()
} else {
app.dock.setMenu(menu)
}
tray?.addListener('right-click', () => {
tray?.addListener('right-click', async () => {
if (mainWindow?.isVisible()) {
mainWindow?.close()
} else {
showMainWindow()
}
})
tray?.addListener('click', async () => {
await updateTrayMenu()
})
} else {
tray?.addListener('click', () => {
if (mainWindow?.isVisible()) {
Expand All @@ -185,13 +217,13 @@ export async function createTray(): Promise<void> {
showMainWindow()
}
})
tray?.addListener('right-click', async () => {
await updateTrayMenu()
})
}
}

async function updateTrayMenu(): Promise<void> {
const menu = await buildContextMenu()
if (process.platform === 'darwin') {
app.dock.setMenu(menu) // 更新dock菜单
}
tray?.setContextMenu(menu) // 更新菜单
tray?.popUpContextMenu(menu) // 弹出菜单
}
12 changes: 6 additions & 6 deletions src/main/utils/init.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,11 +109,11 @@ export async function init(): Promise<void> {
await startPacServer()
const { sysProxy } = await getAppConfig()
await triggerSysProxy(sysProxy.enable)
startCore().then(() => {
startMihomoTraffic()
setTimeout(async () => {
await initProfileUpdater()
}, 60000)
})
await startCore()
await startMihomoTraffic()
setTimeout(async () => {
await initProfileUpdater()
}, 60000)

initDeeplink()
}
1 change: 1 addition & 0 deletions src/main/utils/template.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ export const defaultConfig: IAppConfig = {
core: 'mihomo',
silentStart: false,
appTheme: 'system',
proxyInTray: true,
proxyDisplayMode: 'simple',
proxyDisplayOrder: 'default',
autoCheckUpdate: true,
Expand Down
1 change: 0 additions & 1 deletion src/renderer/src/hooks/use-app-config.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ export const useAppConfig = (listenUpdate = false): RetuenType => {
alert(e)
} finally {
mutateAppConfig()
window.electron.ipcRenderer.send('appConfigUpdated')
}
}

Expand Down
1 change: 0 additions & 1 deletion src/renderer/src/hooks/use-controled-mihomo-config.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ export const useControledMihomoConfig = (listenUpdate = false): RetuenType => {
alert(e)
} finally {
mutateControledMihomoConfig()
window.electron.ipcRenderer.send('controledMihomoConfigUpdated')
}
}

Expand Down
1 change: 1 addition & 0 deletions src/renderer/src/pages/proxies.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ const Proxies: React.FC = () => {
if (autoCloseConnection) {
await mihomoCloseAllConnections()
}
// window.electron.ipcRenderer.send('mihomoGroupsUpdated')
mutate()
}

Expand Down
10 changes: 10 additions & 0 deletions src/renderer/src/pages/settings.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ const Settings: React.FC = () => {
controlSniff = true,
useDockIcon = true,
showTraffic = true,
proxyInTray = true,
delayTestUrl,
delayTestTimeout,
autoCheckUpdate,
Expand Down Expand Up @@ -194,6 +195,15 @@ const Settings: React.FC = () => {
}}
/>
</SettingItem>
<SettingItem title="托盘菜单显示节点信息" divider>
<Switch
size="sm"
isSelected={proxyInTray}
onValueChange={(v) => {
patchAppConfig({ proxyInTray: v })
}}
/>
</SettingItem>
{platform === 'darwin' && (
<>
<SettingItem title="显示Dock图标" divider>
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 @@ -214,6 +214,7 @@ interface IAppConfig {
core: 'mihomo' | 'mihomo-alpha'
proxyDisplayMode: 'simple' | 'full'
proxyDisplayOrder: 'default' | 'delay' | 'name'
proxyInTray: boolean
siderOrder: string[]
appTheme: AppTheme
autoCheckUpdate: boolean
Expand Down

0 comments on commit 206255d

Please sign in to comment.