From b494a69a76a4b231627ea5e3751fbc13c6e89bfa Mon Sep 17 00:00:00 2001 From: "5684185+vsariola@users.noreply.github.com" <5684185+vsariola@users.noreply.github.com> Date: Tue, 15 Oct 2024 09:09:17 +0300 Subject: [PATCH] refactor(tracker): change Iterate() func(yield):s to Iterate(yield) --- cmd/sointu-vsti/main.go | 4 +- tracker/alert.go | 10 ++-- tracker/gioui/instrument_editor.go | 2 +- tracker/gioui/popup_alert.go | 2 +- tracker/gioui/songpanel.go | 2 +- tracker/gioui/unit_editor.go | 4 +- tracker/gomidi/midi.go | 26 +++++----- tracker/list.go | 56 ++++++++++----------- tracker/model.go | 2 +- tracker/model_test.go | 6 +-- tracker/params.go | 80 +++++++++++++++--------------- 11 files changed, 90 insertions(+), 104 deletions(-) diff --git a/cmd/sointu-vsti/main.go b/cmd/sointu-vsti/main.go index 74b5b46..bfa216a 100644 --- a/cmd/sointu-vsti/main.go +++ b/cmd/sointu-vsti/main.go @@ -27,9 +27,7 @@ type ( NullMIDIContext struct{} ) -func (m NullMIDIContext) ListInputDevices() func(yield func(tracker.MIDIDevice) bool) { - return func(yield func(tracker.MIDIDevice) bool) {} -} +func (m NullMIDIContext) InputDevices(yield func(tracker.MIDIDevice) bool) {} func (m NullMIDIContext) Close() {} diff --git a/tracker/alert.go b/tracker/alert.go index 871e828..93d19df 100644 --- a/tracker/alert.go +++ b/tracker/alert.go @@ -35,12 +35,10 @@ func (m *Model) Alerts() *Alerts { return (*Alerts)(m) } // Alerts methods -func (m *Alerts) Iterate() func(yield func(index int, alert Alert) bool) { - return func(yield func(index int, alert Alert) bool) { - for i, a := range m.alerts { - if !yield(i, a) { - break - } +func (m *Alerts) Iterate(yield func(index int, alert Alert) bool) { + for i, a := range m.alerts { + if !yield(i, a) { + break } } } diff --git a/tracker/gioui/instrument_editor.go b/tracker/gioui/instrument_editor.go index 5128ae7..0516878 100644 --- a/tracker/gioui/instrument_editor.go +++ b/tracker/gioui/instrument_editor.go @@ -339,7 +339,7 @@ func (ie *InstrumentEditor) layoutUnitList(gtx C, t *Tracker) D { addUnitBtnStyle.IconButtonStyle.Inset = layout.UniformInset(unit.Dp(4)) var units [256]tracker.UnitListItem - for i, item := range (*tracker.Units)(t.Model).Iterate() { + for i, item := range (*tracker.Units)(t.Model).Iterate { if i >= 256 { break } diff --git a/tracker/gioui/popup_alert.go b/tracker/gioui/popup_alert.go index c44a5db..ac5f2b0 100644 --- a/tracker/gioui/popup_alert.go +++ b/tracker/gioui/popup_alert.go @@ -36,7 +36,7 @@ func (a *PopupAlert) Layout(gtx C) D { a.prevUpdate = now var totalY float64 = float64(gtx.Dp(38)) - for _, alert := range a.alerts.Iterate() { + for _, alert := range a.alerts.Iterate { var color, textColor, shadeColor color.NRGBA switch alert.Priority { case tracker.Warning: diff --git a/tracker/gioui/songpanel.go b/tracker/gioui/songpanel.go index e74ecc2..37bbb03 100644 --- a/tracker/gioui/songpanel.go +++ b/tracker/gioui/songpanel.go @@ -84,7 +84,7 @@ func NewSongPanel(model *tracker.Model) *SongPanel { {IconBytes: icons.ContentRedo, Text: "Redo", ShortcutText: keyActionMap["Redo"], Doer: model.Redo()}, {IconBytes: icons.ImageCrop, Text: "Remove unused data", ShortcutText: keyActionMap["RemoveUnused"], Doer: model.RemoveUnused()}, } - for input := range model.MIDI.ListInputDevices() { + for input := range model.MIDI.InputDevices { ret.midiMenuItems = append(ret.midiMenuItems, MenuItem{ IconBytes: icons.ImageControlPoint, Text: input.String(), diff --git a/tracker/gioui/unit_editor.go b/tracker/gioui/unit_editor.go index 7f33a2d..ac962f4 100644 --- a/tracker/gioui/unit_editor.go +++ b/tracker/gioui/unit_editor.go @@ -102,7 +102,7 @@ func (pe *UnitEditor) layoutSliders(gtx C, t *Tracker) D { } index := 0 - for param := range t.Model.Params().Iterate() { + for param := range t.Model.Params().Iterate { pe.Parameters[index].Parameter = param index++ } @@ -176,7 +176,7 @@ func (pe *UnitEditor) layoutFooter(gtx C, t *Tracker) D { func (pe *UnitEditor) layoutUnitTypeChooser(gtx C, t *Tracker) D { var names [256]string - for i, item := range t.Model.SearchResults().Iterate() { + for i, item := range t.Model.SearchResults().Iterate { if i >= 256 { break } diff --git a/tracker/gomidi/midi.go b/tracker/gomidi/midi.go index 956ac8e..d154735 100644 --- a/tracker/gomidi/midi.go +++ b/tracker/gomidi/midi.go @@ -23,20 +23,18 @@ type ( } ) -func (m *RTMIDIContext) ListInputDevices() func(yield func(tracker.MIDIDevice) bool) { - return func(yield func(tracker.MIDIDevice) bool) { - if m.driver == nil { - return - } - ins, err := m.driver.Ins() - if err != nil { - return - } - for i := 0; i < len(ins); i++ { - device := RTMIDIDevice{context: m, in: ins[i]} - if !yield(device) { - break - } +func (m *RTMIDIContext) InputDevices(yield func(tracker.MIDIDevice) bool) { + if m.driver == nil { + return + } + ins, err := m.driver.Ins() + if err != nil { + return + } + for i := 0; i < len(ins); i++ { + device := RTMIDIDevice{context: m, in: ins[i]} + if !yield(device) { + break } } } diff --git a/tracker/list.go b/tracker/list.go index c86d4df..a39d65c 100644 --- a/tracker/list.go +++ b/tracker/list.go @@ -323,26 +323,24 @@ func (m *Units) SetSelectedType(t string) { m.d.Song.Patch[m.d.InstrIndex].Units[m.d.UnitIndex].ID = oldUnit.ID // keep the ID of the replaced unit } -func (v *Units) Iterate() func(yield UnitYieldFunc) { - return func(yield UnitYieldFunc) { - if v.d.InstrIndex < 0 || v.d.InstrIndex >= len(v.d.Song.Patch) { - return - } - stackBefore := 0 - for i, unit := range v.d.Song.Patch[v.d.InstrIndex].Units { - stackAfter := stackBefore + unit.StackChange() - if !yield(i, UnitListItem{ - Type: unit.Type, - Comment: unit.Comment, - Disabled: unit.Disabled, - StackNeed: unit.StackNeed(), - StackBefore: stackBefore, - StackAfter: stackAfter, - }) { - break - } - stackBefore = stackAfter +func (v *Units) Iterate(yield UnitYieldFunc) { + if v.d.InstrIndex < 0 || v.d.InstrIndex >= len(v.d.Song.Patch) { + return + } + stackBefore := 0 + for i, unit := range v.d.Song.Patch[v.d.InstrIndex].Units { + stackAfter := stackBefore + unit.StackChange() + if !yield(i, UnitListItem{ + Type: unit.Type, + Comment: unit.Comment, + Disabled: unit.Disabled, + StackNeed: unit.StackNeed(), + StackBefore: stackBefore, + StackAfter: stackAfter, + }) { + break } + stackBefore = stackAfter } } @@ -745,18 +743,16 @@ func (v *SearchResults) List() List { return List{v} } -func (l *SearchResults) Iterate() func(yield UnitSearchYieldFunc) { - return func(yield UnitSearchYieldFunc) { - index := 0 - for _, name := range sointu.UnitNames { - if !strings.HasPrefix(name, l.d.UnitSearchString) { - continue - } - if !yield(index, name) { - break - } - index++ +func (l *SearchResults) Iterate(yield UnitSearchYieldFunc) { + index := 0 + for _, name := range sointu.UnitNames { + if !strings.HasPrefix(name, l.d.UnitSearchString) { + continue } + if !yield(index, name) { + break + } + index++ } } diff --git a/tracker/model.go b/tracker/model.go index 4e4c1e8..a28ae42 100644 --- a/tracker/model.go +++ b/tracker/model.go @@ -124,7 +124,7 @@ type ( Dialog int MIDIContext interface { - ListInputDevices() func(yield func(MIDIDevice) bool) + InputDevices(yield func(MIDIDevice) bool) Close() } diff --git a/tracker/model_test.go b/tracker/model_test.go index 36ef169..ffa2a0a 100644 --- a/tracker/model_test.go +++ b/tracker/model_test.go @@ -21,9 +21,7 @@ func (NullContext) BPM() (bpm float64, ok bool) { return 0, false } -func (NullContext) ListInputDevices() func(yield func(tracker.MIDIDevice) bool) { - return func(yield func(tracker.MIDIDevice) bool) {} -} +func (NullContext) InputDevices(yield func(tracker.MIDIDevice) bool) {} func (NullContext) Close() {} @@ -292,7 +290,7 @@ func FuzzModel(f *testing.F) { index-- return index > 0 }, seed) - for _, a := range model.Alerts().Iterate() { + for _, a := range model.Alerts().Iterate { if a.Name == "IDCollision" { t.Errorf("Path: %s Model has ID collisions", totalPath) } diff --git a/tracker/params.go b/tracker/params.go index afbf3df..9287ddf 100644 --- a/tracker/params.go +++ b/tracker/params.go @@ -77,7 +77,7 @@ func (pl *Params) change(n string, severity ChangeSeverity) func() { func (pl *Params) Count() int { count := 0 - for range pl.Iterate() { + for range pl.Iterate { count++ } return count @@ -85,7 +85,7 @@ func (pl *Params) Count() int { func (pl *Params) SelectedItem() (ret Parameter) { index := pl.Selected() - for param := range pl.Iterate() { + for param := range pl.Iterate { if index == 0 { ret = param } @@ -94,55 +94,53 @@ func (pl *Params) SelectedItem() (ret Parameter) { return } -func (pl *Params) Iterate() func(yield ParamYieldFunc) { - return func(yield ParamYieldFunc) { - if pl.d.InstrIndex < 0 || pl.d.InstrIndex >= len(pl.d.Song.Patch) { +func (pl *Params) Iterate(yield ParamYieldFunc) { + if pl.d.InstrIndex < 0 || pl.d.InstrIndex >= len(pl.d.Song.Patch) { + return + } + if pl.d.UnitIndex < 0 || pl.d.UnitIndex >= len(pl.d.Song.Patch[pl.d.InstrIndex].Units) { + return + } + unit := &pl.d.Song.Patch[pl.d.InstrIndex].Units[pl.d.UnitIndex] + unitType, ok := sointu.UnitTypes[unit.Type] + if !ok { + return + } + for i := range unitType { + if !unitType[i].CanSet { + continue + } + if unit.Type == "oscillator" && unit.Parameters["type"] != sointu.Sample && i >= 11 { + break // don't show the sample related params unless necessary + } + if !yield(NamedParameter{ + parameter: parameter{m: (*Model)(pl), unit: unit}, + up: &unitType[i], + }) { return } - if pl.d.UnitIndex < 0 || pl.d.UnitIndex >= len(pl.d.Song.Patch[pl.d.InstrIndex].Units) { + } + if unit.Type == "oscillator" && unit.Parameters["type"] == sointu.Sample { + if !yield(GmDlsEntryParameter{parameter: parameter{m: (*Model)(pl), unit: unit}}) { return } - unit := &pl.d.Song.Patch[pl.d.InstrIndex].Units[pl.d.UnitIndex] - unitType, ok := sointu.UnitTypes[unit.Type] - if !ok { + } + switch { + case unit.Type == "delay": + if unit.Parameters["stereo"] == 1 && len(unit.VarArgs)%2 == 1 { + unit.VarArgs = append(unit.VarArgs, 1) + } + if !yield(ReverbParameter{parameter: parameter{m: (*Model)(pl), unit: unit}}) { return } - for i := range unitType { - if !unitType[i].CanSet { - continue - } - if unit.Type == "oscillator" && unit.Parameters["type"] != sointu.Sample && i >= 11 { - break // don't show the sample related params unless necessary - } - if !yield(NamedParameter{ - parameter: parameter{m: (*Model)(pl), unit: unit}, - up: &unitType[i], - }) { - return - } + if !yield(DelayLinesParameter{parameter: parameter{m: (*Model)(pl), unit: unit}}) { + return } - if unit.Type == "oscillator" && unit.Parameters["type"] == sointu.Sample { - if !yield(GmDlsEntryParameter{parameter: parameter{m: (*Model)(pl), unit: unit}}) { + for i := range unit.VarArgs { + if !yield(DelayTimeParameter{parameter: parameter{m: (*Model)(pl), unit: unit}, index: i}) { return } } - switch { - case unit.Type == "delay": - if unit.Parameters["stereo"] == 1 && len(unit.VarArgs)%2 == 1 { - unit.VarArgs = append(unit.VarArgs, 1) - } - if !yield(ReverbParameter{parameter: parameter{m: (*Model)(pl), unit: unit}}) { - return - } - if !yield(DelayLinesParameter{parameter: parameter{m: (*Model)(pl), unit: unit}}) { - return - } - for i := range unit.VarArgs { - if !yield(DelayTimeParameter{parameter: parameter{m: (*Model)(pl), unit: unit}, index: i}) { - return - } - } - } } }