mirror of
https://git.sr.ht/~eliasnaur/gio
synced 2026-07-05 17:35:36 +00:00
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>
This commit is contained in:
+12
-17
@@ -6,9 +6,10 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
type Enum struct {
|
type Enum struct {
|
||||||
|
Value string
|
||||||
|
|
||||||
clicks []gesture.Click
|
clicks []gesture.Click
|
||||||
values []string
|
values []string
|
||||||
value string
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func index(vs []string, t string) int {
|
func index(vs []string, t string) int {
|
||||||
@@ -20,32 +21,26 @@ func index(vs []string, t string) int {
|
|||||||
return -1
|
return -1
|
||||||
}
|
}
|
||||||
|
|
||||||
// Value processes events and returns the last selected value, or
|
// Update the Value according to incoming events.
|
||||||
// the empty string.
|
func (e *Enum) Update(gtx *layout.Context) {
|
||||||
func (e *Enum) Value(gtx *layout.Context) string {
|
|
||||||
for i := range e.clicks {
|
for i := range e.clicks {
|
||||||
for _, ev := range e.clicks[i].Events(gtx) {
|
for _, ev := range e.clicks[i].Events(gtx) {
|
||||||
switch ev.Type {
|
switch ev.Type {
|
||||||
case gesture.TypeClick:
|
case gesture.TypeClick:
|
||||||
e.value = e.values[i]
|
e.Value = e.values[i]
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return e.value
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Layout adds the event handler for key.
|
// Layout adds the event handler for key.
|
||||||
func (rg *Enum) Layout(gtx *layout.Context, key string) {
|
func (e *Enum) Layout(gtx *layout.Context, key string) {
|
||||||
if index(rg.values, key) == -1 {
|
if index(e.values, key) == -1 {
|
||||||
rg.values = append(rg.values, key)
|
e.values = append(e.values, key)
|
||||||
rg.clicks = append(rg.clicks, gesture.Click{})
|
e.clicks = append(e.clicks, gesture.Click{})
|
||||||
rg.clicks[len(rg.clicks)-1].Add(gtx.Ops)
|
e.clicks[len(e.clicks)-1].Add(gtx.Ops)
|
||||||
} else {
|
} else {
|
||||||
idx := index(rg.values, key)
|
idx := index(e.values, key)
|
||||||
rg.clicks[idx].Add(gtx.Ops)
|
e.clicks[idx].Add(gtx.Ops)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (rg *Enum) SetValue(value string) {
|
|
||||||
rg.value = value
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -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) {
|
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)
|
enum.Layout(gtx, r.Key)
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user