/
check.go
55 lines (44 loc) · 974 Bytes
/
check.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
package fyner
import (
"sync"
"deedles.dev/state"
"fyne.io/fyne/v2"
"fyne.io/fyne/v2/widget"
)
// Check wraps widget.Check to provide a toggleable checkbox
// component.
type Check struct {
once sync.Once
w *widget.Check
Text state.State[string]
textCancel state.CancelFunc
Checked state.MutableState[bool]
checkedCancel state.CancelFunc
}
func (check *Check) init() {
check.once.Do(func() {
check.w = widget.NewCheck("", nil)
})
}
func (check *Check) CanvasObject() fyne.CanvasObject {
check.init()
return check.w
}
func (check *Check) Bind() {
check.init()
check.Unbind()
if check.Text != nil {
check.textCancel = check.Text.Listen(func(v string) {
check.w.Text = v
check.w.Refresh()
})
}
if check.Checked != nil {
check.checkedCancel = check.Checked.Listen(check.w.SetChecked)
check.w.OnChanged = check.Checked.Set
}
}
func (check *Check) Unbind() {
cancel(&check.textCancel)
cancel(&check.checkedCancel)
}