~eliasnaur/gio

ref: f08568a6dfc3890dcc8ea76c11f1a4cd436a57c1 gio/widget/bool.go -rw-r--r-- 629 bytes
f08568a6Elias Naur widget: report whether Value changed after Enum.Update and Bool.Update 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
34
35
36
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,
// and reports whether Value changed.
func (b *Bool) Update(gtx *layout.Context) bool {
	was := b.Value
	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
		}
	}
	return b.Value != was
}

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