refactor(tracker/gioui): use enums (iota) for EditorEvent

This commit is contained in:
5684185+vsariola@users.noreply.github.com
2025-06-21 12:04:08 +03:00
parent beef8fe1e0
commit 0ea20ea5bf
3 changed files with 17 additions and 35 deletions

View File

@ -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() {}

View File

@ -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()) {

View File

@ -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, "---")