diff --git a/backend/services/preferences_service.go b/backend/services/preferences_service.go index 1f5b1b0..05705df 100644 --- a/backend/services/preferences_service.go +++ b/backend/services/preferences_service.go @@ -1,8 +1,10 @@ package services import ( + "context" "encoding/json" "github.com/adrg/sysfont" + runtime2 "github.com/wailsapp/wails/v2/pkg/runtime" "net/http" "sort" "strings" @@ -139,6 +141,38 @@ func (p *preferencesService) GetWindowSize() (width, height int, maximised bool) return } +func (p *preferencesService) GetWindowPosition(ctx context.Context) (x, y int) { + data := p.pref.GetPreferences() + x, y = data.Behavior.WindowPosX, data.Behavior.WindowPosY + width, height := data.Behavior.WindowWidth, data.Behavior.WindowHeight + var screenWidth, screenHeight int + if screens, err := runtime2.ScreenGetAll(ctx); err == nil { + for _, screen := range screens { + if screen.IsCurrent { + screenWidth, screenHeight = screen.Size.Width, screen.Size.Height + break + } + } + } + if screenWidth <= 0 || screenHeight <= 0 { + screenWidth, screenHeight = consts.DEFAULT_WINDOW_WIDTH, consts.DEFAULT_WINDOW_HEIGHT + } + if x <= 0 || x+width > screenWidth || y <= 0 || y+height > screenHeight { + // out of screen, reset to center + x, y = (screenWidth-width)/2, (screenHeight-height)/2 + } + return +} + +func (p *preferencesService) SaveWindowPosition(x, y int) { + if x > 0 || y > 0 { + p.UpdatePreferences(map[string]any{ + "behavior.windowPosX": x, + "behavior.windowPosY": y, + }) + } +} + func (p *preferencesService) GetScanSize() int { data := p.pref.GetPreferences() size := data.General.ScanSize diff --git a/backend/services/system_service.go b/backend/services/system_service.go index d4b2e5e..2be30b2 100644 --- a/backend/services/system_service.go +++ b/backend/services/system_service.go @@ -63,7 +63,7 @@ func (s *systemService) SelectFile(title string) (resp types.JSResp) { func (s *systemService) loopWindowEvent() { var fullscreen, maximised, minimised, normal bool - var width, height int + var width, height, posx, posy int var dirty bool for { time.Sleep(300 * time.Millisecond) @@ -84,6 +84,12 @@ func (s *systemService) loopWindowEvent() { dirty = true } + if x, y := runtime.WindowGetPosition(s.ctx); x != posx || y != posy { + // window position changed + posx, posy = x, y + dirty = true + } + if m := runtime.WindowIsMaximised(s.ctx); m != maximised { maximised = m dirty = true @@ -110,8 +116,9 @@ func (s *systemService) loopWindowEvent() { }) if !fullscreen && !minimised { - // save window size + // save window size and position Preferences().SaveWindowSize(width, height, maximised) + Preferences().SaveWindowPosition(posx, posy) } } } diff --git a/backend/types/preferences.go b/backend/types/preferences.go index 1a1e595..01815ec 100644 --- a/backend/types/preferences.go +++ b/backend/types/preferences.go @@ -36,6 +36,8 @@ type PreferencesBehavior struct { WindowWidth int `json:"windowWidth" yaml:"window_width"` WindowHeight int `json:"windowHeight" yaml:"window_height"` WindowMaximised bool `json:"windowMaximised" yaml:"window_maximised"` + WindowPosX int `json:"windowPosX" yaml:"window_pos_x"` + WindowPosY int `json:"windowPosY" yaml:"window_pos_y"` } type PreferencesGeneral struct { diff --git a/main.go b/main.go index 34cab2c..e5ec020 100644 --- a/main.go +++ b/main.go @@ -74,6 +74,8 @@ func main() { }, OnDomReady: func(ctx context.Context) { runtime2.WindowShow(ctx) + x, y := prefSvc.GetWindowPosition(ctx) + runtime2.WindowSetPosition(ctx, x, y) }, OnShutdown: func(ctx context.Context) { browserSvc.Stop()