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"
"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"`

View File

@ -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": "",

View File

@ -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(() => {
<div
id="app-toolbar-title"
:style="{
width: `${data.navMenuWidth + prefStore.general.asideWidth - 4}px`,
width: `${data.navMenuWidth + prefStore.behavior.asideWidth - 4}px`,
paddingLeft: isMacOS() ? '70px' : '10px',
}">
<n-space align="center" :wrap-item="false" :wrap="false" :size="3">

View File

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

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/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{}{