Lyrebird 2.0.0 — Linux support
Consolidated release: the cross-platform Linux port (engine, generators, CLI, REPL, NRT, portable DSP), live ALSA audio + ALSA MIDI + POSIX OSC + portable WAV codec, the GTK4 GUI (LyrebirdUI on the SwiftOpenUI backend), and the Linux live-coding scratchpad (LiveCompile). Apple platforms unchanged; all additive. See RELEASES.md and LINUX_README.md. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
@@ -4,6 +4,7 @@
|
||||
// Copyright (c) 2026 Joshua Parmenter / realizedsound
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
#if canImport(SwiftUI)
|
||||
import SwiftUI
|
||||
import LyrebirdSynth
|
||||
|
||||
@@ -133,3 +134,126 @@ public struct MultiSlider: View {
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
#else
|
||||
// Linux/GTK4 counterpart to `LyrebirdUI.MultiSlider`: a row of `count` vertical
|
||||
// bars bound to consecutive `LyrebirdBuffer` cells, drawn on a `Canvas` with an
|
||||
// `.onDrag` gesture (the same custom-drawn approach as `ParameterSlider`). The
|
||||
// SwiftUI `GeometryReader`/`ZStack`/`DragGesture` are replaced: the Canvas has a
|
||||
// fixed total width (`count × columnWidth`) so the drag maps pointer-x to a
|
||||
// column directly, and pointer-y over the fixed `height` gives the value — a
|
||||
// Canvas renderer gets its `size`, so no `GeometryReader` is needed. Per-bar
|
||||
// writes go through `LyrebirdBuffer.setValue(_:at:)` exactly as on the Apple side.
|
||||
//
|
||||
// SwiftOpenUI + BackendGTK4 are always present in this target (see Package.swift).
|
||||
import SwiftOpenUI
|
||||
import BackendGTK4
|
||||
import LyrebirdSynth
|
||||
import Foundation
|
||||
|
||||
/// A row of adjacent vertical sliders bound to a `LyrebirdBuffer` for the
|
||||
/// Linux/GTK4 UI. Mirrors `LyrebirdUI.MultiSlider`; same public API/init as the
|
||||
/// Apple side.
|
||||
public struct MultiSlider: View {
|
||||
|
||||
// MARK: - Configuration
|
||||
|
||||
private let buffer: LyrebirdBuffer
|
||||
private let count: Int
|
||||
private let startIndex: Int
|
||||
private let range: ClosedRange<Double>
|
||||
private let height: CGFloat
|
||||
|
||||
// MARK: - State
|
||||
|
||||
@State private var values: [Double]
|
||||
|
||||
// Fixed per-column width (points). The Apple side fills the available width
|
||||
// via GeometryReader; on Linux the Canvas needs a concrete width, so each bar
|
||||
// gets a fixed column and the row is `count × columnWidth` wide.
|
||||
private let columnWidth: Double = 14
|
||||
|
||||
// MARK: - Init
|
||||
|
||||
/// - Parameters:
|
||||
/// - buffer: Backing `LyrebirdBuffer`. Mono only. Initial values are read
|
||||
/// once from `buffer[startIndex + i]` at construction.
|
||||
/// - count: Number of sliders. Must be ≥ 1 and `startIndex + count <= buffer.count`.
|
||||
/// - startIndex: First buffer index this view writes to. Defaults to `0`.
|
||||
/// - range: Value range. Bar-top maps to `range.upperBound`, bar-bottom to
|
||||
/// `range.lowerBound`.
|
||||
/// - height: View height in points. Default `100`.
|
||||
public init(
|
||||
buffer: LyrebirdBuffer,
|
||||
count: Int,
|
||||
startIndex: Int = 0,
|
||||
range: ClosedRange<Double>,
|
||||
height: CGFloat = 100
|
||||
) {
|
||||
precondition(buffer.channelCount == 1,
|
||||
"MultiSlider: buffer must be mono, got channelCount=\(buffer.channelCount)")
|
||||
precondition(count >= 1,
|
||||
"MultiSlider: count must be >= 1, got \(count)")
|
||||
precondition(startIndex >= 0 && startIndex + count <= buffer.count,
|
||||
"MultiSlider: startIndex \(startIndex) + count \(count) "
|
||||
+ "exceeds buffer.count \(buffer.count)")
|
||||
precondition(range.lowerBound < range.upperBound,
|
||||
"MultiSlider: range must be non-empty, got \(range)")
|
||||
|
||||
self.buffer = buffer
|
||||
self.count = count
|
||||
self.startIndex = startIndex
|
||||
self.range = range
|
||||
self.height = height
|
||||
|
||||
var initial: [Double] = [Double](repeating: range.lowerBound, count: count)
|
||||
for i: Int in 0..<count {
|
||||
let raw: Double = buffer[startIndex + i]
|
||||
initial[i] = min(max(raw, range.lowerBound), range.upperBound)
|
||||
}
|
||||
_values = State(wrappedValue: initial)
|
||||
}
|
||||
|
||||
// MARK: - Body
|
||||
|
||||
public var body: some View {
|
||||
let totalWidth: Double = Double(count) * columnWidth
|
||||
return Canvas(renderer: { (ctx: DrawingContext, size: CGSize) in
|
||||
let h: Double = Double(size.height)
|
||||
let w: Double = columnWidth
|
||||
|
||||
// Panel background.
|
||||
ctx.setColor(r: 0.5, g: 0.5, b: 0.5, a: 0.08)
|
||||
ctx.rectangle(x: 0, y: 0, width: Double(size.width), height: h)
|
||||
ctx.fill()
|
||||
|
||||
// Bars.
|
||||
let span: Double = range.upperBound - range.lowerBound
|
||||
ctx.setColor(r: 0.30, g: 0.80, b: 1.0)
|
||||
for i: Int in 0..<count {
|
||||
let v: Double = values[i]
|
||||
let norm: Double = (v - range.lowerBound) / span
|
||||
let barH: Double = h * norm
|
||||
let x: Double = Double(i) * w
|
||||
ctx.rectangle(x: x + 1, y: h - barH, width: max(1, w - 2), height: barH)
|
||||
ctx.fill()
|
||||
}
|
||||
})
|
||||
.frame(width: totalWidth, height: Double(height))
|
||||
.onDrag(
|
||||
minimumDistance: 0,
|
||||
onChanged: { (drag: DragGestureValue) in
|
||||
let column: Int = Int(drag.location.x / columnWidth)
|
||||
guard column >= 0 && column < count else { return }
|
||||
let hgt: Double = Double(height)
|
||||
let yClamped: Double = min(max(drag.location.y, 0), hgt)
|
||||
let norm: Double = 1 - (yClamped / hgt)
|
||||
let span: Double = range.upperBound - range.lowerBound
|
||||
let value: Double = range.lowerBound + norm * span
|
||||
values[column] = value
|
||||
buffer.setValue(value, at: startIndex + column)
|
||||
}
|
||||
)
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
@@ -4,6 +4,7 @@
|
||||
// Copyright (c) 2026 Joshua Parmenter / realizedsound
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
#if canImport(SwiftUI)
|
||||
import SwiftUI
|
||||
import LyrebirdSynth
|
||||
|
||||
@@ -101,3 +102,124 @@ public struct ParameterButton: View {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#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
|
||||
|
||||
@@ -4,6 +4,7 @@
|
||||
// Copyright (c) 2026 Joshua Parmenter / realizedsound
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
#if canImport(SwiftUI)
|
||||
import SwiftUI
|
||||
import LyrebirdSynth
|
||||
|
||||
@@ -24,7 +25,7 @@ public struct ParameterKnob: View {
|
||||
private let range: ClosedRange<Double>
|
||||
|
||||
/// Pixels of vertical drag that span the full parameter range. Default 200.
|
||||
public var dragSensitivity: CGFloat = 200
|
||||
public var dragSensitivity: Double = 200
|
||||
|
||||
// MARK: - Private drag state
|
||||
|
||||
@@ -110,7 +111,7 @@ public struct ParameterKnob: View {
|
||||
}
|
||||
let delta: CGFloat = dragStartPosition - gesture.location.y // up = positive
|
||||
let span: Double = range.upperBound - range.lowerBound
|
||||
let change: Double = Double(delta / dragSensitivity) * span
|
||||
let change: Double = (Double(delta) / dragSensitivity) * span
|
||||
let clamped: Double = min(max(dragStartValue + change, range.lowerBound), range.upperBound)
|
||||
node.update(clamped)
|
||||
}
|
||||
@@ -134,3 +135,119 @@ public struct ParameterKnob: View {
|
||||
formattedParameterValue(node.value)
|
||||
}
|
||||
}
|
||||
|
||||
#else
|
||||
// Experimental Linux GUI, built only under `LYREBIRD_ENABLE_LINUX_UI=1`
|
||||
// (see Package.swift). This is the first control ported from `LyrebirdUI`'s
|
||||
// SwiftUI `ParameterKnob` to SwiftOpenUI's GTK4 surface: a rotary knob drawn
|
||||
// with the Cairo-backed `DrawingContext` (SwiftOpenUI's stand-in for SwiftUI's
|
||||
// `GraphicsContext`) and a `.onDrag` vertical drag-to-set.
|
||||
//
|
||||
// It binds to the same core `UIParamNode` the macOS `ParameterKnob` binds to,
|
||||
// so a synthesizer's `@UIParam` drives both platforms from one model. Because
|
||||
// `UIParamNode` is `@Observable`, a drag-driven `node.update(_:)` takes the
|
||||
// GTK4 backend's narrow mutation (redraw-in-place) path carried by the pinned
|
||||
// SwiftOpenUI fork, so the in-flight drag gesture survives and the knob tracks
|
||||
// continuously.
|
||||
//
|
||||
// SwiftOpenUI + BackendGTK4 are always present in this target (see Package.swift),
|
||||
// so no `canImport` guard is needed; `BackendGTK4` carries the Cairo-backed
|
||||
// `DrawingContext` drawing methods (setColor/arc/moveTo/…), which are extensions
|
||||
// not present in SwiftOpenUI's core.
|
||||
import SwiftOpenUI
|
||||
import BackendGTK4
|
||||
import LyrebirdSynth
|
||||
import Foundation
|
||||
|
||||
/// A rotary knob for the Linux/GTK4 UI, drawn on a `Canvas` and set by a
|
||||
/// vertical drag. Binds to a core ``UIParamNode`` (via a synthesizer's
|
||||
/// `@UIParam` projected value, `synth.$paramName`) — the same model the macOS
|
||||
/// `ParameterKnob` uses. Mirrors that control's behaviour: −135°…+135° sweep,
|
||||
/// drag-up increases.
|
||||
public struct ParameterKnob: View {
|
||||
private let label: String
|
||||
private let node: UIParamNode
|
||||
private let range: ClosedRange<Double>
|
||||
|
||||
/// Pixels of vertical drag spanning the full parameter range. Matches the
|
||||
/// macOS knob's default.
|
||||
public var dragSensitivity: Double = 200
|
||||
|
||||
// Drag origin, captured on the first `onChanged` of each gesture. An
|
||||
// explicit flag avoids a sentinel collision when the value is genuinely at
|
||||
// the range's lower bound.
|
||||
@State private var dragStartValue: Double = 0
|
||||
@State private var dragInProgress: Bool = false
|
||||
|
||||
/// Creates a knob bound to a `UIParamNode`.
|
||||
///
|
||||
/// - Parameters:
|
||||
/// - label: Text displayed below the knob.
|
||||
/// - node: The `UIParamNode` to bind to (pass `synth.$paramName`).
|
||||
/// - range: The minimum and maximum parameter values.
|
||||
public init(
|
||||
_ label: String,
|
||||
node: UIParamNode,
|
||||
range: ClosedRange<Double>
|
||||
) {
|
||||
self.label = label
|
||||
self.node = node
|
||||
self.range = range
|
||||
}
|
||||
|
||||
public var body: some View {
|
||||
VStack {
|
||||
Canvas(renderer: { (ctx: DrawingContext, size: CGSize) in
|
||||
let cx: Double = Double(size.width) / 2
|
||||
let cy: Double = Double(size.height) / 2
|
||||
let radius: Double = min(cx, cy) - 4
|
||||
|
||||
// Dial face ring.
|
||||
ctx.setColor(r: 0.20, g: 0.20, b: 0.20)
|
||||
ctx.setLineWidth(3)
|
||||
ctx.arc(centerX: cx, centerY: cy, radius: radius,
|
||||
startAngle: 0, endAngle: 2 * Double.pi)
|
||||
ctx.stroke()
|
||||
|
||||
// Indicator: −135°…+135° sweep (270° total), like the macOS knob.
|
||||
let startAngleRadians: Double = 0.75 * Double.pi
|
||||
let sweepRadians: Double = 1.5 * Double.pi
|
||||
let angle: Double = startAngleRadians + normalise(node.value) * sweepRadians
|
||||
ctx.setColor(r: 0.30, g: 0.80, b: 1.0)
|
||||
ctx.setLineWidth(3)
|
||||
ctx.moveTo(x: cx, y: cy)
|
||||
ctx.lineTo(x: cx + radius * cos(angle), y: cy + radius * sin(angle))
|
||||
ctx.stroke()
|
||||
})
|
||||
.frame(width: 50, height: 50)
|
||||
.onDrag(
|
||||
minimumDistance: 0,
|
||||
onChanged: { (drag: DragGestureValue) in
|
||||
if !dragInProgress {
|
||||
dragInProgress = true
|
||||
dragStartValue = node.value
|
||||
}
|
||||
// Up (negative height) increases, matching the macOS knob.
|
||||
let span: Double = range.upperBound - range.lowerBound
|
||||
let change: Double = (-drag.height / dragSensitivity) * span
|
||||
let target: Double = min(range.upperBound,
|
||||
max(range.lowerBound, dragStartValue + change))
|
||||
node.update(target)
|
||||
},
|
||||
onEnded: { (_: DragGestureValue) in
|
||||
dragInProgress = false
|
||||
}
|
||||
)
|
||||
Text(label)
|
||||
}
|
||||
.padding()
|
||||
}
|
||||
|
||||
private func normalise(_ value: Double) -> Double {
|
||||
let lo: Double = range.lowerBound
|
||||
let hi: Double = range.upperBound
|
||||
guard hi > lo else { return 0 }
|
||||
return min(max((value - lo) / (hi - lo), 0), 1)
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
@@ -4,6 +4,7 @@
|
||||
// Copyright (c) 2026 Joshua Parmenter / realizedsound
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
#if canImport(SwiftUI)
|
||||
import SwiftUI
|
||||
import LyrebirdSynth
|
||||
|
||||
@@ -59,3 +60,68 @@ public struct ParameterLabel: View {
|
||||
String(format: "%.\(decimalPlaces)f", node.value)
|
||||
}
|
||||
}
|
||||
|
||||
#else
|
||||
// Linux/GTK4 counterpart to `LyrebirdUI.ParameterLabel`: a read-only display of
|
||||
// a core `UIParamNode`'s current value, updated automatically via `@Observable`
|
||||
// (no polling). Uses only the core View DSL; no BackendGTK4 import.
|
||||
//
|
||||
// SwiftOpenUI is always present in this target (see Package.swift), so no
|
||||
// `canImport` guard is needed.
|
||||
import SwiftOpenUI
|
||||
import LyrebirdSynth
|
||||
import Foundation // String(format:) for the value readout
|
||||
|
||||
/// A read-only label for the Linux/GTK4 UI that shows a ``UIParamNode``'s
|
||||
/// current value. Mirrors `LyrebirdUI.ParameterLabel`: a leading descriptive
|
||||
/// label and a trailing monospaced value. Refreshes automatically because
|
||||
/// `UIParamNode` is `@Observable`.
|
||||
public struct ParameterLabel: View {
|
||||
|
||||
// MARK: - Properties
|
||||
|
||||
private let label: String
|
||||
private let node: UIParamNode
|
||||
private let decimalPlaces: Int
|
||||
|
||||
// Layout constants (points), matching the macOS label.
|
||||
private let labelMinWidth: Double = 80
|
||||
private let valueMinWidth: Double = 60
|
||||
|
||||
// MARK: - Init
|
||||
|
||||
/// Creates a read-only parameter label.
|
||||
///
|
||||
/// - Parameters:
|
||||
/// - label: Descriptive text displayed before the value.
|
||||
/// - node: The `UIParamNode` to monitor (pass `synth.$paramName`).
|
||||
/// - decimalPlaces: Number of decimal places shown. Default 3.
|
||||
public init(
|
||||
_ label: String,
|
||||
node: UIParamNode,
|
||||
decimalPlaces: Int = 3
|
||||
) {
|
||||
self.label = label
|
||||
self.node = node
|
||||
self.decimalPlaces = decimalPlaces
|
||||
}
|
||||
|
||||
// MARK: - Body
|
||||
|
||||
public var body: some View {
|
||||
HStack {
|
||||
Text(label)
|
||||
.frame(minWidth: labelMinWidth, alignment: .leading)
|
||||
Text(formattedValue)
|
||||
.monospacedDigit()
|
||||
.frame(minWidth: valueMinWidth, alignment: .trailing)
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: - Formatting
|
||||
|
||||
private var formattedValue: String {
|
||||
String(format: "%.\(decimalPlaces)f", node.value)
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
@@ -4,6 +4,7 @@
|
||||
// Copyright (c) 2026 Joshua Parmenter / realizedsound
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
#if canImport(SwiftUI)
|
||||
import SwiftUI
|
||||
import LyrebirdSynth
|
||||
|
||||
@@ -84,6 +85,8 @@ public struct ParameterSlider: View {
|
||||
private func toLinear(_ value: Double) -> Double {
|
||||
let lo: Double = range.lowerBound
|
||||
let hi: Double = range.upperBound
|
||||
// Degenerate range: avoid a 0/0 NaN thumb position.
|
||||
guard hi > lo else { return 0 }
|
||||
switch taper {
|
||||
case .linear:
|
||||
return (value - lo) / (hi - lo)
|
||||
@@ -116,3 +119,167 @@ public struct ParameterSlider: View {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#else
|
||||
// Linux/GTK4 counterpart to `LyrebirdUI.ParameterSlider`. Drawn on a `Canvas`
|
||||
// with a `.onDrag` gesture — the same custom-drawn approach as
|
||||
// `ParameterKnob` — rather than a native `Slider` (GtkScale). This is what
|
||||
// lets a slider drag update the parameter on every motion event (like the knob
|
||||
// and the macOS slider), and, crucially, lets the slider **track live** when a
|
||||
// paired control bound to the same node is dragged: a Canvas closure reads
|
||||
// `node.value` at paint time, so the GTK4 backend's per-drag redraw of every
|
||||
// linked control repaints it in step. A native GtkScale, whose thumb position is
|
||||
// pushed on rebuild (deferred during a drag), could only reconcile on release.
|
||||
//
|
||||
// Binds to a core `UIParamNode` (via `synth.$paramName`) with the same linear /
|
||||
// exponential taper mapping the macOS slider uses. `BackendGTK4` carries the
|
||||
// Cairo-backed `DrawingContext` drawing methods.
|
||||
import SwiftOpenUI
|
||||
import BackendGTK4
|
||||
import LyrebirdSynth
|
||||
import Foundation // log/pow for the exponential taper
|
||||
|
||||
/// A labelled continuous slider for the Linux/GTK4 UI, bound to a core
|
||||
/// ``UIParamNode``. Mirrors `LyrebirdUI.ParameterSlider`: a leading label, a
|
||||
/// horizontal Canvas-drawn track with a draggable thumb, and a trailing value
|
||||
/// readout, with an optional exponential taper for ratio-based parameters
|
||||
/// (frequency, time).
|
||||
public struct ParameterSlider: View {
|
||||
|
||||
// MARK: - Taper
|
||||
|
||||
/// Value mapping applied between the slider's linear position and the
|
||||
/// parameter value.
|
||||
public enum Taper {
|
||||
/// No mapping — slider position maps directly to parameter value.
|
||||
case linear
|
||||
/// Exponential mapping — equal visual steps produce equal ratios
|
||||
/// (good for frequency, time).
|
||||
case exponential
|
||||
}
|
||||
|
||||
// MARK: - Properties
|
||||
|
||||
private let label: String
|
||||
private let node: UIParamNode
|
||||
private let range: ClosedRange<Double>
|
||||
private let taper: Taper
|
||||
|
||||
// Layout constants (points), matching the macOS slider's proportions.
|
||||
private let labelMinWidth: Double = 80
|
||||
private let valueMinWidth: Double = 60
|
||||
/// Track dimensions. The drag maps pointer x over this fixed width, so it
|
||||
/// matches the Canvas's own size (`.frame(width: trackWidth, …)`).
|
||||
private let trackWidth: Double = 160
|
||||
private let trackHeight: Double = 24
|
||||
/// Thumb radius; the usable track is inset by this so the thumb stays fully
|
||||
/// visible at both ends.
|
||||
private let thumbRadius: Double = 7
|
||||
|
||||
// MARK: - Init
|
||||
|
||||
/// Creates a slider bound to a `UIParamNode`.
|
||||
///
|
||||
/// - Parameters:
|
||||
/// - label: Text displayed beside the slider.
|
||||
/// - node: The `UIParamNode` to bind to (pass `synth.$paramName`).
|
||||
/// - range: The minimum and maximum parameter values.
|
||||
/// - taper: Value mapping. Default `.linear`.
|
||||
public init(
|
||||
_ label: String,
|
||||
node: UIParamNode,
|
||||
range: ClosedRange<Double>,
|
||||
taper: Taper = .linear
|
||||
) {
|
||||
self.label = label
|
||||
self.node = node
|
||||
self.range = range
|
||||
self.taper = taper
|
||||
}
|
||||
|
||||
// MARK: - Body
|
||||
|
||||
public var body: some View {
|
||||
HStack {
|
||||
Text(label)
|
||||
.frame(minWidth: labelMinWidth, alignment: .leading)
|
||||
Canvas(renderer: { (ctx: DrawingContext, size: CGSize) in
|
||||
let w: Double = Double(size.width)
|
||||
let h: Double = Double(size.height)
|
||||
let cy: Double = h / 2
|
||||
let x0: Double = thumbRadius
|
||||
let x1: Double = w - thumbRadius
|
||||
let usable: Double = max(x1 - x0, 1)
|
||||
let pos: Double = toLinear(node.value)
|
||||
let thumbX: Double = x0 + pos * usable
|
||||
|
||||
// Track background (full width).
|
||||
ctx.setColor(r: 0.20, g: 0.20, b: 0.20)
|
||||
ctx.setLineWidth(4)
|
||||
ctx.moveTo(x: x0, y: cy)
|
||||
ctx.lineTo(x: x1, y: cy)
|
||||
ctx.stroke()
|
||||
|
||||
// Filled portion up to the thumb.
|
||||
ctx.setColor(r: 0.30, g: 0.80, b: 1.0)
|
||||
ctx.setLineWidth(4)
|
||||
ctx.moveTo(x: x0, y: cy)
|
||||
ctx.lineTo(x: thumbX, y: cy)
|
||||
ctx.stroke()
|
||||
|
||||
// Thumb.
|
||||
ctx.setColor(r: 0.90, g: 0.90, b: 0.95)
|
||||
ctx.arc(centerX: thumbX, centerY: cy, radius: thumbRadius,
|
||||
startAngle: 0, endAngle: 2 * Double.pi)
|
||||
ctx.fill()
|
||||
})
|
||||
.frame(width: trackWidth, height: trackHeight)
|
||||
.onDrag(
|
||||
minimumDistance: 0,
|
||||
onChanged: { (drag: DragGestureValue) in
|
||||
// Absolute: the thumb follows the pointer x within the track.
|
||||
let x0: Double = thumbRadius
|
||||
let usable: Double = max(trackWidth - 2 * thumbRadius, 1)
|
||||
let pos: Double = min(1, max(0, (drag.location.x - x0) / usable))
|
||||
node.update(fromLinear(pos))
|
||||
}
|
||||
)
|
||||
Text(formattedValue)
|
||||
.frame(minWidth: valueMinWidth, alignment: .trailing)
|
||||
.monospacedDigit()
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: - Private helpers
|
||||
|
||||
private func toLinear(_ value: Double) -> Double {
|
||||
let lo: Double = range.lowerBound
|
||||
let hi: Double = range.upperBound
|
||||
// Degenerate range: avoid a 0/0 NaN thumb position.
|
||||
guard hi > lo else { return 0 }
|
||||
switch taper {
|
||||
case .linear:
|
||||
return (value - lo) / (hi - lo)
|
||||
case .exponential:
|
||||
guard lo > 0, hi > lo else { return (value - lo) / (hi - lo) }
|
||||
return log(value / lo) / log(hi / lo)
|
||||
}
|
||||
}
|
||||
|
||||
private func fromLinear(_ position: Double) -> Double {
|
||||
let lo: Double = range.lowerBound
|
||||
let hi: Double = range.upperBound
|
||||
switch taper {
|
||||
case .linear:
|
||||
return lo + position * (hi - lo)
|
||||
case .exponential:
|
||||
guard lo > 0, hi > lo else { return lo + position * (hi - lo) }
|
||||
return lo * pow(hi / lo, position)
|
||||
}
|
||||
}
|
||||
|
||||
private var formattedValue: String {
|
||||
formattedParameterValue(node.value)
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
@@ -4,6 +4,7 @@
|
||||
// Copyright (c) 2026 Joshua Parmenter / realizedsound
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
#if canImport(SwiftUI)
|
||||
import SwiftUI
|
||||
import LyrebirdSynth
|
||||
|
||||
@@ -163,3 +164,148 @@ public struct ParameterXY: View {
|
||||
formattedParameterValue(v)
|
||||
}
|
||||
}
|
||||
|
||||
#else
|
||||
// Linux/GTK4 counterpart to `LyrebirdUI.ParameterXY`: a two-dimensional pad that
|
||||
// drives two core `UIParamNode`s at once. Drawn on a single `Canvas` (background,
|
||||
// centre grid, cursor) with an absolute-position `.onDrag` — the touch point maps
|
||||
// straight to the cursor, X left→right, Y bottom→top.
|
||||
//
|
||||
// Same gesture path as `ParameterKnob`: the drag calls `node.update(_:)`,
|
||||
// and because `UIParamNode` is `@Observable` the Canvas redraw takes the GTK4
|
||||
// backend's narrow `.canvasContent` mutation path (the pinned fork's fix), so the
|
||||
// cursor tracks continuously without the gesture being torn down by a rebuild.
|
||||
//
|
||||
// SwiftOpenUI + BackendGTK4 are always present in this target (see Package.swift),
|
||||
// so no `canImport` guard is needed; `BackendGTK4` carries the Cairo-backed
|
||||
// `DrawingContext` drawing methods.
|
||||
import SwiftOpenUI
|
||||
import BackendGTK4
|
||||
import LyrebirdSynth
|
||||
import Foundation // CGSize (Canvas renderer size)
|
||||
|
||||
/// A two-axis control pad for the Linux/GTK4 UI, bound to two ``UIParamNode``s.
|
||||
/// Mirrors `LyrebirdUI.ParameterXY`: drag within the pad to set X (horizontal,
|
||||
/// left→right) and Y (vertical, bottom→top) independently.
|
||||
public struct ParameterXY: View {
|
||||
|
||||
// MARK: - Properties
|
||||
|
||||
private let xLabel: String
|
||||
private let xNode: UIParamNode
|
||||
private let xRange: ClosedRange<Double>
|
||||
|
||||
private let yLabel: String
|
||||
private let yNode: UIParamNode
|
||||
private let yRange: ClosedRange<Double>
|
||||
|
||||
// Presentation constants (points).
|
||||
private let padSize: Double = 160
|
||||
private let cursorRadius: Double = 6
|
||||
|
||||
// MARK: - Init
|
||||
|
||||
/// Creates an XY pad bound to two `UIParamNode` values.
|
||||
///
|
||||
/// - Parameters:
|
||||
/// - xLabel: Label for the X axis.
|
||||
/// - xNode: The `UIParamNode` controlled by horizontal position.
|
||||
/// - xRange: Value range for the X axis.
|
||||
/// - yLabel: Label for the Y axis.
|
||||
/// - yNode: The `UIParamNode` controlled by vertical position.
|
||||
/// - yRange: Value range for the Y axis.
|
||||
public init(
|
||||
xLabel: String,
|
||||
xNode: UIParamNode,
|
||||
xRange: ClosedRange<Double>,
|
||||
yLabel: String,
|
||||
yNode: UIParamNode,
|
||||
yRange: ClosedRange<Double>
|
||||
) {
|
||||
self.xLabel = xLabel
|
||||
self.xNode = xNode
|
||||
self.xRange = xRange
|
||||
self.yLabel = yLabel
|
||||
self.yNode = yNode
|
||||
self.yRange = yRange
|
||||
}
|
||||
|
||||
// MARK: - Body
|
||||
|
||||
public var body: some View {
|
||||
VStack {
|
||||
Canvas(renderer: { (ctx: DrawingContext, size: CGSize) in
|
||||
let w: Double = Double(size.width)
|
||||
let h: Double = Double(size.height)
|
||||
|
||||
// Background fill.
|
||||
ctx.setColor(r: 0.15, g: 0.15, b: 0.15)
|
||||
ctx.rectangle(x: 0, y: 0, width: w, height: h)
|
||||
ctx.fill()
|
||||
|
||||
// Border.
|
||||
ctx.setColor(r: 0.40, g: 0.40, b: 0.40)
|
||||
ctx.setLineWidth(1)
|
||||
ctx.rectangle(x: 0, y: 0, width: w, height: h)
|
||||
ctx.stroke()
|
||||
|
||||
// Centre grid lines (faint).
|
||||
let midX: Double = w / 2
|
||||
let midY: Double = h / 2
|
||||
ctx.setColor(r: 0.40, g: 0.40, b: 0.40, a: 0.4)
|
||||
ctx.setLineWidth(0.5)
|
||||
ctx.moveTo(x: midX, y: 0)
|
||||
ctx.lineTo(x: midX, y: h)
|
||||
ctx.moveTo(x: 0, y: midY)
|
||||
ctx.lineTo(x: w, y: midY)
|
||||
ctx.stroke()
|
||||
|
||||
// Cursor: X left→right, Y bottom→top (flip vertical).
|
||||
let nx: Double = normalise(xNode.value, range: xRange)
|
||||
let ny: Double = 1.0 - normalise(yNode.value, range: yRange)
|
||||
let cx: Double = nx * w
|
||||
let cy: Double = ny * h
|
||||
ctx.setColor(r: 0.30, g: 0.80, b: 1.0)
|
||||
ctx.arc(centerX: cx, centerY: cy, radius: cursorRadius,
|
||||
startAngle: 0, endAngle: 2 * Double.pi)
|
||||
ctx.fill()
|
||||
})
|
||||
.frame(width: padSize, height: padSize)
|
||||
.onDrag(
|
||||
minimumDistance: 0,
|
||||
onChanged: { (drag: DragGestureValue) in
|
||||
// Absolute position: the touch point is the cursor.
|
||||
let nx: Double = min(max(drag.location.x / padSize, 0), 1)
|
||||
let ny: Double = min(max(drag.location.y / padSize, 0), 1)
|
||||
xNode.update(denormalise(nx, range: xRange))
|
||||
yNode.update(denormalise(1.0 - ny, range: yRange)) // flip Y
|
||||
}
|
||||
)
|
||||
Text("\(xLabel): \(formattedParameterValue(xNode.value))")
|
||||
.monospacedDigit()
|
||||
Text("\(yLabel): \(formattedParameterValue(yNode.value))")
|
||||
.monospacedDigit()
|
||||
}
|
||||
.padding()
|
||||
}
|
||||
|
||||
// MARK: - Helpers
|
||||
|
||||
private func normalise(
|
||||
_ value: Double,
|
||||
range: ClosedRange<Double>
|
||||
) -> Double {
|
||||
let lo: Double = range.lowerBound
|
||||
let hi: Double = range.upperBound
|
||||
guard hi > lo else { return 0 }
|
||||
return min(max((value - lo) / (hi - lo), 0), 1)
|
||||
}
|
||||
|
||||
private func denormalise(
|
||||
_ position: Double,
|
||||
range: ClosedRange<Double>
|
||||
) -> Double {
|
||||
range.lowerBound + position * (range.upperBound - range.lowerBound)
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
@@ -4,6 +4,7 @@
|
||||
// Copyright (c) 2026 Joshua Parmenter / realizedsound
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
#if canImport(SwiftUI)
|
||||
import SwiftUI
|
||||
#if os(macOS)
|
||||
import AppKit
|
||||
@@ -20,3 +21,27 @@ extension Color {
|
||||
public static let lyrebirdSeparator = Color(UIColor.separator)
|
||||
#endif
|
||||
}
|
||||
|
||||
#else
|
||||
// Linux/GTK4 counterpart to the Apple `PlatformColors`. AppKit/UIKit system
|
||||
// colours have no GTK4 equivalent, so these are fixed neutral tones chosen to
|
||||
// read on both light and dark GTK themes: a faint panel fill and a mid-grey
|
||||
// hairline separator. `Color` here is SwiftOpenUI's, not SwiftUI's.
|
||||
//
|
||||
// SwiftOpenUI is always present in this target (see Package.swift).
|
||||
import SwiftOpenUI
|
||||
|
||||
extension Color {
|
||||
// Computed (not stored) because SwiftOpenUI's `Color` is not `Sendable`, so a
|
||||
// `static let` would be a non-concurrency-safe global. A computed `static var`
|
||||
// rebuilds the value on each access — trivially cheap for a plain colour.
|
||||
/// Subtle panel background used behind grouped controls/diagnostics views.
|
||||
public static var lyrebirdControlBackground: Color {
|
||||
Color(red: 0.5, green: 0.5, blue: 0.5, opacity: 0.08)
|
||||
}
|
||||
/// Hairline separator/stroke colour.
|
||||
public static var lyrebirdSeparator: Color {
|
||||
Color(red: 0.5, green: 0.5, blue: 0.5, opacity: 0.35)
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
Reference in New Issue
Block a user