Skip to content

Commit 080cd55

Browse files
committed
refactor(tray): unify tray and menu updates
1 parent 338a290 commit 080cd55

File tree

11 files changed

+49
-32
lines changed

11 files changed

+49
-32
lines changed

bridge/tray.go

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -29,14 +29,26 @@ func CreateTray(a *App, icon []byte) (trayStart, trayEnd func()) {
2929
}, nil)
3030
}
3131

32+
func (a *App) UpdateTray(tray TrayContent) {
33+
log.Printf("UpdateTray")
34+
updateTray(a, tray)
35+
}
36+
3237
func (a *App) UpdateTrayMenus(menus []MenuItem) {
3338
log.Printf("UpdateTrayMenus")
39+
updateTrayMenus(a, menus)
40+
}
3441

35-
systray.ResetMenu()
42+
func (a *App) UpdateTrayAndMenus(tray TrayContent, menus []MenuItem) {
43+
log.Printf("UpdateTrayAndMenus")
44+
updateTray(a, tray)
45+
updateTrayMenus(a, menus)
46+
}
3647

37-
for _, menu := range menus {
38-
createMenuItem(menu, a, nil)
39-
}
48+
func (a *App) ExitApp() {
49+
systray.Quit()
50+
runtime.Quit(a.Ctx)
51+
os.Exit(0)
4052
}
4153

4254
func addClickMenuItem(title, tooltip string, action func()) *systray.MenuItem {
@@ -72,7 +84,7 @@ func createMenuItem(menu MenuItem, a *App, parent *systray.MenuItem) {
7284
}
7385
}
7486

