mirror of
https://git.sr.ht/~eliasnaur/gio
synced 2026-07-01 07:35:40 +00:00
f1e266a9e7
The Value method both updated the enum value and returned it. In order to access the current value withoutm, expose the Value field of the enum and rename the method to Update. As a bonus we can get rid of the SetValue method as well. Updates gio#96 Signed-off-by: Elias Naur <mail@eliasnaur.com>
47 lines
844 B
Go
47 lines
844 B
Go
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)
|
|
}
|
|
}
|