Skip to content

Commit 61062a0

Browse files
committed
refactor: enhance code readability and structure
1 parent 1d143fd commit 61062a0

File tree

34 files changed

+346
-260
lines changed

34 files changed

+346
-260
lines changed

frontend/src/bridge/app.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import type { TrayContent } from '@/types/app'
12
import * as App from '@wails/go/bridge/App'
23

34
export const RestartApp = App.RestartApp

frontend/src/components/Menu/index.vue

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
import { useI18n } from 'vue-i18n'
33
import { onMounted, onUnmounted, ref, watch, nextTick, useTemplateRef } from 'vue'
44
5+
import type { Menu } from '@/types/app'
6+
57
interface Props {
68
position: { x: number; y: number }
79
menuList: Menu[]

frontend/src/components/Table/index.vue

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
<script lang="ts" setup>
22
import { ref, computed } from 'vue'
33
4+
import type { Menu } from '@/types/app'
5+
46
import useI18n from '@/lang'
57
import { getValue } from '@/utils'
68
import vMenu from '@/directives/menu'

frontend/src/components/TitleBar.vue

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
<script setup lang="ts">
22
import { onMounted, onUnmounted, ref } from 'vue'
33
4+
import type { Menu } from '@/types/app'
5+
46
import { APP_TITLE, APP_VERSION, debounce, exitApp } from '@/utils'
57
import { useAppSettingsStore, useKernelApiStore, useEnvStore } from '@/stores'
68
import {

frontend/src/constant/app.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import { Color, ControllerCloseMode, PluginTrigger, ScheduledTasksType, View } from '@/enums/app'
22

3+
export const UserFilePath = 'data/user.yaml'
4+
35
export const ProfilesFilePath = 'data/profiles.yaml'
46

57
export const SubscribesFilePath = 'data/subscribes.yaml'

frontend/src/constant/kernel.ts

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -249,3 +249,36 @@ export const EmptyRuleSet = {
249249
}
250250

251251
export const DefaultExcludeProtocols = 'direct|reject|selector|urltest|block|dns|shadowsocksr'
252+
253+
export const DefaultConnections = () => {
254+
return {
255+
visibility: {
256+
'metadata.type': true,
257+
'metadata.processPath': false,
258+
'metadata.host': true,
259+
'metadata.sourceIP': false,
260+
'metadata.destinationIP': false,
261+
rule: true,
262+
chains: true,
263+
up: true,
264+
down: true,
265+
upload: true,
266+
download: true,
267+
start: true,
268+
},
269+
order: [
270+
'metadata.type',
271+
'metadata.processPath',
272+
'metadata.host',
273+
'metadata.sourceIP',
274+
'metadata.destinationIP',
275+
'rule',
276+
'chains',
277+
'up',
278+
'down',
279+
'upload',
280+
'download',
281+
'start',
282+
],
283+
}
284+
}

frontend/src/enums/app.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,11 @@ export enum Color {
4242
Purple = 'purple',
4343
}
4444

45+
export enum Branch {
46+
Main = 'main',
47+
Alpha = 'alpha',
48+
}
49+
4550
export enum ScheduledTasksType {
4651
UpdateSubscription = 'update::subscription',
4752
UpdateRuleset = 'update::ruleset',

frontend/src/hooks/useCoreBranch.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { useI18n } from 'vue-i18n'
22
import { computed, ref, watch } from 'vue'
33

4+
import { Branch } from '@/enums/app'
45
import { CoreWorkingDirectory } from '@/constant/kernel'
56
import { useAppSettingsStore, useEnvStore, useKernelApiStore } from '@/stores'
67
import {
@@ -57,7 +58,7 @@ export const useCoreBranch = (isAlpha = false) => {
5758
const restartable = computed(() => {
5859
const { running, branch } = appSettings.app.kernel
5960
if (!running) return false
60-
return localVersion.value && downloadCompleted.value && (branch === 'alpha') === isAlpha
61+
return localVersion.value && downloadCompleted.value && (branch === Branch.Alpha) === isAlpha
6162
})
6263

6364
const updatable = computed(
@@ -204,7 +205,7 @@ export const useCoreBranch = (isAlpha = false) => {
204205
const doRollback = () => Movefile(CoreBakFilePath, CoreFilePath)
205206

206207
const { running, branch } = appSettings.app.kernel
207-
const isCurrentRunning = running && (branch === 'alpha') === isAlpha
208+
const isCurrentRunning = running && (branch === Branch.Alpha) === isAlpha
208209
if (isCurrentRunning) {
209210
await kernelApiStore.restartKernel(doRollback)
210211
} else {

frontend/src/stores/app.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ import { useI18n } from 'vue-i18n'
22
import { h, computed, ref, type VNode, isVNode, resolveComponent } from 'vue'
33
import { defineStore } from 'pinia'
44

5+
import type { Menu } from '@/types/app'
6+
57
import { useEnvStore } from './env'
68
import {
79
APP_TITLE,

frontend/src/stores/appSettings.ts

Lines changed: 23 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,12 @@ import { ref, watch } from 'vue'
22
import { defineStore } from 'pinia'
33
import { parse, stringify } from 'yaml'
44

5+
import type { AppSettings } from '@/types/app'
6+
57
import i18n from '@/lang'
8+
import { DefaultConnections } from '@/constant/kernel'
69
import { debounce, updateTrayMenus, APP_TITLE, ignoredError, APP_VERSION } from '@/utils'
7-
import { Colors, DefaultFontFamily, DefaultTestURL } from '@/constant/app'
10+
import { Colors, DefaultFontFamily, DefaultTestURL, UserFilePath } from '@/constant/app'
811
import {
912
Theme,
1013
WindowStartState,
@@ -13,6 +16,7 @@ import {
1316
Color,
1417
WebviewGpuPolicy,
1518
ControllerCloseMode,
19+
Branch,
1620
} from '@/enums/app'
1721
import {
1822
Readfile,
@@ -22,50 +26,6 @@ import {
2226
WindowIsMinimised,
2327
} from '@/bridge'
2428

25-
type AppSettings = {
26-
lang: Lang
27-
theme: Theme
28-
color: Color
29-
'font-family': string
30-
profilesView: View
31-
subscribesView: View
32-
rulesetsView: View
33-
pluginsView: View
34-
scheduledtasksView: View
35-
windowStartState: WindowStartState
36-
webviewGpuPolicy: WebviewGpuPolicy
37-
width: number
38-
height: number
39-
exitOnClose: boolean
40-
closeKernelOnExit: boolean
41-
autoSetSystemProxy: boolean
42-
autoStartKernel: boolean
43-
userAgent: string
44-
startupDelay: number
45-
connections: {
46-
visibility: Record<string, boolean>
47-
order: string[]
48-
}
49-
kernel: {
50-
branch: 'main' | 'alpha'
51-
profile: string
52-
pid: number
53-
running: boolean
54-
autoClose: boolean
55-
unAvailable: boolean
56-
cardMode: boolean
57-
sortByDelay: boolean
58-
testUrl: string
59-
controllerCloseMode: ControllerCloseMode
60-
}
61-
pluginSettings: Record<string, Record<string, any>>
62-
githubApiToken: string
63-
multipleInstance: boolean
64-
addPluginToMenu: boolean
65-
rollingRelease: boolean
66-
pages: string[]
67-
}
68-
6929
export const useAppSettingsStore = defineStore('app-settings', () => {
7030
let firstOpen = true
7131
let latestUserConfig = ''
@@ -76,7 +36,7 @@ export const useAppSettingsStore = defineStore('app-settings', () => {
7636
lang: Lang.EN,
7737
theme: Theme.Auto,
7838
color: Color.Default,
79-
'font-family': DefaultFontFamily,
39+
fontFamily: DefaultFontFamily,
8040
profilesView: View.Grid,
8141
subscribesView: View.Grid,
8242
rulesetsView: View.Grid,
@@ -92,38 +52,9 @@ export const useAppSettingsStore = defineStore('app-settings', () => {
9252
autoStartKernel: false,
9353
userAgent: APP_TITLE + '/' + APP_VERSION,
9454
startupDelay: 30,
95-
connections: {
96-
visibility: {
97-
'metadata.type': true,
98-
'metadata.processPath': false,
99-
'metadata.host': true,
100-
'metadata.sourceIP': false,
101-
'metadata.destinationIP': false,
102-
rule: true,
103-
chains: true,
104-
up: true,
105-
down: true,
106-
upload: true,
107-
download: true,
108-
start: true,
109-
},
110-
order: [
111-
'metadata.type',
112-
'metadata.processPath',
113-
'metadata.host',
114-
'metadata.sourceIP',
115-
'metadata.destinationIP',
116-
'rule',
117-
'chains',
118-
'up',
119-
'down',
120-
'upload',
121-
'download',
122-
'start',
123-
],
124-
},
55+
connections: DefaultConnections(),
12556
kernel: {
126-
branch: 'main',
57+
branch: Branch.Main,
12758
profile: '',
12859
pid: 0,
12960
running: false,
@@ -143,19 +74,26 @@ export const useAppSettingsStore = defineStore('app-settings', () => {
14374
})
14475

14576
const saveAppSettings = debounce((config: string) => {
146-
Writefile('data/user.yaml', config)
77+
Writefile(UserFilePath, config)
14778
}, 500)
14879

14980
const setupAppSettings = async () => {
150-
const data = await ignoredError(Readfile, 'data/user.yaml')
81+
const data = await ignoredError(Readfile, UserFilePath)
15182
data && (app.value = Object.assign(app.value, parse(data)))
15283

15384
if ((app.value.kernel.branch as any) === 'latest') {
154-
app.value.kernel.branch = 'alpha'
85+
app.value.kernel.branch = Branch.Alpha
15586
}
15687
if (app.value.kernel.controllerCloseMode === undefined) {
15788
app.value.kernel.controllerCloseMode = ControllerCloseMode.All
15889
}
90+
// @ts-expect-error(Deprecated)
91+
if (app.value['font-family'] !== undefined) {
92+
// @ts-expect-error(Deprecated)
93+
app.value.fontFamily = app.value['font-family']
94+
// @ts-expect-error(Deprecated)
95+
delete app.value['font-family']
96+
}
15997

16098
if (typeof app.value.connections.visibility['metadata.destinationIP'] === 'undefined') {
16199
app.value.connections.visibility['metadata.destinationIP'] = false
@@ -173,7 +111,6 @@ export const useAppSettingsStore = defineStore('app-settings', () => {
173111

174112
const mediaQueryList = window.matchMedia('(prefers-color-scheme: dark)')
175113
mediaQueryList.addEventListener('change', ({ matches }) => {
176-
console.log('onSystemThemeChange')
177114
if (app.value.theme === Theme.Auto) {
178115
themeMode.value = matches ? Theme.Dark : Theme.Light
179116
}
@@ -201,7 +138,7 @@ export const useAppSettingsStore = defineStore('app-settings', () => {
201138
const { primary, secondary } = Colors[settings.color]
202139
document.documentElement.style.setProperty('--primary-color', primary)
203140
document.documentElement.style.setProperty('--secondary-color', secondary)
204-
document.body.style.fontFamily = settings['font-family']
141+
document.body.style.fontFamily = settings.fontFamily
205142
}
206143

207144
watch(
@@ -228,8 +165,10 @@ export const useAppSettingsStore = defineStore('app-settings', () => {
228165
window.addEventListener(
229166
'resize',
230167
debounce(async () => {
231-
const isMinimised = await WindowIsMinimised()
232-
const isMaximised = await WindowIsMaximised()
168+
const [isMinimised, isMaximised] = await Promise.all([
169+
WindowIsMinimised(),
170+
WindowIsMaximised(),
171+
])
233172
if (!isMinimised && !isMaximised) {
234173
app.value.width = document.documentElement.clientWidth
235174
app.value.height = document.documentElement.clientHeight

0 commit comments

Comments
 (0)