Skip to content

Focus and switch the running app when picking a database from the tray - #2180

Merged
mstomar125 merged 2 commits into
mainfrom
worktree-issue-2040-tray-live-refresh
Aug 20, 2026
Merged

mstomar125 merged 2 commits into
mainfrom
worktree-issue-2040-tray-live-refresh

Conversation

@mstomar125

Copy link
Copy Markdown
Contributor

Second half of #2040. Closes it.

Problem

open_ui checked whether a UI process it had spawned earlier was still alive and, if so, returned early — a no-op click. Worse, if the app was launched outside the tray (Dock, Spotlight, a prior run), there was no child to check at all, so the same click spawned a second app process on the chosen database. Nothing dedupes UI processes in src-tauri.

Change

Rather than have the daemon track UI-process liveness — which it fundamentally can't do reliably, since a process it never spawned is invisible to it — dedup is delegated to the app itself via tauri-plugin-single-instance:

  • open_ui now always spawns a fresh UI process. The requested database rides two channels so either launch path can read it: the existing INITIAL_DATABASE_ENV, read by a cold launch (no other instance running) at startup, and a new --database <id> CLI argument — the only thing visible to an already-running instance's relaunch handler, since a spawned-then-immediately-exited process's environment isn't. The spawned child is reaped on a background OS thread so a dedup'd relaunch that exits almost immediately doesn't zombie.
  • The desktop app registers tauri-plugin-single-instance as its first plugin. Its relaunch handler focuses the main window (unminimize + show + set_focus — the cross-process equivalent of NSRunningApplication::activate on every desktop platform Tauri targets) and, when a database was requested, emits tray:select-database with the id.
  • The frontend listens for tray:select-database in app-shell.svelte and calls databaseStore.switchTo(id) — the same path an in-app switcher click already uses (flushes pending saves, re-points routed clients, clears caches, resets the workspace).

This gets both acceptance boxes at once: exactly one UI process ever survives regardless of launch path (the app-level lock dedupes a genuinely-second launch instead of the daemon guessing), and picking a database while the app is running switches it in place and focuses the window — including a plain "Open NodeSpace" click while already running, which previously no-op'd and now raises the window.

Context

Live tray-submenu refresh (registry/open-set changes pushed without a daemon restart, and the open marker) was the first half of #2040 and already landed in #2042. This PR is Rust-only on the daemon side plus Rust+Svelte on the desktop-app side; it does not touch packages/agent.

Tests

  • packages/daemon/src/tray.rs: build_ui_command extracted from open_ui so its env/args are assertable without spawning a process — tests cover the plain-open (no env, no arg) and database-pick (both env and --database <id>) cases.
  • packages/desktop-app/src-tauri/src/lib.rs: parse_relaunch_database_arg unit tests cover present/absent/valueless/blank/flag-shaped-value argv.
  • Full bun run test:all green (4744 frontend tests, 24 skill tests, every Rust workspace crate) plus a separate full cargo test -p nodespace-app -- --test-threads=1 run (rust:test excludes that crate) — all green, no regressions.
  • bun run quality:fix clean. (It also reformatted two unrelated pre-existing-drift files in packages/agent; reverted those before committing to keep this diff scoped and out of that lane.)

Not independently verified here

Native cross-process window focus/activation isn't interactively testable from this headless session (no GUI console). The mechanism is standard (this is the same technique tauri-plugin-single-instance's own docs describe, and what VS Code/Slack/Chrome use for the same purpose), set_focus/unminimize/show are the documented Tauri APIs for it, and everything up to and including the IPC/event wiring is compiled, unit-tested, and integration-tested — but the actual "does the window visibly come to the front" behavior wants a real desktop session to confirm.

Co-Authored-By: Claude Sonnet 5 [email protected]

closes #2040)

open_ui previously no-op'd (or, when the app was launched outside the
tray, silently spawned a second process) whenever it tried to check
whether a UI process was already alive — the daemon has no reliable
way to know that, especially for a process it never spawned itself.

Delegate dedup to the app instead of tracking it in the daemon:
tauri-plugin-single-instance now guards the desktop app, so open_ui
always spawns a fresh UI process and a genuinely-second launch detects
the running instance, forwards its argv, and exits immediately. The
requested database rides both the existing INITIAL_DATABASE_ENV (read
by a cold launch at startup) and a new `--database <id>` CLI arg (the
only thing visible to an already-running instance's relaunch handler,
since a spawned-then-immediately-exited process's environment is not).

The relaunch handler focuses the main window (unminimize + show +
set_focus, the cross-process equivalent of NSRunningApplication::
activate on every desktop platform Tauri targets) and, when a database
was requested, emits `tray:select-database` so the frontend switches
in place through the same databaseStore.switchTo path an in-app
switcher click already uses.

Live tray-submenu refresh (registry/open-set changes pushed to the
tray without a daemon restart, and the `open` marker) already landed
in #2042; this closes the second half of #2040.
…nism

- Validate the tray-supplied database id against the registered list
  before committing to a switch in databaseStore.switchTo, mirroring
  the registered() guard load() already applies to this exact input.
  Previously an id for a database removed (or otherwise not
  registered) between the tray click and the relaunch event arriving
  would clear every cache and reset the workspace before the daemon's
  per-request NOT_FOUND ever surfaced, with no rollback.

- Add a pull-based fallback for the tray:select-database event: Tauri
  does not buffer/replay an event emitted with zero current listeners,
  and the frontend's listener only exists once app-shell.svelte has
  mounted, so a relaunch landing in that narrow boot window would
  silently drop the switch (the window still focused correctly).
  handle_relaunch now also stashes the id in a process-wide slot that
  the frontend pulls once via a new take_pending_tray_database_selection
  command right after registering the listener; switchTo is idempotent
  against the value the live event also happened to deliver.

- Log unminimize/show/set_focus failures in handle_relaunch instead of
  swallowing them silently, matching the emit's existing warn-on-error.

- Soften the open_ui/plugin-registration doc comments: this plugin's
  macOS backend claims its lock with a connect-then-bind probe rather
  than one atomic OS primitive (Windows uses a named mutex, Linux a
  D-Bus name claim), so two processes launched in that scheduling gap
  can theoretically both survive. Not fixable in this codebase; now
  documented rather than overstated as an absolute guarantee.

- Add test coverage for handle_relaunch (generic over the Tauri
  runtime, like graceful_shutdown/handle_run_event, so it's testable
  against tauri::test::mock_app()): no-main-window early return, and
  the full stash/pull/consume cycle for the fallback.

Found by a fresh adversarial review agent against the real PR diff.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant