Files
josh 8519cabe42 v1.4.2 release: live-coding editor — Run Selection, slot release, engine panel, console, manual completion
Additive conveniences to the macOS-only Live Scratch editor. No changes to
existing public API.

- Run Selection (⌘E): evaluate just the highlighted text, additively.
- Graceful slot release: context.release(slot:) / free(slot:).
- Engine panel: live oscilloscope (ScopeCapture) + CPU avg/peak + counts.
- Reset Session / Restart to Reclaim Memory.
- Always-open console panel (report messages + errors with line:col).
- Faster typing: debounced highlight, updateNSView fast-path, publish guards.
- Manual code completion (Esc / F5), no auto-trigger on `.`; completion
  replaces the partial word, not the receiver expression.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-07-21 11:09:30 -07:00

86 lines
3.5 KiB
Swift

// ScratchScopeView.swift
// Lyrebird
//
// Copyright (c) 2026 Joshua Parmenter / realizedsound
// SPDX-License-Identifier: MIT
// LyrebirdUI/LiveCompile
//
// A compact real-time oscilloscope for the scratch editor's engine panel.
// Reimplemented here (rather than reusing `LyrebirdUI.LyrebirdScopeView`) because
// the LiveCompile stack must not link `LyrebirdUI` — that would static-fold a
// second copy of `LyrebirdSynth`'s ObjC classes. It reads the same core
// `ScopeCapture` the engine writes to.
#if os(macOS)
import SwiftUI
import LyrebirdSynth
/// Polls a ``ScopeCapture`` at ~30 Hz and draws each captured channel's waveform
/// with `Canvas`. Channels stack vertically; channel 0 cyan, channel 1 orange.
struct ScratchScopeView: View {
let capture: ScopeCapture
@State private var channels: [[Float]] = []
private static let pollIntervalNs: UInt64 = 33_333_333 // ~30 Hz
private static let channelColors: [Color] = [.cyan, .orange, .green, .yellow]
private static let backgroundColor: Color = Color(white: 0.08)
private static let zeroLineOpacity: Double = 0.25
private static let separatorOpacity: Double = 0.3
private static let waveformHeightFraction: CGFloat = 0.44
var body: some View {
Canvas { ctx, size in
ctx.fill(Path(CGRect(origin: .zero, size: size)),
with: .color(ScratchScopeView.backgroundColor))
guard !channels.isEmpty else { return }
let count: Int = channels.count
let rowH: CGFloat = size.height / CGFloat(count)
for (ch, samples): (Int, [Float]) in channels.enumerated() {
let midY: CGFloat = rowH * (CGFloat(ch) + 0.5)
let halfH: CGFloat = rowH * ScratchScopeView.waveformHeightFraction
var guide: Path = Path()
guide.move(to: CGPoint(x: 0, y: midY))
guide.addLine(to: CGPoint(x: size.width, y: midY))
ctx.stroke(guide, with: .color(.white.opacity(ScratchScopeView.zeroLineOpacity)),
lineWidth: 0.5)
if ch < count - 1 {
let sepY: CGFloat = rowH * CGFloat(ch + 1)
var sep: Path = Path()
sep.move(to: CGPoint(x: 0, y: sepY))
sep.addLine(to: CGPoint(x: size.width, y: sepY))
ctx.stroke(sep, with: .color(.white.opacity(ScratchScopeView.separatorOpacity)),
lineWidth: 0.5)
}
guard samples.count > 1 else { continue }
let color: Color = ScratchScopeView.channelColors[ch % ScratchScopeView.channelColors.count]
let xStep: CGFloat = size.width / CGFloat(samples.count - 1)
var path: Path = Path()
for (i, sample): (Int, Float) in samples.enumerated() {
let x: CGFloat = CGFloat(i) * xStep
let y: CGFloat = midY - CGFloat(sample) * halfH
if i == 0 { path.move(to: CGPoint(x: x, y: y)) }
else { path.addLine(to: CGPoint(x: x, y: y)) }
}
ctx.stroke(path, with: .color(color), lineWidth: 1.0)
}
}
.background(ScratchScopeView.backgroundColor)
.clipShape(RoundedRectangle(cornerRadius: 4))
.task {
while !Task.isCancelled {
channels = capture.snapshot()
try? await Task.sleep(nanoseconds: ScratchScopeView.pollIntervalNs)
}
}
}
}
#endif