creat shortcut

This commit is contained in:
pompurin404 2024-08-22 15:54:00 +08:00
parent 0252ad58e2
commit 4b0a467d8e
No known key found for this signature in database
5 changed files with 111 additions and 25 deletions

View File

@ -1,9 +1,7 @@
### New Features ### New Features
- 自动删除过期日志与更新缓存 - 支持创建没有UAC弹窗的启动快捷方式
- 添加更新内核功能
### Bug Fixes ### Bug Fixes
- 修复接管dns后自定义hosts无法删除的问题 - 修复节点排序无法恢复默认的问题
- 删除退出应用全局快捷键(仅支持应用内CommandOrCtrl+Q退出)

View File

@ -126,3 +126,63 @@ export async function disableAutoRun(): Promise<void> {
await rm(desktopFilePath) await rm(desktopFilePath)
} }
} }
const shortcutTaskXml = `<?xml version="1.0" encoding="UTF-16"?>
<Task version="1.2" xmlns="http://schemas.microsoft.com/windows/2004/02/mit/task">
<RegistrationInfo>
<Date>${new Date().toISOString()}</Date>
<Author>${process.env.USERNAME}</Author>
</RegistrationInfo>
<Triggers />
<Principals>
<Principal id="Author">
<LogonType>InteractiveToken</LogonType>
<RunLevel>HighestAvailable</RunLevel>
</Principal>
</Principals>
<Settings>
<MultipleInstancesPolicy>Parallel</MultipleInstancesPolicy>
<DisallowStartIfOnBatteries>false</DisallowStartIfOnBatteries>
<StopIfGoingOnBatteries>false</StopIfGoingOnBatteries>
<AllowHardTerminate>false</AllowHardTerminate>
<StartWhenAvailable>false</StartWhenAvailable>
<RunOnlyIfNetworkAvailable>false</RunOnlyIfNetworkAvailable>
<IdleSettings>
<StopOnIdleEnd>false</StopOnIdleEnd>
<RestartOnIdle>false</RestartOnIdle>
</IdleSettings>
<AllowStartOnDemand>true</AllowStartOnDemand>
<Enabled>true</Enabled>
<Hidden>false</Hidden>
<RunOnlyIfIdle>false</RunOnlyIfIdle>
<WakeToRun>false</WakeToRun>
<ExecutionTimeLimit>PT72H</ExecutionTimeLimit>
<Priority>7</Priority>
</Settings>
<Actions Context="Author">
<Exec>
<Command>${exePath()}</Command>
</Exec>
</Actions>
</Task>
`
export async function createShortcut(): Promise<void> {
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' })
}
}

View File

