From fe5ddcf0c25b0b9db17d9a1417a0468c2101524c Mon Sep 17 00:00:00 2001 From: pompurin404 Date: Wed, 18 Sep 2024 21:15:55 +0800 Subject: [PATCH] support change profile on tray menu #126 --- changelog.md | 4 ++++ src/main/resolve/tray.ts | 21 +++++++++++++++++++ src/renderer/src/hooks/use-profile-config.tsx | 5 +++++ 3 files changed, 30 insertions(+) diff --git a/changelog.md b/changelog.md index 06bba18..a316455 100644 --- a/changelog.md +++ b/changelog.md @@ -2,6 +2,10 @@ - 1.2.x YAML覆写语法有所变动,请更新后参考文档进行修改 +### Features + +- 支持托盘菜单切换订阅 + ### Bug Fixes - 修复覆写更新后全局启用状态丢失的问题 diff --git a/src/main/resolve/tray.ts b/src/main/resolve/tray.ts index 566d15c..c5440dd 100644 --- a/src/main/resolve/tray.ts +++ b/src/main/resolve/tray.ts @@ -1,6 +1,8 @@ import { + changeCurrentProfile, getAppConfig, getControledMihomoConfig, + getProfileConfig, patchAppConfig, patchControledMihomoConfig } from '../config' @@ -75,6 +77,7 @@ const buildContextMenu = async (): Promise => { // 避免出错时无法创建托盘菜单 } } + const { current, items = [] } = await getProfileConfig() const contextMenu = [ { @@ -163,6 +166,24 @@ const buildContextMenu = async (): Promise => { }, ...groupsMenu, { type: 'separator' }, + { + type: 'submenu', + label: '订阅配置', + submenu: items.map((item) => { + return { + type: 'radio', + label: item.name, + checked: item.id === current, + click: async (): Promise => { + if (item.id === current) return + await changeCurrentProfile(item.id) + mainWindow?.webContents.send('profileConfigUpdated') + ipcMain.emit('updateTrayMenu') + } + } + }) + }, + { type: 'separator' }, { type: 'submenu', label: '打开目录', diff --git a/src/renderer/src/hooks/use-profile-config.tsx b/src/renderer/src/hooks/use-profile-config.tsx index 167b61d..ee5b626 100644 --- a/src/renderer/src/hooks/use-profile-config.tsx +++ b/src/renderer/src/hooks/use-profile-config.tsx @@ -33,6 +33,7 @@ export const ProfileConfigProvider: React.FC<{ children: ReactNode }> = ({ child alert(e) } finally { mutateProfileConfig() + window.electron.ipcRenderer.send('updateTrayMenu') } } @@ -43,6 +44,7 @@ export const ProfileConfigProvider: React.FC<{ children: ReactNode }> = ({ child alert(e) } finally { mutateProfileConfig() + window.electron.ipcRenderer.send('updateTrayMenu') } } @@ -53,6 +55,7 @@ export const ProfileConfigProvider: React.FC<{ children: ReactNode }> = ({ child alert(e) } finally { mutateProfileConfig() + window.electron.ipcRenderer.send('updateTrayMenu') } } @@ -63,6 +66,7 @@ export const ProfileConfigProvider: React.FC<{ children: ReactNode }> = ({ child alert(e) } finally { mutateProfileConfig() + window.electron.ipcRenderer.send('updateTrayMenu') } } @@ -73,6 +77,7 @@ export const ProfileConfigProvider: React.FC<{ children: ReactNode }> = ({ child alert(e) } finally { mutateProfileConfig() + window.electron.ipcRenderer.send('updateTrayMenu') } }