75-
func (a *App) UpdateTray(tray TrayContent) {
87+
func updateTray(a *App, tray TrayContent) {
7688
if tray.Icon != "" {
7789
ico, err := os.ReadFile(GetPath(tray.Icon))
7890
if err == nil {
@@ -88,8 +100,10 @@ func (a *App) UpdateTray(tray TrayContent) {
88100
}
89101
}
90102

91-
func (a *App) ExitApp() {
92-
systray.Quit()
93-
runtime.Quit(a.Ctx)
94-
os.Exit(0)
103+
func updateTrayMenus(a *App, menus []MenuItem) {
104+
systray.ResetMenu()
105+
106+
for _, menu := range menus {
107+
createMenuItem(menu, a, nil)
108+
}
95109
}

bridge/types.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -82,9 +82,9 @@ type AppConfig struct {
8282
}
8383

8484
type TrayContent struct {
85-
Icon string `json:"icon"`
86-
Title string `json:"title"`
87-
Tooltip string `json:"tooltip"`
85+
Icon string `json:"icon,omitempty"`
86+
Title string `json:"title,omitempty"`
87+
Tooltip string `json:"tooltip,omitempty"`
8888
}
8989

9090
type WriteTracker struct {

frontend/src/bridge/app.ts

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,17 @@
11
import * as App from '@wails/go/bridge/App'
22

3-
import type { TrayContent } from '@/types/app'
4-
53
export const RestartApp = App.RestartApp
64

75
export const ExitApp = App.ExitApp
86

97
export const ShowMainWindow = App.ShowMainWindow
108

11-
export const UpdateTray = async (tray: TrayContent) => {
12-
const { icon = '', title = '', tooltip = '' } = tray
13-
await App.UpdateTray({ icon, title, tooltip })
14-
}
9+
export const UpdateTray = App.UpdateTray
1510

1611
export const UpdateTrayMenus = App.UpdateTrayMenus
1712

13+
export const UpdateTrayAndMenus = App.UpdateTrayAndMenus
14+
1815
export const GetEnv = App.GetEnv
1916

2017
export const IsStartup = App.IsStartup

frontend/src/bridge/wailsjs/go/bridge/App.d.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,8 @@ export function UnzipZIPFile(arg1:string,arg2:string):Promise<bridge.FlagResult>
7070

7171
export function UpdateTray(arg1:bridge.TrayContent):Promise<void>;
7272

73+
export function UpdateTrayAndMenus(arg1:bridge.TrayContent,arg2:Array<bridge.MenuItem>):Promise<void>;
74+
7375
export function UpdateTrayMenus(arg1:Array<bridge.MenuItem>):Promise<void>;
7476

7577
export function Upload(arg1:string,arg2:string,arg3:string,arg4:Record<string, string>,arg5:string,arg6:bridge.RequestOptions):Promise<bridge.HTTPResult>;

frontend/src/bridge/wailsjs/go/bridge/App.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,10 @@ export function UpdateTray(arg1) {
138138
return window['go']['bridge']['App']['UpdateTray'](arg1);
139139
}
140140

141+
export function UpdateTrayAndMenus(arg1, arg2) {
142+
return window['go']['bridge']['App']['UpdateTrayAndMenus'](arg1, arg2);
143+
}
144+
141145
export function UpdateTrayMenus(arg1) {
142146
return window['go']['bridge']['App']['UpdateTrayMenus'](arg1);
143147
}

frontend/src/bridge/wailsjs/go/models.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -185,9 +185,9 @@ export namespace bridge {
185185
}
186186
}
187187
export class TrayContent {
188-
icon: string;
189-
title: string;
190-
tooltip: string;
188+
icon?: string;
189+
title?: string;
190+
tooltip?: string;
191191

192192
static createFrom(source: any = {}) {
193193
return new TrayContent(source);

frontend/src/stores/appSettings.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ import {
3232
Branch,
3333
} from '@/enums/app'
3434
import i18n, { loadLocaleMessages, reloadLocale } from '@/lang'
35-
import { debounce, updateTrayMenus, ignoredError, sleep, GetSystemProxyBypass } from '@/utils'
35+
import { debounce, updateTrayAndMenus, ignoredError, sleep, GetSystemProxyBypass } from '@/utils'
3636

3737
import { useEnvStore } from './env'
3838

@@ -233,7 +233,7 @@ export const useAppSettingsStore = defineStore('app-settings', () => {
233233
() => app.value.lang,
234234
() => app.value.addPluginToMenu,
235235
],
236-
updateTrayMenus,
236+
updateTrayAndMenus,
237237
)
238238
watch(themeMode, setAppTheme, { immediate: true })
239239

frontend/src/stores/env.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { ref, watch } from 'vue'
33

44
import { GetEnv } from '@/bridge'
55
import { useAppSettingsStore, useKernelApiStore } from '@/stores'
6-
import { updateTrayMenus } from '@/utils'
6+
import { updateTrayAndMenus } from '@/utils'
77
import { SetSystemProxy, GetSystemProxy } from '@/utils'
88

99
export const useEnvStore = defineStore('env', () => {
@@ -75,7 +75,7 @@ export const useEnvStore = defineStore('env', () => {
7575
else await clearSystemProxy()
7676
}
7777

78-
watch(systemProxy, updateTrayMenus)
78+
watch(systemProxy, updateTrayAndMenus)
7979

8080
return {
8181
env,

frontend/src/stores/kernelApi.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ import {
2727
} from '@/stores'
2828
import {
2929
generateConfigFile,
30-
updateTrayMenus,
30+
updateTrayAndMenus,
3131
getKernelFileName,
3232
message,
3333
getKernelRuntimeArgs,
@@ -326,7 +326,7 @@ export const useKernelApiStore = defineStore('kernelApi', () => {
326326
return source.concat([proxySignature, unAvailable, sortByDelay]).join('')
327327
})
328328

329-
watch([watchSources, running], updateTrayMenus)
329+
watch([watchSources, running], updateTrayAndMenus)
330330

331331
return {
332332
startCore,

frontend/src/stores/plugins.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import {
1010
omitArray,
1111
ignoredError,
1212
isNumber,
13-
updateTrayMenus,
13+
updateTrayAndMenus,
1414
stringifyNoFolding,
1515
deepClone,
1616
confirm,
@@ -512,7 +512,7 @@ export const usePluginsStore = defineStore('plugins', () => {
512512

513513
watch([_watchMenus, _watchDisabled], () => {
514514
if (appSettingsStore.app.addPluginToMenu) {
515-
updateTrayMenus()
515+
updateTrayAndMenus()
516516
}
517517
})
518518

0 commit comments

Comments
 (0)