perf: save window size before close #34

This commit is contained in:
tiny-craft 2023-09-28 21:45:44 +08:00
parent da4b8b9a4f
commit 7aba27e5f9
6 changed files with 63 additions and 11 deletions

View File

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

View File

@ -7,6 +7,7 @@ import (
"sort" "sort"
"strings" "strings"
"sync" "sync"
"tinyrdm/backend/consts"
storage2 "tinyrdm/backend/storage" storage2 "tinyrdm/backend/storage"
"tinyrdm/backend/types" "tinyrdm/backend/types"
"tinyrdm/backend/utils/coll" "tinyrdm/backend/utils/coll"
@ -101,6 +102,28 @@ func (p *preferencesService) GetAppVersion() (resp types.JSResp) {
return 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 { type latestRelease struct {
Name string `json:"name"` Name string `json:"name"`
TagName string `json:"tag_name"` TagName string `json:"tag_name"`

View File

@ -5,6 +5,7 @@ import (
"gopkg.in/yaml.v3" "gopkg.in/yaml.v3"
"strings" "strings"
"sync" "sync"
"tinyrdm/backend/consts"
) )
type PreferencesStorage struct { type PreferencesStorage struct {
@ -20,15 +21,19 @@ func NewPreferences() *PreferencesStorage {
func (p *PreferencesStorage) DefaultPreferences() map[string]any { func (p *PreferencesStorage) DefaultPreferences() map[string]any {
return 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{ "general": map[string]any{
"language": "auto", "language": "auto",
"font": "", "font": "",
"font_size": 14, "font_size": consts.DEFAULT_FONT_SIZE,
"use_sys_proxy": false, "use_sys_proxy": false,
"use_sys_proxy_http": false, "use_sys_proxy_http": false,
"check_update": true, "check_update": true,
"skip_version": "", "skip_version": "",
"aside_width": 300,
}, },
"editor": map[string]any{ "editor": map[string]any{
"font": "", "font": "",

View File

@ -59,7 +59,7 @@ const startResize = () => {
} }
const asideWidthVal = computed(() => { const asideWidthVal = computed(() => {
return prefStore.general.asideWidth + 'px' return prefStore.behavior.asideWidth + 'px'
}) })
const dragging = computed(() => { const dragging = computed(() => {
@ -115,7 +115,7 @@ const border = computed(() => {
<div <div
id="app-toolbar-title" id="app-toolbar-title"
:style="{ :style="{
width: `${data.navMenuWidth + prefStore.general.asideWidth - 4}px`, width: `${data.navMenuWidth + prefStore.behavior.asideWidth - 4}px`,
paddingLeft: isMacOS() ? '70px' : '10px', paddingLeft: isMacOS() ? '70px' : '10px',
}"> }">
<n-space align="center" :wrap-item="false" :wrap="false" :size="3"> <n-space align="center" :wrap-item="false" :wrap="false" :size="3">

View File

@ -31,6 +31,11 @@ const usePreferencesStore = defineStore('preferences', {
* @returns {Preferences} * @returns {Preferences}
*/ */
state: () => ({ state: () => ({
behavior: {
asideWidth: 300,
windowWidth: 0,
windowHeight: 0,
},
general: { general: {
theme: 'auto', theme: 'auto',
language: 'en', language: 'en',
@ -40,7 +45,6 @@ const usePreferencesStore = defineStore('preferences', {
useSysProxyHttp: false, useSysProxyHttp: false,
checkUpdate: false, checkUpdate: false,
skipVersion: '', skipVersion: '',
asideWidth: 300,
}, },
editor: { editor: {
font: '', font: '',
@ -211,7 +215,12 @@ const usePreferencesStore = defineStore('preferences', {
} }
return result 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) const { success, msg } = await SetPreferences(pf)
return success === true return success === true
}, },
@ -241,7 +250,7 @@ const usePreferencesStore = defineStore('preferences', {
}, },
setAsideWidth(width) { setAsideWidth(width) {
this.general.asideWidth = width this.behavior.asideWidth = width
}, },
async checkForUpdate(manual = false) { async checkForUpdate(manual = false) {

17
main.go
View File

@ -10,7 +10,9 @@ import (
"github.com/wailsapp/wails/v2/pkg/options/linux" "github.com/wailsapp/wails/v2/pkg/options/linux"
"github.com/wailsapp/wails/v2/pkg/options/mac" "github.com/wailsapp/wails/v2/pkg/options/mac"
"github.com/wailsapp/wails/v2/pkg/options/windows" "github.com/wailsapp/wails/v2/pkg/options/windows"
runtime2 "github.com/wailsapp/wails/v2/pkg/runtime"
"runtime" "runtime"
"tinyrdm/backend/consts"
"tinyrdm/backend/services" "tinyrdm/backend/services"
) )
@ -28,6 +30,7 @@ func main() {
connSvc := services.Connection() connSvc := services.Connection()
prefSvc := services.Preferences() prefSvc := services.Preferences()
prefSvc.SetAppVersion(version) prefSvc.SetAppVersion(version)
windowWidth, windowHeight := prefSvc.GetWindowSize()
// menu // menu
appMenu := menu.NewMenu() appMenu := menu.NewMenu()
@ -40,10 +43,10 @@ func main() {
// Create application with options // Create application with options
err := wails.Run(&options.App{ err := wails.Run(&options.App{
Title: "Tiny RDM", Title: "Tiny RDM",
Width: 1024, Width: windowWidth,
Height: 768, Height: windowHeight,
MinWidth: 1024, MinWidth: consts.DEFAULT_WINDOW_WIDTH,
MinHeight: 768, MinHeight: consts.DEFAULT_WINDOW_HEIGHT,
Frameless: runtime.GOOS != "darwin", Frameless: runtime.GOOS != "darwin",
Menu: appMenu, Menu: appMenu,
AssetServer: &assetserver.Options{ AssetServer: &assetserver.Options{
@ -55,6 +58,12 @@ func main() {
connSvc.Start(ctx) connSvc.Start(ctx)
}, },
OnShutdown: func(ctx context.Context) { 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) connSvc.Stop(ctx)
}, },
Bind: []interface{}{ Bind: []interface{}{