Files
ShoeGaze/Sources/LyrebirdShoeGazeUI/PedalSwitchView.swift
T
josh 1f5b70edc7 feat(undo): parameter undo for knobs, sliders, switches, selectors
Tier 2 of the rig-undo feature, on the same undo stack as the structural
(tier 1) edits. Records one undo checkpoint when the user finishes changing a
pedal control, so ⌘Z / the ↶ button step back through knob turns and switch
flips as well as add/remove/move.

- UndoEntry.parameter + PedalControlGroup (knob/advancedKnob/slider/switch/
  selector); UndoCoordinator.recordParameterEdit + .parameter handling in
  undo()/redo(). applyParameter re-resolves the LIVE node by (pedalID, group,
  controlLabel) via ShoeGaze.pedal(id:), so an undo lands on the right control
  even after an intervening structural rebuild replaced the pedal's node
  (pedal ids persist through rebuild). old == new records nothing.
- Knobs record via Lyrebird 2.3.0's ParameterKnob.onEditingEnded (one edit per
  drag): PedalView.recordingKnob (main / advanced / mix / level / looper) and
  TempoSyncKnobView.
- Self-contained views (VerticalParameterSlider, PedalSwitchView,
  PedalSelectorKnobView) record via a new \.pedalParameterEditRecorder
  environment closure PedalView installs, capturing pedal.id + the coordinator.
  Slider/selector snapshot node.value at drag start; switch records old→new on
  the committing tap.
