From d78ef98e731a00b309595282537290b2d7f0792a Mon Sep 17 00:00:00 2001 From: "5684185+vsariola@users.noreply.github.com" <5684185+vsariola@users.noreply.github.com> Date: Wed, 25 Jun 2025 18:54:00 +0300 Subject: [PATCH] refactor(tracker/gioui): upgrade gio & store Tracker to gtx.Values --- go.mod | 2 +- go.sum | 2 ++ tracker/gioui/note_editor.go | 3 ++- tracker/gioui/order_editor.go | 3 ++- tracker/gioui/patch_panel.go | 24 ++++++++++++++---------- tracker/gioui/songpanel.go | 24 +++++++++++------------- tracker/gioui/tracker.go | 27 +++++++++++++++------------ tracker/gioui/unit_editor.go | 3 ++- 8 files changed, 49 insertions(+), 39 deletions(-) diff --git a/go.mod b/go.mod index 0bb4683..c944cbc 100644 --- a/go.mod +++ b/go.mod @@ -5,7 +5,7 @@ go 1.23.8 toolchain go1.24.0 require ( - gioui.org v0.8.1-0.20250526181049-1a17e9ea3725 + gioui.org v0.8.1-0.20250624114559-c3ce484b5e80 gioui.org/x v0.7.1 github.com/Masterminds/sprig v2.22.0+incompatible github.com/ebitengine/oto/v3 v3.3.0 diff --git a/go.sum b/go.sum index c4a066e..d759b18 100644 --- a/go.sum +++ b/go.sum @@ -2,6 +2,8 @@ eliasnaur.com/font v0.0.0-20230308162249-dd43949cb42d h1:ARo7NCVvN2NdhLlJE9xAbKw eliasnaur.com/font v0.0.0-20230308162249-dd43949cb42d/go.mod h1:OYVuxibdk9OSLX8vAqydtRPP87PyTFcT9uH3MlEGBQA= gioui.org v0.8.1-0.20250526181049-1a17e9ea3725 h1:8dzkqzvWLIwW6HEQv5CinK53vMeANmUEETzpcbtPRp0= gioui.org v0.8.1-0.20250526181049-1a17e9ea3725/go.mod h1:CjNig0wAhLt9WZxOPAusgFD8x8IRvqt26LdDBa3Jvao= +gioui.org v0.8.1-0.20250624114559-c3ce484b5e80 h1:cnimNlq1PEHY4z1Cy32n6In86VUF5/VLi7cWHAM1XcY= +gioui.org v0.8.1-0.20250624114559-c3ce484b5e80/go.mod h1:CjNig0wAhLt9WZxOPAusgFD8x8IRvqt26LdDBa3Jvao= gioui.org/cpu v0.0.0-20210808092351-bfe733dd3334/go.mod h1:A8M0Cn5o+vY5LTMlnRoK3O5kG+rH0kWfJjeKd9QpBmQ= gioui.org/shader v1.0.8 h1:6ks0o/A+b0ne7RzEqRZK5f4Gboz2CfG+mVliciy6+qA= gioui.org/shader v1.0.8/go.mod h1:mWdiME581d/kV7/iEhLmUgUK5iZ09XR5XpduXzbePVM= diff --git a/tracker/gioui/note_editor.go b/tracker/gioui/note_editor.go index 73c02a7..07f5c0d 100644 --- a/tracker/gioui/note_editor.go +++ b/tracker/gioui/note_editor.go @@ -117,7 +117,8 @@ func NewNoteEditor(model *tracker.Model) *NoteEditor { return ret } -func (te *NoteEditor) Layout(gtx layout.Context, t *Tracker) layout.Dimensions { +func (te *NoteEditor) Layout(gtx layout.Context) layout.Dimensions { + t := TrackerFromContext(gtx) for { e, ok := gtx.Event(te.eventFilters...) if !ok { diff --git a/tracker/gioui/order_editor.go b/tracker/gioui/order_editor.go index 6b03161..6e7caa6 100644 --- a/tracker/gioui/order_editor.go +++ b/tracker/gioui/order_editor.go @@ -48,7 +48,8 @@ func NewOrderEditor(m *tracker.Model) *OrderEditor { } } -func (oe *OrderEditor) Layout(gtx C, t *Tracker) D { +func (oe *OrderEditor) Layout(gtx C) D { + t := TrackerFromContext(gtx) if oe.scrollTable.CursorMoved() { cursor := t.TrackEditor.scrollTable.Table.Cursor() t.TrackEditor.scrollTable.ColTitleList.CenterOn(cursor.X) diff --git a/tracker/gioui/patch_panel.go b/tracker/gioui/patch_panel.go index a570629..503364e 100644 --- a/tracker/gioui/patch_panel.go +++ b/tracker/gioui/patch_panel.go @@ -90,14 +90,14 @@ func NewPatchPanel(model *tracker.Model) *PatchPanel { } } -func (pp *PatchPanel) Layout(gtx C, t *Tracker) D { +func (pp *PatchPanel) Layout(gtx C) D { return layout.Flex{Axis: layout.Vertical}.Layout(gtx, - layout.Rigid(func(gtx C) D { return pp.instrList.Layout(gtx, t) }), - layout.Rigid(func(gtx C) D { return pp.tools.Layout(gtx, t) }), + layout.Rigid(pp.instrList.Layout), + layout.Rigid(pp.tools.Layout), layout.Flexed(1, func(gtx C) D { return layout.Flex{Axis: layout.Horizontal}.Layout(gtx, - layout.Rigid(func(gtx C) D { return pp.unitList.Layout(gtx, t) }), - layout.Flexed(1, func(gtx C) D { return pp.unitEditor.Layout(gtx, t) }), + layout.Rigid(pp.unitList.Layout), + layout.Flexed(1, pp.unitEditor.Layout), ) })) } @@ -148,7 +148,8 @@ func MakeInstrumentTools(m *tracker.Model) InstrumentTools { return ret } -func (it *InstrumentTools) Layout(gtx C, t *Tracker) D { +func (it *InstrumentTools) Layout(gtx C) D { + t := TrackerFromContext(gtx) it.update(gtx, t) voicesLabel := Label(t.Theme, &t.Theme.InstrumentEditor.Voices, "Voices") splitInstrumentBtn := ActionIconBtn(t.SplitInstrument(), t.Theme, it.splitInstrumentBtn, icons.CommunicationCallSplit, it.splitInstrumentHint) @@ -256,7 +257,8 @@ func MakeInstrList(model *tracker.Model) InstrumentList { } } -func (il *InstrumentList) Layout(gtx C, t *Tracker) D { +func (il *InstrumentList) Layout(gtx C) D { + t := TrackerFromContext(gtx) il.update(gtx, t) octave := NumUpDown(t.Model.Octave(), t.Theme, t.OctaveNumberInput, "Octave") linkInstrTrackBtn := ToggleIconBtn(t.Model.LinkInstrTrack(), t.Theme, il.linkInstrTrackBtn, icons.NotificationSyncDisabled, icons.NotificationSync, il.linkDisabledHint, il.linkEnabledHint) @@ -264,7 +266,7 @@ func (il *InstrumentList) Layout(gtx C, t *Tracker) D { addInstrumentBtn := ActionIconBtn(t.Model.AddInstrument(), t.Theme, il.newInstrumentBtn, icons.ContentAdd, il.addInstrumentHint) return layout.Flex{Axis: layout.Horizontal, Alignment: layout.Middle}.Layout( gtx, - layout.Flexed(1, func(gtx C) D { return il.actualLayout(gtx, t) }), + layout.Flexed(1, il.actualLayout), layout.Rigid(layout.Spacer{Width: 10}.Layout), layout.Rigid(Label(t.Theme, &t.Theme.InstrumentEditor.Octave, "Octave").Layout), layout.Rigid(layout.Spacer{Width: 4}.Layout), @@ -275,7 +277,8 @@ func (il *InstrumentList) Layout(gtx C, t *Tracker) D { ) } -func (il *InstrumentList) actualLayout(gtx C, t *Tracker) D { +func (il *InstrumentList) actualLayout(gtx C) D { + t := TrackerFromContext(gtx) gtx.Constraints.Max.Y = gtx.Dp(36) gtx.Constraints.Min.Y = gtx.Dp(36) element := func(gtx C, i int) D { @@ -365,7 +368,8 @@ func MakeUnitList(m *tracker.Model) UnitList { return ret } -func (ul *UnitList) Layout(gtx C, t *Tracker) D { +func (ul *UnitList) Layout(gtx C) D { + t := TrackerFromContext(gtx) ul.update(gtx, t) var units [256]tracker.UnitListItem for i, item := range (*tracker.Units)(t.Model).Iterate { diff --git a/tracker/gioui/songpanel.go b/tracker/gioui/songpanel.go index ca0046b..7b04d5d 100644 --- a/tracker/gioui/songpanel.go +++ b/tracker/gioui/songpanel.go @@ -68,22 +68,18 @@ func (s *SongPanel) Update(gtx C, t *Tracker) { } } -func (s *SongPanel) Layout(gtx C, t *Tracker) D { +func (s *SongPanel) Layout(gtx C) D { + t := TrackerFromContext(gtx) s.Update(gtx, t) return layout.Flex{Axis: layout.Vertical}.Layout(gtx, - layout.Rigid(func(gtx C) D { - return s.MenuBar.Layout(gtx, t) - }), - layout.Rigid(func(gtx C) D { - return s.PlayBar.Layout(gtx, t) - }), - layout.Rigid(func(gtx C) D { - return s.layoutSongOptions(gtx, t) - }), + layout.Rigid(s.MenuBar.Layout), + layout.Rigid(s.PlayBar.Layout), + layout.Rigid(s.layoutSongOptions), ) } -func (t *SongPanel) layoutSongOptions(gtx C, tr *Tracker) D { +func (t *SongPanel) layoutSongOptions(gtx C) D { + tr := TrackerFromContext(gtx) paint.FillShape(gtx.Ops, tr.Theme.SongPanel.Bg, clip.Rect(image.Rect(0, 0, gtx.Constraints.Max.X, gtx.Constraints.Max.Y)).Op()) var weightingTxt string @@ -324,7 +320,8 @@ func NewMenuBar(tr *Tracker) *MenuBar { return ret } -func (t *MenuBar) Layout(gtx C, tr *Tracker) D { +func (t *MenuBar) Layout(gtx C) D { + tr := TrackerFromContext(gtx) gtx.Constraints.Max.Y = gtx.Dp(unit.Dp(36)) gtx.Constraints.Min.Y = gtx.Dp(unit.Dp(36)) @@ -409,7 +406,8 @@ func NewPlayBar() *PlayBar { return ret } -func (pb *PlayBar) Layout(gtx C, tr *Tracker) D { +func (pb *PlayBar) Layout(gtx C) D { + tr := TrackerFromContext(gtx) playBtn := ToggleIconBtn(tr.Playing(), tr.Theme, pb.PlayingBtn, icons.AVPlayArrow, icons.AVStop, pb.playHint, pb.stopHint) rewindBtn := ActionIconBtn(tr.PlaySongStart(), tr.Theme, pb.RewindBtn, icons.AVFastRewind, pb.rewindHint) recordBtn := ToggleIconBtn(tr.IsRecording(), tr.Theme, pb.RecordBtn, icons.AVFiberManualRecord, icons.AVFiberSmartRecord, pb.recordHint, pb.stopRecordHint) diff --git a/tracker/gioui/tracker.go b/tracker/gioui/tracker.go index ddc2232..257dc73 100644 --- a/tracker/gioui/tracker.go +++ b/tracker/gioui/tracker.go @@ -121,6 +121,8 @@ func (t *Tracker) Main() { recoveryTicker := time.NewTicker(time.Second * 30) var ops op.Ops titlePath := "" + globals := make(map[string]any, 1) + globals["Tracker"] = t for !t.Quitted() { w := t.newWindow() w.Option(app.Title(titleFromPath(titlePath))) @@ -173,6 +175,7 @@ func (t *Tracker) Main() { w.Option(app.Title(titleFromPath(titlePath))) } gtx := app.NewContext(&ops, e) + gtx.Values = globals t.Layout(gtx, w) e.Frame(gtx.Ops) if t.Quitted() { @@ -190,6 +193,14 @@ func (t *Tracker) Main() { close(t.Broker().FinishedGUI) } +func TrackerFromContext(gtx C) *Tracker { + t, ok := gtx.Values["Tracker"] + if !ok { + panic("Tracker not found in context values") + } + return t.(*Tracker) +} + func (t *Tracker) newWindow() *app.Window { w := new(app.Window) w.Option(app.Size(t.preferences.WindowSize())) @@ -342,24 +353,16 @@ func (t *Tracker) explorerCreateFile(success func(io.WriteCloser), filename stri func (t *Tracker) layoutBottom(gtx layout.Context) layout.Dimensions { return t.BottomHorizontalSplit.Layout(gtx, &t.Theme.Split, - func(gtx C) D { - return t.OrderEditor.Layout(gtx, t) - }, - func(gtx C) D { - return t.TrackEditor.Layout(gtx, t) - }, + t.OrderEditor.Layout, + t.TrackEditor.Layout, ) } func (t *Tracker) layoutTop(gtx layout.Context) layout.Dimensions { return t.TopHorizontalSplit.Layout(gtx, &t.Theme.Split, - func(gtx C) D { - return t.SongPanel.Layout(gtx, t) - }, - func(gtx C) D { - return t.PatchPanel.Layout(gtx, t) - }, + t.SongPanel.Layout, + t.PatchPanel.Layout, ) } diff --git a/tracker/gioui/unit_editor.go b/tracker/gioui/unit_editor.go index 6ac6eb0..fdda865 100644 --- a/tracker/gioui/unit_editor.go +++ b/tracker/gioui/unit_editor.go @@ -61,7 +61,8 @@ func NewUnitEditor(m *tracker.Model) *UnitEditor { return ret } -func (pe *UnitEditor) Layout(gtx C, t *Tracker) D { +func (pe *UnitEditor) Layout(gtx C) D { + t := TrackerFromContext(gtx) for { e, ok := gtx.Event( key.Filter{Focus: pe.sliderList, Name: key.NameLeftArrow, Optional: key.ModShift},