From f156dd1311bf8230484355a4dbdbdb22924fd7fe Mon Sep 17 00:00:00 2001 From: pompurin404 Date: Fri, 20 Sep 2024 14:15:50 +0800 Subject: [PATCH] support multiple env copy type --- changelog.md | 3 +- src/main/resolve/tray.ts | 37 ++++++++++++++----- src/main/utils/init.ts | 5 +++ src/main/utils/ipc.ts | 2 +- src/renderer/src/App.tsx | 20 +++++----- .../components/settings/general-config.tsx | 23 +++++++++--- src/renderer/src/utils/ipc.ts | 4 +- src/shared/types.d.ts | 2 +- 8 files changed, 66 insertions(+), 30 deletions(-) diff --git a/changelog.md b/changelog.md index 3093c1b..e78a4aa 100644 --- a/changelog.md +++ b/changelog.md @@ -4,5 +4,6 @@ ### Features -- 支持自定义CSS样式 +- 支持自定义主题 +- 复制环境变量类型支持多选 - 添加用户验证相关设置 diff --git a/src/main/resolve/tray.ts b/src/main/resolve/tray.ts index c5440dd..d5c2966 100644 --- a/src/main/resolve/tray.ts +++ b/src/main/resolve/tray.ts @@ -27,6 +27,7 @@ const buildContextMenu = async (): Promise => { const { mode, tun } = await getControledMihomoConfig() const { sysProxy, + envType = process.platform === 'win32' ? ['powershell'] : ['bash'], autoCloseConnection, proxyInTray = true, triggerSysProxyShortcut = '', @@ -210,12 +211,29 @@ const buildContextMenu = async (): Promise => { } ] }, - { - id: 'copyenv', - label: '复制环境变量', - type: 'normal', - click: copyEnv - }, + envType.length > 1 + ? { + type: 'submenu', + label: '复制环境变量', + submenu: envType.map((type) => { + return { + id: type, + label: type, + type: 'normal', + click: async (): Promise => { + await copyEnv(type) + } + } + }) + } + : { + id: 'copyenv', + label: '复制环境变量', + type: 'normal', + click: async (): Promise => { + await copyEnv(envType[0]) + } + }, { type: 'separator' }, { id: 'quitWithoutCore', @@ -316,12 +334,11 @@ async function updateTrayMenu(): Promise { } } -export async function copyEnv(): Promise { - const defaultType = process.platform === 'win32' ? 'powershell' : 'bash' +export async function copyEnv(type: 'bash' | 'cmd' | 'powershell'): Promise { const { 'mixed-port': mixedPort = 7890 } = await getControledMihomoConfig() - const { envType = defaultType, sysProxy } = await getAppConfig() + const { sysProxy } = await getAppConfig() const { host } = sysProxy - switch (envType) { + switch (type) { case 'bash': { clipboard.writeText( `export https_proxy=http://${host || '127.0.0.1'}:${mixedPort} http_proxy=http://${host || '127.0.0.1'}:${mixedPort} all_proxy=http://${host || '127.0.0.1'}:${mixedPort}` diff --git a/src/main/utils/init.ts b/src/main/utils/init.ts index 2b7077f..89158a9 100644 --- a/src/main/utils/init.ts +++ b/src/main/utils/init.ts @@ -146,6 +146,7 @@ async function migration(): Promise { 'substore' ], appTheme = 'system', + envType = [process.platform === 'win32' ? 'powershell' : 'bash'], useSubStore = true } = await getAppConfig() const { @@ -183,6 +184,10 @@ async function migration(): Promise { if (!['system', 'light', 'dark'].includes(appTheme)) { await patchAppConfig({ appTheme: 'system' }) } + // change env type + if (typeof envType === 'string') { + await patchAppConfig({ envType: [envType] }) + } } function initDeeplink(): void { diff --git a/src/main/utils/ipc.ts b/src/main/utils/ipc.ts index 710db23..bb364a8 100644 --- a/src/main/utils/ipc.ts +++ b/src/main/utils/ipc.ts @@ -205,7 +205,7 @@ export function registerIpcMainHandlers(): void { ipcMain.handle('resolveThemes', () => ipcErrorWrapper(resolveThemes)()) ipcMain.handle('fetchThemes', () => ipcErrorWrapper(fetchThemes)()) ipcMain.handle('applyTheme', (_e, theme) => ipcErrorWrapper(applyTheme)(theme)) - ipcMain.handle('copyEnv', ipcErrorWrapper(copyEnv)) + ipcMain.handle('copyEnv', (_e, type) => ipcErrorWrapper(copyEnv)(type)) ipcMain.handle('alert', (_e, msg) => { dialog.showErrorBox('Mihomo Party', msg) }) diff --git a/src/renderer/src/App.tsx b/src/renderer/src/App.tsx index 481003b..14bf605 100644 --- a/src/renderer/src/App.tsx +++ b/src/renderer/src/App.tsx @@ -84,16 +84,18 @@ const App: React.FC = () => { setTheme(appTheme) if (customTheme) applyTheme(customTheme) if (!useWindowFrame) { - const options = { height: 48 } as TitleBarOverlayOptions - try { - if (platform !== 'darwin') { - options.color = window.getComputedStyle(document.documentElement).backgroundColor - options.symbolColor = window.getComputedStyle(document.documentElement).color + setTimeout(() => { + const options = { height: 48 } as TitleBarOverlayOptions + try { + if (platform !== 'darwin') { + options.color = window.getComputedStyle(document.documentElement).backgroundColor + options.symbolColor = window.getComputedStyle(document.documentElement).color + } + setTitleBarOverlay(options) + } catch (e) { + // ignore } - setTitleBarOverlay(options) - } catch (e) { - // ignore - } + }, 0) } }, [appTheme, systemTheme, customTheme]) diff --git a/src/renderer/src/components/settings/general-config.tsx b/src/renderer/src/components/settings/general-config.tsx index b04f448..6fdca3d 100644 --- a/src/renderer/src/components/settings/general-config.tsx +++ b/src/renderer/src/components/settings/general-config.tsx @@ -36,7 +36,7 @@ const GeneralConfig: React.FC = () => { autoQuitWithoutCore = false, autoQuitWithoutCoreDelay = 60, customTheme = 'default.css', - envType = platform === 'win32' ? 'powershell' : 'bash', + envType = [platform === 'win32' ? 'powershell' : 'bash'], autoCheckUpdate, appTheme = 'system' } = appConfig || {} @@ -125,20 +125,31 @@ const GeneralConfig: React.FC = () => { )} + actions={envType.map((type) => ( + - } + ))} divider >