~eliasnaur/gio

ref: 05efb9b5f4529fa3d6446d238f461a84792f3644 gio/widget/enum.go -rw-r--r-- 844 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
34
35
36
37
38
39
40
41
42
43
44
45
46
package widget

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

type Enum struct {
	Value string

	clicks []gesture.Click
	values []string
}

func index(vs []string, t string) int {
	for i, v := range vs {
		if v == t {
			return i
		}
	}
	return -1
}

// Update the Value according to incoming events.
func (e *Enum) Update(gtx *layout.Context) {
	for i := range e.clicks {
		for _, ev := range e.clicks[i].Events(gtx) {
			switch ev.Type {
			case gesture.TypeClick:
				e.Value = e.values[i]
			}
		}
	}
}

// Layout adds the event handler for key.
func (e *Enum) Layout(gtx *layout.Context, key string) {
	if index(e.values, key) == -1 {
		e.values = append(e.values, key)
		e.clicks = append(e.clicks, gesture.Click{})
		e.clicks[len(e.clicks)-1].Add(gtx.Ops)
	} else {
		idx := index(e.values, key)
		e.clicks[idx].Add(gtx.Ops)
	}
}