From ad9f13d557a06c7019d335343ca5f1dd1a3ef759 Mon Sep 17 00:00:00 2001 From: tiny-craft <137850705+tiny-craft@users.noreply.github.com> Date: Sun, 15 Oct 2023 04:02:09 +0800 Subject: [PATCH] perf: use goroutine to implement window resize events. perf: optimized window display in fullscreen and maximized modes. #19 --- .gitignore | 1 + backend/services/preferences_service.go | 10 +- backend/services/system_dialog.go | 47 -------- backend/services/system_service.go | 105 ++++++++++++++++++ frontend/src/AppContent.vue | 73 +++++++++--- .../common/ToolbarControlWidget.vue | 22 +++- frontend/src/components/icons/WindowClose.vue | 10 +- frontend/src/components/icons/WindowMax.vue | 10 +- frontend/src/components/icons/WindowMin.vue | 6 +- .../src/components/icons/WindowRestore.vue | 27 +++++ main.go | 9 -- 11 files changed, 225 insertions(+), 95 deletions(-) delete mode 100644 backend/services/system_dialog.go create mode 100644 backend/services/system_service.go create mode 100644 frontend/src/components/icons/WindowRestore.vue diff --git a/.gitignore b/.gitignore index f44b591..715e173 100644 --- a/.gitignore +++ b/.gitignore @@ -5,3 +5,4 @@ frontend/wailsjs design/ .vscode .idea +test diff --git a/backend/services/preferences_service.go b/backend/services/preferences_service.go index 7604b58..bfe00fa 100644 --- a/backend/services/preferences_service.go +++ b/backend/services/preferences_service.go @@ -113,10 +113,12 @@ func (p *preferencesService) GetAppVersion() (resp types.JSResp) { } func (p *preferencesService) SaveWindowSize(width, height int) { - p.UpdatePreferences(map[string]any{ - "behavior.windowWidth": width, - "behavior.windowHeight": height, - }) + if width >= consts.DEFAULT_WINDOW_WIDTH && height >= consts.DEFAULT_WINDOW_HEIGHT { + p.UpdatePreferences(map[string]any{ + "behavior.windowWidth": width, + "behavior.windowHeight": height, + }) + } } func (p *preferencesService) GetWindowSize() (width, height int) { diff --git a/backend/services/system_dialog.go b/backend/services/system_dialog.go deleted file mode 100644 index 6f06f43..0000000 --- a/backend/services/system_dialog.go +++ /dev/null @@ -1,47 +0,0 @@ -package services - -import ( - "context" - "github.com/wailsapp/wails/v2/pkg/runtime" - "log" - "sync" - "tinyrdm/backend/types" -) - -type systemService struct { - ctx context.Context -} - -var system *systemService -var onceSystem sync.Once - -func System() *systemService { - if system == nil { - onceSystem.Do(func() { - system = &systemService{} - }) - } - return system -} - -func (s *systemService) Start(ctx context.Context) { - s.ctx = ctx -} - -// SelectFile open file dialog to select a file -func (s *systemService) SelectFile(title string) (resp types.JSResp) { - filepath, err := runtime.OpenFileDialog(s.ctx, runtime.OpenDialogOptions{ - Title: title, - ShowHiddenFiles: true, - }) - if err != nil { - log.Println(err) - resp.Msg = err.Error() - return - } - resp.Success = true - resp.Data = map[string]any{ - "path": filepath, - } - return -} diff --git a/backend/services/system_service.go b/backend/services/system_service.go new file mode 100644 index 0000000..0c6cc4e --- /dev/null +++ b/backend/services/system_service.go @@ -0,0 +1,105 @@ +package services + +import ( + "context" + "github.com/wailsapp/wails/v2/pkg/runtime" + "log" + "sync" + "time" + "tinyrdm/backend/types" +) + +type systemService struct { + ctx context.Context +} + +var system *systemService +var onceSystem sync.Once + +func System() *systemService { + if system == nil { + onceSystem.Do(func() { + system = &systemService{} + go system.loopWindowEvent() + }) + } + return system +} + +func (s *systemService) Start(ctx context.Context) { + s.ctx = ctx +} + +// SelectFile open file dialog to select a file +func (s *systemService) SelectFile(title string) (resp types.JSResp) { + filepath, err := runtime.OpenFileDialog(s.ctx, runtime.OpenDialogOptions{ + Title: title, + ShowHiddenFiles: true, + }) + if err != nil { + log.Println(err) + resp.Msg = err.Error() + return + } + resp.Success = true + resp.Data = map[string]any{ + "path": filepath, + } + return +} + +func (s *systemService) loopWindowEvent() { + var fullscreen, maximised, minimised, normal bool + var width, height int + var dirty bool + for { + time.Sleep(300 * time.Millisecond) + if s.ctx == nil { + continue + } + + dirty = false + if f := runtime.WindowIsFullscreen(s.ctx); f != fullscreen { + // full-screen switched + fullscreen = f + dirty = true + } + + if w, h := runtime.WindowGetSize(s.ctx); w != width || h != height { + // window size changed + width, height = w, h + dirty = true + } + + if m := runtime.WindowIsMaximised(s.ctx); m != maximised { + maximised = m + dirty = true + } + + if m := runtime.WindowIsMinimised(s.ctx); m != minimised { + minimised = m + dirty = true + } + + if n := runtime.WindowIsNormal(s.ctx); n != normal { + normal = n + dirty = true + } + + if dirty { + runtime.EventsEmit(s.ctx, "window_changed", map[string]any{ + "fullscreen": fullscreen, + "width": width, + "height": height, + "maximised": maximised, + "minimised": minimised, + "normal": normal, + }) + + if !fullscreen && !minimised { + // save window size + Preferences().SaveWindowSize(width, height) + } + } + } +} diff --git a/frontend/src/AppContent.vue b/frontend/src/AppContent.vue index 70ffa3f..aaac1ac 100644 --- a/frontend/src/AppContent.vue +++ b/frontend/src/AppContent.vue @@ -1,7 +1,7 @@