Files
ShoeGaze/Sources/LyrebirdShoeGazeUI/MIDIMappingSheet.swift
T
josh ae8f370fa8 Address review; make all pedal controls (incl. sliders) CC-mappable
Review fixes:
- Remove stale phase-plan references from MIDIController / MIDIMappingSheet
  doc-comments (describe current behavior instead).
- insertPedal: a board with no linear pedals but a trailing routing region
  now inserts at the head (before the region) instead of appending after it.
- Drop the unused MIDIController.retarget (the app recreates the controller
  against a fresh engine rather than re-pointing it).
- CC steal now matches controller AND channel, so per-channel CC bindings
  can coexist.

Feature (requested): CC mapping now covers every continuous pedal control,
not just rotary knobs. New PedalControl descriptor + Pedal.ccMappableControls
(knobs followed by effect sliders); the Expression sheet lists them grouped
per pedal with a header, so slider params (e.g. EQ bands) are assignable and
labelled clearly. Knob-first ordering keeps existing CC bindings stable.

Tests: +2 (CC drives a slider control; insert into a region-only board lands
before the region). 318 total green; both app targets build.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-07-01 08:34:30 -07:00

226 lines
8.5 KiB
Swift

// MIDIMappingSheet.swift
// Lyrebird
//
// Copyright (c) 2026 Joshua Parmenter / realizedsound
// SPDX-License-Identifier: MIT
import SwiftUI
import LyrebirdShoeGaze
import LyrebirdUI
/// The MIDI mapping panel: shows connected devices and lets the user bind expression-pedal CCs to
/// pedal knobs via MIDI Learn.
///
/// Presented from the transport-row ``MIDIIndicatorView``. Rows are generated by walking the live
/// `ShoeGaze.boards` / `board.pedals` and each pedal's controls, so bindings address controls
/// positionally. A Switches section binds Program Change to toggle actions; an Expression section
/// binds Control Change to continuous pedal controls.
public struct MIDIMappingSheet: View {
// MARK: - Constants
private static let sheetMinWidth: CGFloat = 460
private static let sheetMinHeight: CGFloat = 520
private static let rowVerticalPadding: CGFloat = 2
// MARK: - Inputs
private let controller: MIDIController
private let shoeGaze: ShoeGaze
@Environment(\.dismiss) private var dismiss
/// Creates a MIDI mapping sheet.
///
/// - Parameters:
/// - controller: The MIDI controller that owns the connection, learn state, and bindings.
/// - shoeGaze: The rig whose boards / pedals / knobs are enumerated as binding targets.
public init(controller: MIDIController, shoeGaze: ShoeGaze) {
self.controller = controller
self.shoeGaze = shoeGaze
}
// MARK: - Body
public var body: some View {
NavigationStack {
List {
devicesSection
switchesSection
expressionSection
}
.navigationTitle("MIDI Mapping")
.toolbar {
ToolbarItem(placement: .confirmationAction) {
Button("Done") {
controller.cancelLearn()
dismiss()
}
}
}
}
.frame(minWidth: MIDIMappingSheet.sheetMinWidth, minHeight: MIDIMappingSheet.sheetMinHeight)
}
// MARK: - Devices
private var devicesSection: some View {
Section("Devices") {
if controller.isConnected {
ForEach(controller.connectedDeviceNames, id: \.self) { name in
Label(name, systemImage: "pianokeys")
.foregroundStyle(Color.green)
}
} else {
Label("No MIDI device detected", systemImage: "pianokeys.inverse")
.foregroundStyle(.secondary)
}
}
}
// 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"
)
}
}
}
}
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)
return HStack {
VStack(alignment: .leading, spacing: 1) {
Text(label)
Text(switchBindingLabel(isArmed: isArmed, binding: binding))
.font(.caption)
.foregroundStyle(isArmed ? Color.orange : .secondary)
}
Spacer()
if binding != nil {
Button("Clear") { controller.clearSwitchBinding(for: action) }
.buttonStyle(.borderless)
}
Button(isArmed ? "Cancel" : "Learn") {
if isArmed {
controller.cancelLearn()
} else {
controller.armSwitchLearn(action)
}
}
.buttonStyle(.bordered)
.tint(.orange)
}
.padding(.vertical, MIDIMappingSheet.rowVerticalPadding)
}
private func switchBindingLabel(isArmed: Bool, binding: MIDIMap.SwitchBinding?) -> String {
if isArmed { return "Listening — press a footswitch…" }
guard let binding: MIDIMap.SwitchBinding = binding else { return "Unassigned" }
let channelText: String = binding.channel.map { " · ch \($0 + 1)" } ?? ""
return "PC \(binding.program)\(channelText)"
}
// MARK: - Expression (CC)
private var expressionSection: some View {
Section("Expression (Control Change)") {
if shoeGaze.boards.isEmpty {
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
)
}
}
}
}
@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
)
return HStack {
VStack(alignment: .leading, spacing: 1) {
Text(controlLabel)
Text(bindingLabel(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(isArmed ? "Cancel" : "Learn") {
if isArmed {
controller.cancelLearn()
} else {
controller.armCCLearn(
boardIndex: boardIndex, pedalIndex: pedalIndex, knobIndex: knobIndex
)
}
}
.buttonStyle(.bordered)
.tint(.orange)
}
.padding(.vertical, MIDIMappingSheet.rowVerticalPadding)
}
private func bindingLabel(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)" } ?? ""
return "CC \(binding.controller)\(channelText)"
}
}