From 7aba27e5f960db78067bc6a0c0ad25ce7d874dd2 Mon Sep 17 00:00:00 2001 From: tiny-craft <137850705+tiny-craft@users.noreply.github.com> Date: Thu, 28 Sep 2023 21:45:44 +0800 Subject: [PATCH] perf: save window size before close #34 --- backend/consts/default_config.go | 6 ++++++ backend/services/preferences_service.go | 23 +++++++++++++++++++++++ backend/storage/preferences.go | 9 +++++++-- frontend/src/AppContent.vue | 4 ++-- frontend/src/stores/preferences.js | 15 ++++++++++++--- main.go | 17 +++++++++++++---- 6 files changed, 63 insertions(+), 11 deletions(-) create mode 100644 backend/consts/default_config.go diff --git a/backend/consts/default_config.go b/backend/consts/default_config.go new file mode 100644 index 0000000..58f168b --- /dev/null +++ b/backend/consts/default_config.go @@ -0,0 +1,6 @@ +package consts + +const DEFAULT_FONT_SIZE = 14 +const DEFAULT_ASIDE_WIDTH = 300 +const DEFAULT_WINDOW_WIDTH = 1024 +const DEFAULT_WINDOW_HEIGHT = 768 diff --git a/backend/services/preferences_service.go b/backend/services/preferences_service.go index ef58fec..4c92cbf 100644 --- a/backend/services/preferences_service.go +++ b/backend/services/preferences_service.go @@ -7,6 +7,7 @@ import ( "sort" "strings" "sync" + "tinyrdm/backend/consts" storage2 "tinyrdm/backend/storage" "tinyrdm/backend/types" "tinyrdm/backend/utils/coll" @@ -101,6 +102,28 @@ func (p *preferencesService) GetAppVersion() (resp types.JSResp) { return } +func (p *preferencesService) SaveWindowSize(width, height int) { + p.SetPreferences(map[string]any{ + "behavior": map[string]any{ + "window_width": width, + "window_height": height, + }, + }) +} + +func (p *preferencesService) GetWindowSize() (width, height int) { + data := p.pref.GetPreferences() + w, h := data["behavior.window_width"], data["behavior.window_height"] + var ok bool + if width, ok = w.(int); !ok { + return consts.DEFAULT_WINDOW_WIDTH, consts.DEFAULT_WINDOW_HEIGHT + } + if height, ok = h.(int); !ok { + return consts.DEFAULT_WINDOW_WIDTH, consts.DEFAULT_WINDOW_HEIGHT + } + return +} + type latestRelease struct { Name string `json:"name"` TagName string `json:"tag_name"` diff --git a/backend/storage/preferences.go b/backend/storage/preferences.go index 6d678b8..1590d44 100644 --- a/backend/storage/preferences.go +++ b/backend/storage/preferences.go @@ -5,6 +5,7 @@ import ( "gopkg.in/yaml.v3" "strings" "sync" + "tinyrdm/backend/consts" ) type PreferencesStorage struct { @@ -20,15 +21,19 @@ func NewPreferences() *PreferencesStorage { func (p *PreferencesStorage) DefaultPreferences() map[string]any { return map[string]any{ + "behavior": map[string]any{ + "aside_width": consts.DEFAULT_ASIDE_WIDTH, + "window_width": consts.DEFAULT_WINDOW_WIDTH, + "window_height": consts.DEFAULT_WINDOW_HEIGHT, + }, "general": map[string]any{ "language": "auto", "font": "", - "font_size": 14, + "font_size": consts.DEFAULT_FONT_SIZE, "use_sys_proxy": false, "use_sys_proxy_http": false, "check_update": true, "skip_version": "", - "aside_width": 300, }, "editor": map[string]any{ "font": "", diff --git a/frontend/src/AppContent.vue b/frontend/src/AppContent.vue index 020ebe2..70ffa3f 100644 --- a/frontend/src/AppContent.vue +++ b/frontend/src/AppContent.vue @@ -59,7 +59,7 @@ const startResize = () => { } const asideWidthVal = computed(() => { - return prefStore.general.asideWidth + 'px' + return prefStore.behavior.asideWidth + 'px' }) const dragging = computed(() => { @@ -115,7 +115,7 @@ const border = computed(() => {
diff --git a/frontend/src/stores/preferences.js b/frontend/src/stores/preferences.js index 6345290..84a783a 100644 --- a/frontend/src/stores/preferences.js +++ b/frontend/src/stores/preferences.js @@ -31,6 +31,11 @@ const usePreferencesStore = defineStore('preferences', { * @returns {Preferences} */ state: () => ({ + behavior: { + asideWidth: 300, + windowWidth: 0, + windowHeight: 0, + }, general: { theme: 'auto', language: 'en', @@ -40,7 +45,6 @@ const usePreferencesStore = defineStore('preferences', { useSysProxyHttp: false, checkUpdate: false, skipVersion: '', - asideWidth: 300, }, editor: { font: '', @@ -211,7 +215,12 @@ const usePreferencesStore = defineStore('preferences', { } return result } - const pf = Object.assign({}, obj2Map('general', this.general), obj2Map('editor', this.editor)) + const pf = Object.assign( + {}, + obj2Map('behavior', this.behavior), + obj2Map('general', this.general), + obj2Map('editor', this.editor), + ) const { success, msg } = await SetPreferences(pf) return success === true }, @@ -241,7 +250,7 @@ const usePreferencesStore = defineStore('preferences', { }, setAsideWidth(width) { - this.general.asideWidth = width + this.behavior.asideWidth = width }, async checkForUpdate(manual = false) { diff --git a/main.go b/main.go index a9605ca..b159bdf 100644 --- a/main.go +++ b/main.go @@ -10,7 +10,9 @@ import ( "github.com/wailsapp/wails/v2/pkg/options/linux" "github.com/wailsapp/wails/v2/pkg/options/mac" "github.com/wailsapp/wails/v2/pkg/options/windows" + runtime2 "github.com/wailsapp/wails/v2/pkg/runtime" "runtime" + "tinyrdm/backend/consts" "tinyrdm/backend/services" ) @@ -28,6 +30,7 @@ func main() { connSvc := services.Connection() prefSvc := services.Preferences() prefSvc.SetAppVersion(version) + windowWidth, windowHeight := prefSvc.GetWindowSize() // menu appMenu := menu.NewMenu() @@ -40,10 +43,10 @@ func main() { // Create application with options err := wails.Run(&options.App{ Title: "Tiny RDM", - Width: 1024, - Height: 768, - MinWidth: 1024, - MinHeight: 768, + Width: windowWidth, + Height: windowHeight, + MinWidth: consts.DEFAULT_WINDOW_WIDTH, + MinHeight: consts.DEFAULT_WINDOW_HEIGHT, Frameless: runtime.GOOS != "darwin", Menu: appMenu, AssetServer: &assetserver.Options{ @@ -55,6 +58,12 @@ func main() { connSvc.Start(ctx) }, OnShutdown: func(ctx context.Context) { + // save current window size + width, height := runtime2.WindowGetSize(ctx) + if width > 0 && height > 0 { + prefSvc.SaveWindowSize(width, height) + } + connSvc.Stop(ctx) }, Bind: []interface{}{