@ -21,7 +21,7 @@ import {
stopMihomoConnections, stopMihomoConnections,
stopMihomoLogs stopMihomoLogs
} from '../core/mihomoApi' } from '../core/mihomoApi'
import { checkAutoRun, disableAutoRun, enableAutoRun } from '../sys/autoRun' import { checkAutoRun, createShortcut, disableAutoRun, enableAutoRun } from '../sys/autoRun'
import { import {
getAppConfig, getAppConfig,
patchAppConfig, patchAppConfig,
@ -165,6 +165,7 @@ export function registerIpcMainHandlers(): void {
ipcMain.handle('registerShortcut', (_e, oldShortcut, newShortcut, action) => ipcMain.handle('registerShortcut', (_e, oldShortcut, newShortcut, action) =>
ipcErrorWrapper(registerShortcut)(oldShortcut, newShortcut, action) ipcErrorWrapper(registerShortcut)(oldShortcut, newShortcut, action)
) )
ipcMain.handle('createShortcut', ipcErrorWrapper(createShortcut))
ipcMain.handle('setNativeTheme', (_e, theme) => { ipcMain.handle('setNativeTheme', (_e, theme) => {
setNativeTheme(theme) setNativeTheme(theme)
}) })

View File

@ -1,4 +1,4 @@
import React, { Key } from 'react' import React, { Key, useState } from 'react'
import SettingCard from '../base/base-setting-card' import SettingCard from '../base/base-setting-card'
import SettingItem from '../base/base-setting-item' import SettingItem from '../base/base-setting-item'
import { Button, Select, SelectItem, Switch, Tab, Tabs } from '@nextui-org/react' import { Button, Select, SelectItem, Switch, Tab, Tabs } from '@nextui-org/react'
@ -7,6 +7,7 @@ import useSWR from 'swr'
import { import {
checkAutoRun, checkAutoRun,
copyEnv, copyEnv,
createShortcut,
disableAutoRun, disableAutoRun,
enableAutoRun, enableAutoRun,
isPortable, isPortable,
@ -22,6 +23,7 @@ const GeneralConfig: React.FC = () => {
const { data: enable, mutate: mutateEnable } = useSWR('checkAutoRun', checkAutoRun) const { data: enable, mutate: mutateEnable } = useSWR('checkAutoRun', checkAutoRun)
const { data: portable, mutate: mutatePortable } = useSWR('isPortable', isPortable) const { data: portable, mutate: mutatePortable } = useSWR('isPortable', isPortable)
const { appConfig, patchAppConfig } = useAppConfig() const { appConfig, patchAppConfig } = useAppConfig()
const [creating, setCreating] = useState(false)
const { setTheme } = useTheme() const { setTheme } = useTheme()
const { const {
silentStart = false, silentStart = false,
@ -164,25 +166,46 @@ const GeneralConfig: React.FC = () => {
</> </>
)} )}
{platform === 'win32' && ( {platform === 'win32' && (
<SettingItem title="数据存储路径" divider> <>
<Select <SettingItem title="创建无UAC弹窗快捷方式" divider>
className="w-[150px]" <Button
size="sm" size="sm"
selectedKeys={new Set([portable ? 'portable' : 'data'])} color="primary"
onSelectionChange={async (v) => { isLoading={creating}
try { onClick={async () => {
await setPortable(v.currentKey === 'portable') try {
} catch (e) { setCreating(true)
alert(e) await createShortcut()
} finally { } catch (e) {
mutatePortable() alert(e)
} } finally {
}} setCreating(false)
> }
<SelectItem key="data">AppData</SelectItem> }}
<SelectItem key="portable"></SelectItem> >
</Select>
</SettingItem> </Button>
</SettingItem>
<SettingItem title="数据存储路径" divider>
<Select
className="w-[150px]"
size="sm"
selectedKeys={new Set([portable ? 'portable' : 'data'])}
onSelectionChange={async (v) => {
try {
await setPortable(v.currentKey === 'portable')
} catch (e) {
alert(e)
} finally {
mutatePortable()
}
}}
>
<SelectItem key="data">AppData</SelectItem>
<SelectItem key="portable"></SelectItem>
</Select>
</SettingItem>
</>
)} )}
<SettingItem title="背景色" divider={appTheme !== 'system'}> <SettingItem title="背景色" divider={appTheme !== 'system'}>
<Tabs <Tabs

View File

@ -287,6 +287,10 @@ export async function quitApp(): Promise<void> {
return ipcErrorWrapper(await window.electron.ipcRenderer.invoke('quitApp')) return ipcErrorWrapper(await window.electron.ipcRenderer.invoke('quitApp'))
} }
export async function createShortcut(): Promise<void> {
return ipcErrorWrapper(await window.electron.ipcRenderer.invoke('createShortcut'))
}
export async function setNativeTheme(theme: 'system' | 'light' | 'dark'): Promise<void> { export async function setNativeTheme(theme: 'system' | 'light' | 'dark'): Promise<void> {
return ipcErrorWrapper(await window.electron.ipcRenderer.invoke('setNativeTheme', theme)) return ipcErrorWrapper(await window.electron.ipcRenderer.invoke('setNativeTheme', theme))
} }