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
}