Fix CC section: stop switch actions leaking in; add board-level target
The MIDI Mapping sheet's two sections both keyed their ForEach rows on the enumeration offset (0,1,2…) and the Expression section mixed a bare header Text with a ForEach. SwiftUI's List reused structurally identical row views across the sections, so Program-Change switch rows (e.g. "Board 1 · Solo", "Board 1 · Overdrive · Bypass") leaked into the Expression (CC) section and their Learn button armed a Program Change instead of a Control Change. - Rebuild both sections from precomputed row models with globally-unique, section-namespaced string ids and a single ForEach each; no bare headers. - CC target is now an enum: `.pedalControl` (a knob/slider) or `.boardLevel` (the board fader). Board master level is now CC-assignable, shown as "<Board> · Level". - CC rows are labelled "<Pedal> · <Control>" (e.g. "Overdrive · Drive"). - Stale-binding + steal-on-reassign now key off an address matcher on the target enum. Tests updated for the enum target; added a board-level CC test. 319 green; both app targets build. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
@@ -78,47 +78,109 @@ public struct MIDIMappingSheet: View {
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: - Row models
|
||||
|
||||
/// A Program-Change switch row: a toggle action + its display label. `id` is globally unique so
|
||||
/// SwiftUI never reuses a row view across the two sections (which would leak the wrong Learn
|
||||
/// action into a row).
|
||||
private struct SwitchRowModel: Identifiable {
|
||||
let id: String
|
||||
let action: MIDIMap.Action
|
||||
let label: String
|
||||
}
|
||||
|
||||
/// A Control-Change row: a continuous target (address-only, hints `nil`) + its display label.
|
||||
private struct CCRowModel: Identifiable {
|
||||
let id: String
|
||||
let target: MIDIMap.CCTarget
|
||||
let label: String
|
||||
}
|
||||
|
||||
private var switchRowModels: [SwitchRowModel] {
|
||||
var rows: [SwitchRowModel] = [
|
||||
SwitchRowModel(id: "sw-rig", action: .rigBypass, label: "Rig Bypass"),
|
||||
]
|
||||
for boardPair in shoeGaze.boards.enumerated() {
|
||||
let boardIndex: Int = boardPair.offset
|
||||
let board: PedalBoard = boardPair.element
|
||||
rows.append(SwitchRowModel(id: "sw-solo-\(boardIndex)",
|
||||
action: .boardSolo(boardIndex: boardIndex),
|
||||
label: "\(board.label) · Solo"))
|
||||
rows.append(SwitchRowModel(id: "sw-aud-\(boardIndex)",
|
||||
action: .boardAudible(boardIndex: boardIndex),
|
||||
label: "\(board.label) · Audible"))
|
||||
rows.append(SwitchRowModel(id: "sw-byp-\(boardIndex)",
|
||||
action: .boardBypass(boardIndex: boardIndex),
|
||||
label: "\(board.label) · Bypass"))
|
||||
for pedalPair in board.pedals.enumerated() {
|
||||
rows.append(SwitchRowModel(
|
||||
id: "sw-pb-\(boardIndex)-\(pedalPair.offset)",
|
||||
action: .pedalBypass(boardIndex: boardIndex, pedalIndex: pedalPair.offset),
|
||||
label: "\(board.label) · \(pedalPair.element.displayName) · Bypass"))
|
||||
}
|
||||
}
|
||||
return rows
|
||||
}
|
||||
|
||||
private var ccRowModels: [CCRowModel] {
|
||||
var rows: [CCRowModel] = []
|
||||
for boardPair in shoeGaze.boards.enumerated() {
|
||||
let boardIndex: Int = boardPair.offset
|
||||
let board: PedalBoard = boardPair.element
|
||||
// Board master level (the board fader).
|
||||
rows.append(CCRowModel(
|
||||
id: "cc-blvl-\(boardIndex)",
|
||||
target: .boardLevel(boardIndex: boardIndex, boardLabelHint: nil),
|
||||
label: "\(board.label) · Level"))
|
||||
for pedalPair in board.pedals.enumerated() {
|
||||
let pedalIndex: Int = pedalPair.offset
|
||||
let pedal: Pedal = pedalPair.element
|
||||
for controlPair in pedal.ccMappableControls.enumerated() {
|
||||
rows.append(CCRowModel(
|
||||
id: "cc-\(boardIndex)-\(pedalIndex)-\(controlPair.offset)",
|
||||
target: .pedalControl(
|
||||
boardIndex: boardIndex,
|
||||
pedalIndex: pedalIndex,
|
||||
controlIndex: controlPair.offset,
|
||||
pedalNameHint: nil,
|
||||
controlLabelHint: nil),
|
||||
label: "\(pedal.displayName) · \(controlPair.element.label)"))
|
||||
}
|
||||
}
|
||||
}
|
||||
return rows
|
||||
}
|
||||
|
||||
// MARK: - Switches (Program Change)
|
||||
|
||||
private var switchesSection: some View {
|
||||
Section("Switches (Program Change)") {
|
||||
switchRow(action: .rigBypass, label: "Rig Bypass")
|
||||
ForEach(Array(shoeGaze.boards.enumerated()), id: \.offset) { boardPair in
|
||||
let boardIndex: Int = boardPair.offset
|
||||
let board: PedalBoard = boardPair.element
|
||||
switchRow(action: .boardSolo(boardIndex: boardIndex), label: "\(board.label) · Solo")
|
||||
switchRow(action: .boardAudible(boardIndex: boardIndex), label: "\(board.label) · Audible")
|
||||
switchRow(action: .boardBypass(boardIndex: boardIndex), label: "\(board.label) · Bypass")
|
||||
ForEach(Array(board.pedals.enumerated()), id: \.offset) { pedalPair in
|
||||
switchRow(
|
||||
action: .pedalBypass(boardIndex: boardIndex, pedalIndex: pedalPair.offset),
|
||||
label: "\(board.label) · \(pedalPair.element.displayName) · Bypass"
|
||||
)
|
||||
}
|
||||
ForEach(switchRowModels) { row in
|
||||
switchRow(row)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private func switchRow(action: MIDIMap.Action, label: String) -> some View {
|
||||
let isArmed: Bool = controller.learnTarget == .action(action)
|
||||
let binding: MIDIMap.SwitchBinding? = controller.switchBinding(for: action)
|
||||
private func switchRow(_ row: SwitchRowModel) -> some View {
|
||||
let isArmed: Bool = controller.learnTarget == .action(row.action)
|
||||
let binding: MIDIMap.SwitchBinding? = controller.switchBinding(for: row.action)
|
||||
return HStack {
|
||||
VStack(alignment: .leading, spacing: 1) {
|
||||
Text(label)
|
||||
Text(row.label)
|
||||
Text(switchBindingLabel(isArmed: isArmed, binding: binding))
|
||||
.font(.caption)
|
||||
.foregroundStyle(isArmed ? Color.orange : .secondary)
|
||||
}
|
||||
Spacer()
|
||||
if binding != nil {
|
||||
Button("Clear") { controller.clearSwitchBinding(for: action) }
|
||||
Button("Clear") { controller.clearSwitchBinding(for: row.action) }
|
||||
.buttonStyle(.borderless)
|
||||
}
|
||||
Button(isArmed ? "Cancel" : "Learn") {
|
||||
if isArmed {
|
||||
controller.cancelLearn()
|
||||
} else {
|
||||
controller.armSwitchLearn(action)
|
||||
controller.armSwitchLearn(row.action)
|
||||
}
|
||||
}
|
||||
.buttonStyle(.bordered)
|
||||
@@ -142,72 +204,32 @@ public struct MIDIMappingSheet: View {
|
||||
Text("Add a pedal to assign expression control.")
|
||||
.foregroundStyle(.secondary)
|
||||
}
|
||||
ForEach(Array(shoeGaze.boards.enumerated()), id: \.offset) { boardPair in
|
||||
let boardIndex: Int = boardPair.offset
|
||||
let board: PedalBoard = boardPair.element
|
||||
ForEach(Array(board.pedals.enumerated()), id: \.offset) { pedalPair in
|
||||
pedalRows(
|
||||
boardIndex: boardIndex,
|
||||
pedalIndex: pedalPair.offset,
|
||||
pedal: pedalPair.element,
|
||||
boardLabel: board.label
|
||||
)
|
||||
}
|
||||
ForEach(ccRowModels) { row in
|
||||
ccRow(row)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ViewBuilder
|
||||
private func pedalRows(boardIndex: Int, pedalIndex: Int, pedal: Pedal, boardLabel: String) -> some View {
|
||||
// Per-pedal header so each control row can show just its own name.
|
||||
Text("\(boardLabel) · \(pedal.displayName)")
|
||||
.font(.subheadline.weight(.semibold))
|
||||
.foregroundStyle(.secondary)
|
||||
ForEach(Array(pedal.ccMappableControls.enumerated()), id: \.offset) { controlPair in
|
||||
knobRow(
|
||||
boardIndex: boardIndex,
|
||||
pedalIndex: pedalIndex,
|
||||
knobIndex: controlPair.offset,
|
||||
controlLabel: controlPair.element.label
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
private func knobRow(
|
||||
boardIndex: Int,
|
||||
pedalIndex: Int,
|
||||
knobIndex: Int,
|
||||
controlLabel: String
|
||||
) -> some View {
|
||||
let isArmed: Bool = controller.learnTarget == .cc(
|
||||
boardIndex: boardIndex, pedalIndex: pedalIndex, knobIndex: knobIndex
|
||||
)
|
||||
let binding: MIDIMap.CCBinding? = controller.ccBinding(
|
||||
boardIndex: boardIndex, pedalIndex: pedalIndex, knobIndex: knobIndex
|
||||
)
|
||||
private func ccRow(_ row: CCRowModel) -> some View {
|
||||
let isArmed: Bool = controller.learnTarget == .cc(row.target)
|
||||
let binding: MIDIMap.CCBinding? = controller.ccBinding(for: row.target)
|
||||
return HStack {
|
||||
VStack(alignment: .leading, spacing: 1) {
|
||||
Text(controlLabel)
|
||||
Text(bindingLabel(isArmed: isArmed, binding: binding))
|
||||
Text(row.label)
|
||||
Text(ccBindingLabel(isArmed: isArmed, binding: binding))
|
||||
.font(.caption)
|
||||
.foregroundStyle(isArmed ? Color.orange : .secondary)
|
||||
}
|
||||
Spacer()
|
||||
if binding != nil {
|
||||
Button("Clear") {
|
||||
controller.clearCCBinding(
|
||||
boardIndex: boardIndex, pedalIndex: pedalIndex, knobIndex: knobIndex
|
||||
)
|
||||
}
|
||||
.buttonStyle(.borderless)
|
||||
Button("Clear") { controller.clearCCBinding(for: row.target) }
|
||||
.buttonStyle(.borderless)
|
||||
}
|
||||
Button(isArmed ? "Cancel" : "Learn") {
|
||||
if isArmed {
|
||||
controller.cancelLearn()
|
||||
} else {
|
||||
controller.armCCLearn(
|
||||
boardIndex: boardIndex, pedalIndex: pedalIndex, knobIndex: knobIndex
|
||||
)
|
||||
controller.armCCLearn(row.target)
|
||||
}
|
||||
}
|
||||
.buttonStyle(.bordered)
|
||||
@@ -216,7 +238,7 @@ public struct MIDIMappingSheet: View {
|
||||
.padding(.vertical, MIDIMappingSheet.rowVerticalPadding)
|
||||
}
|
||||
|
||||
private func bindingLabel(isArmed: Bool, binding: MIDIMap.CCBinding?) -> String {
|
||||
private func ccBindingLabel(isArmed: Bool, binding: MIDIMap.CCBinding?) -> String {
|
||||
if isArmed { return "Listening — move an expression pedal…" }
|
||||
guard let binding: MIDIMap.CCBinding = binding else { return "Unassigned" }
|
||||
let channelText: String = binding.channel.map { " · ch \($0 + 1)" } ?? ""
|
||||
|
||||
Reference in New Issue
Block a user