mirror of
https://github.com/vsariola/sointu.git
synced 2025-05-28 03:10:24 -04:00
refactor(tracker): use go v1.23 style iterators throughout
This commit is contained in:
parent
2b7ce39069
commit
97e59c5650
@ -18,7 +18,7 @@ type (
|
||||
}
|
||||
|
||||
AlertPriority int
|
||||
AlertYieldFunc func(alert Alert)
|
||||
AlertYieldFunc func(index int, alert Alert) bool
|
||||
Alerts Model
|
||||
)
|
||||
|
||||
@ -35,9 +35,13 @@ func (m *Model) Alerts() *Alerts { return (*Alerts)(m) }
|
||||
|
||||
// Alerts methods
|
||||
|
||||
func (m *Alerts) Iterate(yield AlertYieldFunc) {
|
||||
for _, a := range m.alerts {
|
||||
yield(a)
|
||||
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
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -338,13 +338,13 @@ func (ie *InstrumentEditor) layoutUnitList(gtx C, t *Tracker) D {
|
||||
addUnitBtnStyle.IconButtonStyle.Background = t.Theme.Fg
|
||||
addUnitBtnStyle.IconButtonStyle.Inset = layout.UniformInset(unit.Dp(4))
|
||||
|
||||
index := 0
|
||||
var units [256]tracker.UnitListItem
|
||||
(*tracker.Units)(t.Model).Iterate(func(item tracker.UnitListItem) (ok bool) {
|
||||
units[index] = item
|
||||
index++
|
||||
return index <= 256
|
||||
})
|
||||
for i, item := range (*tracker.Units)(t.Model).Iterate() {
|
||||
if i >= 256 {
|
||||
break
|
||||
}
|
||||
units[i] = item
|
||||
}
|
||||
count := intMin(ie.unitDragList.TrackerList.Count(), 256)
|
||||
|
||||
element := func(gtx C, i int) D {
|
||||
|
@ -36,7 +36,7 @@ func (a *PopupAlert) Layout(gtx C) D {
|
||||
a.prevUpdate = now
|
||||
|
||||
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
|
||||
switch alert.Priority {
|
||||
case tracker.Warning:
|
||||
@ -76,6 +76,6 @@ func (a *PopupAlert) Layout(gtx C) D {
|
||||
return dims
|
||||
})
|
||||
})
|
||||
})
|
||||
}
|
||||
return D{}
|
||||
}
|
||||
|
@ -102,11 +102,10 @@ func (pe *UnitEditor) layoutSliders(gtx C, t *Tracker) D {
|
||||
}
|
||||
|
||||
index := 0
|
||||
t.Model.Params().Iterate(func(param tracker.Parameter) {
|
||||
for param := range t.Model.Params().Iterate() {
|
||||
pe.Parameters[index].Parameter = param
|
||||
index++
|
||||
})
|
||||
|
||||
}
|
||||
element := func(gtx C, index int) D {
|
||||
if index < 0 || index >= numItems {
|
||||
return D{}
|
||||
@ -177,12 +176,12 @@ func (pe *UnitEditor) layoutFooter(gtx C, t *Tracker) D {
|
||||
|
||||
func (pe *UnitEditor) layoutUnitTypeChooser(gtx C, t *Tracker) D {
|
||||
var names [256]string
|
||||
index := 0
|
||||
t.Model.SearchResults().Iterate(func(item string) (ok bool) {
|
||||
names[index] = item
|
||||
index++
|
||||
return index <= 256
|
||||
})
|
||||
for i, item := range t.Model.SearchResults().Iterate() {
|
||||
if i >= 256 {
|
||||
break
|
||||
}
|
||||
names[i] = item
|
||||
}
|
||||
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}
|
||||
if i == pe.searchList.TrackerList.Selected() {
|
||||
|
@ -38,8 +38,8 @@ type (
|
||||
StackNeed, StackBefore, StackAfter int
|
||||
}
|
||||
|
||||
UnitYieldFunc func(item UnitListItem) (ok bool)
|
||||
UnitSearchYieldFunc func(item string) (ok bool)
|
||||
UnitYieldFunc func(index int, item UnitListItem) (ok bool)
|
||||
UnitSearchYieldFunc func(index int, item string) (ok bool)
|
||||
|
||||
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
|
||||
@ -323,14 +323,15 @@ 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(yield UnitYieldFunc) {
|
||||
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 _, unit := range v.d.Song.Patch[v.d.InstrIndex].Units {
|
||||
for i, unit := range v.d.Song.Patch[v.d.InstrIndex].Units {
|
||||
stackAfter := stackBefore + unit.StackChange()
|
||||
if !yield(UnitListItem{
|
||||
if !yield(i, UnitListItem{
|
||||
Type: unit.Type,
|
||||
Comment: unit.Comment,
|
||||
Disabled: unit.Disabled,
|
||||
@ -343,6 +344,7 @@ func (v *Units) Iterate(yield UnitYieldFunc) {
|
||||
stackBefore = stackAfter
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (v *Units) Selected() int {
|
||||
return intMax(intMin(v.d.UnitIndex, v.Count()-1), 0)
|
||||
@ -743,14 +745,18 @@ func (v *SearchResults) List() List {
|
||||
return List{v}
|
||||
}
|
||||
|
||||
func (l *SearchResults) Iterate(yield UnitSearchYieldFunc) {
|
||||
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(name) {
|
||||
if !yield(index, name) {
|
||||
break
|
||||
}
|
||||
index++
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -292,14 +292,14 @@ func FuzzModel(f *testing.F) {
|
||||
index--
|
||||
return index > 0
|
||||
}, seed)
|
||||
state.model.Alerts().Iterate(func(a tracker.Alert) {
|
||||
for _, a := range model.Alerts().Iterate() {
|
||||
if a.Name == "IDCollision" {
|
||||
t.Errorf("Path: %s Model has ID collisions", totalPath)
|
||||
}
|
||||
if a.Name == "InvalidUnitParameters" {
|
||||
t.Errorf("Path: %s Model units with invalid parameters", totalPath)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
closeChan <- struct{}{}
|
||||
})
|
||||
|
@ -41,7 +41,7 @@ type (
|
||||
|
||||
Params Model
|
||||
|
||||
ParamYieldFunc func(Parameter)
|
||||
ParamYieldFunc func(param Parameter) bool
|
||||
|
||||
ParameterType int
|
||||
)
|
||||
@ -77,24 +77,25 @@ func (pl *Params) change(n string, severity ChangeSeverity) func() {
|
||||
|
||||
func (pl *Params) Count() int {
|
||||
count := 0
|
||||
pl.Iterate(func(p Parameter) {
|
||||
for range pl.Iterate() {
|
||||
count++
|
||||
})
|
||||
}
|
||||
return count
|
||||
}
|
||||
|
||||
func (pl *Params) SelectedItem() (ret Parameter) {
|
||||
index := pl.Selected()
|
||||
pl.Iterate(func(param Parameter) {
|
||||
for param := range pl.Iterate() {
|
||||
if index == 0 {
|
||||
ret = param
|
||||
}
|
||||
index--
|
||||
})
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func (pl *Params) Iterate(yield ParamYieldFunc) {
|
||||
func (pl *Params) Iterate() func(yield ParamYieldFunc) {
|
||||
return func(yield ParamYieldFunc) {
|
||||
if pl.d.InstrIndex < 0 || pl.d.InstrIndex >= len(pl.d.Song.Patch) {
|
||||
return
|
||||
}
|
||||
@ -113,23 +114,34 @@ func (pl *Params) Iterate(yield ParamYieldFunc) {
|
||||
if unit.Type == "oscillator" && unit.Parameters["type"] != sointu.Sample && i >= 11 {
|
||||
break // don't show the sample related params unless necessary
|
||||
}
|
||||
yield(NamedParameter{
|
||||
if !yield(NamedParameter{
|
||||
parameter: parameter{m: (*Model)(pl), unit: unit},
|
||||
up: &unitType[i],
|
||||
})
|
||||
}) {
|
||||
return
|
||||
}
|
||||
}
|
||||
if unit.Type == "oscillator" && unit.Parameters["type"] == sointu.Sample {
|
||||
yield(GmDlsEntryParameter{parameter: parameter{m: (*Model)(pl), unit: unit}})
|
||||
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)
|
||||
}
|
||||
yield(ReverbParameter{parameter: parameter{m: (*Model)(pl), unit: unit}})
|
||||
yield(DelayLinesParameter{parameter: parameter{m: (*Model)(pl), unit: unit}})
|
||||
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 {
|
||||
yield(DelayTimeParameter{parameter: parameter{m: (*Model)(pl), unit: unit}, index: i})
|
||||
if !yield(DelayTimeParameter{parameter: parameter{m: (*Model)(pl), unit: unit}, index: i}) {
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user