-
Notifications
You must be signed in to change notification settings - Fork 0
/
switch.go
110 lines (101 loc) · 3.24 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
package freyja
import (
"image"
"gioui.org/io/semantic"
"gioui.org/layout"
"gioui.org/op"
"gioui.org/op/clip"
"gioui.org/unit"
"gioui.org/widget"
)
// Switch is a toggle button with knob.
type Switch struct {
Origin widget.Bool // Origin is the bool if this switch.
Background op.CallOp // Background is used to draw the background for this switch.
BackgroundDisabled op.CallOp // BackgroundDisabled is used instead of Background when the switch is disabled.
EnvironmentShadow Shadow // EnvironmentShadow is the shadow casted into the switch.
KnobShadow Shadow // KnobShadow is the shadow casted by the knob.
Tint op.CallOp // Tint is used to draw the tinted background overlay of the switch in "On" mode.
TintDisabled op.CallOp // TintDisabled is used instead of Tint when the switch is disabled.
Knob op.CallOp // Knob is used to draw the knob of this switch.
KnobDisabled op.CallOp // KnobDisabled is used instead of Knob when the switch is disabled.
KnobSize unit.Dp // KnobSize is the diameter of the knob.
Inset unit.Dp // Inset is the gab between the knob and the borders of switch.
Shift unit.Dp // Shift is the distance that the knob shifts to the right.
}
// Layout lays Switch out to the context.
func (s *Switch) Layout(gtx layout.Context) layout.Dimensions {
disabled := gtx.Queue == nil
return s.Origin.Layout(
gtx,
func(gtx layout.Context) layout.Dimensions {
semantic.Switch.Add(gtx.Ops)
return layout.Stack{Alignment: layout.Center}.Layout(
gtx,
layout.Expanded(func(gtx layout.Context) layout.Dimensions {
var (
radius = gtx.Dp(s.KnobSize)/2 + gtx.Dp(s.Inset)
size = gtx.Constraints.Min
shape = clip.UniformRRect(image.Rectangle{Max: size}, radius)
)
func() {
defer shape.Push(gtx.Ops).Pop()
if disabled {
s.BackgroundDisabled.Add(gtx.Ops)
if s.Origin.Value {
s.TintDisabled.Add(gtx.Ops)
}
} else {
s.Background.Add(gtx.Ops)
if s.Origin.Value {
s.Tint.Add(gtx.Ops)
}
}
s.EnvironmentShadow.Layout(
gtx,
shape.Path(gtx.Ops),
func(gtx layout.Context) layout.Dimensions {
return layout.Dimensions{Size: size}
},
)
}()
return layout.Dimensions{Size: size}
}),
layout.Stacked(func(gtx layout.Context) layout.Dimensions {
inset := layout.Inset{
Top: s.Inset, Bottom: s.Inset,
Left: s.Inset, Right: s.Inset,
}
return inset.Layout(
gtx,
func(gtx layout.Context) layout.Dimensions {
var (
size = gtx.Dp(s.KnobSize)
shiftSize = gtx.Dp(s.Shift)
shift image.Point
shape = clip.Ellipse{Max: image.Pt(size, size)}
)
if s.Origin.Value {
shift.X = shiftSize
}
defer op.Offset(shift).Push(gtx.Ops).Pop()
return s.KnobShadow.Layout(
gtx,
shape.Path(gtx.Ops),
func(gtx layout.Context) layout.Dimensions {
defer shape.Push(gtx.Ops).Pop()
if disabled {
s.KnobDisabled.Add(gtx.Ops)
} else {
s.Knob.Add(gtx.Ops)
}
return layout.Dimensions{Size: image.Pt(size+shiftSize, size)}
},
)
},
)
}),
)
},
)
}