Repository navigation
Focus and switch the running app when picking a database from the tray - #2180
Merged
Merged
Conversation
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.
This was referenced Aug 20, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Second half of #2040. Closes it.
Problem
open_uichecked 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 insrc-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_uinow always spawns a fresh UI process. The requested database rides two channels so either launch path can read it: the existingINITIAL_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.tauri-plugin-single-instanceas its first plugin. Its relaunch handler focuses the main window (unminimize+show+set_focus— the cross-process equivalent ofNSRunningApplication::activateon every desktop platform Tauri targets) and, when a database was requested, emitstray:select-databasewith the id.tray:select-databaseinapp-shell.svelteand callsdatabaseStore.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
openmarker) 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 touchpackages/agent.Tests
packages/daemon/src/tray.rs:build_ui_commandextracted fromopen_uiso 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_argunit tests cover present/absent/valueless/blank/flag-shaped-value argv.bun run test:allgreen (4744 frontend tests, 24 skill tests, every Rust workspace crate) plus a separate fullcargo test -p nodespace-app -- --test-threads=1run (rust:test excludes that crate) — all green, no regressions.bun run quality:fixclean. (It also reformatted two unrelated pre-existing-drift files inpackages/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/showare 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]