Skip to content

fix: prevent duplicate tray instances on relaunch#1458

Merged
shm11C3 merged 1 commit into
developfrom
fix/windows-single-instance-relaunch
May 5, 2026
Merged

fix: prevent duplicate tray instances on relaunch#1458
shm11C3 merged 1 commit into
developfrom
fix/windows-single-instance-relaunch

Conversation

@shm11C3

@shm11C3 shm11C3 commented May 5, 2026

Copy link
Copy Markdown
Owner

Summary

  • Add Tauri's single-instance plugin so a second launch is routed to the existing process.
  • Restore the existing main window when a second launch attempt is received.

Why

On Windows, close-to-tray keeps the first process alive. Launching the app again created a second process, which registered another tray icon and caused tray icon duplication.

This PR is stacked on #1457 so it can reuse lifecycle::restore_main_window without duplicating the macOS Dock reopen changes.

Validation

  • cargo check --manifest-path src-tauri/Cargo.toml
  • cargo test --manifest-path src-tauri/Cargo.toml lifecycle::tests

Summary by CodeRabbit

  • New Features
    • Added single-instance behavior: launching the app while it's already running now restores the existing window instead of opening a duplicate instance.

@coderabbitai

coderabbitai Bot commented May 5, 2026

Copy link
Copy Markdown
Contributor

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yml

Review profile: CHILL

Plan: Pro

Run ID: 7c99cf84-4252-4ac7-96b1-80d23763ec96

📥 Commits

Reviewing files that changed from the base of the PR and between 59b4f49 and 9ef61ed.

