Files
ShoeGaze/Sources/LyrebirdShoeGazeUI/ControlMappingStrip.swift
T
josh b0294612f8 Fix mapping-badge actor warnings; un-crowd the transport row
- MappingBadge.pcFill/ccFill/keyFill are marked nonisolated so the nonisolated
  MappingBadgeKind.fill can read them (a SwiftUI View's statics are otherwise
  main-actor-isolated). Clears three "actor-isolated ... from a nonisolated
  context" warnings.
- The input-mute button pushed the docked desktop transport cluster past the row
  width, clipping the Rate picker and I/O steppers. Render the three rig toggles
  (Bypass / Mute / Input) icon-only there via .labelStyle(.iconOnly); the .help
  tooltips remain and the compact layout still shows text labels.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-07-11 15:38:22 -07:00

254 lines
10 KiB
Swift

// ControlMappingStrip.swift
// Lyrebird
//
// Copyright (c) 2026 Joshua Parmenter / realizedsound
// SPDX-License-Identifier: MIT
import SwiftUI
import LyrebirdShoeGaze
/// The rig's current control map, injected into the board view tree so each ``PedalView`` can draw
/// its own mapping strip without every call site having to thread the map through. `nil` when the
/// rig has no bindings. Set once high in the builder (`builderColumn`) from the observable
/// `ShoeGaze.midiMap`, so learning or clearing a binding updates every strip live.
private struct ControlMappingMapKey: EnvironmentKey {
static let defaultValue: MIDIMap? = nil
}
extension EnvironmentValues {
/// The rig's control map for in-context mapping badges, or `nil` when unset / empty.
var controlMappingMap: MIDIMap? {
get { self[ControlMappingMapKey.self] }
set { self[ControlMappingMapKey.self] = newValue }
}
}
/// The kind of trigger a mapping badge represents, and its colour.
enum MappingBadgeKind {
case programChange
case controlChange
case key
/// The badge fill colour (matches the mapping-sheet chip colours).
var fill: Color {
switch self {
case .programChange: return MappingBadge.pcFill
case .controlChange: return MappingBadge.ccFill
case .key: return MappingBadge.keyFill
}
}
}
/// One coloured pill — e.g. `CC11`, `PC3`, `⇧B` — marking a control's binding.
struct MappingBadge: View {
// `nonisolated` so the nonisolated `MappingBadgeKind.fill` can read them — a SwiftUI `View`'s
// statics are otherwise main-actor-isolated. The values are immutable `Sendable` colours.
nonisolated static let pcFill: Color = Color(red: 0.11, green: 0.37, blue: 0.65)
nonisolated static let ccFill: Color = Color(red: 0.33, green: 0.29, blue: 0.72)
nonisolated static let keyFill: Color = Color(red: 0.73, green: 0.46, blue: 0.09)
private static let fontSize: CGFloat = 9
private static let paddingH: CGFloat = 5
private static let paddingV: CGFloat = 1
private static let cornerRadius: CGFloat = 4
private let kind: MappingBadgeKind
private let text: String
/// Creates a mapping badge.
///
/// - Parameters:
/// - kind: The trigger kind (drives the fill colour).
/// - text: The badge label (e.g. `"CC11"`, `"PC3"`, `"⇧B"`).
init(kind: MappingBadgeKind, text: String) {
self.kind = kind
self.text = text
}
var body: some View {
Text(text)
.font(.system(size: MappingBadge.fontSize, weight: .medium))
.foregroundStyle(Color.white)
.padding(.horizontal, MappingBadge.paddingH)
.padding(.vertical, MappingBadge.paddingV)
.background(
RoundedRectangle(cornerRadius: MappingBadge.cornerRadius)
.fill(kind.fill)
)
}
}
/// A one-line legend keying the badge colours (`PC` footswitch, `CC` expression, `KEY` letter key),
/// shown once above the board canvas.
struct ControlMappingLegend: View {
private static let itemSpacing: CGFloat = 10
private static let pairSpacing: CGFloat = 5
private static let fontSize: CGFloat = 11
var body: some View {
HStack(spacing: ControlMappingLegend.itemSpacing) {
legendItem(kind: .programChange, badge: "PC", caption: "footswitch")
legendItem(kind: .controlChange, badge: "CC", caption: "expression")
legendItem(kind: .key, badge: "KEY", caption: "letter key")
}
.foregroundStyle(Color.white.opacity(0.55))
}
private func legendItem(kind: MappingBadgeKind, badge: String, caption: String) -> some View {
HStack(spacing: ControlMappingLegend.pairSpacing) {
MappingBadge(kind: kind, text: badge)
Text(caption)
.font(.system(size: ControlMappingLegend.fontSize))
}
}
}
/// One row of a pedal's mapping strip: a control label and the badges bound to it.
struct PedalMappingRow: Identifiable {
let id: String
let label: String
let badges: [PedalMappingBadge]
}
/// A single badge within a ``PedalMappingRow``.
struct PedalMappingBadge: Identifiable {
let id: String
let kind: MappingBadgeKind
let text: String
}
/// The per-pedal mapping strip: a compact footer listing this pedal's mapped controls and the
/// footswitch / expression / key bindings driving them. Renders nothing when the pedal has no
/// mappings, so unmapped pedals gain no height.
struct PedalMappingStrip: View {
private static let rowSpacing: CGFloat = 3
private static let labelFontSize: CGFloat = 10
private static let badgeSpacing: CGFloat = 4
private static let topPadding: CGFloat = 6
private static let dividerOpacity: Double = 0.18
private let rows: [PedalMappingRow]
/// Creates a mapping strip for a pedal against the current map.
///
/// - Parameters:
/// - pedal: The pedal whose bindings are listed.
/// - map: The rig's control map, or `nil` when there are no bindings.
init(pedal: Pedal, map: MIDIMap?) {
self.rows = PedalMappingStrip.rows(for: pedal, in: map)
}
var body: some View {
if rows.isEmpty {
EmptyView()
} else {
VStack(spacing: PedalMappingStrip.rowSpacing) {
Divider().overlay(Color.white.opacity(PedalMappingStrip.dividerOpacity))
ForEach(rows) { row in
HStack(spacing: PedalMappingStrip.badgeSpacing) {
Text(row.label)
.font(.system(size: PedalMappingStrip.labelFontSize))
.foregroundStyle(Color.white.opacity(0.6))
Spacer(minLength: 0)
ForEach(row.badges) { badge in
MappingBadge(kind: badge.kind, text: badge.text)
}
}
}
}
.padding(.top, PedalMappingStrip.topPadding)
}
}
// MARK: - Row assembly
/// Builds the strip rows for a pedal from the map: one row per mapped CC control, one for a
/// mapped bypass (footswitch + key), and one per mapped looper transport action.
static func rows(for pedal: Pedal, in map: MIDIMap?) -> [PedalMappingRow] {
guard let map: MIDIMap = map else { return [] }
let pedalID: UUID = pedal.id
var rows: [PedalMappingRow] = []
// Continuous controls driven by an expression pedal (CC).
for controlPair: (offset: Int, element: PedalControl) in pedal.ccMappableControls.enumerated() {
let target: MIDIMap.CCTarget = .pedalControl(
pedalID: pedalID, controlIndex: controlPair.offset,
pedalNameHint: nil, controlLabelHint: nil)
guard let cc: MIDIMap.CCBinding = map.ccs.first(where: { $0.target.addresses(target) })
else { continue }
rows.append(PedalMappingRow(
id: "cc-\(controlPair.offset)",
label: controlPair.element.label,
badges: [PedalMappingBadge(id: "cc", kind: .controlChange, text: "CC\(cc.controller)")]))
}
// Bypass — a footswitch and/or a key.
let bypassBadges: [PedalMappingBadge] = badges(for: .pedalBypass(pedalID: pedalID), in: map)
if bypassBadges.isEmpty == false {
rows.append(PedalMappingRow(id: "bypass", label: "Bypass", badges: bypassBadges))
}
// Looper transport actions.
if pedal is LooperPedal {
for kind: MIDIMap.PedalActionKind in loopedActionOrder {
let action: MIDIMap.Action = .pedalAction(pedalID: pedalID, kind: kind)
let actionBadges: [PedalMappingBadge] = badges(for: action, in: map)
if actionBadges.isEmpty { continue }
rows.append(PedalMappingRow(
id: "la-\(looperActionName(kind))",
label: looperActionName(kind),
badges: actionBadges))
}
}
return rows
}
/// The footswitch (PC) and key badges bound to a toggle action, in that order.
private static func badges(for action: MIDIMap.Action, in map: MIDIMap) -> [PedalMappingBadge] {
var badges: [PedalMappingBadge] = []
if let pc: MIDIMap.SwitchBinding = map.switches.first(where: { $0.action == action }) {
badges.append(PedalMappingBadge(id: "pc", kind: .programChange, text: "PC\(pc.program)"))
}
if let key: MIDIMap.KeyBinding = map.keys.first(where: { $0.action == action }) {
badges.append(PedalMappingBadge(id: "key", kind: .key, text: keyLabel(key)))
}
return badges
}
/// A key binding formatted with modifier glyphs (macOS order: control, option, shift).
private static func keyLabel(_ binding: MIDIMap.KeyBinding) -> String {
var prefix: String = ""
if binding.modifiers.contains(.control) { prefix += "⌃" }
if binding.modifiers.contains(.option) { prefix += "⌥" }
if binding.modifiers.contains(.shift) { prefix += "⇧" }
return prefix + binding.key.uppercased()
}
/// The looper transport actions in the order they appear in the mapping sheet.
private static let loopedActionOrder: [MIDIMap.PedalActionKind] = [
.looperRecordStop, .looperOverdubToggle, .looperLoopToggle, .looperStop,
.looperCueStop, .looperReverse, .looperSyncToggle, .looperReset,
.looperTrack(index: 0), .looperTrack(index: 1),
.looperTrack(index: 2), .looperTrack(index: 3),
]
/// A short user-facing name for a looper transport action.
private static func looperActionName(_ kind: MIDIMap.PedalActionKind) -> String {
switch kind {
case .looperRecordStop: return "Record"
case .looperOverdubToggle: return "Overdub"
case .looperLoopToggle: return "Loop"
case .looperStop: return "Stop"
case .looperCueStop: return "Cue Stop"
case .looperReverse: return "Reverse"
case .looperSyncToggle: return "Sync"
case .looperReset: return "Reset"
case .looperTrack(let index): return "Track \(LooperPedal.trackLabel(index))"
}
}
}