-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathconfig_window.go
More file actions
222 lines (203 loc) · 5.14 KB
/
config_window.go
File metadata and controls
222 lines (203 loc) · 5.14 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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
package main
import (
"fmt"
"log"
"github.com/lxn/walk"
. "github.com/lxn/walk/declarative"
)
// ShowConfigWindow 显示配置对话框
func ShowConfigWindow(cfg *Config, onUpdate func()) {
var mainWindow *walk.MainWindow
var db *walk.DataBinder
// 临时结构体用于数据绑定
type ConfigViewModel struct {
TailLength float64
TailWidth float64
IsRainbow bool
IsRipple bool
RippleGrowthSpeed float64
RippleDecaySpeed float64
RippleWidth float64
Red int
Green int
Blue int
Language string
}
vm := &ConfigViewModel{
TailLength: float64(cfg.TailLength),
TailWidth: cfg.TailWidth,
IsRainbow: cfg.IsRainbow,
IsRipple: cfg.IsRipple,
RippleGrowthSpeed: cfg.RippleGrowthSpeed,
RippleDecaySpeed: cfg.RippleDecaySpeed,
RippleWidth: cfg.RippleWidth,
Red: int(cfg.TailColor[0]),
Green: int(cfg.TailColor[1]),
Blue: int(cfg.TailColor[2]),
Language: cfg.Language,
}
// 语言选项
type LangOption struct {
Name string
Value string
}
langOptions := []*LangOption{
{Name: T("LangAuto"), Value: "auto"},
{Name: T("LangZh"), Value: "zh"},
{Name: T("LangEn"), Value: "en"},
}
// 更新配置的回调
update := func() {
if err := db.Submit(); err != nil {
log.Println(err)
return
}
cfg.TailLength = int(vm.TailLength)
cfg.TailWidth = vm.TailWidth
cfg.IsRainbow = vm.IsRainbow
cfg.IsRipple = vm.IsRipple
cfg.RippleGrowthSpeed = vm.RippleGrowthSpeed
cfg.RippleDecaySpeed = vm.RippleDecaySpeed
cfg.RippleWidth = vm.RippleWidth
cfg.TailColor[0] = uint8(vm.Red)
cfg.TailColor[1] = uint8(vm.Green)
cfg.TailColor[2] = uint8(vm.Blue)
cfg.Language = vm.Language
// 应用语言设置 (注意:当前窗口的文本不会立即刷新,但下次打开或托盘菜单会生效)
SetLanguage(cfg.Language)
if onUpdate != nil {
onUpdate()
}
}
if _, err := (MainWindow{
AssignTo: &mainWindow,
Title: T("Title"),
Size: Size{Width: 320, Height: 450}, // 稍微增加高度
Layout: VBox{},
DataBinder: DataBinder{
AssignTo: &db,
Name: "vm",
DataSource: vm,
ErrorPresenter: ToolTipErrorPresenter{},
},
Children: []Widget{
GroupBox{
Title: T("Language"),
Layout: HBox{},
Children: []Widget{
Label{Text: T("Language")},
ComboBox{
Value: Bind("Language"),
Model: langOptions,
BindingMember: "Value",
DisplayMember: "Name",
OnCurrentIndexChanged: update, // 选择即生效
},
},
},
GroupBox{
Title: T("Appearance"),
Layout: Grid{Columns: 2},
Children: []Widget{
Label{Text: T("Length")},
NumberEdit{
Value: Bind("TailLength"),
OnValueChanged: update,
Decimals: 0,
},
Label{Text: T("Width")},
NumberEdit{
Value: Bind("TailWidth"),
OnValueChanged: update,
Decimals: 1,
},
},
},
GroupBox{
Title: T("RippleSettings"),
Layout: Grid{Columns: 2},
Children: []Widget{
CheckBox{
Text: T("ClickRipple"),
Checked: Bind("IsRipple"),
OnCheckedChanged: update,
ColumnSpan: 2,
},
Label{Text: T("RippleGrowth")},
NumberEdit{
Value: Bind("RippleGrowthSpeed"),
OnValueChanged: update,
Decimals: 1,
Enabled: Bind("vm.IsRipple"),
},
Label{Text: T("RippleDecay")},
NumberEdit{
Value: Bind("RippleDecaySpeed"),
OnValueChanged: update,
Decimals: 3,
Enabled: Bind("vm.IsRipple"),
},
Label{Text: T("RippleWidth")},
NumberEdit{
Value: Bind("RippleWidth"),
OnValueChanged: update,
Decimals: 1,
Enabled: Bind("vm.IsRipple"),
},
},
},
GroupBox{
Title: T("ColorEffects"),
Layout: Grid{Columns: 2},
Children: []Widget{
CheckBox{
Text: T("RainbowMode"),
Checked: Bind("IsRainbow"),
OnCheckedChanged: update,
ColumnSpan: 2,
},
Label{Text: T("Red")},
Slider{
Value: Bind("Red"),
MinValue: 0,
MaxValue: 255,
OnValueChanged: update,
Enabled: Bind("!vm.IsRainbow"),
},
Label{Text: T("Green")},
Slider{
Value: Bind("Green"),
MinValue: 0,
MaxValue: 255,
OnValueChanged: update,
Enabled: Bind("!vm.IsRainbow"),
},
Label{Text: T("Blue")},
Slider{
Value: Bind("Blue"),
MinValue: 0,
MaxValue: 255,
OnValueChanged: update,
Enabled: Bind("!vm.IsRainbow"),
},
},
},
Composite{
Layout: HBox{},
Children: []Widget{
HSpacer{},
PushButton{
Text: T("SaveClose"),
OnClicked: func() {
update()
SaveConfig("config.json", cfg)
mainWindow.Close()
},
},
},
},
},
}).Run(); err != nil {
fmt.Println(err)
}
}