// DemoRootView.swift // Lyrebird // // Copyright (c) 2026 Joshua Parmenter / realizedsound // SPDX-License-Identifier: MIT #if os(macOS) import SwiftUI import LyrebirdSynth import LyrebirdUI import LyrebirdShoeGaze import LyrebirdShoeGazeUI /// Root view for the ShoeGaze demo window. /// /// Presents a top toolbar with a mode picker, an input-source toggle, and a Start/Stop /// transport button, followed by a content area that switches between four showcase /// modes as the user selects them. /// /// Because `ShoeGazeDemoController` is `@Observable`, SwiftUI re-renders this view and /// all its children whenever `controller.mode`, `controller.shoeGaze.boards`, /// `controller.router`, `controller.parallelSection`, or `controller.isRunning` change — /// including after a ``ShoeGazeDemoController/rebuild()`` that replaces `shoeGaze`. /// /// ## Mode showcases /// /// - `.boards` → `ShoeGazeView` — two boards, eight pedals, master gain per board. /// - `.editor` → `BoardEditorView` — board + FlowLayout pedal area + version snapshots. /// - `.parallel` → `ParallelSectionView` — splitter, two branch rows, merger (via board). /// - `.router` → `ChainRouterView` — dual-board chain router with splitter + master. /// - `.stereo` → `StereoSplitSectionView` — stereo split with the four route modes. struct DemoRootView: View { // MARK: - Layout constants private static let toolbarSpacing: CGFloat = 10 private static let toolbarPadding: CGFloat = 12 private static let contentPadding: CGFloat = 16 private static let windowMinWidth: CGFloat = 1_100 private static let windowMinHeight: CGFloat = 640 /// Dark stage-floor background (warm almost-black). private static let backgroundRed: Double = 0.08 private static let backgroundGreen: Double = 0.07 private static let backgroundBlue: Double = 0.06 private static let dividerOpacity: Double = 0.15 // MARK: - Properties private let controller: ShoeGazeDemoController private let hardwareSampleRate: () -> Double? /// Bumped on each transport press so the buttons re-read `controller.isRunning` /// (which derives from the non-observable engine). @State private var transportTick: Bool = false // MARK: - Init /// Creates the root demo view bound to the given controller. /// /// - Parameters: /// - controller: The `ShoeGazeDemoController` that owns the engine, active `ShoeGaze` /// session, and current mode. All toolbar actions dispatch through this. /// - hardwareSampleRate: Returns the actual hardware sample rate (or `nil`), for the engine- /// config mismatch warning. Defaults to `nil`. init( controller: ShoeGazeDemoController, hardwareSampleRate: @escaping () -> Double? = { nil } ) { self.controller = controller self.hardwareSampleRate = hardwareSampleRate } // MARK: - Body var body: some View { VStack(spacing: 0) { toolbar Divider() .overlay(Color.white.opacity(DemoRootView.dividerOpacity)) contentArea .padding(DemoRootView.contentPadding) Divider() .overlay(Color.white.opacity(DemoRootView.dividerOpacity)) ShoeGazeScopeView(shoeGaze: controller.shoeGaze) .padding(DemoRootView.contentPadding) } .frame( minWidth: DemoRootView.windowMinWidth, minHeight: DemoRootView.windowMinHeight ) .background( Color( red: DemoRootView.backgroundRed, green: DemoRootView.backgroundGreen, blue: DemoRootView.backgroundBlue ) ) .alert( "Microphone Access Needed", isPresented: Binding( get: { controller.microphoneAccessDenied }, set: { controller.microphoneAccessDenied = $0 } ) ) { Button("OK", role: .cancel) { } } message: { Text("Live input needs microphone access. Enable it in System Settings › Privacy & " + "Security › Microphone, then select the input again.") } } // MARK: - Toolbar /// Top toolbar: mode picker, input-source picker, transport, and CPU meter. private var toolbar: some View { HStack(spacing: DemoRootView.toolbarSpacing) { modePicker Spacer() inputSourcePicker // In Boards mode the pedalboard view (`ShoeGazeView`) supplies its own // Start/Stop, so the board starts only when its own button is pressed. The // other modes have no in-content transport, so show it here for them. if controller.mode != .boards { transportButtons } } .padding(DemoRootView.toolbarPadding) } /// Segmented picker for `DemoMode`. /// /// The selection binding's setter delegates to `controller.select(_:)` rather than /// writing a `@State` directly, so the controller handles the mode switch and rebuild. private var modePicker: some View { let modeBinding: Binding = Binding( get: { controller.mode }, set: { controller.select($0) } ) return Picker("Mode", selection: modeBinding) { ForEach(DemoMode.allCases) { demoMode in Text(demoMode.title).tag(demoMode) } } .pickerStyle(.segmented) .frame(maxWidth: 380) .foregroundStyle(Color.white) } /// A `Binding` mapping the input Picker tag to `InputSource`. /// /// Tag `-1` = `.testLoop`; tags `0 ..< numInputChannels` = `.liveInput(channel:)`. /// Setting the binding calls `controller.setInputSource(_:)`. private var inputSourceBinding: Binding { Binding( get: { switch controller.inputSource { case .testLoop: return -1 case .liveInput(let channel): return channel } }, set: { newValue in if newValue == -1 { controller.setInputSource(.testLoop) } else { controller.setInputSource(.liveInput(channel: newValue)) } } ) } /// Picker listing "Test Loop" and one option per available hardware input channel. /// /// When `engine.numInputChannels == 0` only "Test Loop" is shown; the live options /// are omitted so the picker never offers channels that do not exist. private var inputSourcePicker: some View { HStack(spacing: DemoRootView.toolbarSpacing) { Text("Input:") .font(.subheadline) .foregroundStyle(Color.white.opacity(0.65)) Picker("Input", selection: inputSourceBinding) { Text("Test Loop").tag(-1) ForEach(Array(0..