/
form.go
256 lines (221 loc) · 7.13 KB
/
form.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
245
246
247
248
249
250
251
252
253
254
255
256
package widget
import (
"errors"
"fyne.io/fyne/v2"
"fyne.io/fyne/v2/canvas"
"fyne.io/fyne/v2/internal/cache"
"fyne.io/fyne/v2/layout"
"fyne.io/fyne/v2/theme"
)
// errFormItemInitialState defines the error if the initial validation for a FormItem result
// in an error
var errFormItemInitialState = errors.New("widget.FormItem initial state error")
// FormItem provides the details for a row in a form
type FormItem struct {
Text string
Widget fyne.CanvasObject
// Since: 2.0
HintText string
validationError error
invalid bool
helperOutput *canvas.Text
}
// NewFormItem creates a new form item with the specified label text and input widget
func NewFormItem(text string, widget fyne.CanvasObject) *FormItem {
return &FormItem{Text: text, Widget: widget}
}
// Form widget is two column grid where each row has a label and a widget (usually an input).
// The last row of the grid will contain the appropriate form control buttons if any should be shown.
// Setting OnSubmit will set the submit button to be visible and call back the function when tapped.
// Setting OnCancel will do the same for a cancel button.
// If you change OnSubmit/OnCancel after the form is created and rendered, you need to call
// Refresh() to update the form with the correct buttons.
// Setting OnSubmit/OnCancel to nil will remove the buttons.
type Form struct {
BaseWidget
Items []*FormItem
OnSubmit func()
OnCancel func()
SubmitText string
CancelText string
itemGrid *fyne.Container
buttonBox *fyne.Container
cancelButton *Button
submitButton *Button
}
// Append adds a new row to the form, using the text as a label next to the specified Widget
func (f *Form) Append(text string, widget fyne.CanvasObject) {
item := &FormItem{Text: text, Widget: widget}
f.AppendItem(item)
}
// AppendItem adds the specified row to the end of the Form
func (f *Form) AppendItem(item *FormItem) {
f.ExtendBaseWidget(f) // could be called before render
f.Items = append(f.Items, item)
if f.itemGrid != nil {
f.itemGrid.Add(f.createLabel(item.Text))
f.itemGrid.Add(f.createInput(item))
f.setUpValidation(item.Widget, len(f.Items)-1)
}
f.Refresh()
}
// MinSize returns the size that this widget should not shrink below
func (f *Form) MinSize() fyne.Size {
f.ExtendBaseWidget(f)
return f.BaseWidget.MinSize()
}
// Refresh updates the widget state when requested.
func (f *Form) Refresh() {
cache.Renderer(f.super()) // we are about to make changes to renderer created content... not great!
f.updateButtons()
f.updateLabels()
f.BaseWidget.Refresh()
canvas.Refresh(f.super()) // refresh ourselves for BG color - the above updates the content
}
func (f *Form) createInput(item *FormItem) fyne.CanvasObject {
_, ok := item.Widget.(fyne.Validatable)
if item.HintText == "" {
if !ok {
return item.Widget
}
if e, ok := item.Widget.(*Entry); ok && e.Validator == nil { // we don't have validation
return item.Widget
}
}
text := canvas.NewText(item.HintText, theme.PlaceHolderColor())
text.TextSize = theme.CaptionTextSize()
text.Move(fyne.NewPos(theme.Padding()*2, theme.Padding()*-0.5))
item.helperOutput = text
f.updateHelperText(item)
return fyne.NewContainerWithLayout(layout.NewVBoxLayout(), item.Widget, fyne.NewContainerWithoutLayout(text))
}
func (f *Form) createLabel(text string) *canvas.Text {
return &canvas.Text{Text: text,
Alignment: fyne.TextAlignTrailing,
TextSize: theme.TextSize(),
TextStyle: fyne.TextStyle{Bold: true}}
}
func (f *Form) updateButtons() {
if f.CancelText == "" {
f.CancelText = "Cancel"
}
if f.SubmitText == "" {
f.SubmitText = "Submit"
}
// set visibility on the buttons
if f.OnCancel == nil {
f.cancelButton.Hide()
} else {
f.cancelButton.SetText(f.CancelText)
f.cancelButton.OnTapped = f.OnCancel
f.cancelButton.Show()
}
if f.OnSubmit == nil {
f.submitButton.Hide()
} else {
f.submitButton.SetText(f.SubmitText)
f.submitButton.OnTapped = f.OnSubmit
f.submitButton.Show()
}
if f.OnCancel == nil && f.OnSubmit == nil {
f.buttonBox.Hide()
} else {
f.buttonBox.Show()
}
}
func (f *Form) checkValidation(err error) {
if err != nil {
f.submitButton.Disable()
return
}
for _, item := range f.Items {
if item.invalid {
f.submitButton.Disable()
return
}
}
f.submitButton.Enable()
}
func (f *Form) setUpValidation(widget fyne.CanvasObject, i int) {
updateValidation := func(err error) {
if err == errFormItemInitialState {
return
}
f.Items[i].validationError = err
f.Items[i].invalid = err != nil
f.checkValidation(err)
f.updateHelperText(f.Items[i])
}
if w, ok := widget.(fyne.Validatable); ok {
f.Items[i].invalid = w.Validate() != nil
if e, ok := w.(*Entry); ok {
e.onFocusChanged = func(bool) {
updateValidation(e.validationError)
}
if e.Validator != nil && f.Items[i].invalid {
// set initial state error to guarantee next error (if triggers) is always different
e.SetValidationError(errFormItemInitialState)
}
}
w.SetOnValidationChanged(updateValidation)
}
}
func (f *Form) updateHelperText(item *FormItem) {
if item.helperOutput == nil {
return // testing probably, either way not rendered yet
}
showHintIfError := false
if e, ok := item.Widget.(*Entry); ok && (!e.dirty || e.focused) {
showHintIfError = true
}
if item.validationError == nil || showHintIfError {
item.helperOutput.Text = item.HintText
item.helperOutput.Color = theme.PlaceHolderColor()
} else {
item.helperOutput.Text = item.validationError.Error()
item.helperOutput.Color = theme.ErrorColor()
}
item.helperOutput.Refresh()
}
func (f *Form) updateLabels() {
for i, item := range f.Items {
l := f.itemGrid.Objects[i*2].(*canvas.Text)
l.TextSize = theme.TextSize()
if dis, ok := item.Widget.(fyne.Disableable); ok {
if dis.Disabled() {
l.Color = theme.DisabledColor()
} else {
l.Color = theme.ForegroundColor()
}
}
l.Text = item.Text
l.Refresh()
f.updateHelperText(item)
}
}
// CreateRenderer is a private method to Fyne which links this widget to its renderer
func (f *Form) CreateRenderer() fyne.WidgetRenderer {
f.ExtendBaseWidget(f)
f.cancelButton = &Button{Icon: theme.CancelIcon(), OnTapped: f.OnCancel}
f.submitButton = &Button{Icon: theme.ConfirmIcon(), OnTapped: f.OnSubmit, Importance: HighImportance}
f.buttonBox = fyne.NewContainerWithLayout(layout.NewHBoxLayout(), layout.NewSpacer(), f.cancelButton, f.submitButton)
objects := make([]fyne.CanvasObject, len(f.Items)*2)
for i, item := range f.Items {
objects[i*2] = f.createLabel(item.Text)
f.setUpValidation(item.Widget, i)
objects[i*2+1] = f.createInput(item)
}
f.itemGrid = fyne.NewContainerWithLayout(layout.NewFormLayout(), objects...)
renderer := &simpleRenderer{content: fyne.NewContainerWithLayout(layout.NewVBoxLayout(), f.itemGrid, f.buttonBox)}
f.updateButtons()
f.updateLabels()
f.checkValidation(nil) // will trigger a validation check for correct intial validation status
return renderer
}
// NewForm creates a new form widget with the specified rows of form items
// and (if any of them should be shown) a form controls row at the bottom
func NewForm(items ...*FormItem) *Form {
form := &Form{Items: items}
form.ExtendBaseWidget(form)
return form
}