refactor(tracker): change Iterate() func(yield):s to Iterate(yield)

This commit is contained in:
5684185+vsariola@users.noreply.github.com 2024-10-15 09:09:17 +03:00
parent 3986bbede7
commit b494a69a76
11 changed files with 90 additions and 104 deletions

View File

@ -27,9 +27,7 @@ type (
NullMIDIContext struct{} NullMIDIContext struct{}
) )
func (m NullMIDIContext) ListInputDevices() func(yield func(tracker.MIDIDevice) bool) { func (m NullMIDIContext) InputDevices(yield func(tracker.MIDIDevice) bool) {}
return func(yield func(tracker.MIDIDevice) bool) {}
}
func (m NullMIDIContext) Close() {} func (m NullMIDIContext) Close() {}

View File

@ -35,12 +35,10 @@ func (m *Model) Alerts() *Alerts { return (*Alerts)(m) }
// Alerts methods // Alerts methods
func (m *Alerts) Iterate() func(yield func(index int, alert Alert) bool) { func (m *Alerts) Iterate(yield func(index int, alert Alert) bool) {
return func(yield func(index int, alert Alert) bool) { for i, a := range m.alerts {
for i, a := range m.alerts { if !yield(i, a) {
if !yield(i, a) { break
break
}
} }
} }
} }

View File

@ -339,7 +339,7 @@ func (ie *InstrumentEditor) layoutUnitList(gtx C, t *Tracker) D {
addUnitBtnStyle.IconButtonStyle.Inset = layout.UniformInset(unit.Dp(4)) addUnitBtnStyle.IconButtonStyle.Inset = layout.UniformInset(unit.Dp(4))
var units [256]tracker.UnitListItem 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 { if i >= 256 {
break break
} }

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))
for _, alert := range a.alerts.Iterate() { 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:

View File

@ -84,7 +84,7 @@ func NewSongPanel(model *tracker.Model) *SongPanel {
{IconBytes: icons.ContentRedo, Text: "Redo", ShortcutText: keyActionMap["Redo"], Doer: model.Redo()}, {IconBytes: icons.ContentRedo, Text: "Redo", ShortcutText: keyActionMap["Redo"], Doer: model.Redo()},
{IconBytes: icons.ImageCrop, Text: "Remove unused data", ShortcutText: keyActionMap["RemoveUnused"], Doer: model.RemoveUnused()}, {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{ ret.midiMenuItems = append(ret.midiMenuItems, MenuItem{
IconBytes: icons.ImageControlPoint, IconBytes: icons.ImageControlPoint,
Text: input.String(), Text: input.String(),

View File

@ -102,7 +102,7 @@ func (pe *UnitEditor) layoutSliders(gtx C, t *Tracker) D {
} }
index := 0 index := 0
for param := range t.Model.Params().Iterate() { for param := range t.Model.Params().Iterate {
pe.Parameters[index].Parameter = param pe.Parameters[index].Parameter = param
index++ index++
} }
@ -176,7 +176,7 @@ 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
for i, item := range t.Model.SearchResults().Iterate() { for i, item := range t.Model.SearchResults().Iterate {
if i >= 256 { if i >= 256 {
break break
} }

View File

@ -23,20 +23,18 @@ type (
} }
) )
func (m *RTMIDIContext) ListInputDevices() func(yield func(tracker.MIDIDevice) bool) { func (m *RTMIDIContext) InputDevices(yield func(tracker.MIDIDevice) bool) {
return func(yield func(tracker.MIDIDevice) bool) { if m.driver == nil {
if m.driver == nil { return
return }
} ins, err := m.driver.Ins()
ins, err := m.driver.Ins() if err != nil {
if err != nil { return
return }
} for i := 0; i < len(ins); i++ {
for i := 0; i < len(ins); i++ { device := RTMIDIDevice{context: m, in: ins[i]}
device := RTMIDIDevice{context: m, in: ins[i]} if !yield(device) {
if !yield(device) { break
break
}
} }
} }
} }

View File

@ -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 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) { func (v *Units) Iterate(yield UnitYieldFunc) {
return func(yield UnitYieldFunc) { if v.d.InstrIndex < 0 || v.d.InstrIndex >= len(v.d.Song.Patch) {
if v.d.InstrIndex < 0 || v.d.InstrIndex >= len(v.d.Song.Patch) { return
return }
} stackBefore := 0
stackBefore := 0 for i, 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()
stackAfter := stackBefore + unit.StackChange() if !yield(i, UnitListItem{
if !yield(i, UnitListItem{ Type: unit.Type,
Type: unit.Type, Comment: unit.Comment,
Comment: unit.Comment, Disabled: unit.Disabled,
Disabled: unit.Disabled, StackNeed: unit.StackNeed(),
StackNeed: unit.StackNeed(), StackBefore: stackBefore,
StackBefore: stackBefore, StackAfter: stackAfter,
StackAfter: stackAfter, }) {
}) { break
break
}
stackBefore = stackAfter
} }
stackBefore = stackAfter
} }
} }
@ -745,18 +743,16 @@ func (v *SearchResults) List() List {
return List{v} return List{v}
} }
func (l *SearchResults) Iterate() func(yield UnitSearchYieldFunc) { func (l *SearchResults) Iterate(yield UnitSearchYieldFunc) {
return func(yield UnitSearchYieldFunc) { index := 0
index := 0 for _, name := range sointu.UnitNames {
for _, name := range sointu.UnitNames { if !strings.HasPrefix(name, l.d.UnitSearchString) {
if !strings.HasPrefix(name, l.d.UnitSearchString) { continue
continue
}
if !yield(index, name) {
break
}
index++
} }
if !yield(index, name) {
break
}
index++
} }
} }

