Files
Lyrebird/Demos/LyrebirdScratch/LyrebirdScratch/AppDelegate.swift
T
josh 62b2f5dbec v1.5.0 release: LyrebirdRandom + Live Scratch editor
LyrebirdRandom scalar distribution sampling (seedable value type + shared
Random) with DistributionKind.sample dispatcher. Live Scratch editor: rename to
LyrebirdScratch, App Support artifact staging, Format Code (⌥⌘F), Render to File
(⌥⌘R offline CAF), line-number gutter, manual completion, Foundation in snippets,
scroll-jump fix, scheduling/NRT docs.
2026-07-22 07:53:12 -07:00

107 lines
4.7 KiB
Swift

// AppDelegate.swift
// Lyrebird
//
// Copyright (c) 2026 Joshua Parmenter / realizedsound
// SPDX-License-Identifier: MIT
import AppKit
import SwiftUI
import LyrebirdSynth
// LyrebirdLiveCompile (LiveSession, …) is re-exported by LyrebirdLiveCompileUI.
import LyrebirdLiveCompileUI
// This demo deliberately does NOT link `LyrebirdUI`. The live-compile stack
// (`LyrebirdLiveCompileUIDynamic`) dynamic-links the single shared
// `LyrebirdDynamic.framework` copy of `LyrebirdSynth`; linking the static
// `LyrebirdUI` product would static-fold a SECOND copy of every `LyrebirdSynth`
// ObjC class into the app binary (intra-package `LyrebirdUI → LyrebirdSynth` is
// always a static edge), producing duplicate-class registration and the
// "mysterious crashes" the runtime warns about. The only `LyrebirdUI` symbol
// this demo used was `LyrebirdWindow`, an `NSWindow`/`NSHostingController`
// wrapper with no `LyrebirdSynth` dependency — inlined below instead.
// Entitlements / signing posture (rationale kept here because Xcode strips
// comments from `LyrebirdScratch.entitlements` on every project resave):
//
// • App Sandbox is intentionally OFF. Live-compile shells out to `swiftc`
// (`posix_spawn`, disallowed under the sandbox) and `dlopen`s the resulting
// UNSIGNED snippet dylib (rejected by the sandbox). This is a direct-
// distribution developer tool, not a Mac App Store app; sandbox-off is the
// only viable configuration. (An XPC-worker split could revisit this.)
// • Hardened Runtime is ON, so the entitlement
// `com.apple.security.cs.disable-library-validation = true` is REQUIRED —
// without it dyld rejects `dlopen` of the unsigned, non-team-signed snippet
// dylib ("code signing blocked mmap()").
//
// Do not remove that entitlement or the app can't load compiled snippets.
final class AppDelegate: NSObject, NSApplicationDelegate {
let engine: SynthesisEngine
let session: LiveSession
var model: LyrebirdScratchModel?
var menuController: LyrebirdScratchMenuController?
var window: NSWindow?
@MainActor
init(engine: SynthesisEngine) {
self.engine = engine
// Stage the Lyrebird compile artifacts into ~/Library/Application Support
// (ungated) before the session reads its default -I/-L/rpath. Without
// this the snippet compiler points swiftc at the SPM build dir under
// ~/Documents, tripping the macOS Documents-folder permission gate.
// Best-effort: if staging fails the session falls back to the build dir.
LiveSession.stageArtifacts()
self.session = LiveSession(engine: engine)
}
@MainActor
func applicationDidFinishLaunching(_ notification: Notification) {
do {
try engine.start()
} catch {
print("[LyrebirdScratch] Engine failed to start: \(error)")
}
let model: LyrebirdScratchModel = LyrebirdScratchModel(session: session)
self.model = model
let menuController: LyrebirdScratchMenuController = LyrebirdScratchMenuController(model: model)
menuController.installMainMenu()
self.menuController = menuController
let hosting: NSHostingController<LyrebirdScratchView> =
NSHostingController(rootView: LyrebirdScratchView(model: model))
let win: NSWindow = NSWindow(
contentRect: NSRect(origin: .zero, size: CGSize(width: 720, height: 560)),
styleMask: [.titled, .closable, .resizable, .miniaturizable],
backing: .buffered,
defer: false
)
// Keep ownership in Swift — NSWindow defaults to releasing on close.
win.isReleasedWhenClosed = false
win.title = "Lyrebird — Live Scratch"
win.contentViewController = hosting
// Base geometry: three-quarters of the screen wide, three-quarters tall,
// anchored to the left edge and vertically centered in the visible area
// (below the menu bar, above the Dock).
let visible: NSRect = (win.screen ?? NSScreen.main)?.visibleFrame
?? NSRect(x: 0, y: 0, width: 1440, height: 900)
let width: CGFloat = visible.width * 0.75
let height: CGFloat = visible.height * 0.75
let originX: CGFloat = visible.minX
let originY: CGFloat = visible.minY + (visible.height - height) / 2
win.setFrame(NSRect(x: originX, y: originY, width: width, height: height), display: true)
win.makeKeyAndOrderFront(nil)
self.window = win
}
func applicationShouldTerminateAfterLastWindowClosed(_ sender: NSApplication) -> Bool {
true
}
func applicationWillTerminate(_ notification: Notification) {
engine.stop()
session.clearTracked()
}
}