-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathsettings.go
More file actions
79 lines (67 loc) · 1.69 KB
/
Copy pathsettings.go
File metadata and controls
79 lines (67 loc) · 1.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
package main
import (
"encoding/json"
"fyne.io/fyne/v2"
"fyne.io/fyne/v2/theme"
)
// AppSettings 应用设置
type AppSettings struct {
ThemeType string `json:"themeType"`
FontName string `json:"fontName"`
FontSize float32 `json:"fontSize"`
}
const (
ThemeTypeDark = "dark"
ThemeTypeLight = "light"
ThemeTypeSystem = "system"
// 设置存储键
APP_SETTINGS = "app_settings"
)
// DefaultSettings 返回默认设置
func DefaultSettings() *AppSettings {
return &AppSettings{
ThemeType: ThemeTypeDark,
FontName: "",
FontSize: 0, // 0表示使用默认字体大小
}
}
// GetTheme 根据设置获取主题
func (s *AppSettings) GetTheme() fyne.Theme {
switch s.ThemeType {
case ThemeTypeLight:
return theme.LightTheme()
case ThemeTypeDark:
return theme.DarkTheme()
default:
return theme.DarkTheme()
}
}
// SaveSettings 保存设置到应用偏好
func (w *Window) SaveSettings(settings *AppSettings) error {
data, err := json.Marshal(settings)
if err != nil {
return err
}
w.app.Preferences().SetString(APP_SETTINGS, string(data))
return nil
}
// LoadSettings 从应用偏好加载设置
func (w *Window) LoadSettings() *AppSettings {
settingsJson := w.app.Preferences().String(APP_SETTINGS)
if settingsJson == "" {
return DefaultSettings()
}
var settings AppSettings
err := json.Unmarshal([]byte(settingsJson), &settings)
if err != nil {
return DefaultSettings()
}
return &settings
}
// ApplySettings 应用设置
func (w *Window) ApplySettings(settings *AppSettings) {
// 应用主题
w.app.Settings().SetTheme(settings.GetTheme())
// TODO: 应用字体设置需要进一步实现
// Fyne目前不直接支持全局字体设置,但可以在控件级别设置
}