View File

@ -124,7 +124,7 @@ type (
Dialog int Dialog int
MIDIContext interface { MIDIContext interface {
ListInputDevices() func(yield func(MIDIDevice) bool) InputDevices(yield func(MIDIDevice) bool)
Close() Close()
} }

View File

@ -21,9 +21,7 @@ func (NullContext) BPM() (bpm float64, ok bool) {
return 0, false return 0, false
} }
func (NullContext) ListInputDevices() func(yield func(tracker.MIDIDevice) bool) { func (NullContext) InputDevices(yield func(tracker.MIDIDevice) bool) {}
return func(yield func(tracker.MIDIDevice) bool) {}
}
func (NullContext) Close() {} func (NullContext) Close() {}
@ -292,7 +290,7 @@ func FuzzModel(f *testing.F) {
index-- index--
return index > 0 return index > 0
}, seed) }, seed)
for _, a := range model.Alerts().Iterate() { 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)
} }

View File

@ -77,7 +77,7 @@ func (pl *Params) change(n string, severity ChangeSeverity) func() {
func (pl *Params) Count() int { func (pl *Params) Count() int {
count := 0 count := 0
for range pl.Iterate() { for range pl.Iterate {
count++ count++
} }
return count return count
@ -85,7 +85,7 @@ func (pl *Params) Count() int {
func (pl *Params) SelectedItem() (ret Parameter) { func (pl *Params) SelectedItem() (ret Parameter) {
index := pl.Selected() index := pl.Selected()
for param := range pl.Iterate() { for param := range pl.Iterate {
if index == 0 { if index == 0 {
ret = param ret = param
} }
@ -94,55 +94,53 @@ func (pl *Params) SelectedItem() (ret Parameter) {
return return
} }
func (pl *Params) Iterate() func(yield ParamYieldFunc) { func (pl *Params) Iterate(yield ParamYieldFunc) {
return func(yield ParamYieldFunc) { if pl.d.InstrIndex < 0 || pl.d.InstrIndex >= len(pl.d.Song.Patch) {
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 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 return
} }
unit := &pl.d.Song.Patch[pl.d.InstrIndex].Units[pl.d.UnitIndex] }
unitType, ok := sointu.UnitTypes[unit.Type] switch {
if !ok { 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 return
} }
for i := range unitType { if !yield(DelayLinesParameter{parameter: parameter{m: (*Model)(pl), unit: unit}}) {
if !unitType[i].CanSet { return
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 unit.Type == "oscillator" && unit.Parameters["type"] == sointu.Sample { for i := range unit.VarArgs {
if !yield(GmDlsEntryParameter{parameter: parameter{m: (*Model)(pl), unit: unit}}) { if !yield(DelayTimeParameter{parameter: parameter{m: (*Model)(pl), unit: unit}, index: i}) {
return 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
}
}
}
} }
} }