- rebuildsOnChange switches/selectors are deliberately NOT recorded (they alter
  graph topology and would fight the structural restore's live-value merge) —
  documented as a known gap, along with tempo-sync division changes.
- Bump the Lyrebird pin to 2.3.0 (the released tag with onEditingEnded), drop
  the dev-time local path override.

Tests: 18 UndoCoordinator tests (knob/slider/switch/selector round-trips,
post-rebuild re-resolution, + a catalogue test asserting unique control labels
per group so the by-label resolution can't mis-target). Full suite 837 green;
both demo schemes build against 2.3.0. External review + caveman sweep: no
blocking findings.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-11-25 14:23:00 -08:00

158 lines
6.4 KiB
Swift

// PedalSwitchView.swift
// Lyrebird
//
// Copyright (c) 2026 Joshua Parmenter / realizedsound
// SPDX-License-Identifier: MIT
import SwiftUI
import LyrebirdSynth
import LyrebirdUI
import LyrebirdShoeGaze
// MARK: - PedalSwitchView
/// A segmented multi-position switch bound to a live ``UIParamNode``.
///
/// Renders one button per ``PedalSwitch/Option`` in a horizontal pill, with a caption
/// label beneath — mirroring the look of ``ParameterKnob``. The same view serves both a
/// two-state toggle (two options, e.g. sweep Up / Down) and a three-state selector (three
/// options, e.g. filter LP / BP / HP); the segment count follows `pedalSwitch.options`.
///
/// The lit segment is the option whose `value` is nearest the node's current value.
/// Tapping a segment writes that option's exact `value` via `node.update(_:)`. Reading
/// `node.value` in `body` registers the view for automatic re-render (UIParamNode is
/// `@Observable`).
///
/// ## Usage
/// ```swift
/// PedalSwitchView(pedalSwitch)
/// ```
public struct PedalSwitchView: View {
// MARK: - Layout constants
private static let cornerRadius: CGFloat = 6
private static let segmentSpacing: CGFloat = 3
private static let labelSpacing: CGFloat = 4
private static let segmentPaddingV: CGFloat = 5
private static let segmentPaddingH: CGFloat = 6
private static let fontSize: CGFloat = 11
private static let captionFontSize: CGFloat = 11
private static let activeFillOpacity: Double = 0.88
private static let inactiveFillOpacity: Double = 0.16
private static let borderOpacity: Double = 0.35
private static let borderLineWidth: CGFloat = 1
private static let activeTextBrightness: Double = 0.12
/// Opacity applied to a tempo-dependent switch while the tempo indicator is turned off.
private static let tempoDisabledOpacity: Double = 0.4
// MARK: - Properties
private let pedalSwitch: PedalSwitch
/// Called once when a tap commits a different position — used to trigger a board rebuild for
/// `rebuildsOnChange` switches (e.g. a channel switch). `nil` for plain switches.
private let onCommit: (() -> Void)?
@Environment(\.shoeGazeTempoEnabled) private var tempoEnabled: Bool
/// Records one undo checkpoint per committed change. Installed by ``PedalView``; no-op elsewhere.
@Environment(\.pedalParameterEditRecorder) private var recordEdit
// MARK: - Init
/// Creates a segmented switch view.
///
/// - Parameters:
/// - pedalSwitch: The discrete switch descriptor to render.
/// - onCommit: Called when a tap selects a different position. Used for `rebuildsOnChange`
/// switches; `nil` otherwise.
public init(_ pedalSwitch: PedalSwitch, onCommit: (() -> Void)? = nil) {
self.pedalSwitch = pedalSwitch
self.onCommit = onCommit
}
// MARK: - Body
public var body: some View {
// Reading `node.value` registers this view for @Observable re-render on change.
let current: Double = pedalSwitch.node.value
let selectedIndex: Int = nearestIndex(to: current)
// A tempo-synced switch is inert while the tempo indicator is turned off.
let inert: Bool = pedalSwitch.tempoDependent && !tempoEnabled
VStack(spacing: PedalSwitchView.labelSpacing) {
HStack(spacing: PedalSwitchView.segmentSpacing) {
ForEach(Array(pedalSwitch.options.enumerated()), id: \.offset) { pair in
let option: PedalSwitch.Option = pair.element
let isActive: Bool = pair.offset == selectedIndex
segment(option: option, isActive: isActive)
}
}
.overlay(
RoundedRectangle(cornerRadius: PedalSwitchView.cornerRadius)
.stroke(Color.black.opacity(PedalSwitchView.borderOpacity),
lineWidth: PedalSwitchView.borderLineWidth)
)
Text(inert ? "\(pedalSwitch.label) (tempo off)" : pedalSwitch.label)
.font(.system(size: PedalSwitchView.captionFontSize))
.foregroundStyle(Color.white)
.lineLimit(1)
}
.disabled(inert)
.opacity(inert ? PedalSwitchView.tempoDisabledOpacity : 1.0)
}
// MARK: - Subviews
private func segment(option: PedalSwitch.Option, isActive: Bool) -> some View {
Button(action: {
let oldValue: Double = pedalSwitch.node.value
let changed: Bool = option.value != oldValue
pedalSwitch.node.update(option.value)
if changed {
onCommit?()
// A rebuildsOnChange switch alters graph topology: it is NOT recorded as a parameter
// edit (and isn't undoable yet — recording it would fight the structural restore's
// live-value merge). Only plain live-value switches become parameter edits.
if !pedalSwitch.rebuildsOnChange {
recordEdit(.switch, pedalSwitch.label, oldValue, option.value)
}
}
}) {
Text(option.title)
.font(.system(size: PedalSwitchView.fontSize, weight: .semibold))
.foregroundStyle(isActive
? Color(white: PedalSwitchView.activeTextBrightness)
: Color.white)
.padding(.vertical, PedalSwitchView.segmentPaddingV)
.padding(.horizontal, PedalSwitchView.segmentPaddingH)
.frame(maxWidth: .infinity)
.background(
RoundedRectangle(cornerRadius: PedalSwitchView.cornerRadius)
.fill(Color.white.opacity(isActive
? PedalSwitchView.activeFillOpacity
: PedalSwitchView.inactiveFillOpacity))
)
}
.buttonStyle(.plain)
}
// MARK: - Helpers
/// Index of the option whose `value` is nearest `value` — the lit segment.
private func nearestIndex(to value: Double) -> Int {
var bestIndex: Int = 0
var bestDistance: Double = .greatestFiniteMagnitude
for index: Int in pedalSwitch.options.indices {
let distance: Double = abs(pedalSwitch.options[index].value - value)
if distance < bestDistance {
bestDistance = distance
bestIndex = index
}
}
return bestIndex
}
}