Files
synchronicity/docs/desktop-shell-design.md

7.6 KiB

Desktop App Shell Design

Overview

A thin presentation layer for the desktop version of the app, sitting above the shared UniFFI library and below application-specific UI components. The shell is not a general-purpose GUI toolkit — it provides exactly what this app needs, with full control over the render pipeline.

Dependency Stack

Concern Crate Notes
Window / event loop / IME winit Handles OS window, input events, IME pre-edit
GPU rendering wgpu Cross-platform; Metal on macOS, Vulkan/GL on Linux
Layout taffy Flexbox/grid; treat as a black box
Text shaping & layout cosmic-text Built on harfbuzz + fontconfig
Accessibility tree accesskit + accesskit-winit Must be maintained alongside widget tree
Clipboard arboard Cross-platform, solid
Icon rendering resvg Render SVGs to wgpu textures at startup
Shared library bridge uniffi (existing) Unchanged; desktop platform layer adapts these types

Reference implementations: Read Floem's src/ for winit event loop integration, IME event handling, and AccessKit wiring before writing any of this. Not copying the architecture, just learning from solved problems.


Architectural Layers

┌─────────────────────────────────────┐
│         Application Components       │  PDF viewer, audio player,
│   (notes editor, sidebar, etc.)      │  text editor, file tree...
├─────────────────────────────────────┤
│        Desktop Platform Layer        │  Mirrors role of Kotlin platform
│                                      │  code; adapts UniFFI types for
│                                      │  reactive GUI; handles desktop
│                                      │  lifecycle (focus, suspend, etc.)
├─────────────────────────────────────┤
│           Shell / Widget Layer       │  Component model, reactive state,
│                                      │  input routing, accessibility tree
├─────────────────────────────────────┤
│        Render Pipeline               │  winit + wgpu + cosmic-text +
│                                      │  taffy + resvg
├─────────────────────────────────────┤
│         Shared UniFFI Library        │  Domain model, persistence,
│          (existing, unchanged)       │  business logic
└─────────────────────────────────────┘

Component Model

Core Trait

trait View {
    type State;
    fn layout(&self, state: &Self::State, taffy: &mut TaffyTree) -> NodeId;
    fn render(&self, state: &Self::State, ctx: &mut RenderCtx);
    fn handle_event(&self, state: &mut Self::State, event: &InputEvent) -> EventResult;
    fn accessibility(&self, state: &Self::State, builder: &mut AccessNodeBuilder);
}

Components own their local state. No global message bus. No forced single update pathway. Parent components compose children explicitly.

Reactive State

Start simple: explicit mark_dirty() calls + a dirty-flag invalidation pass over the widget tree. Add fine-grained reactivity only where profiling shows it's needed. The domain state is in the shared library; the reactive layer's job is mostly "observe UniFFI state changes, re-render affected components."

Local vs Shared UI State

State Location Rationale
Theme (light/dark) Shared library settings store Cross-platform, user expectation
Last open document / scroll position Shared library settings store Relationship to user's data
Sidebar collapsed/expanded Shared library settings store Worth persisting; may have Android analog
Divider drag positions Desktop-local config Pure chrome, not worth sharing
Session-only transient state In-memory component state Never persisted

Heuristic: if losing it on reinstall would feel like losing something, it goes in the shared store. If it's just window chrome, keep it local or ephemeral.


Render Pipeline

Each frame:

  1. Layout pass — run taffy over dirty subtrees
  2. Render pass — traverse widget tree, emit wgpu draw calls
  3. Accessibility pass — update AccessKit tree for changed nodes (must stay in sync; not optional)

Custom wgpu draw calls are first-class — components can drop down to raw GPU code when needed (e.g. PDF page compositing, video frames).

Text

  • cosmic-text for all shaping and layout (handles non-Latin, RTL, mixed scripts)
  • For display text: cosmic-text renders glyphs to an atlas, blit via wgpu
  • For editable text: own the editing model separately (ropey for the rope), use cosmic-text only for rendering. Do not conflate the two.

Icons

Render SVG assets to wgpu textures via resvg at startup. Consistent across Linux and macOS. Same source SVGs can be shared with Android assets where practical.


Platform Adaptation Layer

Mirrors the role of the Kotlin platform code on Android. Responsibilities:

  • Subscribe to shared library state changes and translate to component state updates
  • Handle desktop-specific lifecycle (window focus/unfocus, system sleep, file associations, drag-and-drop)
  • Own desktop-local config persistence (separate from the shared library settings store)
  • Provide mock implementations of UniFFI interfaces for GUI-layer testing

This layer should be small. Android has aggressive lifecycle management that forces a lot of adaptation code; desktop does not.


IME Handling

This is fiddly. Key points:

  • winit emits Ime::Preedit and Ime::Commit events
  • The pre-edit string must be rendered inline at the cursor position with underline decoration
  • cosmic-text cursor positioning needs to account for pre-edit length
  • Study Floem's IME handling before writing this; the bugs are subtle

IME support is non-negotiable given the app's text-handling requirements.


Build Order

Build in this sequence to validate the stack incrementally:

  1. PDF viewer — winit window → wgpu swapchain → mupdf page → texture → blit. No widget tree yet. Validates the render pipeline. Bounded scope.
  2. Basic widget layer — taffy layout, a handful of primitives (rect, text label, button), input routing, dirty/redraw cycle.
  3. Audio player UI — simple reactive state (play/pause, progress), good test of the component model without text complexity.
  4. File tree sidebar — recursive widget structure, local collapsed/expanded state, scroll, hover/click, icons. Validates the component model for real use. Clear spec since it's already implemented twice (Slint + Compose).
  5. Text editor — last, because it requires cosmic-text deeply integrated, an editing model, IME, and accessibility. Make architectural decisions here after the simpler components have established patterns.

Accessibility

AccessKit is not optional and not an afterthought. Maintain the accessibility tree in sync with the widget tree throughout. Estimated ~20% of total widget layer effort. Every new widget type needs an accessibility() implementation.


What This Is Not

  • Not a general-purpose GUI toolkit
  • Not trying to solve problems the app doesn't have
  • Not reimplementing layout (taffy) or text shaping (cosmic-text/harfbuzz)
  • Not fighting the platform — drop to raw wgpu or native APIs whenever the abstraction is in the way