widget: [API] move Enum state update to Changed, rename it to Update

Similar to an earlier change for other widgets, this change separate
Enum state changes for access earlier than Layout.

Signed-off-by: Elias Naur <mail@eliasnaur.com>
This commit is contained in:
Elias Naur
2023-10-02 18:47:12 -05:00
parent b9837def5c
commit fe85136f99
+42 -43
View File
@@ -20,8 +20,6 @@ type Enum struct {
focus string
focused bool
changed bool
keys []*enumKey
}
@@ -40,40 +38,15 @@ func (e *Enum) index(k string) *enumKey {
return nil
}
// Changed reports whether Value has changed by user interaction since the last
// call to Changed.
func (e *Enum) Changed() bool {
changed := e.changed
e.changed = false
return changed
// Update the state and report whether Value has changed by user interaction.
func (e *Enum) Update(gtx layout.Context) bool {
if gtx.Queue == nil {
e.focused = false
}
// Hovered returns the key that is highlighted, or false if none are.
func (e *Enum) Hovered() (string, bool) {
return e.hovered, e.hovering
}
// Focused reports the focused key, or false if no key is focused.
func (e *Enum) Focused() (string, bool) {
return e.focus, e.focused
}
// Layout adds the event handler for the key k.
func (e *Enum) Layout(gtx layout.Context, k string, content layout.Widget) layout.Dimensions {
m := op.Record(gtx.Ops)
dims := content(gtx)
c := m.Stop()
defer clip.Rect{Max: dims.Size}.Push(gtx.Ops).Pop()
state := e.index(k)
if state == nil {
state = &enumKey{
key: k,
}
e.keys = append(e.keys, state)
}
clk := &state.click
for _, ev := range clk.Events(gtx) {
e.hovering = false
changed := false
for _, state := range e.keys {
for _, ev := range state.click.Events(gtx) {
switch ev.Kind {
case gesture.KindPress:
if ev.Source == pointer.Mouse {
@@ -82,7 +55,7 @@ func (e *Enum) Layout(gtx layout.Context, k string, content layout.Widget) layou
case gesture.KindClick:
if state.key != e.Value {
e.Value = state.key
e.changed = true
changed = true
}
}
}
@@ -104,23 +77,49 @@ func (e *Enum) Layout(gtx layout.Context, k string, content layout.Widget) layou
}
if state.key != e.Value {
e.Value = state.key
e.changed = true
changed = true
}
}
}
if clk.Hovered() {
e.hovered = k
if state.click.Hovered() {
e.hovered = state.key
e.hovering = true
} else if e.hovered == k {
e.hovering = false
}
}
return changed
}
// Hovered returns the key that is highlighted, or false if none are.
func (e *Enum) Hovered() (string, bool) {
return e.hovered, e.hovering
}
// Focused reports the focused key, or false if no key is focused.
func (e *Enum) Focused() (string, bool) {
return e.focus, e.focused
}
// Layout adds the event handler for the key k.
func (e *Enum) Layout(gtx layout.Context, k string, content layout.Widget) layout.Dimensions {
e.Update(gtx)
m := op.Record(gtx.Ops)
dims := content(gtx)
c := m.Stop()
defer clip.Rect{Max: dims.Size}.Push(gtx.Ops).Pop()
state := e.index(k)
if state == nil {
state = &enumKey{
key: k,
}
e.keys = append(e.keys, state)
}
clk := &state.click
clk.Add(gtx.Ops)
enabled := gtx.Queue != nil
if enabled {
key.InputOp{Tag: &state.tag, Keys: "⏎|Space"}.Add(gtx.Ops)
} else if e.focus == k {
e.focused = false
}
semantic.SelectedOp(k == e.Value).Add(gtx.Ops)
semantic.EnabledOp(enabled).Add(gtx.Ops)