-
Notifications
You must be signed in to change notification settings - Fork 1
/
check_button.go
155 lines (135 loc) · 3.39 KB
/
check_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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
package ui
import (
"fmt"
"strconv"
"git.kirsle.net/go/render"
)
// CheckButton implements a checkbox and radiobox widget. It's based on a
// Button and holds a boolean or string pointer (boolean for checkbox,
// string for radio).
type CheckButton struct {
Button
BoolVar *bool
StringVar *string
Value string
}
// NewCheckButton creates a new CheckButton.
func NewCheckButton(name string, boolVar *bool, child Widget) *CheckButton {
w := &CheckButton{
BoolVar: boolVar,
}
w.Button.child = child
w.IDFunc(func() string {
return fmt.Sprintf("CheckButton<%s %+v>", name, w.BoolVar)
})
w.SetStyle(Theme.Button)
w.setup()
return w
}
// NewRadioButton creates a CheckButton bound to a string variable.
func NewRadioButton(name string, stringVar *string, value string, child Widget) *CheckButton {
w := &CheckButton{
StringVar: stringVar,
Value: value,
}
w.Button.child = child
w.IDFunc(func() string {
return fmt.Sprintf(`RadioButton<%s "%s" %s>`, name, w.Value, strconv.FormatBool(*w.StringVar == w.Value))
})
w.SetStyle(Theme.Button)
w.setup()
return w
}
// Compute to re-evaluate the button state (in the case of radio buttons where
// a different button will affect the state of this one when clicked).
func (w *CheckButton) Compute(e render.Engine) {
if w.StringVar != nil {
// Radio button, always re-assign the border style in case a sister
// radio button has changed the value.
if *w.StringVar == w.Value {
w.SetBorderStyle(BorderSunken)
} else {
w.SetBorderStyle(BorderRaised)
}
} else if w.BoolVar != nil {
// Checkbutton, always re-assign the border style in case the caller
// has flipped the boolean behind our back.
if *w.BoolVar {
w.SetBorderStyle(BorderSunken)
} else {
w.SetBorderStyle(BorderRaised)
}
}
w.Button.Compute(e)
}
// setup the common things between checkboxes and radioboxes.
func (w *CheckButton) setup() {
var (
borderStyle BorderStyle = BorderRaised
background = w.style.Background
)
if w.BoolVar != nil {
if *w.BoolVar == true {
borderStyle = BorderSunken
background = w.style.Background.Darken(40)
}
}
w.Configure(Config{
BorderSize: 2,
BorderStyle: borderStyle,
OutlineSize: 1,
OutlineColor: w.style.OutlineColor,
Background: background,
})
w.Handle(MouseOver, func(ed EventData) error {
w.hovering = true
w.SetBackground(w.style.HoverBackground)
return nil
})
w.Handle(MouseOut, func(ed EventData) error {
w.hovering = false
var sunken bool
if w.BoolVar != nil {
sunken = *w.BoolVar == true
} else if w.StringVar != nil {
sunken = *w.StringVar == w.Value
}
if sunken {
w.SetBackground(w.style.Background.Darken(40))
} else {
w.SetBackground(w.style.Background)
}
return nil
})
w.Handle(MouseDown, func(ed EventData) error {
w.clicked = true
w.SetBorderStyle(BorderSunken)
return nil
})
w.Handle(MouseUp, func(ed EventData) error {
w.clicked = false
return nil
})
w.Handle(Click, func(ed EventData) error {
var sunken bool
if w.BoolVar != nil {
if *w.BoolVar {
*w.BoolVar = false
} else {
*w.BoolVar = true
sunken = true
}
} else if w.StringVar != nil {
*w.StringVar = w.Value
sunken = true
}
if sunken {
w.SetBorderStyle(BorderSunken)
w.SetBackground(w.style.Background.Darken(40))
} else {
w.SetBorderStyle(BorderRaised)
w.SetBackground(w.style.Background)
}
return nil
})
}