forked from marcetin/parallelcoin
/
bool.go
65 lines (55 loc) · 1.33 KB
/
bool.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
package gui
import (
"gioui.org/layout"
)
type BoolHook func(b bool)
type Bool struct {
*Window
value bool
clk *Clickable
changed bool
changeState BoolHook
}
// Bool creates a new boolean widget
func (w *Window) Bool(value bool) *Bool {
return &Bool{
Window: w,
value: value,
clk: w.Clickable(),
changed: false,
changeState: func(b bool) {},
}
}
// GetValue gets the boolean value stored in the widget
func (b *Bool) GetValue() bool {
return b.value
}
// Value sets the value of the boolean stored in the widget
func (b *Bool) Value(value bool) {
b.value = value
}
// SetOnChange sets the callback function to run when the state changes
func (b *Bool) SetOnChange(fn BoolHook) *Bool {
b.changeState = fn
return b
}
// Changed reports whether value has changed since the last call to Changed
func (b *Bool) Changed() bool {
changed := b.changed
b.changed = false
return changed
}
// History returns the history of presses in the buffer
func (b *Bool) History() []press {
return b.clk.History()
}
// Fn renders the events of the boolean widget
func (b *Bool) Fn(gtx layout.Context) layout.Dimensions {
dims := b.clk.Fn(gtx)
for b.clk.Clicked() {
b.value = !b.value
b.changed = true
b.Window.Runner <- func() (e error) { b.changeState(b.value); return nil }
}
return dims
}