fix tray crash on linux

This commit is contained in:
pompurin404 2024-08-19 13:41:13 +08:00
parent 940bed5401
commit 469e9d2818
No known key found for this signature in database
8 changed files with 51 additions and 23 deletions

View File

@ -1,5 +1,3 @@
### New Features ### Bug Fixes
- 添加覆写脚本执行日志功能 - 修复托盘菜单在Linux下崩溃的问题
- 支持查看局域网网络信息
- 支持托盘菜单显示节点信息

View File

@ -193,13 +193,15 @@ const mihomoTraffic = async (): Promise<void> => {
} else { } else {
tray?.setTitle('') tray?.setTitle('')
} }
if (process.platform !== 'linux') {
tray?.setToolTip( tray?.setToolTip(
'↑' + '↑' +
`${calcTraffic(json.up)}/s`.padStart(12) + `${calcTraffic(json.up)}/s`.padStart(12) +
'\n↓' + '\n↓' +
`${calcTraffic(json.down)}/s`.padStart(12) `${calcTraffic(json.down)}/s`.padStart(12)
) )
}
trafficRetry = 10 trafficRetry = 10
mainWindow?.webContents.send('mihomoTraffic', json) mainWindow?.webContents.send('mihomoTraffic', json)
} }

View File

@ -14,7 +14,7 @@ import {
patchMihomoConfig patchMihomoConfig
} from '../core/mihomoApi' } from '../core/mihomoApi'
import { mainWindow, showMainWindow } from '..' import { mainWindow, showMainWindow } from '..'
import { app, Menu, nativeImage, shell, Tray } from 'electron' import { app, ipcMain, Menu, nativeImage, shell, Tray } from 'electron'
import { dataDir, logDir, mihomoCoreDir, mihomoWorkDir } from '../utils/dirs' import { dataDir, logDir, mihomoCoreDir, mihomoWorkDir } from '../utils/dirs'
import { triggerSysProxy } from '../sys/sysproxy' import { triggerSysProxy } from '../sys/sysproxy'
@ -24,7 +24,7 @@ const buildContextMenu = async (): Promise<Menu> => {
const { mode, tun } = await getControledMihomoConfig() const { mode, tun } = await getControledMihomoConfig()
const { sysProxy, autoCloseConnection, proxyInTray = true } = await getAppConfig() const { sysProxy, autoCloseConnection, proxyInTray = true } = await getAppConfig()
let groupsMenu: Electron.MenuItemConstructorOptions[] = [] let groupsMenu: Electron.MenuItemConstructorOptions[] = []
if (proxyInTray) { if (proxyInTray && process.platform !== 'linux') {
try { try {
const groups = await mihomoGroups() const groups = await mihomoGroups()
groupsMenu = groups.map((group) => { groupsMenu = groups.map((group) => {
@ -81,6 +81,7 @@ const buildContextMenu = async (): Promise<Menu> => {
await patchControledMihomoConfig({ mode: 'rule' }) await patchControledMihomoConfig({ mode: 'rule' })
await patchMihomoConfig({ mode: 'rule' }) await patchMihomoConfig({ mode: 'rule' })
mainWindow?.webContents.send('controledMihomoConfigUpdated') mainWindow?.webContents.send('controledMihomoConfigUpdated')
await updateTrayMenu()
} }
}, },
{ {
@ -92,6 +93,7 @@ const buildContextMenu = async (): Promise<Menu> => {
await patchControledMihomoConfig({ mode: 'global' }) await patchControledMihomoConfig({ mode: 'global' })
await patchMihomoConfig({ mode: 'global' }) await patchMihomoConfig({ mode: 'global' })
mainWindow?.webContents.send('controledMihomoConfigUpdated') mainWindow?.webContents.send('controledMihomoConfigUpdated')
await updateTrayMenu()
} }
}, },
{ {
@ -103,6 +105,7 @@ const buildContextMenu = async (): Promise<Menu> => {
await patchControledMihomoConfig({ mode: 'direct' }) await patchControledMihomoConfig({ mode: 'direct' })
await patchMihomoConfig({ mode: 'direct' }) await patchMihomoConfig({ mode: 'direct' })
mainWindow?.webContents.send('controledMihomoConfigUpdated') mainWindow?.webContents.send('controledMihomoConfigUpdated')
await updateTrayMenu()
} }
}, },
{ type: 'separator' }, { type: 'separator' },
@ -119,6 +122,7 @@ const buildContextMenu = async (): Promise<Menu> => {
await patchAppConfig({ sysProxy: { enable: !enable } }) await patchAppConfig({ sysProxy: { enable: !enable } })
} finally { } finally {
mainWindow?.webContents.send('appConfigUpdated') mainWindow?.webContents.send('appConfigUpdated')
await updateTrayMenu()
} }
} }
}, },
@ -135,6 +139,7 @@ const buildContextMenu = async (): Promise<Menu> => {
} }
await patchMihomoConfig({ tun: { enable } }) await patchMihomoConfig({ tun: { enable } })
mainWindow?.webContents.send('controledMihomoConfigUpdated') mainWindow?.webContents.send('controledMihomoConfigUpdated')
await updateTrayMenu()
} }
}, },
...groupsMenu, ...groupsMenu,
@ -184,6 +189,8 @@ export async function createTray(): Promise<void> {
const { useDockIcon = true } = await getAppConfig() const { useDockIcon = true } = await getAppConfig()
if (process.platform === 'linux') { if (process.platform === 'linux') {
tray = new Tray(pngIcon) tray = new Tray(pngIcon)
const menu = await buildContextMenu()
tray.setContextMenu(menu)
} }
if (process.platform === 'darwin') { if (process.platform === 'darwin') {
const icon = nativeImage.createFromPath(templateIcon) const icon = nativeImage.createFromPath(templateIcon)
@ -209,7 +216,8 @@ export async function createTray(): Promise<void> {
tray?.addListener('click', async () => { tray?.addListener('click', async () => {
await updateTrayMenu() await updateTrayMenu()
}) })
} else { }
if (process.platform === 'win32') {
tray?.addListener('click', () => { tray?.addListener('click', () => {
if (mainWindow?.isVisible()) { if (mainWindow?.isVisible()) {
mainWindow?.close() mainWindow?.close()
@ -221,9 +229,24 @@ export async function createTray(): Promise<void> {
await updateTrayMenu() await updateTrayMenu()
}) })
} }
if (process.platform === 'linux') {
tray?.addListener('click', () => {
if (mainWindow?.isVisible()) {
mainWindow?.close()
} else {
showMainWindow()
}
})
ipcMain.on('updateTrayMenu', async () => {
await updateTrayMenu()
})
}
} }
async function updateTrayMenu(): Promise<void> { async function updateTrayMenu(): Promise<void> {
const menu = await buildContextMenu() const menu = await buildContextMenu()
tray?.popUpContextMenu(menu) // 弹出菜单 tray?.popUpContextMenu(menu) // 弹出菜单
if (process.platform === 'linux') {
tray?.setContextMenu(menu)
}
} }

View File

@ -16,6 +16,7 @@ const OutboundModeSwitcher: React.FC = () => {
if (autoCloseConnection) { if (autoCloseConnection) {
await mihomoCloseAllConnections() await mihomoCloseAllConnections()
} }
window.electron.ipcRenderer.send('updateTrayMenu')
} }
if (!mode) return null if (!mode) return null
return ( return (

View File

@ -29,6 +29,7 @@ const SysproxySwitcher: React.FC = () => {
const onChange = async (enable: boolean): Promise<void> => { const onChange = async (enable: boolean): Promise<void> => {
await triggerSysProxy(enable) await triggerSysProxy(enable)
await patchAppConfig({ sysProxy: { enable } }) await patchAppConfig({ sysProxy: { enable } })
window.electron.ipcRenderer.send('updateTrayMenu')
} }
return ( return (

View File

@ -54,6 +54,7 @@ const TunSwitcher: React.FC = () => {
await patchControledMihomoConfig({ tun: { enable } }) await patchControledMihomoConfig({ tun: { enable } })
} }
await patchMihomoConfig({ tun: { enable } }) await patchMihomoConfig({ tun: { enable } })
window.electron.ipcRenderer.send('updateTrayMenu')
} }
return ( return (

View File

@ -62,7 +62,6 @@ const Proxies: React.FC = () => {
if (autoCloseConnection) { if (autoCloseConnection) {
await mihomoCloseAllConnections() await mihomoCloseAllConnections()
} }
// window.electron.ipcRenderer.send('mihomoGroupsUpdated')
mutate() mutate()
} }

View File

@ -195,15 +195,18 @@ const Settings: React.FC = () => {
}} }}
/> />
</SettingItem> </SettingItem>
{platform !== 'linux' && (
<SettingItem title="托盘菜单显示节点信息" divider> <SettingItem title="托盘菜单显示节点信息" divider>
<Switch <Switch
size="sm" size="sm"
isSelected={proxyInTray} isSelected={proxyInTray}
onValueChange={(v) => { onValueChange={async (v) => {
patchAppConfig({ proxyInTray: v }) await patchAppConfig({ proxyInTray: v })
}} }}
/> />
</SettingItem> </SettingItem>
)}
{platform === 'darwin' && ( {platform === 'darwin' && (
<> <>
<SettingItem title="显示Dock图标" divider> <SettingItem title="显示Dock图标" divider>