~eliasnaur/gio

ref: 05efb9b5f4529fa3d6446d238f461a84792f3644 gio/widget/bool.go -rw-r--r-- 547 bytes
05efb9b5Elias Naur example/kitchen: add Switch widget 1 year, 4 months ago
                                                                                
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
package widget

import (
	"gioui.org/gesture"
	"gioui.org/layout"
)

type Bool struct {
	Value bool

	// Last is the last registered click.
	Last Click

	gesture gesture.Click
}

// Update the checked state according to incoming events.
func (b *Bool) Update(gtx *layout.Context) {
	for _, e := range b.gesture.Events(gtx) {
		switch e.Type {
		case gesture.TypeClick:
			b.Last = Click{
				Time:     gtx.Now(),
				Position: e.Position,
			}
			b.Value = !b.Value
		}
	}
}

func (b *Bool) Layout(gtx *layout.Context) {
	b.gesture.Add(gtx.Ops)
}