-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbutton.go
114 lines (97 loc) · 1.86 KB
/
button.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
package main
import (
"fyne.io/fyne/v2"
"fyne.io/fyne/v2/data/binding"
"fyne.io/fyne/v2/widget"
)
type Button struct {
widget.Button
dataListener binding.DataListener
data binding.String
disableListener binding.DataListener
disable binding.Bool
}
var _ fyne.Widget = (*Button)(nil)
func NewButton(text string, f func()) *Button {
r := Button{Button: widget.Button{Text: text, OnTapped: f}}
r.ExtendBaseWidget(&r)
return &r
}
func (b *Button) Unbind() {
if b.dataListener == nil {
return
}
b.data.RemoveListener(b.dataListener)
b.dataListener = nil
b.Text = ""
}
func (b *Button) Bind(s binding.String) {
if b.data == s {
goto refreshText
}
if b.dataListener != nil {
b.Unbind()
}
b.data = s
b.dataListener = binding.NewDataListener(func() {
b.SetText(getString(s))
})
s.AddListener(b.dataListener)
refreshText:
b.SetText(getString(s))
}
func (b *Button) UnbindDisable() {
if b.disableListener == nil {
return
}
b.disable.RemoveListener(b.disableListener)
b.disableListener = nil
}
func (b *Button) BindDisable(data binding.Bool) {
applyDisable := func(disable bool) {
if disable {
b.Disable()
} else {
b.Enable()
}
}
if b.disable == data {
goto refreshDisable
}
if b.disableListener != nil {
b.UnbindDisable()
}
b.disableListener = binding.NewDataListener(func() {
isDisable := b.Disabled()
disable := getBool(data)
if isDisable == disable {
return
}
applyDisable(disable)
})
b.disable = data
b.disable.AddListener(b.disableListener)
refreshDisable:
disable := getBool(data)
applyDisable(disable)
}
func getString(data binding.String) string {
if data == nil {
return ""
}
v, err := data.Get()
if err != nil {
return ""
}
return v
}
func getBool(data binding.Bool) bool {
if data == nil {
return false
}
v, err := data.Get()
if err != nil {
return false
}
return v
}