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 focus string
focused bool focused bool
changed bool
keys []*enumKey keys []*enumKey
} }
@@ -40,40 +38,15 @@ func (e *Enum) index(k string) *enumKey {
return nil return nil
} }
// Changed reports whether Value has changed by user interaction since the last // Update the state and report whether Value has changed by user interaction.
// call to Changed. func (e *Enum) Update(gtx layout.Context) bool {
func (e *Enum) Changed() bool { if gtx.Queue == nil {
changed := e.changed e.focused = false
e.changed = false
return changed
} }
e.hovering = false
// Hovered returns the key that is highlighted, or false if none are. changed := false
func (e *Enum) Hovered() (string, bool) { for _, state := range e.keys {
return e.hovered, e.hovering for _, ev := range state.click.Events(gtx) {
}
// 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) {
switch ev.Kind { switch ev.Kind {
case gesture.KindPress: case gesture.KindPress:
if ev.Source == pointer.Mouse { 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: case gesture.KindClick:
if state.key != e.Value { if state.key != e.Value {
e.Value = state.key 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 { if state.key != e.Value {
e.Value = state.key e.Value = state.key
e.changed = true changed = true
} }
} }
} }
if clk.Hovered() { if state.click.Hovered() {
e.hovered = k e.hovered = state.key
e.hovering = true 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) clk.Add(gtx.Ops)
enabled := gtx.Queue != nil enabled := gtx.Queue != nil
if enabled { if enabled {
key.InputOp{Tag: &state.tag, Keys: "⏎|Space"}.Add(gtx.Ops) 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.SelectedOp(k == e.Value).Add(gtx.Ops)
semantic.EnabledOp(enabled).Add(gtx.Ops) semantic.EnabledOp(enabled).Add(gtx.Ops)