diff --git a/src/main/resolve/shortcut.ts b/src/main/resolve/shortcut.ts index c267cf3..1dad28a 100644 --- a/src/main/resolve/shortcut.ts +++ b/src/main/resolve/shortcut.ts @@ -1,4 +1,4 @@ -import { app, globalShortcut, ipcMain } from 'electron' +import { app, globalShortcut, ipcMain, Notification } from 'electron' import { mainWindow, showMainWindow } from '..' import { getAppConfig, @@ -8,7 +8,7 @@ import { } from '../config' import { triggerSysProxy } from '../sys/sysproxy' import { patchMihomoConfig } from '../core/mihomoApi' -import { quitWithoutCore } from '../core/manager' +import { quitWithoutCore, restartCore } from '../core/manager' export async function registerShortcut( oldShortcut: string, @@ -39,6 +39,9 @@ export async function registerShortcut( try { await triggerSysProxy(!enable) await patchAppConfig({ sysProxy: { enable: !enable } }) + new Notification({ + title: `系统代理已${!enable ? '开启' : '关闭'}` + }).show() } catch { // ignore } finally { @@ -51,16 +54,31 @@ export async function registerShortcut( return globalShortcut.register(newShortcut, async () => { const { tun } = await getControledMihomoConfig() const enable = tun?.enable ?? false - await patchControledMihomoConfig({ tun: { enable: !enable } }) - await patchMihomoConfig({ tun: { enable: !enable } }) - mainWindow?.webContents.send('controledMihomoConfigUpdated') - ipcMain.emit('updateTrayMenu') + try { + if (!enable) { + await patchControledMihomoConfig({ tun: { enable: !enable }, dns: { enable: true } }) + } else { + await patchControledMihomoConfig({ tun: { enable: !enable } }) + } + await restartCore() + new Notification({ + title: `虚拟网卡已${!enable ? '开启' : '关闭'}` + }).show() + } catch { + // ignore + } finally { + mainWindow?.webContents.send('controledMihomoConfigUpdated') + ipcMain.emit('updateTrayMenu') + } }) } case 'ruleModeShortcut': { return globalShortcut.register(newShortcut, async () => { await patchControledMihomoConfig({ mode: 'rule' }) await patchMihomoConfig({ mode: 'rule' }) + new Notification({ + title: '已切换至规则模式' + }).show() mainWindow?.webContents.send('controledMihomoConfigUpdated') ipcMain.emit('updateTrayMenu') }) @@ -69,6 +87,9 @@ export async function registerShortcut( return globalShortcut.register(newShortcut, async () => { await patchControledMihomoConfig({ mode: 'global' }) await patchMihomoConfig({ mode: 'global' }) + new Notification({ + title: '已切换至全局模式' + }).show() mainWindow?.webContents.send('controledMihomoConfigUpdated') ipcMain.emit('updateTrayMenu') }) @@ -77,6 +98,9 @@ export async function registerShortcut( return globalShortcut.register(newShortcut, async () => { await patchControledMihomoConfig({ mode: 'direct' }) await patchMihomoConfig({ mode: 'direct' }) + new Notification({ + title: '已切换至直连模式' + }).show() mainWindow?.webContents.send('controledMihomoConfigUpdated') ipcMain.emit('updateTrayMenu') })