// ParameterButton.swift // Lyrebird // // Copyright (c) 2026 Joshua Parmenter / realizedsound // SPDX-License-Identifier: MIT #if canImport(SwiftUI) import SwiftUI import LyrebirdSynth /// A button control for triggering actions or toggling a `UIParamNode` between two values. /// /// Two styles: /// - `.momentary` (default) — executes a closure on press; no node binding required. /// - `.toggle` — flips the bound node between `offValue` and `onValue` on each press. /// /// ```swift /// // Momentary action button /// ParameterButton("Trigger") { synth.trigger() } /// /// // Toggle bound to a UIParamNode /// ParameterButton("Mute", node: synth.$muted, style: .toggle) /// ``` public struct ParameterButton: View { // MARK: - Style public enum Style { /// Executes the action closure on each press. case momentary /// Flips the node between `offValue` (0) and `onValue` (1) on each press. case toggle } // MARK: - Properties private let label: String private let node: UIParamNode? private let style: Style private let action: (() -> Void)? /// Value representing the "off" state for `.toggle` style (default 0). public var offValue: Double = 0.0 /// Value representing the "on" state for `.toggle` style (default 1). public var onValue: Double = 1.0 // MARK: - Inits /// Creates a momentary button that runs `action` on press. /// /// - Parameters: /// - label: Button text. /// - action: Closure called each time the button is pressed. public init( _ label: String, action: @escaping ( ) -> Void) { self.label = label self.node = nil self.style = .momentary self.action = action } /// Creates a toggle button bound to a `UIParamNode`. /// /// The node's value is interpreted as a boolean: any value ≥ 0.5 is "on". /// /// - Parameters: /// - label: Button text. /// - node: The `UIParamNode` to toggle (pass `synth.$paramName`). /// - style: Must be `.toggle`; `.momentary` is handled by the other initializer. public init( _ label: String, node: UIParamNode, style: Style = .toggle ) { self.label = label self.node = node self.style = style self.action = nil } // MARK: - Body public var body: some View { switch style { case .momentary: Button(label) { action?() } .buttonStyle(.bordered) case .toggle: let isOn: Bool = (node?.value ?? 0) >= 0.5 Button(action: { node?.update(isOn ? offValue : onValue) }) { Text(label) .frame(minWidth: 60) } .buttonStyle(.bordered) .background(isOn ? Color.accentColor.opacity(0.3) : Color.clear) .cornerRadius(6) } } } #else // Linux/GTK4 counterpart to `LyrebirdUI.ParameterButton`. Two styles: a // `.momentary` button that runs a closure on press (no node), and a `.toggle` // that flips a bound core `UIParamNode` between `offValue` and `onValue`. Uses // SwiftOpenUI's native `Button`; no BackendGTK4 import (no Cairo drawing). // // SwiftOpenUI is always present in this target (see Package.swift), so no // `canImport` guard is needed. import SwiftOpenUI import LyrebirdSynth /// A button for the Linux/GTK4 UI. Mirrors `LyrebirdUI.ParameterButton`: /// `.momentary` runs an action on press; `.toggle` flips a bound ``UIParamNode`` /// between `offValue` and `onValue`, highlighting when on. public struct ParameterButton: View { // MARK: - Style public enum Style { /// Executes the action closure on each press. case momentary /// Flips the node between `offValue` and `onValue` on each press. case toggle } // MARK: - Properties private let label: String private let node: UIParamNode? private let style: Style private let action: (() -> Void)? /// Value representing the "off" state for `.toggle` style (default 0). public var offValue: Double = 0.0 /// Value representing the "on" state for `.toggle` style (default 1). public var onValue: Double = 1.0 // Presentation constants. private let labelMinWidth: Double = 60 private let cornerRadiusPoints: Double = 6 private let onHighlightOpacity: Double = 0.3 private let toggleThreshold: Double = 0.5 // MARK: - Inits /// Creates a momentary button that runs `action` on press. /// /// - Parameters: /// - label: Button text. /// - action: Closure called each time the button is pressed. public init( _ label: String, action: @escaping () -> Void ) { self.label = label self.node = nil self.style = .momentary self.action = action } /// Creates a toggle button bound to a `UIParamNode`. /// /// The node's value is interpreted as a boolean: any value ≥ 0.5 is "on". /// /// - Parameters: /// - label: Button text. /// - node: The `UIParamNode` to toggle (pass `synth.$paramName`). /// - style: Must be `.toggle`; `.momentary` is handled by the other initializer. public init( _ label: String, node: UIParamNode, style: Style = .toggle ) { self.label = label self.node = node self.style = style self.action = nil } // MARK: - Body public var body: some View { // ONE static Button node — deliberately NOT a `@ViewBuilder` if/else. // A conditional builds a `_ConditionalView`, which the GTK4 backend's // descriptor differ can plan as a structural replace; because the narrow // in-place mutation path requires EVERY node in the plan to be // narrow-applicable (`allSatisfy`), a single such node poisons the whole // plan and forces a FULL rebuild of the window on every state change — // tearing down in-flight drag gestures on sibling controls (knob / XY) // and adding latency. A single static node keeps changes on the narrow // path. The `.momentary` case simply carries `isToggledOn == false` and a // `.clear` background; `press()` re-reads the node's value at click time // (not captured) so a fast second click before the async rebuild still // flips correctly. Button(action: { press() }) { Text(label) .frame(minWidth: labelMinWidth) } .buttonStyle(.bordered) .background(isToggledOn ? Color.cyan.opacity(onHighlightOpacity) : Color.clear) .cornerRadius(cornerRadiusPoints) } // MARK: - Private helpers private var isToggledOn: Bool { guard case .toggle = style else { return false } return (node?.value ?? 0) >= toggleThreshold } private func press() { switch style { case .momentary: action?() case .toggle: node?.update(isToggledOn ? offValue : onValue) } } } #endif