/
switch.go
334 lines (306 loc) · 8.32 KB
/
switch.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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
// Copyright (c) 2023, Cogent Core. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package core
import (
"cogentcore.org/core/abilities"
"cogentcore.org/core/colors"
"cogentcore.org/core/cursors"
"cogentcore.org/core/events"
"cogentcore.org/core/icons"
"cogentcore.org/core/states"
"cogentcore.org/core/styles"
"cogentcore.org/core/tree"
"cogentcore.org/core/units"
)
// Switch is a widget that can toggle between an on and off state.
// It can be displayed as a switch, checkbox, or radio button.
type Switch struct {
Box
// the type of switch that this is
Type SwitchTypes `set:"-"`
// the label text for the switch
Text string
// icon to use for the on, checked state of the switch
IconOn icons.Icon `view:"show-name"`
// icon to use for the off, unchecked state of the switch
IconOff icons.Icon `view:"show-name"`
// icon to use for the indeterminate (unknown) state
IconIndeterminate icons.Icon `view:"show-name"`
}
// SwitchTypes contains the different types of [Switch]es
type SwitchTypes int32 //enums:enum -trim-prefix Switch
const (
// SwitchSwitch indicates to display a switch as a switch (toggle slider).
SwitchSwitch SwitchTypes = iota
// SwitchChip indicates to display a switch as chip (like Material Design's filter chip),
// which is typically only used in the context of [Switches].
SwitchChip
// SwitchCheckbox indicates to display a switch as a checkbox.
SwitchCheckbox
// SwitchRadioButton indicates to display a switch as a radio button.
SwitchRadioButton
// SwitchSegmentedButton indicates to display a segmented button, which is typically only used in
// the context of [Switches].
SwitchSegmentedButton
)
func (sw *Switch) OnInit() {
sw.Box.OnInit()
sw.HandleEvents()
sw.SetStyles()
}
// IsChecked tests if this switch is checked
func (sw *Switch) IsChecked() bool {
return sw.StateIs(states.Checked)
}
// SetChecked sets the checked state and updates the icon accordingly
func (sw *Switch) SetChecked(on bool) *Switch {
sw.SetState(on, states.Checked)
sw.SetState(false, states.Indeterminate)
sw.SetIconFromState()
return sw
}
// SetIconFromState updates icon state based on checked status
func (sw *Switch) SetIconFromState() {
if sw.Parts == nil {
return
}
ist := sw.Parts.ChildByName("stack", 0)
if ist == nil {
return
}
st := ist.(*Layout)
switch {
case sw.StateIs(states.Indeterminate):
st.StackTop = 2
case sw.IsChecked():
st.StackTop = 0
default:
if sw.Type == SwitchChip {
// chips render no icon when off
st.StackTop = -1
return
}
st.StackTop = 1
}
}
func (sw *Switch) HandleEvents() {
sw.HandleSelectToggle() // on widgetbase
sw.HandleClickOnEnterSpace()
sw.OnFinal(events.Click, func(e events.Event) {
sw.SetChecked(sw.IsChecked())
if sw.Type == SwitchChip {
sw.NeedsLayout()
} else {
sw.NeedsRender()
}
sw.SendChange(e)
})
}
func (sw *Switch) SetStyles() {
sw.Style(func(s *styles.Style) {
s.SetAbilities(true, abilities.Activatable, abilities.Focusable, abilities.Hoverable, abilities.Checkable)
if !sw.IsReadOnly() {
s.Cursor = cursors.Pointer
}
s.Text.Align = styles.Start
s.Text.AlignV = styles.Center
s.Padding.Set(units.Dp(4))
s.Border.Radius = styles.BorderRadiusSmall
if sw.Type == SwitchChip {
if s.Is(states.Checked) {
s.Background = colors.C(colors.Scheme.SurfaceVariant)
s.Color = colors.C(colors.Scheme.OnSurfaceVariant)
} else if !s.Is(states.Focused) {
s.Border.Color.Set(colors.C(colors.Scheme.Outline))
s.Border.Width.Set(units.Dp(1))
}
}
if sw.Type == SwitchSegmentedButton {
if !s.Is(states.Focused) {
s.Border.Color.Set(colors.C(colors.Scheme.Outline))
s.Border.Width.Set(units.Dp(1))
}
if s.Is(states.Checked) {
s.Background = colors.C(colors.Scheme.SurfaceVariant)
s.Color = colors.C(colors.Scheme.OnSurfaceVariant)
}
}
if s.Is(states.Selected) {
s.Background = colors.C(colors.Scheme.Select.Container)
}
})
sw.OnWidgetAdded(func(w Widget) {
switch w.PathFrom(sw) {
case "parts":
w.Style(func(s *styles.Style) {
s.Gap.Zero()
s.Align.Content = styles.Center
s.Align.Items = styles.Center
s.Text.AlignV = styles.Center
})
case "parts/stack":
w.Style(func(s *styles.Style) {
s.Display = styles.Stacked
s.Grow.Set(0, 0)
s.Gap.Zero()
})
case "parts/stack/icon0": // on
w.Style(func(s *styles.Style) {
if sw.Type == SwitchChip {
s.Color = colors.C(colors.Scheme.OnSurfaceVariant)
} else {
s.Color = colors.C(colors.Scheme.Primary.Base)
}
// switches need to be bigger
if sw.Type == SwitchSwitch {
s.Min.X.Em(2)
s.Min.Y.Em(1.5)
} else {
s.Min.X.Em(1.5)
s.Min.Y.Em(1.5)
}
})
case "parts/stack/icon1": // off
w.Style(func(s *styles.Style) {
switch sw.Type {
case SwitchSwitch:
// switches need to be bigger
s.Min.X.Em(2)
s.Min.Y.Em(1.5)
case SwitchChip:
// chips render no icon when off
s.Min.X.Zero()
s.Min.Y.Zero()
default:
s.Min.X.Em(1.5)
s.Min.Y.Em(1.5)
}
})
case "parts/stack/icon2": // indeterminate
w.Style(func(s *styles.Style) {
switch sw.Type {
case SwitchSwitch:
// switches need to be bigger
s.Min.X.Em(2)
s.Min.Y.Em(1.5)
case SwitchChip:
// chips render no icon when off
s.Min.X.Zero()
s.Min.Y.Zero()
default:
s.Min.X.Em(1.5)
s.Min.Y.Em(1.5)
}
})
case "parts/space":
w.Style(func(s *styles.Style) {
s.Min.X.Ch(0.1)
})
case "parts/label":
w.Style(func(s *styles.Style) {
s.SetNonSelectable()
s.SetTextWrap(false)
s.Margin.Zero()
s.Padding.Zero()
s.Text.AlignV = styles.Center
s.FillMargin = false
})
}
})
}
// SetType sets the styling type of the switch
func (sw *Switch) SetType(typ SwitchTypes) *Switch {
sw.Type = typ
sw.IconIndeterminate = icons.Blank
switch sw.Type {
case SwitchSwitch:
// TODO: material has more advanced switches with a checkmark
// if they are turned on; we could implement that at some point
sw.IconOn = icons.ToggleOn.Fill()
sw.IconOff = icons.ToggleOff
sw.IconIndeterminate = icons.ToggleMid
case SwitchChip, SwitchSegmentedButton:
sw.IconOn = icons.Check
sw.IconOff = icons.None
sw.IconIndeterminate = icons.None
case SwitchCheckbox:
sw.IconOn = icons.CheckBox.Fill()
sw.IconOff = icons.CheckBoxOutlineBlank
sw.IconIndeterminate = icons.IndeterminateCheckBox
case SwitchRadioButton:
sw.IconOn = icons.RadioButtonChecked
sw.IconOff = icons.RadioButtonUnchecked
sw.IconIndeterminate = icons.RadioButtonPartial
}
sw.NeedsLayout()
return sw
}
// LabelWidget returns the label widget if present
func (sw *Switch) LabelWidget() *Label {
lbi := sw.Parts.ChildByName("label")
if lbi == nil {
return nil
}
return lbi.(*Label)
}
// SetIcons sets the icons for the on (checked), off (unchecked)
// and indeterminate (unknown) states. See [SetIconsUpdate] for
// a version that updates the icon rendering
func (sw *Switch) SetIcons(on, off, unk icons.Icon) *Switch {
sw.IconOn = on
sw.IconOff = off
sw.IconIndeterminate = unk
return sw
}
// ClearIcons sets all of the switch icons to [icons.None]
func (sw *Switch) ClearIcons() *Switch {
sw.IconOn = icons.None
sw.IconOff = icons.None
sw.IconIndeterminate = icons.None
return sw
}
func (sw *Switch) Config() {
config := tree.Config{}
if sw.IconOn == "" {
sw.IconOn = icons.ToggleOn.Fill() // fallback
}
if sw.IconOff == "" {
sw.IconOff = icons.ToggleOff // fallback
}
ici := 0 // always there
lbi := -1
config.Add(LayoutType, "stack")
if sw.Text != "" {
config.Add(SpaceType, "space")
lbi = len(config)
config.Add(LabelType, "label")
}
sw.ConfigParts(config, func() {
ist := sw.Parts.Child(ici).(*Layout)
ist.SetNChildren(3, IconType, "icon")
icon := ist.Child(0).(*Icon)
icon.SetIcon(sw.IconOn)
icoff := ist.Child(1).(*Icon)
icoff.SetIcon(sw.IconOff)
icunk := ist.Child(2).(*Icon)
icunk.SetIcon(sw.IconIndeterminate)
sw.SetIconFromState()
if lbi >= 0 {
lbl := sw.Parts.Child(lbi).(*Label)
if lbl.Text != sw.Text {
lbl.SetText(sw.Text)
}
}
})
}
func (sw *Switch) Render() {
sw.SetIconFromState() // make sure we're always up-to-date on render
if sw.Parts != nil {
ist := sw.Parts.ChildByName("stack", 0)
if ist != nil {
ist.(*Layout).UpdateStackedVisibility()
}
}
sw.Box.Render()
}