mirror of
https://git.sr.ht/~eliasnaur/gio
synced 2026-07-06 18:05:35 +00:00
widget,widget/material: convert Switch to use Clickable
Signed-off-by: Elias Naur <mail@eliasnaur.com>
This commit is contained in:
+13
-28
@@ -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
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user