Files
gio-patched/widget/enum.go
T
Elias Naur f1e266a9e7 widget,widget/material: export Enum.Value
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>
2020-05-03 21:28:33 +02:00

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)
}
}