# Linux parity plan — the two must-dos + the scratch window Where Lyrebird's Linux GUI stands and what closes the gap to a defensible "parity with macOS" claim. The interactive widget layer is done and validated: every control/view is ported to a GTK4 `#else` body, and the SwiftOpenUI fork's narrow-mutation path (Patches A–F) makes them behave like real SwiftUI, not a degraded port. What remains are two functional gaps and one follow-on. Prereq context: `docs/ui/linux-view-port-plan.md` (the view ports), `spikes/SwiftOpenUIKnob/FORK_PATCH_PLAN.md` + `NARROW_PATH_GENERALIZATION_PLAN.md` (the fork), and the `linux-gui-swiftopenui` memory. The fork is pinned by revision in `Package.swift`; this Mac cannot build the GTK4 backend, so every change is prepare-diff → push fork → bump pin → validate on the Linux VM. Ordering: **(1) LyrebirdWindow → (2) MouseTracker → (3) scratch window.** The scratch window depends on (1). (1) and (2) are independent of each other. --- ## Must-do 1 — `LyrebirdWindow` (host-window abstraction) — ✅ DONE **Status (branch `parity/linux-window-mousetracker`).** Shipped. A `#elseif os(Linux)` body backs `LyrebirdWindow` with a runtime-opened `GtkApplicationWindow` via a new fork API `GTK4Backend.openStandaloneWindow` (+ `gtk_swift_get_default_gtk_application`); `presentInWindow` is ported on all six views and the EngineStatus "Open Window" buttons are re-enabled. VM-runtime confirmed. **Design note that DIVERGES from the original plan below:** the Linux `LyrebirdWindow` / `presentInWindow` are **nonisolated**, NOT `@MainActor` — the whole Linux/GTK4 UI is confined to the GTK main loop (not the Swift main actor), and keeping `@MainActor` forced an `assumeIsolated` hop that captured task-isolated `self` → a "sending self risks data races" error. Only the `@MainActor` `LyrebirdMessageCenter.shared` access needs a self-less `assumeIsolated`. The original recommendation below to "Keep it `@MainActor`" is therefore superseded — do not follow it. **Why it was a must-do.** There was no first-class way to open/manage a window on Linux. `LyrebirdWindow` was Apple-only (`NSWindow` + `NSHostingController`); every view's `presentInWindow(...)` convenience, and the EngineStatus "Open Window" buttons, were therefore macOS-only. The demo hand-rolls a single GTK window via SwiftOpenUI's `App`/`Scene`/`WindowGroup` scene model — there's no imperative "open another window hosting this view" equivalent. **Current Apple surface** (`Sources/LyrebirdUI/LyrebirdWindow.swift`): `@MainActor final class LyrebirdWindow` — `init(title:width:height:)`, `open(@ViewBuilder content:)` (wraps `NSHostingController`), `close()`, `nsWindow`, and an `NSWindowDelegate` that nils the reference on user close. Consumed by `presentInWindow` on EngineStatus / NodeTree / Message / EventList / Process / RunningProcesses views. **Linux approach.** Add a GTK4 `#else` body with the same public API, backed by a `GtkWindow`: - `open(content:)` → create a `GtkApplicationWindow` (or `GtkWindow`), set title + default size, `gtkRenderView(content())` (BackendGTK4) as the child, `present`. - `close()` → destroy the window; wire the `close-request` / destroy signal to nil the reference (parity with the `NSWindowDelegate`). - Keep it `@MainActor`. **Key unknown to resolve first (spike).** GTK needs a running `GtkApplication` + main loop before a window can present. Two questions: 1. Does SwiftOpenUI already expose a way to open a standalone window at runtime outside the `App`/`WindowGroup` scene (an imperative `GTK4Backend` window API)? If yes, build on it. If not, `LyrebirdWindow` must own the `gtk_init` + application + main-loop lifecycle itself. 2. **App entry point.** On macOS `LyrebirdWindow` works standalone (implicit `NSApp`). On Linux, opening a window from a CLI/REPL with no scene needs an owned GTK app + loop. Decide: does `LyrebirdWindow` bootstrap GTK if none is running (so `presentInWindow` works headless-of-a-scene), or is it only valid inside a `GTK4Backend().run(App)` context? Recommendation: make it work in an existing loop first (covers the in-app "Open Window" buttons), then add a bootstrap path for standalone use. **Effort:** moderate–substantial. The view hosting is straightforward (`gtkRenderView`); the GTK app/main-loop lifecycle + multi-window management is the real work, sized by what SwiftOpenUI already gives us (the spike answers it). **Validation:** `presentInWindow` on EngineStatus/NodeTree/Message from the demo opens a second GTK window that renders + tracks; closing it (button + WM close) nils cleanly; no leak/crash on repeated open/close. **Fork implication:** if SwiftOpenUI lacks a standalone-window API, that's a fork addition (a `GTK4Backend.openWindow(title:size:content:)`), upstreamable. --- ## Must-do 2 — `MouseTracker` / `MouseInput` (Linux pointer source) — ✅ DONE **Status (branch `parity/linux-window-mousetracker`).** Shipped the window-local baseline. A `#elseif os(Linux)` `MouseTracker` (`MouseInputBridge` subclass, `Atomic` x/y/button, nonisolated) is fed by a new fork API `GTK4PointerTracking` — a `GtkEventControllerMotion` + window-level `GtkGestureClick` attached to every window the backend opens, forwarding normalized (y-flipped) coordinates to a process-wide sink. `installShared()` registers both `MouseInput.install(self)` and the sink; install timing is not sensitive (controllers read the sink at event time). x/y fully covered, button best-effort. Validated by `LYREBIRD_DEMO=mouse`. The X11-global enhancement below remains an optional future add. **Why it was a must-do.** The `Mouse.X` / `Mouse.Y` / `Mouse.Click` *generators* (core, `Sources/Lyrebird/Generators/UI/`) read from `MouseInput`, which is fed by a `MouseInputBridge`. The only bridge is `MouseTracker` (AppKit `NSEvent` global monitors) — Apple-only. On Linux the generators silently read the default `0.5/0.5/0` — any patch using mouse control does nothing. **Current seam (already platform-neutral — this is the good news):** - `open class MouseInputBridge` (`x`/`y`/`button`, default 0.5/0.5/0) — core. - `enum MouseInput` with `install(_ bridge:)` / `currentX/Y/Button` — core seam. - `MouseTracker: MouseInputBridge` (AppKit) installs `NSEvent` global monitors and overrides `x/y/button` from atomics — Apple-only, in `LyrebirdUI`. So the port is a **new `MouseInputBridge` conformer for Linux** + installing it at startup via `MouseInput.install`. No core changes. **Linux approach — pick the pointer source (design decision):** - **Window-local (recommended baseline, portable):** a `GtkEventControllerMotion` on the app window updates x/y (normalized to the window), plus a click gesture for `button`. Works on both X11 and Wayland. Limitation: only tracks while the pointer is over a Lyrebird window (not truly global) — acceptable for a synth control surface, and matches how you'd actually perform with it. - **Global (X11-only enhancement):** poll `XQueryPointer` (or XInput2) on a timer/thread for screen-global position. True parity with the macOS global monitor, but **X11 only** — Wayland forbids global pointer access (needs a portal). Gate behind an availability check; fall back to window-local. Recommendation: ship window-local first (covers the real use case, no display- server dependency), add X11-global as an optional path if global is wanted. **Effort:** moderate. The bridge + install is small; the pointer plumbing (GTK motion controller, or X11 polling) is the work. Live values are read on the RT-adjacent path via atomics exactly as the AppKit tracker does — mirror that. **Validation:** a patch with `Mouse.X.Control()` driving a parameter responds to pointer motion over the window on the VM; `MouseInput.currentX/Y/Button` update. **Placement:** the Linux bridge lives in `LyrebirdUI` (like `MouseTracker`), conforms to the core `MouseInputBridge`, installed via `MouseInput.install` at app startup — same contract as macOS. --- ## Follow-on — the LiveCompile scratch window (after 1 & 2) The live-coding scratchpad (`LiveCompile/` sub-package) is macOS-only. It has two layers with very different portability; full detail lives with the engine spike, summarized here for sequencing. - **Engine (`LyrebirdLiveCompile`, ~3.3k LOC) — moderate, independent of 1 & 2.** `LiveCompiler` shells `swiftc -emit-library` → dylib, `dlopen`/`dlsym`; `SourceKitLSPClient` drives `sourcekit-lsp`. All portable (Linux toolchain ships `swiftc`, `dlopen`, `sourcekit-lsp`). Adapt: `.dylib`→`.so`, drop `xcrun` (PATH), Linux `-target`/SDK + module paths, dynamic-link the snippet against one `libLyrebird.so` (Linux analog of the `libLyrebirdDynamic.dylib` design), and a Linux `SwiftToolchainStatus`. - **Spike first (½ day, do before committing):** prove a `swiftc → .so → dlopen` snippet resolves Lyrebird's symbols on the VM against the shared `libLyrebird.so`. Swift-on-Linux cross-`.so` symbol/metadata resolution is the one real unknown; go/no-go for the whole path. - **Editor UI (`LyrebirdLiveCompileUI`, ~3.1k LOC) — depends on Must-do 1.** Built on `NSTextView` (`ScratchTextView`, `NSViewRepresentable`, AppKit menus) + hand-rolled highlighter/gutter/LSP-completion/position-mapping. Two landings: - **Minimal (modest):** a `LyrebirdWindow` hosting SwiftOpenUI `TextEditor` + Run/Stop + console + the ported `LyrebirdScopeView`. No highlighting/completion. - **Full parity (large, fork work):** wrap **GtkSourceView** (GTK's code editor — syntax highlighting, gutter, completion, UTF-8) as a new SwiftOpenUI widget, rewire `ScratchModel`, feed it the LSP completions. GtkSourceView gives the highlighting/gutter/completion for *less* custom code than the AppKit version, but wrapping it in SwiftOpenUI is a genuine new-backend-widget project. **Scratch-window sequencing:** engine spike → engine port (anytime) → Must-do 1 → minimal scratch window → (optional) full GtkSourceView editor. --- ## Cross-cutting (applies to all three) - **Fork discipline.** Any SwiftOpenUI gap (standalone window API, GtkSourceView widget) is a fork commit: push `joshpar/SwiftOpenUI` `develop`, bump the `Package.swift` revision, validate on the VM. Keep each a clean upstreamable delta (see `FORK_PATCH_PLAN.md`). - **No Linux UI CI.** Core CI skips the UI (needs GTK). Before calling any of this "done", add at least a Linux **build** job for the UI + demo so regressions surface (a compile-only GTK4 job via the `swift:6.x` + `libgtk-4-dev` container noted in `linux-ci-x86-verification`). - **Validate on display, not just compile.** The Process/EventList/Message-family views compile but aren't exercised on the VM yet; fold them into the demo while doing Must-do 1 (they're what `presentInWindow` opens). ## Definition of "Linux at parity" (the checklist this plan satisfies) 1. ✅ `LyrebirdWindow` opens/manages GTK windows; `presentInWindow` works for every view; the EngineStatus "Open Window" buttons return. 2. ✅ `Mouse.X/Y/Click` generators respond to the pointer on Linux (window-local baseline). 3. ◐ Fork on a tagged release + UI has a Linux build job. The Linux CI job is in (`bitbucket-pipelines.yml` — one GTK-enabled build+test step compiles the UI graph). The fork is pinned to its first tagged release, `joshpar/SwiftOpenUI` `0.1.0`, instead of a bare commit. Still open: upstreaming the GTK4 patches to `codelynx/SwiftOpenUI` (externally gated — one maintainer, a prior PR went stale), after which the pin can move to an upstream version. 4. ✅ Ported views exercised on display (VM-confirmed): EngineStatus (+ node-tree / messages detached windows), the mouse demo, and — via `LYREBIRD_DEMO=events` — the Process/EventList family (`LyrebirdEventListPerformanceView` inline, hosting `LyrebirdEventListView` + per-process `LyrebirdProcessView` strips, plus `presentInWindow` for EventListPerformance / EventList / RunningProcesses). 5. (Stretch) live-coding scratchpad usable on Linux — not started.