widget,widget/material: convert Switch to use Clickable

Signed-off-by: Elias Naur <mail@eliasnaur.com>
This commit is contained in:
Elias Naur
2020-06-09 22:05:13 +02:00
parent f7fea02312
commit 0715c801e2
2 changed files with 35 additions and 51 deletions
+13 -28
View File
@@ -1,23 +1,15 @@
package widget
import (
"image"
"time"
"gioui.org/gesture"
"gioui.org/io/pointer"
"gioui.org/layout"
"gioui.org/op"
)
type Bool struct {
Value bool
// Last is the last registered click.
Last Press
clk Clickable
changed bool
gesture gesture.Click
}
// Changed reports whether Value has changed since the last
@@ -28,22 +20,15 @@ func (b *Bool) Changed() bool {
return changed
}
func (b *Bool) Layout(gtx layout.Context) layout.Dimensions {
for _, e := range b.gesture.Events(gtx) {
switch e.Type {
case gesture.TypeClick:
now := gtx.Now()
b.Last = Press{
Start: now,
End: now.Add(time.Second),
Position: e.Position,
}
b.Value = !b.Value
b.changed = true
}
}
defer op.Push(gtx.Ops).Pop()
pointer.Rect(image.Rectangle{Max: gtx.Constraints.Min}).Add(gtx.Ops)
b.gesture.Add(gtx.Ops)
return layout.Dimensions{Size: gtx.Constraints.Min}
func (b *Bool) History() []Press {
return b.clk.History()
}
func (b *Bool) Layout(gtx layout.Context) layout.Dimensions {
dims := b.clk.Layout(gtx)
for b.clk.Clicked() {
b.Value = !b.Value
b.changed = true
}
return dims
}
+22 -23
View File
@@ -51,6 +51,27 @@ func (s SwitchStyle) Layout(gtx layout.Context) layout.Dimensions {
paint.PaintOp{Rect: trackRect}.Add(gtx.Ops)
stack.Pop()
// Draw thumb ink.
stack = op.Push(gtx.Ops)
inkSize := gtx.Px(unit.Dp(44))
rr := float32(inkSize) * .5
inkOff := f32.Point{
X: float32(trackWidth)*.5 - rr,
Y: -rr + float32(trackHeight)*.5 + trackOff,
}
op.TransformOp{}.Offset(inkOff).Add(gtx.Ops)
gtx.Constraints.Min = image.Pt(inkSize, inkSize)
clip.Rect{
Rect: f32.Rectangle{
Max: layout.FPt(gtx.Constraints.Min),
},
NE: rr, NW: rr, SE: rr, SW: rr,
}.Op(gtx.Ops).Add(gtx.Ops)
for _, p := range s.Switch.History() {
drawInk(gtx, p)
}
stack.Pop()
// Compute thumb offset and color.
stack = op.Push(gtx.Ops)
col := rgb(0xffffff)
@@ -73,29 +94,6 @@ func (s SwitchStyle) Layout(gtx layout.Context) layout.Dimensions {
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)
dims := image.Point{X: trackWidth, Y: thumbSize}
gtx.Constraints.Min = dims
drawInk(gtx, s.Switch.Last)
stack.Pop()
// Set up click area.
stack = op.Push(gtx.Ops)
clickSize := gtx.Px(unit.Dp(40))
@@ -110,6 +108,7 @@ func (s SwitchStyle) Layout(gtx layout.Context) layout.Dimensions {
s.Switch.Layout(gtx)
stack.Pop()
dims := image.Point{X: trackWidth, Y: thumbSize}
return layout.Dimensions{Size: dims}
}