Skip to content

Window per database: route backend emits to the matching window instead of "main" #2033

Closed

Description

@mstomar125

Context

Split out of #1536, which flagged this piece as optional and separable ("split if it grows"). #1536 keeps the tray database menu; this issue is the multi-window half.

Problem

The app is single-window and every backend emit targets get_webview_window("main"). With one daemon serving N databases (ADR-053), a user cannot have two databases open side by side, and any event that carries a database_id has nowhere else to go.

Scope

  • Window label = database id; each window pins its own database id and stamps it on calls.
  • Replace hardcoded get_webview_window("main") emits in packages/desktop-app/src-tauri/src/lib.rs with routing by the event's database_id to the matching window; fall back to the focused window when an event carries no id.
  • Persist window state (size/position) per database.

The database_id is already present in the event streams, so this is routing existing data rather than new plumbing.

Acceptance

  • Two windows on two databases operate concurrently with no event cross-talk
  • An event carrying a database_id reaches only that database's window
  • An event with no id reaches the focused window
  • Window size/position persist per database
  • bun run test:all green, bun run quality:fix clean

Non-goals

Per-window theming; new sync features; anything beyond routing existing events.

Split from #1536.

Activity

  1. added
    enhancement
    on Aug 11, 2026
  2. mstomar125 commented on Aug 20, 2026

    @mstomar125
    ContributorAuthor

    Done via #2184 (merged ab5bb47f).

    What shipped

    • window_routing.rs (new): WindowDatabaseRegistry (a live window-label -> database-id pin map) and emit_routed, the replacement for every previous app.get_webview_window("main") + .emit(...) call. Routes an event carrying a non-empty database_id to only the window(s) pinned to it; falls back to the focused window (deterministic default when nothing is focused) for events with no id. Falls back to broadcast only when no window exists yet, or no window has pinned to a database yet (the real startup gap before the frontend's async pin_window_database call lands) — never a silent drop.
    • Replaced every get_webview_window("main") site in lib.rs/commands/settings.rs, and watcher.rs's node:*/relationship:* broadcasts (which used to reach every open window regardless of database), with emit_routed.
    • Fixed a related pre-existing bug the first test against this module caught: handle_relaunch's tray:select-database emit called window.emit(...) believing it was scoped to that window — Emitter::emit's default body always broadcasts regardless of the object it's called on (confirmed against the tauri 2.11 source). Now uses Emitter::emit_to(label, ...).
    • A window's Tauri label is its database id wherever known at creation time; the bootstrap window (database resolved asynchronously by the frontend) keeps its static "main" label but is tracked in the registry via the new pin_window_database command, called from databaseStore's load()/switchTo().
    • window_state.rs (new): per-database window size/position persistence — atomic JSON write (shared atomic_file::write_json, also now used by preferences.rs), serialized behind a mutex so concurrent saves can't race, restored via pin_window_database (with a plausibility check rejecting degenerate saved sizes).

    Acceptance criteria

    • Two windows on two databases operate concurrently with no event cross-talk — proven end-to-end in tests/window_routing_no_cross_talk_test.rs: a real spawned daemon, two real registered databases, two concurrent watcher::run tasks, two mock windows pinned to the two databases — a window pinned to database A never receives database B's events.
    • An event carrying a database_id reaches only that database's window — emit_routed, unit-tested via the pure resolve_targets function and integration-tested against real Tauri window/event APIs.
    • An event with no id reaches the focused window — same routing function, focused-window fallback with a deterministic no-focus default.
    • Window size/position persist per database — window_state.rs, save/restore round-trip tested, concurrent-save safety tested.
    • bun run test:all green, bun run quality:fix clean — confirmed on every push through the pre-push gate (which runs test:all plus the full cargo test -p nodespace-app Tauri-seam suite).

    Scope note

    The daemon already supports genuine concurrent multi-database live streaming (independently opened WatchNodes streams per database), but this PR does not wire up N concurrent watcher::run tasks in production or make the shared GrpcClient per-window for outbound commands — both are larger changes, and there is no way to open a second window in the shipped app yet regardless (tauri-plugin-single-instance, from #2180, dedupes any second launch into an in-place switch). This PR is the client-side event-routing foundation; wiring real concurrent windows is a follow-up once an "open a second window" UX exists.

    Not independently verified (no GUI console)

    Actual multi-window rendering — two real OS windows on screen, resized/dragged, showing different databases — needs a real desktop session, and there's no way to open a second window from the shipped UI yet regardless. Covered instead: the routing decision in isolation (pure-function unit tests), the real Tauri event/window APIs wired correctly (mock_app() tests), and the real production watcher + routing path against a real daemon with two real databases (the integration test above).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementui

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions