refactor(tracker): use go v1.23 style iterators throughout

This commit is contained in:
5684185+vsariola@users.noreply.github.com 2024-10-15 00:01:02 +03:00
parent 2b7ce39069
commit 97e59c5650
7 changed files with 108 additions and 87 deletions

View File

@ -18,7 +18,7 @@ type (
} }
AlertPriority int AlertPriority int
AlertYieldFunc func(alert Alert) AlertYieldFunc func(index int, alert Alert) bool
Alerts Model Alerts Model
) )
@ -35,9 +35,13 @@ func (m *Model) Alerts() *Alerts { return (*Alerts)(m) }
// Alerts methods // Alerts methods
func (m *Alerts) Iterate(yield AlertYieldFunc) { func (m *Alerts) Iterate() func(yield func(index int, alert Alert) bool) {
for _, a := range m.alerts { return func(yield func(index int, alert Alert) bool) {
yield(a) for i, a := range m.alerts {
if !yield(i, a) {
break
}
}
} }
} }

View File

@ -338,13 +338,13 @@ func (ie *InstrumentEditor) layoutUnitList(gtx C, t *Tracker) D {
addUnitBtnStyle.IconButtonStyle.Background = t.Theme.Fg addUnitBtnStyle.IconButtonStyle.Background = t.Theme.Fg
addUnitBtnStyle.IconButtonStyle.Inset = layout.UniformInset(unit.Dp(4)) addUnitBtnStyle.IconButtonStyle.Inset = layout.UniformInset(unit.Dp(4))
index := 0
var units [256]tracker.UnitListItem var units [256]tracker.UnitListItem
(*tracker.Units)(t.Model).Iterate(func(item tracker.UnitListItem) (ok bool) { for i, item := range (*tracker.Units)(t.Model).Iterate() {
units[index] = item if i >= 256 {
index++ break
return index <= 256 }
}) units[i] = item
}
count := intMin(ie.unitDragList.TrackerList.Count(), 256) count := intMin(ie.unitDragList.TrackerList.Count(), 256)
element := func(gtx C, i int) D { element := func(gtx C, i int) D {

View File

@ -36,7 +36,7 @@ func (a *PopupAlert) Layout(gtx C) D {
a.prevUpdate = now a.prevUpdate = now
var totalY float64 = float64(gtx.Dp(38)) var totalY float64 = float64(gtx.Dp(38))
a.alerts.Iterate(func(alert tracker.Alert) { for _, alert := range a.alerts.Iterate() {
var color, textColor, shadeColor color.NRGBA var color, textColor, shadeColor color.NRGBA
switch alert.Priority { switch alert.Priority {
case tracker.Warning: case tracker.Warning:
@ -76,6 +76,6 @@ func (a *PopupAlert) Layout(gtx C) D {
return dims return dims
}) })
}) })
}) }
return D{} return D{}
} }

View File

