Skip to content

Commit

Permalink
some settings
Browse files Browse the repository at this point in the history
  • Loading branch information
Pompurin404 committed Aug 3, 2024
1 parent db1766b commit ba6f8bd
Show file tree
Hide file tree
Showing 5 changed files with 115 additions and 21 deletions.
11 changes: 6 additions & 5 deletions src/main/utils/cmds.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ipcMain } from 'electron'
import { app, ipcMain } from 'electron'
import {
mihomoChangeProxy,
mihomoConfig,
Expand Down Expand Up @@ -32,12 +32,12 @@ export function registerIpcMainHandlers(): void {
ipcMain.handle('mihomoConfig', mihomoConfig)
ipcMain.handle('mihomoConnections', mihomoConnections)
ipcMain.handle('mihomoRules', mihomoRules)
ipcMain.handle('mihomoProxies', () => mihomoProxies())
ipcMain.handle('mihomoProxies', mihomoProxies)
ipcMain.handle('mihomoChangeProxy', (_e, group, proxy) => mihomoChangeProxy(group, proxy))
ipcMain.handle('mihomoProxyDelay', (_e, proxy, url) => mihomoProxyDelay(proxy, url))
ipcMain.handle('startMihomoLogs', startMihomoLogs)
ipcMain.handle('stopMihomoLogs', () => stopMihomoLogs())
ipcMain.handle('patchMihomoConfig', async (_e, patch) => await patchMihomoConfig(patch))
ipcMain.handle('stopMihomoLogs', stopMihomoLogs)
ipcMain.handle('patchMihomoConfig', (_e, patch) => patchMihomoConfig(patch))
ipcMain.handle('checkAutoRun', checkAutoRun)
ipcMain.handle('enableAutoRun', enableAutoRun)
ipcMain.handle('disableAutoRun', disableAutoRun)
Expand All @@ -51,6 +51,7 @@ export function registerIpcMainHandlers(): void {
ipcMain.handle('changeCurrentProfile', (_e, id) => changeCurrentProfile(id))
ipcMain.handle('addProfileItem', (_e, item) => addProfileItem(item))
ipcMain.handle('removeProfileItem', (_e, id) => removeProfileItem(id))
ipcMain.handle('restartCore', () => restartCore())
ipcMain.handle('restartCore', restartCore)
ipcMain.handle('triggerSysProxy', (_e, enable) => triggerSysProxy(enable))
ipcMain.handle('quitApp', () => app.quit())
}
43 changes: 29 additions & 14 deletions src/renderer/src/components/base/base-setting-item.tsx
Original file line number Diff line number Diff line change
@@ -1,28 +1,43 @@
import { Divider } from '@nextui-org/react'
import { Button, Divider } from '@nextui-org/react'
import { FaChevronRight } from 'react-icons/fa'
import React from 'react'

interface Props {
onPress?: () => void
title: React.ReactNode
actions?: React.ReactNode
children?: React.ReactNode
divider?: boolean
}

const SettingItem: React.FC<Props> = (props) => {
const { divider = false } = props

return (
<>
<div className="h-[32px] w-full flex justify-between">
<div className="h-full flex items-center">
<h4 className="h-full select-none text-md leading-[32px]">{props.title}</h4>
<div>{props.actions}</div>
const { title, actions, children, divider = false, onPress } = props
if (onPress) {
return (
<>
<div className="p-0 m-0 h-[32px] w-full flex justify-between">
<h4 className="h-full select-none text-md leading-[32px]">{title}</h4>
<Button size="sm" onPress={onPress}>
<FaChevronRight />
</Button>
</div>
{divider && <Divider className="my-2" />}
</>
)
} else {
return (
<>
<div className="h-[32px] w-full flex justify-between">
<div className="h-full flex items-center">
<h4 className="h-full select-none text-md leading-[32px]">{title}</h4>
<div>{actions}</div>
</div>
{children}
</div>
{props.children}
</div>
{divider && <Divider className="my-2" />}
</>
)
{divider && <Divider className="my-2" />}
</>
)
}
}

export default SettingItem
73 changes: 72 additions & 1 deletion src/renderer/src/pages/mihomo.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,78 @@
import { Input, Select, SelectItem, Switch } from '@nextui-org/react'
import BasePage from '@renderer/components/base/base-page'
import SettingCard from '@renderer/components/base/base-setting-card'
import SettingItem from '@renderer/components/base/base-setting-item'
import { useControledMihomoConfig } from '@renderer/hooks/use-controled-mihomo-config'
import { patchMihomoConfig } from '@renderer/utils/ipc'
import React from 'react'

const Mihomo: React.FC = () => {
return <BasePage title="内核设置"></BasePage>
const { controledMihomoConfig, patchControledMihomoConfig } = useControledMihomoConfig()
const {
ipv6,
'log-level': level = 'info',
'allow-lan': lan,
'mixed-port': mixedPort = 7890
} = controledMihomoConfig || {}

const onChange = async (patch: Partial<IMihomoConfig>): Promise<void> => {
await patchControledMihomoConfig(patch)
await patchMihomoConfig(patch)
}

return (
<BasePage title="内核设置">
<SettingCard>
<SettingItem title="混合端口" divider>
<Input
size="sm"
type="number"
className="w-[100px]"
value={mixedPort.toString()}
max={65535}
min={0}
onValueChange={(v) => {
onChange({ 'mixed-port': parseInt(v) })
}}
/>
</SettingItem>
<SettingItem title="IPv6" divider>
<Switch
size="sm"
isSelected={ipv6}
onValueChange={(v) => {
onChange({ ipv6: v })
}}
/>
</SettingItem>
<SettingItem title="允许局域网连接" divider>
<Switch
size="sm"
isSelected={lan}
onValueChange={(v) => {
onChange({ 'allow-lan': v })
}}
/>
</SettingItem>
<SettingItem title="日志等级">
<Select
className="w-[100px]"
size="sm"
selectedKeys={new Set([level])}
onSelectionChange={(v) => {
onChange({ 'log-level': v.currentKey as LogLevel })
}}
>
<SelectItem key="silent">静默</SelectItem>
<SelectItem key="info">信息</SelectItem>
<SelectItem key="warning">警告</SelectItem>
<SelectItem key="error">错误</SelectItem>
<SelectItem key="debug">调试</SelectItem>
</Select>
</SettingItem>
</SettingCard>
</BasePage>
)
}

export default Mihomo
5 changes: 4 additions & 1 deletion src/renderer/src/pages/settings.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import BasePage from '@renderer/components/base/base-page'
import SettingCard from '@renderer/components/base/base-setting-card'
import SettingItem from '@renderer/components/base/base-setting-item'
import { useAppConfig } from '@renderer/hooks/use-config'
import { checkAutoRun, enableAutoRun, disableAutoRun } from '@renderer/utils/ipc'
import { checkAutoRun, enableAutoRun, disableAutoRun, quitApp } from '@renderer/utils/ipc'
import { IoLogoGithub } from 'react-icons/io5'

import useSWR from 'swr'
Expand Down Expand Up @@ -83,6 +83,9 @@ const Settings: React.FC = () => {
></Input>
</SettingItem>
</SettingCard>
<SettingCard>
<SettingItem title="退出应用" onPress={quitApp} />
</SettingCard>
</BasePage>
)
}
Expand Down
4 changes: 4 additions & 0 deletions src/renderer/src/utils/ipc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,3 +97,7 @@ export async function restartCore(): Promise<void> {
export async function triggerSysProxy(enable: boolean): Promise<void> {
return await window.electron.ipcRenderer.invoke('triggerSysProxy', enable)
}

export async function quitApp(): Promise<void> {
return await window.electron.ipcRenderer.invoke('quitApp')
}

0 comments on commit ba6f8bd

Please sign in to comment.