-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathsettings.go
More file actions
171 lines (142 loc) · 4.14 KB
/
settings.go
File metadata and controls
171 lines (142 loc) · 4.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
package main
import (
"errors"
"fmt"
"strconv"
"fyne.io/fyne/v2"
"fyne.io/fyne/v2/data/binding"
"fyne.io/fyne/v2/widget"
)
type Pref struct {
workMinutes int
breakMinutes int
forceWindowFocusDuration int
}
func Load() Pref {
app := fyne.CurrentApp()
workMinutes := app.Preferences().IntWithFallback("workMinutes", 25)
breakMinutes := app.Preferences().IntWithFallback("breakMinutes", 5)
forceWindowFocusDuration := app.Preferences().IntWithFallback("forceWindowFocusDuration", 60)
return Pref{
workMinutes,
breakMinutes,
forceWindowFocusDuration,
}
}
func Save(pref Pref) {
app := fyne.CurrentApp()
app.Preferences().SetInt("workMinutes", pref.workMinutes)
app.Preferences().SetInt("breakMinutes", pref.breakMinutes)
app.Preferences().SetInt("forceWindowFocusDuration", pref.forceWindowFocusDuration)
}
type Settings interface {
Show()
SetOnSubmit(callback func())
SetOnClose(callback func())
}
// type validation
var _ Settings = (*settings)(nil)
type settings struct {
win *fyne.Window
form *widget.Form
}
func NewSettings() Settings {
win := fyne.CurrentApp().NewWindow("Settings")
f := makeForm()
f.OnCancel = func() {
win.Close()
}
// Need to "refresh" to make the Submit and Cancel buttons appears
f.Refresh()
win.SetContent(f)
return &settings{win: &win, form: f}
}
func (s *settings) Show() {
(*s.win).Show()
}
func (s *settings) SetOnSubmit(callback func()) {
formSubmit := s.form.OnSubmit
s.form.OnSubmit = func() {
formSubmit()
(*s.win).Close()
callback()
}
(*s.form).Refresh()
}
func (s *settings) SetOnClose(callback func()) {
(*s.win).SetOnClosed(callback)
}
func makeForm() *widget.Form {
myPref := Load()
form := widget.NewForm()
workMinutesBinding := binding.NewInt()
_ = workMinutesBinding.Set(myPref.workMinutes)
form.AppendItem(newIntegerFormItem(workMinutesBinding, "Work duration in minutes", "Default is: %d minutes. ", NewRangeValidator(0, 999)))
breakMinutesBinding := binding.NewInt()
_ = breakMinutesBinding.Set(myPref.breakMinutes)
form.AppendItem(newIntegerFormItem(breakMinutesBinding, "Break duration in minutes", "Default is: %d minutes. ", NewRangeValidator(0, 999)))
forceWindowFocusDurationBinding := binding.NewInt()
_ = forceWindowFocusDurationBinding.Set(myPref.forceWindowFocusDuration)
form.AppendItem(newIntegerFormItem(forceWindowFocusDurationBinding, "Force Window Focus in seconds", "Default is: %d seconds. ", NewRangeValidator(0, 999)))
form.OnSubmit = func() {
workMinutes, _ := workMinutesBinding.Get()
breakMinutes, _ := breakMinutesBinding.Get()
forceWindowFocusDuration, _ := forceWindowFocusDurationBinding.Get()
newPref := Pref{
workMinutes,
breakMinutes,
forceWindowFocusDuration,
}
Save(newPref)
}
return form
}
func newIntegerFormItem(bind binding.Int, entryText string, hintText string, validator fyne.StringValidator) *widget.FormItem {
value, _ := bind.Get()
entry := newIntegerEntryWithData(binding.IntToString(bind))
entry.Validator = validator
formItem := widget.NewFormItem(entryText, entry)
formItem.HintText = fmt.Sprintf(hintText, value)
return formItem
}
type integerEntry struct {
widget.Entry
}
func newIntegerEntryWithData(data binding.String) *integerEntry {
entry := &integerEntry{}
entry.ExtendBaseWidget(entry)
entry.Bind(data)
return entry
}
func (e *integerEntry) TypedRune(r rune) {
switch r {
case '0', '1', '2', '3', '4', '5', '6', '7', '8', '9':
e.Entry.TypedRune(r)
}
}
func (e *integerEntry) TypedShortcut(shortcut fyne.Shortcut) {
paste, ok := shortcut.(*fyne.ShortcutPaste)
if !ok {
e.Entry.TypedShortcut(shortcut)
return
}
content := paste.Clipboard.Content()
if _, err := strconv.ParseInt(content, 10, 64); err == nil {
e.Entry.TypedShortcut(shortcut)
}
}
func NewRangeValidator(min int64, max int64) fyne.StringValidator {
return func(text string) error {
v, err := strconv.ParseInt(text, 10, 64)
if err != nil {
return errors.New("not a valid number")
}
if v < min {
return fmt.Errorf("must be greater than %d", min)
}
if v > max {
return fmt.Errorf("must be lesser than %d", max)
}
return nil // Nothing to validate with, same as having no validator.
}
}