~eliasnaur/gio

f1e266a9e70f49385dd56666cd8a3ca53a48fcc7 — Elias Naur 1 year, 2 months ago a1166e6
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>
2 files changed, 15 insertions(+), 18 deletions(-)

M widget/enum.go
M widget/material/radiobutton.go
M widget/enum.go => widget/enum.go +12 -17
@@ 6,9 6,10 @@ import (
)

type Enum struct {
	Value string

	clicks []gesture.Click
	values []string
	value  string
}

func index(vs []string, t string) int {


@@ 20,32 21,26 @@ func index(vs []string, t string) int {
	return -1
}

// Value processes events and returns the last selected value, or
// the empty string.
func (e *Enum) Value(gtx *layout.Context) string {
// 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]
				e.Value = e.values[i]
			}
		}
	}
	return e.value
}

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

func (rg *Enum) SetValue(value string) {
	rg.value = value
}

M widget/material/radiobutton.go => widget/material/radiobutton.go +3 -1
@@ 32,7 32,9 @@ func RadioButton(th *Theme, key, label string) RadioButtonStyle {
	}
}

// Layout updates enum and displays the radio button.
func (r RadioButtonStyle) Layout(gtx *layout.Context, enum *widget.Enum) {
	r.layout(gtx, enum.Value(gtx) == r.Key)
	enum.Update(gtx)
	r.layout(gtx, enum.Value == r.Key)
	enum.Layout(gtx, r.Key)
}