-
Notifications
You must be signed in to change notification settings - Fork 5
/
m_settings.go
244 lines (211 loc) · 5.51 KB
/
m_settings.go
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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
package main
import (
"image/color"
"time"
"fyne.io/fyne/v2"
"fyne.io/fyne/v2/canvas"
"fyne.io/fyne/v2/container"
"fyne.io/fyne/v2/theme"
"fyne.io/fyne/v2/widget"
)
const (
DefaultScale = 1.0
DefaultTheme = "dark"
)
// access to user interfaces to control Fyne settings
type Settings struct {
colors []fyne.CanvasObject
}
// a new settings instance with the current configuration loaded
func NewSettings() *Settings {
s := &Settings{}
return s
}
// scale
type scaleItems struct {
scale float32
name string
preview *canvas.Text
button *widget.Button
}
var scales = []*scaleItems{
{scale: 0.5, name: "Tiny"},
{scale: 0.8, name: "Small"},
{scale: 1.0, name: "Normal"},
{scale: 1.3, name: "Big"},
{scale: 1.8, name: "Large"},
{scale: 2.2, name: "Huge"}}
func (s *Settings) choosedScale(value float32) {
a.state.Scale = value
for _, scale := range scales {
if scale.scale == value {
scale.button.Importance = widget.HighImportance
scale.button.Refresh()
} else {
scale.button.Importance = widget.MediumImportance
scale.button.Refresh()
}
}
}
func (s *Settings) scalesRow() *fyne.Container {
var buttons = make([]fyne.CanvasObject, len(scales))
for i, scale := range scales {
value := scale.scale
button := widget.NewButton(scale.name, func() {
s.choosedScale(value)
})
if a.state.Scale == scale.scale {
button.Importance = widget.HighImportance
}
scale.button = button
buttons[i] = button
}
return container.NewGridWithColumns(len(scales), buttons...)
}
func (s *Settings) scalePreviewsRow(value float32) *fyne.Container {
var previews = make([]fyne.CanvasObject, len(scales))
for i, scale := range scales {
text := canvas.NewText("A", theme.ForegroundColor())
text.Alignment = fyne.TextAlignCenter
text.TextSize = theme.TextSize() * scale.scale / value
scale.preview = text
previews[i] = text
}
return container.NewGridWithColumns(len(scales), previews...)
}
// color
type colorButton struct {
widget.BaseWidget
name string
color color.Color
s *Settings
}
func (s *Settings) colorsRow() *fyne.Container {
for _, c := range theme.PrimaryColorNames() {
b := newColorButton(c, theme.PrimaryColorNamed(c), s)
s.colors = append(s.colors, b)
}
return container.NewGridWithColumns(len(s.colors), s.colors...)
}
func newColorButton(n string, c color.Color, s *Settings) *colorButton {
b := &colorButton{name: n, color: c, s: s}
b.ExtendBaseWidget(b)
return b
}
func (b *colorButton) CreateRenderer() fyne.WidgetRenderer {
r := canvas.NewRectangle(b.color)
r.StrokeWidth = 2
if b.name == a.state.Color {
r.StrokeColor = theme.PrimaryColor()
}
return &colorRenderer{btn: b, rect: r, objs: []fyne.CanvasObject{r}}
}
func (b *colorButton) Tapped(_ *fyne.PointEvent) {
a.state.Color = b.name
for _, child := range b.s.colors {
child.Refresh()
}
a.Settings().SetTheme(&Theme{})
}
type colorRenderer struct {
btn *colorButton
rect *canvas.Rectangle
objs []fyne.CanvasObject
}
func (r *colorRenderer) Layout(s fyne.Size) {
r.rect.Resize(s)
}
func (r *colorRenderer) MinSize() fyne.Size {
return fyne.NewSize(20, 20)
}
func (r *colorRenderer) Refresh() {
if r.btn.name == a.state.Color {
r.rect.StrokeColor = theme.PrimaryColor()
} else {
r.rect.StrokeColor = color.Transparent
}
r.rect.FillColor = r.btn.color
r.rect.Refresh()
}
func (r *colorRenderer) Objects() []fyne.CanvasObject {
return r.objs
}
func (r *colorRenderer) Destroy() {
}
// theme
func (s *Settings) themesRow() *widget.RadioGroup {
themeNames := []string{"Dark", "Light"}
themes := widget.NewRadioGroup(themeNames, func(selected string) {
if selected == "Dark" {
a.state.Theme = "dark"
} else {
a.state.Theme = "light"
}
a.Settings().SetTheme(&Theme{})
})
if a.state.Theme == "dark" {
themes.SetSelected("Dark")
} else {
themes.SetSelected("Light")
}
themes.Horizontal = true
return themes
}
// date format
func (s *Settings) datesRow(a *App) *fyne.Container {
toFormat := map[string]string{
"DD.MM.YYYY": "02.01.2006 15:04:05",
"MM.DD.YYYY": "01.02.2006 15:04:05",
"YYYY.MM.DD": "2006.01.02 15:04:05",
}
byFormat := make(map[string]string, len(toFormat))
for k, v := range toFormat {
byFormat[v] = k
}
display := widget.NewLabel(time.Now().Format(a.state.DisplayDateFormat))
label := widget.NewLabel("Sample:")
label.Alignment = fyne.TextAlignTrailing
options := []string{}
for k := range toFormat {
options = append(options, k)
}
choice := widget.NewSelectEntry(options)
choice.SetText(byFormat[a.state.DisplayDateFormat])
choice.OnChanged = func(s string) {
a.state.DisplayDateFormat = toFormat[s]
display.SetText(time.Now().Format(a.state.DisplayDateFormat))
}
return container.NewGridWithColumns(3, choice, label, display)
}
func (s *Settings) modeRow(a *App) *widget.RadioGroup {
mode := widget.NewRadioGroup([]string{"Simple", "Advanced"},
func(selected string) {
if selected == "Simple" {
a.state.Simple = true
} else {
a.state.Simple = false
}
})
if a.state.Simple {
mode.SetSelected("Simple")
} else {
mode.SetSelected("Advanced")
}
mode.Horizontal = true
return mode
}
func (s *Settings) hotkeysRow() *widget.Label {
hotkey := "No shortcut for hotkeys"
for i := range a.ControlShortCuts {
if a.ControlShortCuts[i].Name == "Hotkeys" {
hotkey = "To see hotkeys press Ctrl + " + string(a.ControlShortCuts[i].KeyName)
}
}
for i := range a.AltShortCuts {
if a.AltShortCuts[i].Name == "Hotkeys" {
hotkey = "To see hotkeys press Alt + " + string(a.AltShortCuts[i].KeyName)
}
}
lbl := widget.NewLabel(hotkey)
return lbl
}