Files
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

150 lines
5.7 KiB
Swift

// PedalSelectorKnobView.swift
// Lyrebird
//
// Copyright (c) 2026 Joshua Parmenter / realizedsound
// SPDX-License-Identifier: MIT
import SwiftUI
import LyrebirdShoeGaze
/// Renders a ``PedalSelectorKnob`` — a rotary knob that snaps to a fixed set of labelled stops.
///
/// Visually matches `ParameterKnob` (same dial + indicator), but drag quantises to discrete
/// positions and the caption shows the current stop's title rather than a numeric value. Drag up to
/// advance clockwise through the stops, down to go back.
struct PedalSelectorKnobView: View {
// MARK: - Layout constants
private static let dialSize: CGFloat = 50
private static let indicatorWidth: CGFloat = 2
private static let indicatorHeight: CGFloat = 14
private static let indicatorInset: CGFloat = 3
private static let startAngle: Double = -135.0
private static let totalArc: Double = 270.0
/// Pixels of vertical drag per stop step.
private static let pixelsPerStep: CGFloat = 22
private static let labelSpacing: CGFloat = 4
// MARK: - Input
private let selector: PedalSelectorKnob
/// Called once when a drag ends on a different stop than it began — used to trigger a board
/// rebuild for `rebuildsOnChange` selectors. `nil` for plain live-value selectors.
private let onCommit: (() -> Void)?
/// Records one undo checkpoint per drag that changes the stop. Installed by ``PedalView``.
@Environment(\.pedalParameterEditRecorder) private var recordEdit
// MARK: - Drag state
@State private var dragStartIndex: Int = 0
/// Node value captured when a drag begins — the undo checkpoint's "before" (read from the node,
/// not the stop table, so it can't index out of range if the stops change).
@State private var dragStartValue: Double = 0
@State private var dragInProgress: Bool = false
// MARK: - Init
init(_ selector: PedalSelectorKnob, onCommit: (() -> Void)? = nil) {
self.selector = selector
self.onCommit = onCommit
}
// MARK: - Body
var body: some View {
VStack(spacing: PedalSelectorKnobView.labelSpacing) {
dial
.frame(width: PedalSelectorKnobView.dialSize, height: PedalSelectorKnobView.dialSize)
.gesture(dragGesture)
Text(selector.label)
.font(.caption)
.foregroundStyle(Color.white)
Text(currentStop.title)
.font(.caption)
.foregroundStyle(Color.white)
}
}
// MARK: - Dial
private var dial: some View {
let count: Int = selector.stops.count
let normalised: Double = count > 1 ? Double(currentIndex) / Double(count - 1) : 0
let rotation: Double = PedalSelectorKnobView.startAngle + normalised * PedalSelectorKnobView.totalArc
let offset: CGFloat = -(PedalSelectorKnobView.dialSize / 2
- PedalSelectorKnobView.indicatorHeight / 2
- PedalSelectorKnobView.indicatorInset)
return ZStack {
Circle()
.fill(Color.lyrebirdControlBackground)
.overlay(Circle().stroke(Color.lyrebirdSeparator, lineWidth: 1))
Rectangle()
.fill(Color.accentColor)
.frame(
width: PedalSelectorKnobView.indicatorWidth,
height: PedalSelectorKnobView.indicatorHeight
)
.offset(y: offset)
.rotationEffect(.degrees(rotation))
}
}
// MARK: - Drag
private var dragGesture: some Gesture {
DragGesture(minimumDistance: 1)
.onChanged { gesture in
if !dragInProgress {
dragInProgress = true
dragStartIndex = currentIndex
dragStartValue = selector.node.value
}
let delta: CGFloat = gesture.translation.height // down = positive
// Up should advance (increase index), so subtract the downward delta.
let steps: Int = Int((-delta / PedalSelectorKnobView.pixelsPerStep).rounded())
let target: Int = min(max(dragStartIndex + steps, 0), selector.stops.count - 1)
let value: Double = selector.stops[target].value
if value != selector.node.value {
selector.node.update(value)
}
}
.onEnded { _ in
let changed: Bool = currentIndex != dragStartIndex
dragInProgress = false
if changed {
onCommit?()
// A rebuildsOnChange selector 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 selectors record.
if !selector.rebuildsOnChange {
recordEdit(.selector, selector.label, dragStartValue, selector.node.value)
}
}
}
}
// MARK: - Helpers
/// Index of the stop whose value is nearest the node's current value.
private var currentIndex: Int {
let value: Double = selector.node.value
var bestIndex: Int = 0
var bestDistance: Double = .greatestFiniteMagnitude
for (index, stop) in selector.stops.enumerated() {
let distance: Double = abs(stop.value - value)
if distance < bestDistance {
bestDistance = distance
bestIndex = index
}
}
return bestIndex
}
private var currentStop: PedalSelectorKnob.Stop {
selector.stops[currentIndex]
}
}