@ -102,11 +102,10 @@ func (pe *UnitEditor) layoutSliders(gtx C, t *Tracker) D {
} }
index := 0 index := 0
t.Model.Params().Iterate(func(param tracker.Parameter) { for param := range t.Model.Params().Iterate() {
pe.Parameters[index].Parameter = param pe.Parameters[index].Parameter = param
index++ index++
}) }
element := func(gtx C, index int) D { element := func(gtx C, index int) D {
if index < 0 || index >= numItems { if index < 0 || index >= numItems {
return D{} return D{}
@ -177,12 +176,12 @@ func (pe *UnitEditor) layoutFooter(gtx C, t *Tracker) D {
func (pe *UnitEditor) layoutUnitTypeChooser(gtx C, t *Tracker) D { func (pe *UnitEditor) layoutUnitTypeChooser(gtx C, t *Tracker) D {
var names [256]string var names [256]string
index := 0 for i, item := range t.Model.SearchResults().Iterate() {
t.Model.SearchResults().Iterate(func(item string) (ok bool) { if i >= 256 {
names[index] = item break
index++ }
return index <= 256 names[i] = item
}) }
element := func(gtx C, i int) D { element := func(gtx C, i int) D {
w := LabelStyle{Text: names[i], ShadeColor: black, Color: white, Font: labelDefaultFont, FontSize: unit.Sp(12), Shaper: t.Theme.Shaper} w := LabelStyle{Text: names[i], ShadeColor: black, Color: white, Font: labelDefaultFont, FontSize: unit.Sp(12), Shaper: t.Theme.Shaper}
if i == pe.searchList.TrackerList.Selected() { if i == pe.searchList.TrackerList.Selected() {

View File

@ -38,8 +38,8 @@ type (
StackNeed, StackBefore, StackAfter int StackNeed, StackBefore, StackAfter int
} }
UnitYieldFunc func(item UnitListItem) (ok bool) UnitYieldFunc func(index int, item UnitListItem) (ok bool)
UnitSearchYieldFunc func(item string) (ok bool) UnitSearchYieldFunc func(index int, item string) (ok bool)
Instruments Model // Instruments is a list of instruments, implementing ListData & MutableListData interfaces Instruments Model // Instruments is a list of instruments, implementing ListData & MutableListData interfaces
Units Model // Units is a list of all the units in the selected instrument, implementing ListData & MutableListData interfaces Units Model // Units is a list of all the units in the selected instrument, implementing ListData & MutableListData interfaces
@ -323,24 +323,26 @@ 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 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(yield UnitYieldFunc) { func (v *Units) Iterate() func(yield UnitYieldFunc) {
if v.d.InstrIndex < 0 || v.d.InstrIndex >= len(v.d.Song.Patch) { return func(yield UnitYieldFunc) {
return if v.d.InstrIndex < 0 || v.d.InstrIndex >= len(v.d.Song.Patch) {
} return
stackBefore := 0 }
for _, unit := range v.d.Song.Patch[v.d.InstrIndex].Units { stackBefore := 0
stackAfter := stackBefore + unit.StackChange() for i, unit := range v.d.Song.Patch[v.d.InstrIndex].Units {
if !yield(UnitListItem{ stackAfter := stackBefore + unit.StackChange()
Type: unit.Type, if !yield(i, UnitListItem{
Comment: unit.Comment, Type: unit.Type,
Disabled: unit.Disabled, Comment: unit.Comment,
StackNeed: unit.StackNeed(), Disabled: unit.Disabled,
StackBefore: stackBefore, StackNeed: unit.StackNeed(),
StackAfter: stackAfter, StackBefore: stackBefore,
}) { StackAfter: stackAfter,
break }) {
break
}
stackBefore = stackAfter
} }
stackBefore = stackAfter
} }
} }
@ -743,13 +745,17 @@ func (v *SearchResults) List() List {
return List{v} return List{v}
} }
func (l *SearchResults) Iterate(yield UnitSearchYieldFunc) { func (l *SearchResults) Iterate() func(yield UnitSearchYieldFunc) {
for _, name := range sointu.UnitNames { return func(yield UnitSearchYieldFunc) {
if !strings.HasPrefix(name, l.d.UnitSearchString) { index := 0
continue for _, name := range sointu.UnitNames {
} if !strings.HasPrefix(name, l.d.UnitSearchString) {
if !yield(name) { continue
break }
if !yield(index, name) {
break
}
index++
} }
} }
} }

View File

@ -292,14 +292,14 @@ func FuzzModel(f *testing.F) {
index-- index--
return index > 0 return index > 0
}, seed) }, seed)
state.model.Alerts().Iterate(func(a tracker.Alert) { for _, a := range model.Alerts().Iterate() {
if a.Name == "IDCollision" { if a.Name == "IDCollision" {
t.Errorf("Path: %s Model has ID collisions", totalPath) t.Errorf("Path: %s Model has ID collisions", totalPath)
} }
if a.Name == "InvalidUnitParameters" { if a.Name == "InvalidUnitParameters" {
t.Errorf("Path: %s Model units with invalid parameters", totalPath) t.Errorf("Path: %s Model units with invalid parameters", totalPath)
} }
}) }
} }
closeChan <- struct{}{} closeChan <- struct{}{}
}) })

View File

@ -41,7 +41,7 @@ type (
Params Model Params Model
ParamYieldFunc func(Parameter) ParamYieldFunc func(param Parameter) bool
ParameterType int ParameterType int
) )
@ -77,59 +77,71 @@ func (pl *Params) change(n string, severity ChangeSeverity) func() {
func (pl *Params) Count() int { func (pl *Params) Count() int {
count := 0 count := 0
pl.Iterate(func(p Parameter) { for range pl.Iterate() {
count++ count++
}) }
return count return count
} }
func (pl *Params) SelectedItem() (ret Parameter) { func (pl *Params) SelectedItem() (ret Parameter) {
index := pl.Selected() index := pl.Selected()
pl.Iterate(func(param Parameter) { for param := range pl.Iterate() {
if index == 0 { if index == 0 {
ret = param ret = param
} }
index-- index--
}) }
return return
} }
func (pl *Params) Iterate(yield ParamYieldFunc) { func (pl *Params) Iterate() func(yield ParamYieldFunc) {
if pl.d.InstrIndex < 0 || pl.d.InstrIndex >= len(pl.d.Song.Patch) { return func(yield ParamYieldFunc) {
return 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 { if pl.d.UnitIndex < 0 || pl.d.UnitIndex >= len(pl.d.Song.Patch[pl.d.InstrIndex].Units) {
break // don't show the sample related params unless necessary return
} }
yield(NamedParameter{ unit := &pl.d.Song.Patch[pl.d.InstrIndex].Units[pl.d.UnitIndex]
parameter: parameter{m: (*Model)(pl), unit: unit}, unitType, ok := sointu.UnitTypes[unit.Type]
up: &unitType[i], if !ok {
}) return
}
if unit.Type == "oscillator" && unit.Parameters["type"] == sointu.Sample {
yield(GmDlsEntryParameter{parameter: parameter{m: (*Model)(pl), unit: unit}})
}
switch {
case unit.Type == "delay":
if unit.Parameters["stereo"] == 1 && len(unit.VarArgs)%2 == 1 {
unit.VarArgs = append(unit.VarArgs, 1)
} }
yield(ReverbParameter{parameter: parameter{m: (*Model)(pl), unit: unit}}) for i := range unitType {
yield(DelayLinesParameter{parameter: parameter{m: (*Model)(pl), unit: unit}}) if !unitType[i].CanSet {
for i := range unit.VarArgs { continue
yield(DelayTimeParameter{parameter: parameter{m: (*Model)(pl), unit: unit}, index: i}) }
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 unit.Type == "oscillator" && unit.Parameters["type"] == sointu.Sample {
if !yield(GmDlsEntryParameter{parameter: parameter{m: (*Model)(pl), unit: unit}}) {
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
}
}
} }
} }
} }