From 0ea20ea5bff7721c7eb528868a3e8a7d11fa651d Mon Sep 17 00:00:00 2001 From: "5684185+vsariola@users.noreply.github.com" <5684185+vsariola@users.noreply.github.com> Date: Sat, 21 Jun 2025 12:04:08 +0300 Subject: [PATCH] refactor(tracker/gioui): use enums (iota) for EditorEvent --- tracker/gioui/editor.go | 26 ++++++++++++-------------- tracker/gioui/instrument_editor.go | 20 ++++---------------- tracker/gioui/unit_editor.go | 6 +----- 3 files changed, 17 insertions(+), 35 deletions(-) diff --git a/tracker/gioui/editor.go b/tracker/gioui/editor.go index 1776b22..57f2485 100644 --- a/tracker/gioui/editor.go +++ b/tracker/gioui/editor.go @@ -31,10 +31,13 @@ type ( TextSize unit.Sp } - EditorSubmitEvent struct{} - EditorCancelEvent struct{} + EditorEvent int +) - EditorEvent interface{ isEditorEvent() } +const ( + EditorEventNone EditorEvent = iota + EditorEventSubmit + EditorEventCancel ) func NewEditor(singleLine, submit bool, alignment text.Alignment) *Editor { @@ -59,10 +62,8 @@ func (s *EditorStyle) AsLabelStyle() LabelStyle { } func (e *Editor) Layout(gtx C, str tracker.String, th *Theme, style *EditorStyle, hint string) D { - for { - if _, ok := e.Update(gtx, str); !ok { - break - } + for e.Update(gtx, str) != EditorEventNone { + // just consume all events if the user did not consume them } if e.widgetEditor.Text() != str.Value() { e.widgetEditor.SetText(str.Value()) @@ -75,7 +76,7 @@ func (e *Editor) Layout(gtx C, str tracker.String, th *Theme, style *EditorStyle return me.Layout(gtx) } -func (e *Editor) Update(gtx C, str tracker.String) (ev EditorEvent, ok bool) { +func (e *Editor) Update(gtx C, str tracker.String) EditorEvent { if e.requestFocus { e.requestFocus = false gtx.Execute(key.FocusCmd{Tag: &e.widgetEditor}) @@ -91,7 +92,7 @@ func (e *Editor) Update(gtx C, str tracker.String) (ev EditorEvent, ok bool) { str.SetValue(e.widgetEditor.Text()) } if _, ok := ev.(widget.SubmitEvent); ok { - return EditorSubmitEvent{}, true + return EditorEventSubmit } } for { @@ -100,15 +101,12 @@ func (e *Editor) Update(gtx C, str tracker.String) (ev EditorEvent, ok bool) { break } if e, ok := event.(key.Event); ok && e.State == key.Press && e.Name == key.NameEscape { - return EditorCancelEvent{}, true + return EditorEventCancel } } - return nil, false + return EditorEventNone } func (e *Editor) Focus() { e.requestFocus = true } - -func (s EditorSubmitEvent) isEditorEvent() {} -func (s EditorCancelEvent) isEditorEvent() {} diff --git a/tracker/gioui/instrument_editor.go b/tracker/gioui/instrument_editor.go index 57c3541..35bf3f6 100644 --- a/tracker/gioui/instrument_editor.go +++ b/tracker/gioui/instrument_editor.go @@ -262,11 +262,7 @@ func (ie *InstrumentEditor) layoutInstrumentHeader(gtx C, t *Tracker) D { layout.Rigid(header), layout.Rigid(func(gtx C) D { defer clip.Rect(image.Rect(0, 0, gtx.Constraints.Max.X, gtx.Constraints.Max.Y)).Push(gtx.Ops).Pop() - for { - _, ok := ie.commentEditor.Update(gtx, ie.commentString) - if !ok { - break - } + for ie.commentEditor.Update(gtx, ie.commentString) != EditorEventNone { ie.instrumentDragList.Focus() } ret := layout.UniformInset(unit.Dp(6)).Layout(gtx, func(gtx C) D { @@ -301,11 +297,7 @@ func (ie *InstrumentEditor) layoutInstrumentList(gtx C, t *Tracker) D { s.Color = color.NRGBA{R: 255, G: k, B: 255, A: 255} } if i == ie.instrumentDragList.TrackerList.Selected() { - for { - _, ok := ie.nameEditor.Update(gtx, ie.nameString) - if !ok { - break - } + for ie.nameEditor.Update(gtx, ie.nameString) != EditorEventNone { ie.instrumentDragList.Focus() } return layout.Center.Layout(gtx, func(gtx C) D { @@ -409,12 +401,8 @@ func (ie *InstrumentEditor) layoutUnitList(gtx C, t *Tracker) D { if i == ie.unitDragList.TrackerList.Selected() { defer clip.Rect(image.Rect(0, 0, gtx.Constraints.Max.X, gtx.Constraints.Max.Y)).Push(gtx.Ops).Pop() str := t.Model.UnitSearch() - for { - ev, ok := ie.searchEditor.Update(gtx, str) - if !ok { - break - } - if _, ok := ev.(EditorSubmitEvent); ok { + for ev := ie.searchEditor.Update(gtx, str); ev != EditorEventNone; ev = ie.searchEditor.Update(gtx, str) { + if ev == EditorEventSubmit { if str.Value() != "" { for _, n := range sointu.UnitNames { if strings.HasPrefix(n, str.Value()) { diff --git a/tracker/gioui/unit_editor.go b/tracker/gioui/unit_editor.go index 82a2513..602a914 100644 --- a/tracker/gioui/unit_editor.go +++ b/tracker/gioui/unit_editor.go @@ -160,11 +160,7 @@ func (pe *UnitEditor) layoutFooter(gtx C, t *Tracker) D { return hintText.Layout(gtx) }), layout.Flexed(1, func(gtx C) D { - for { - _, ok := pe.commentEditor.Update(gtx, t.UnitComment()) - if !ok { - break - } + for pe.commentEditor.Update(gtx, t.UnitComment()) != EditorEventNone { t.InstrumentEditor.Focus() } return pe.commentEditor.Layout(gtx, t.UnitComment(), t.Theme, &t.Theme.InstrumentEditor.UnitComment, "---")