bool.go raw
1 package gel
2
3 import (
4 l "github.com/p9c/gio/layout"
5 )
6
7 type BoolHook func(b bool)
8
9 type Bool struct {
10 *Window
11 value bool
12 clk *Clickable
13 changed bool
14 changeState BoolHook
15 }
16
17 // Bool creates a new boolean widget
18 func (w *Window) Bool(value bool) *Bool {
19 return &Bool{
20 Window: w,
21 value: value,
22 clk: w.Clickable(),
23 changed: false,
24 changeState: func(b bool) {},
25 }
26 }
27
28 // GetValue gets the boolean value stored in the widget
29 func (b *Bool) GetValue() bool {
30 return b.value
31 }
32
33 // Value sets the value of the boolean stored in the widget
34 func (b *Bool) Value(value bool) {
35 b.value = value
36 }
37
38 // SetOnChange sets the callback function to run when the state changes
39 func (b *Bool) SetOnChange(fn BoolHook) *Bool {
40 b.changeState = fn
41 return b
42 }
43
44 // Changed reports whether value has changed since the last call to Changed
45 func (b *Bool) Changed() bool {
46 changed := b.changed
47 b.changed = false
48 return changed
49 }
50
51 // History returns the history of presses in the buffer
52 func (b *Bool) History() []press {
53 return b.clk.History()
54 }
55
56 // Fn renders the events of the boolean widget
57 func (b *Bool) Fn(gtx l.Context) l.Dimensions {
58 dims := b.clk.Fn(gtx)
59 for b.clk.Clicked() {
60 b.value = !b.value
61 b.changed = true
62 b.Window.Runner <- func() (e error) { b.changeState(b.value); return nil }
63 }
64 return dims
65 }
66