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 {
tray?.setTitle('')
}
if (process.platform !== 'linux') {
tray?.setToolTip(
'↑' +
`${calcTraffic(json.up)}/s`.padStart(12) +
'\n↓' +
`${calcTraffic(json.down)}/s`.padStart(12)
)
}
tray?.setToolTip(
'↑' +
`${calcTraffic(json.up)}/s`.padStart(12) +
'\n↓' +
`${calcTraffic(json.down)}/s`.padStart(12)
)
trafficRetry = 10
mainWindow?.webContents.send('mihomoTraffic', json)
}

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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