Files
gio-patched/widget/material/switch.go
T
Thomas Bruyelle ae8a377cda op: add op.Push and op.Record funcs
The funcs replace stack.Push and macro.Record, which become private.
This makes stack and macro faster to write, in particular for stacks
where you can just write the following line to save and restore the
state :

  defer op.Push(ops).Pop()

This usage requires Push to return a pointer (since Pop has a pointer
receiver), or else the code doesn't compile.

For consistancy, I tried to do the same for op.Record, but this implied
to turn all the MacroOp fields into pointers, and this caused some
panics. As a result, op.Record doesn't return a pointer.

An other side effect pointed by Larry Clapp: StackOp and MacroOp are not
re-usable any more, you have to allocate a new one for each usage, using
the described funcs above.

Signed-off-by: Thomas Bruyelle <thomas.bruyelle@gmail.com>
2020-06-02 10:39:56 +02:00

125 lines
3.1 KiB
Go

// SPDX-License-Identifier: Unlicense OR MIT
package material
import (
"image"
"image/color"
"gioui.org/f32"
"gioui.org/io/pointer"
"gioui.org/layout"
"gioui.org/op"
"gioui.org/op/clip"
"gioui.org/op/paint"
"gioui.org/unit"
"gioui.org/widget"
)
type SwitchStyle struct {
Color color.RGBA
Switch *widget.Bool
}
func Switch(th *Theme, swtch *widget.Bool) SwitchStyle {
return SwitchStyle{
Switch: swtch,
Color: th.Color.Primary,
}
}
// Layout updates the checkBox and displays it.
func (s SwitchStyle) Layout(gtx layout.Context) layout.Dimensions {
trackWidth := gtx.Px(unit.Dp(36))
trackHeight := gtx.Px(unit.Dp(16))
thumbSize := gtx.Px(unit.Dp(20))
trackOff := float32(thumbSize-trackHeight) * .5
// Draw track.
stack := op.Push(gtx.Ops)
trackCorner := float32(trackHeight) / 2
trackRect := f32.Rectangle{Max: f32.Point{
X: float32(trackWidth),
Y: float32(trackHeight),
}}
op.TransformOp{}.Offset(f32.Point{Y: trackOff}).Add(gtx.Ops)
clip.Rect{
Rect: trackRect,
NE: trackCorner, NW: trackCorner, SE: trackCorner, SW: trackCorner,
}.Op(gtx.Ops).Add(gtx.Ops)
paint.ColorOp{Color: rgb(0x9b9b9b)}.Add(gtx.Ops)
paint.PaintOp{Rect: trackRect}.Add(gtx.Ops)
stack.Pop()
// Compute thumb offset and color.
stack = op.Push(gtx.Ops)
col := rgb(0xffffff)
if s.Switch.Value {
off := trackWidth - thumbSize
op.TransformOp{}.Offset(f32.Point{X: float32(off)}).Add(gtx.Ops)
col = s.Color
}
// Draw thumb shadow, a translucent disc slightly larger than the
// thumb itself.
shadowStack := op.Push(gtx.Ops)
shadowSize := float32(2)
// Center shadow horizontally and slightly adjust its Y.
op.TransformOp{}.Offset(f32.Point{X: -shadowSize / 2, Y: -.75}).Add(gtx.Ops)
drawDisc(gtx.Ops, float32(thumbSize)+shadowSize, argb(0x55000000))
shadowStack.Pop()
// Draw thumb.
drawDisc(gtx.Ops, float32(thumbSize), col)
stack.Pop()
// Draw thumb ink.
stack = op.Push(gtx.Ops)
inkSize := float32(gtx.Px(unit.Dp(44)))
rr := inkSize * .5
inkOff := f32.Point{
X: float32(trackWidth)*.5 - rr,
Y: -rr + float32(trackHeight)*.5 + trackOff,
}
op.TransformOp{}.Offset(inkOff).Add(gtx.Ops)
clip.Rect{
Rect: f32.Rectangle{
Max: f32.Point{
X: inkSize,
Y: inkSize,
},
},
NE: rr, NW: rr, SE: rr, SW: rr,
}.Op(gtx.Ops).Add(gtx.Ops)
drawInk(gtx, s.Switch.Last)
stack.Pop()
// Set up click area.
stack = op.Push(gtx.Ops)
clickSize := gtx.Px(unit.Dp(40))
clickOff := f32.Point{
X: (float32(trackWidth) - float32(clickSize)) * .5,
Y: (float32(trackHeight)-float32(clickSize))*.5 + trackOff,
}
op.TransformOp{}.Offset(clickOff).Add(gtx.Ops)
sz := image.Pt(clickSize, clickSize)
pointer.Ellipse(image.Rectangle{Max: sz}).Add(gtx.Ops)
gtx.Constraints.Min = sz
s.Switch.Layout(gtx)
stack.Pop()
return layout.Dimensions{Size: image.Point{X: trackWidth, Y: thumbSize}}
}
func drawDisc(ops *op.Ops, sz float32, col color.RGBA) {
defer op.Push(ops).Pop()
rr := sz / 2
r := f32.Rectangle{Max: f32.Point{X: sz, Y: sz}}
clip.Rect{
Rect: r,
NE: rr, NW: rr, SE: rr, SW: rr,
}.Op(ops).Add(ops)
paint.ColorOp{Color: col}.Add(ops)
paint.PaintOp{Rect: r}.Add(ops)
}