From 4b0a467d8ea8d7936fc4c9262d004aabebfe3ed1 Mon Sep 17 00:00:00 2001 From: pompurin404 Date: Thu, 22 Aug 2024 15:54:00 +0800 Subject: [PATCH] creat shortcut --- changelog.md | 6 +- src/main/sys/autoRun.ts | 60 ++++++++++++++++++ src/main/utils/ipc.ts | 3 +- .../components/settings/general-config.tsx | 63 +++++++++++++------ src/renderer/src/utils/ipc.ts | 4 ++ 5 files changed, 111 insertions(+), 25 deletions(-) diff --git a/changelog.md b/changelog.md index bc39628..a373919 100644 --- a/changelog.md +++ b/changelog.md @@ -1,9 +1,7 @@ ### New Features -- 自动删除过期日志与更新缓存 -- 添加更新内核功能 +- 支持创建没有UAC弹窗的启动快捷方式 ### Bug Fixes -- 修复接管dns后自定义hosts无法删除的问题 -- 删除退出应用全局快捷键(仅支持应用内CommandOrCtrl+Q退出) +- 修复节点排序无法恢复默认的问题 diff --git a/src/main/sys/autoRun.ts b/src/main/sys/autoRun.ts index 8c10da3..487683c 100644 --- a/src/main/sys/autoRun.ts +++ b/src/main/sys/autoRun.ts @@ -126,3 +126,63 @@ export async function disableAutoRun(): Promise { await rm(desktopFilePath) } } + +const shortcutTaskXml = ` + + + ${new Date().toISOString()} + ${process.env.USERNAME} + + + + + InteractiveToken + HighestAvailable + + + + Parallel + false + false + false + false + false + + false + false + + true + true + false + false + false + PT72H + 7 + + + + ${exePath()} + + + + ` + +export async function createShortcut(): Promise { + if (process.platform === 'win32') { + const execPromise = promisify(exec) + const taskFilePath = path.join(dataDir(), `${appName}-run.xml`) + await writeFile(taskFilePath, Buffer.from(`\ufeff${shortcutTaskXml}`, 'utf-16le')) + await execPromise(`schtasks /create /tn "${appName}-run" /xml "${taskFilePath}" /f`) + const createShortcutCommand = ` +$shortcutPath = [System.IO.Path]::Combine([System.Environment]::GetFolderPath("Desktop"), "Mihomo Party.lnk") +$wshell = New-Object -ComObject WScript.Shell +$shortcut = $wshell.CreateShortcut($shortcutPath) +$shortcut.TargetPath = "C:\\Windows\\System32\\schtasks.exe" +$shortcut.Arguments = "/run /tn ${appName}-run" +$shortcut.Description = "Start Mihomo Party without UAC Prompt" +$shortcut.IconLocation = "${exePath()}" +$shortcut.WindowStyle = 7 +$shortcut.Save()` + await execPromise(createShortcutCommand, { shell: 'powershell' }) + } +} diff --git a/src/main/utils/ipc.ts b/src/main/utils/ipc.ts index 64d6340..70f7d66 100644 --- a/src/main/utils/ipc.ts +++ b/src/main/utils/ipc.ts @@ -21,7 +21,7 @@ import { stopMihomoConnections, stopMihomoLogs } from '../core/mihomoApi' -import { checkAutoRun, disableAutoRun, enableAutoRun } from '../sys/autoRun' +import { checkAutoRun, createShortcut, disableAutoRun, enableAutoRun } from '../sys/autoRun' import { getAppConfig, patchAppConfig, @@ -165,6 +165,7 @@ export function registerIpcMainHandlers(): void { ipcMain.handle('registerShortcut', (_e, oldShortcut, newShortcut, action) => ipcErrorWrapper(registerShortcut)(oldShortcut, newShortcut, action) ) + ipcMain.handle('createShortcut', ipcErrorWrapper(createShortcut)) ipcMain.handle('setNativeTheme', (_e, theme) => { setNativeTheme(theme) }) diff --git a/src/renderer/src/components/settings/general-config.tsx b/src/renderer/src/components/settings/general-config.tsx index 10a98db..6d93ffb 100644 --- a/src/renderer/src/components/settings/general-config.tsx +++ b/src/renderer/src/components/settings/general-config.tsx @@ -1,4 +1,4 @@ -import React, { Key } from 'react' +import React, { Key, useState } from 'react' import SettingCard from '../base/base-setting-card' import SettingItem from '../base/base-setting-item' import { Button, Select, SelectItem, Switch, Tab, Tabs } from '@nextui-org/react' @@ -7,6 +7,7 @@ import useSWR from 'swr' import { checkAutoRun, copyEnv, + createShortcut, disableAutoRun, enableAutoRun, isPortable, @@ -22,6 +23,7 @@ const GeneralConfig: React.FC = () => { const { data: enable, mutate: mutateEnable } = useSWR('checkAutoRun', checkAutoRun) const { data: portable, mutate: mutatePortable } = useSWR('isPortable', isPortable) const { appConfig, patchAppConfig } = useAppConfig() + const [creating, setCreating] = useState(false) const { setTheme } = useTheme() const { silentStart = false, @@ -164,25 +166,46 @@ const GeneralConfig: React.FC = () => { )} {platform === 'win32' && ( - - - + <> + + + + + + + )} { return ipcErrorWrapper(await window.electron.ipcRenderer.invoke('quitApp')) } +export async function createShortcut(): Promise { + return ipcErrorWrapper(await window.electron.ipcRenderer.invoke('createShortcut')) +} + export async function setNativeTheme(theme: 'system' | 'light' | 'dark'): Promise { return ipcErrorWrapper(await window.electron.ipcRenderer.invoke('setNativeTheme', theme)) }