/
entry.go
64 lines (52 loc) · 1.22 KB
/
entry.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
package fyner
import (
"sync"
"deedles.dev/state"
"fyne.io/fyne/v2"
"fyne.io/fyne/v2/widget"
)
// Entry wraps widget.Entry to provide a text entry component.
//
// TODO: Make sure that this API works correctly for strange cases
// like only allowing uppercase letters.
type Entry struct {
once sync.Once
w *widget.Entry
// Text is the editable text currently in the entry.
Text state.MutableState[string]
textCancel state.CancelFunc
// Disabled, if true, prevents user input to the text field.
Disabled state.State[bool]
disabledCancel state.CancelFunc
}
func (entry *Entry) init() {
entry.once.Do(func() {
entry.w = widget.NewEntry()
})
}
func (entry *Entry) CanvasObject() fyne.CanvasObject {
entry.init()
return entry.w
}
func (entry *Entry) Bind() {
entry.init()
entry.Unbind()
if entry.Text != nil {
entry.textCancel = entry.Text.Listen(entry.w.SetText)
entry.w.OnChanged = entry.Text.Set
}
if entry.Disabled != nil {
entry.disabledCancel = entry.Disabled.Listen(func(disabled bool) {
if disabled {
entry.w.Disable()
return
}
entry.w.Enable()
})
}
}
func (entry *Entry) Unbind() {
cancel(&entry.textCancel)
entry.w.OnChanged = nil
cancel(&entry.disabledCancel)
}