⛔ Files ignored due to path filters (1)
  • Cargo.lock is excluded by !**/*.lock
📒 Files selected for processing (3)
  • src-tauri/Cargo.toml
  • src-tauri/src/lib.rs
  • src-tauri/src/lifecycle.rs
✅ Files skipped from review due to trivial changes (2)
  • src-tauri/src/lib.rs
  • src-tauri/src/lifecycle.rs
🚧 Files skipped from review as they are similar to previous changes (1)
  • src-tauri/Cargo.toml

📝 Walkthrough

Walkthrough

This PR implements single-instance app behavior by adding the tauri-plugin-single-instance dependency, initializing it in the Tauri builder with a callback, and implementing a lifecycle hook that restores the main window on subsequent app launch attempts.

Changes

Single-Instance App Behavior

Layer / File(s) Summary
Dependency Addition
src-tauri/Cargo.toml
Adds tauri-plugin-single-instance = "2.4.2" to project dependencies.
Plugin Initialization
src-tauri/src/lib.rs
Plugin is initialized in the Tauri builder chain with a callback that invokes lifecycle::on_second_instance(app) before the setup handler.
Lifecycle Hook
src-tauri/src/lifecycle.rs
New on_second_instance function is exported to restore the main window when a second app launch attempt occurs.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

Possibly related PRs

  • shm11C3/HardwareVisualizer#1457: Adds the lifecycle::restore_main_window utility and on_run_event lifecycle hook that this PR calls from its new on_second_instance handler.

Poem

🐰 Hops of joy, a single gleam,
No more windows—one app's dream!
Second knock? The window springs,
Restored to life with tiny wings!
Tauri hops where once were two.

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 inconclusive)

Check name Status Explanation Resolution
Description check ❓ Inconclusive The PR description covers the summary, motivation, and validation steps, but is missing the Type of Change checklist selection and Testing section required by the template. Add the 'Type of Change' checkbox selection (Bug fix should be checked) and complete the Test Plan section with details on which tests were run.
✅ Passed checks (4 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly and concisely describes the main change: preventing duplicate tray instances when the app is relaunched, which matches the primary objective of the changeset.
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch fix/windows-single-instance-relaunch

Comment @coderabbitai help to get the list of available commands and usage tips.

@github-actions github-actions Bot added bug Something isn't working dependencies Pull requests that update a dependency file configuration hardviz_tauri labels May 5, 2026
@github-actions

github-actions Bot commented May 5, 2026

Copy link
Copy Markdown
Contributor

Rust Tauri Coverage Report

Coverage Details
Filename                                     Regions    Missed Regions     Cover   Functions  Missed Functions  Executed       Lines      Missed Lines     Cover    Branches   Missed Branches     Cover
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
_tests/commands/background_image_test.rs          39                 0   100.00%           6                 0   100.00%          21                 0   100.00%           0                 0         -
_tests/commands/settings_test.rs                 220                 0   100.00%          18                 0   100.00%         165                 0   100.00%           0                 0         -
adapters/tray.rs                                 127               127     0.00%          14                14     0.00%          87                87     0.00%           0                 0         -
adapters/window.rs                               254                69    72.83%          21                 8    61.90%         195                47    75.90%           0                 0         -
app/startup.rs                                   188                87    53.72%          10                 3    70.00%         114                58    49.12%           0                 0         -
commands/background_image.rs                      22                 7    68.18%          11                 5    54.55%          19                 7    63.16%           0                 0         -
commands/hardware.rs                              62                62     0.00%          20                20     0.00%          68                68     0.00%           0                 0         -
commands/settings.rs                             616               616     0.00%         108               108     0.00%         528               528     0.00%           0                 0         -
commands/system.rs                                21                21     0.00%          10                10     0.00%          20                20     0.00%           0                 0         -
commands/ui.rs                                    17                17     0.00%           2                 2     0.00%          13                13     0.00%           0                 0         -
commands/updater.rs                               97                97     0.00%          15                15     0.00%          66                66     0.00%           0                 0         -
enums/error.rs                                   101                10    90.10%           8                 1    87.50%          86                10    88.37%           0                 0         -
enums/hardware.rs                                184                 7    96.20%          15                 1    93.33%         111                 6    94.59%           0                 0         -
enums/settings.rs                                386                16    95.85%          23                 2    91.30%         253                10    96.05%           0                 0         -
infrastructure/database/migration.rs              66                 1    98.48%          10                 0   100.00%          86                 0   100.00%           0                 0         -
lib.rs                                           225               225     0.00%           6                 6     0.00%         143               143     0.00%           0                 0         -
lifecycle.rs                                     254               205    19.29%          31                27    12.90%         186               163    12.37%           0                 0         -
main.rs                                            3                 3     0.00%           1                 1     0.00%           3                 3     0.00%           0                 0         -
models/hardware.rs                               375                83    77.87%          31                12    61.29%         275               100    63.64%           0                 0         -
models/hardware_archive.rs                         8                 0   100.00%           2                 0   100.00%          10                 0   100.00%           0                 0         -
models/settings.rs                               285                 0   100.00%          16                 0   100.00%         252                 0   100.00%           0                 0         -
services/background_image_service.rs             165                96    41.82%          16                10    37.50%          93                59    36.56%           0                 0         -
services/gpu_service.rs                           56                56     0.00%          11                11     0.00%          43                43     0.00%           0                 0         -
services/hardware_service.rs                      85                85     0.00%           4                 4     0.00%          51                51     0.00%           0                 0         -
services/language_service.rs                     101                 0   100.00%          18                 0   100.00%          57                 0   100.00%           0                 0         -
services/memory_service.rs                        12                12     0.00%           3                 3     0.00%           7                 7     0.00%           0                 0         -
services/motherboard_service.rs                   12                12     0.00%           3                 3     0.00%           7                 7     0.00%           0                 0         -
services/network_service.rs                       14                14     0.00%           1                 1     0.00%           8                 8     0.00%           0                 0         -
services/settings_service.rs                     340               156    54.12%          33                15    54.55%         284               142    50.00%           0                 0         -
services/system_service.rs                        22                22     0.00%           2                 2     0.00%          12                12     0.00%           0                 0         -
services/ui_service.rs                            45                45     0.00%           8                 8     0.00%          36                36     0.00%           0                 0         -
tray/surface/mod.rs                                8                 8     0.00%           2                 2     0.00%           5                 5     0.00%           0                 0         -
tray/surface/tauri_surface.rs                     80                80     0.00%           6                 6     0.00%          48                48     0.00%           0                 0         -
tray/widget.rs                                   562                28    95.02%          50                 3    94.00%         505                26    94.85%           0                 0         -
utils/color.rs                                    66                 1    98.48%           4                 0   100.00%          26                 0   100.00%           0                 0         -
utils/file.rs                                    224                 5    97.77%          14                 0   100.00%         144                 4    97.22%           0                 0         -
utils/formatter.rs                                55                 0   100.00%           5                 0   100.00%          39                 0   100.00%           0                 0         -
utils/logger.rs                                   71                71     0.00%           1                 1     0.00%          38                38     0.00%           0                 0         -
utils/tauri.rs                                   138                 0   100.00%          17                 0   100.00%          82                 0   100.00%           0                 0         -
workers/mod.rs                                    40                40     0.00%           2                 2     0.00%          24                24     0.00%           0                 0         -
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
TOTAL                                           5646              2384    57.78%         578               306    47.06%        4210              1839    56.32%           0                 0         -

coderabbitai[bot]
coderabbitai Bot previously approved these changes May 5, 2026
Base automatically changed from fix/macos-dock-reopen-restore to develop May 5, 2026 17:58
@shm11C3 shm11C3 dismissed coderabbitai[bot]’s stale review May 5, 2026 17:58

The base branch was changed.

@shm11C3 shm11C3 force-pushed the fix/windows-single-instance-relaunch branch from 59b4f49 to 9ef61ed Compare May 5, 2026 18:01
@shm11C3 shm11C3 marked this pull request as ready for review May 5, 2026 18:05
Copilot AI review requested due to automatic review settings May 5, 2026 18:05
@shm11C3 shm11C3 enabled auto-merge (squash) May 5, 2026 18:07

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR prevents duplicate tray icon instances on Windows by enforcing a single running Tauri instance and routing subsequent launches to the already-running process, restoring the main window instead of starting a second app process.

Changes:

  • Add tauri-plugin-single-instance and register it in the Tauri builder to prevent multiple concurrent instances.
  • Introduce a lifecycle hook (on_second_instance) that restores the main window when a second launch attempt occurs.
  • Update lockfile to include the new plugin and its dependencies.

Reviewed changes

Copilot reviewed 3 out of 4 changed files in this pull request and generated no comments.

File Description
src-tauri/src/lifecycle.rs Adds on_second_instance to restore the main window when the single-instance plugin reports a second launch attempt.
src-tauri/src/lib.rs Registers the single-instance plugin and wires it to the lifecycle hook to restore the existing window.
src-tauri/Cargo.toml Adds the tauri-plugin-single-instance dependency.
Cargo.lock Records the new dependency resolution for the single-instance plugin.

@shm11C3 shm11C3 merged commit 6055279 into develop May 5, 2026
40 checks passed
@shm11C3 shm11C3 deleted the fix/windows-single-instance-relaunch branch May 5, 2026 18:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working configuration dependencies Pull requests that update a dependency file hardviz_tauri

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants