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{}
)
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() {}

View File

@ -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
}
}
}

View File

@ -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
}

View File

@ -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:

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.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(),

View File

@ -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
}

View File

@ -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
}
}
}

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
}
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++
}
}

View File

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

View File

@ -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)
}

View File

@ -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
}
}
}
}
}