perf: save windows position and restore for next launch #101

This commit is contained in:
Lykin 2023-12-12 11:40:22 +08:00
parent 44770a4a8e
commit 94c7b7ade5
4 changed files with 47 additions and 2 deletions

View File

@ -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

View File

@ -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)
}
}
}

View File

@ -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 {

View File

@ -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()