-
-
Notifications
You must be signed in to change notification settings - Fork 10
/
check_box.go
263 lines (243 loc) · 7.16 KB
/
check_box.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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
// Copyright (c) 2021-2024 by Richard A. Wilkes. All rights reserved.
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, version 2.0. If a copy of the MPL was not distributed with
// this file, You can obtain one at http://mozilla.org/MPL/2.0/.
//
// This Source Code Form is "Incompatible With Secondary Licenses", as
// defined by the Mozilla Public License, version 2.0.
package unison
import (
"time"
"github.com/richardwilkes/toolbox/xmath"
"github.com/richardwilkes/unison/enums/align"
"github.com/richardwilkes/unison/enums/check"
"github.com/richardwilkes/unison/enums/paintstyle"
"github.com/richardwilkes/unison/enums/side"
)
// DefaultCheckBoxTheme holds the default CheckBoxTheme values for CheckBoxes. Modifying this data will not alter
// existing CheckBoxes, but will alter any CheckBoxes created in the future.
var DefaultCheckBoxTheme = CheckBoxTheme{
TextDecoration: TextDecoration{
Font: SystemFont,
OnBackgroundInk: ThemeOnSurface,
},
EdgeInk: ThemeSurfaceEdge,
SelectionInk: ThemeFocus,
OnSelectionInk: ThemeOnFocus,
ControlInk: ThemeAboveSurface,
OnControlInk: ThemeOnAboveSurface,
Gap: StdIconGap,
CornerRadius: 4,
ClickAnimationTime: 100 * time.Millisecond,
HAlign: align.Start,
VAlign: align.Middle,
Side: side.Left,
}
// CheckBoxTheme holds theming data for a CheckBox.
type CheckBoxTheme struct {
EdgeInk Ink
SelectionInk Ink
OnSelectionInk Ink
ControlInk Ink
OnControlInk Ink
TextDecoration
Gap float32
CornerRadius float32
ClickAnimationTime time.Duration
HAlign align.Enum
VAlign align.Enum
Side side.Enum
}
// CheckBox represents a clickable checkbox with an optional label.
type CheckBox struct {
ClickCallback func()
Drawable Drawable
Text *Text
CheckBoxTheme
Panel
State check.Enum
Pressed bool
}
// NewCheckBox creates a new checkbox.
func NewCheckBox() *CheckBox {
c := &CheckBox{CheckBoxTheme: DefaultCheckBoxTheme}
c.Self = c
c.SetFocusable(true)
c.SetSizer(c.DefaultSizes)
c.DrawCallback = c.DefaultDraw
c.GainedFocusCallback = c.DefaultFocusGained
c.LostFocusCallback = c.MarkForRedraw
c.MouseDownCallback = c.DefaultMouseDown
c.MouseDragCallback = c.DefaultMouseDrag
c.MouseUpCallback = c.DefaultMouseUp
c.KeyDownCallback = c.DefaultKeyDown
c.UpdateCursorCallback = c.DefaultUpdateCursor
return c
}
// SetTitle sets the text of the checkbox to the specified text. The theme's TextDecoration will be used, so any
// changes you want to make to it should be done before calling this method. Alternatively, you can directly set the
// .Text field.
func (c *CheckBox) SetTitle(text string) {
c.Text = NewText(text, &c.TextDecoration)
}
// DefaultFocusGained provides the default focus gained handling.
func (c *CheckBox) DefaultFocusGained() {
c.ScrollIntoView()
c.MarkForRedraw()
}
// DefaultSizes provides the default sizing.
func (c *CheckBox) DefaultSizes(hint Size) (minSize, prefSize, maxSize Size) {
prefSize = c.boxAndLabelSize()
if border := c.Border(); border != nil {
prefSize = prefSize.Add(border.Insets().Size())
}
prefSize = prefSize.Ceil().ConstrainForHint(hint)
return prefSize, prefSize, MaxSize(prefSize)
}
func (c *CheckBox) boxAndLabelSize() Size {
boxSize := c.boxSize()
if c.Drawable == nil && c.Text.Empty() {
return Size{Width: boxSize, Height: boxSize}
}
size, _ := LabelContentSizes(c.Text, c.Drawable, c.Font, c.Side, c.Gap)
size.Width += c.Gap + boxSize
if size.Height < boxSize {
size.Height = boxSize
}
return size
}
func (c *CheckBox) boxSize() float32 {
return xmath.Ceil(c.Font.Baseline())
}
// DefaultDraw provides the default drawing.
func (c *CheckBox) DefaultDraw(canvas *Canvas, _ Rect) {
contentRect := c.ContentRect(false)
rect := contentRect
size := c.boxAndLabelSize()
switch c.HAlign {
case align.Middle, align.Fill:
rect.X = xmath.Floor(rect.X + (rect.Width-size.Width)/2)
case align.End:
rect.X += rect.Width - size.Width
default: // align.Start
}
switch c.VAlign {
case align.Middle, align.Fill:
rect.Y = xmath.Floor(rect.Y + (rect.Height-size.Height)/2)
case align.End:
rect.Y += rect.Height - size.Height
default: // align.Start
}
rect.Size = size
boxSize := c.boxSize()
if c.Drawable != nil || !c.Text.Empty() {
r := rect
r.X += boxSize + c.Gap
r.Width -= boxSize + c.Gap
DrawLabel(canvas, r, c.HAlign, c.VAlign, c.Font, c.Text, c.OnBackgroundInk, nil, c.Drawable, c.Side, c.Gap,
!c.Enabled())
}
if rect.Height > boxSize {
rect.Y += xmath.Floor((rect.Height - boxSize) / 2)
}
rect.Width = boxSize
rect.Height = boxSize
var fg, bg Ink
switch {
case c.Pressed:
bg = c.SelectionInk
fg = c.OnSelectionInk
default:
bg = c.ControlInk
fg = c.OnControlInk
}
edge := c.EdgeInk
thickness := float32(1)
if c.Focused() {
thickness++
edge = c.SelectionInk
}
DrawRoundedRectBase(canvas, rect, c.CornerRadius, thickness, bg, edge)
rect = rect.Inset(NewUniformInsets(0.5))
if c.State == check.Off {
return
}
paint := fg.Paint(canvas, contentRect, paintstyle.Stroke)
paint.SetStrokeWidth(2)
if !c.Enabled() {
paint.SetColorFilter(Grayscale30Filter())
}
if c.State == check.On {
path := NewPath()
path.MoveTo(rect.X+rect.Width*0.25, rect.Y+rect.Height*0.55)
path.LineTo(rect.X+rect.Width*0.45, rect.Y+rect.Height*0.7)
path.LineTo(rect.X+rect.Width*0.75, rect.Y+rect.Height*0.3)
canvas.DrawPath(path, paint)
} else {
canvas.DrawLine(rect.X+rect.Width*0.25, rect.Y+rect.Height*0.5, rect.X+rect.Width*0.7, rect.Y+rect.Height*0.5,
paint)
}
}
// Click makes the checkbox behave as if a user clicked on it.
func (c *CheckBox) Click() {
c.updateState()
pressed := c.Pressed
c.Pressed = true
c.MarkForRedraw()
c.FlushDrawing()
c.Pressed = pressed
time.Sleep(c.ClickAnimationTime)
c.MarkForRedraw()
if c.ClickCallback != nil {
c.ClickCallback()
}
}
func (c *CheckBox) updateState() {
if c.State == check.On {
c.State = check.Off
} else {
c.State = check.On
}
}
// DefaultMouseDown provides the default mouse down handling.
func (c *CheckBox) DefaultMouseDown(_ Point, _, _ int, _ Modifiers) bool {
c.Pressed = true
c.MarkForRedraw()
return true
}
// DefaultMouseDrag provides the default mouse drag handling.
func (c *CheckBox) DefaultMouseDrag(where Point, _ int, _ Modifiers) bool {
if pressed := where.In(c.ContentRect(false)); pressed != c.Pressed {
c.Pressed = pressed
c.MarkForRedraw()
}
return true
}
// DefaultMouseUp provides the default mouse up handling.
func (c *CheckBox) DefaultMouseUp(where Point, _ int, _ Modifiers) bool {
c.Pressed = false
c.MarkForRedraw()
if where.In(c.ContentRect(false)) {
c.updateState()
if c.ClickCallback != nil {
c.ClickCallback()
}
}
return true
}
// DefaultKeyDown provides the default key down handling.
func (c *CheckBox) DefaultKeyDown(keyCode KeyCode, mod Modifiers, _ bool) bool {
if IsControlAction(keyCode, mod) {
c.Click()
return true
}
return false
}
// DefaultUpdateCursor provides the default cursor for check boxes.
func (c *CheckBox) DefaultUpdateCursor(_ Point) *Cursor {
if !c.Enabled() {
return ArrowCursor()
}
return PointingCursor()
}