Skip to content

fix(macos): restore the hidden window on Dock-icon click (#248) - #378

Open
aniketshukla1 wants to merge 1 commit into
andrewyng:mainfrom
aniketshukla1:fix/248-macos-dock-reopen-window
Open

fix(macos): restore the hidden window on Dock-icon click (#248)#378
aniketshukla1 wants to merge 1 commit into
andrewyng:mainfrom
aniketshukla1:fix/248-macos-dock-reopen-window

Conversation

@aniketshukla1

Copy link
Copy Markdown

Summary

Closes #248.

After the window is dismissed, clicking the Dock icon activates OpenWorker (the menu bar switches) but no window appears, and open -a OpenWorker doesn't bring it back — only open -n (a brand-new instance) does.

Cause

Close-to-tray hides the main window rather than quitting:

win.on_window_event(move |event| {
    if let WindowEvent::CloseRequested { api, .. } = event {
        let _ = w.hide();
        api.prevent_close();
    }
});

But the top-level RunEvent handler only dealt with ExitRequested/Exit. On macOS a Dock-icon click fires RunEvent::Reopen (the applicationShouldHandleReopen delegate callback), and Tauri does nothing with it by default — so a hidden window is never reshown.

Fix

Handle RunEvent::Reopen by reshowing the main window through the existing show_main helper — the exact path the tray Open item already uses:

.run(|app, event| {
    #[cfg(target_os = "macos")]
    {
        if let RunEvent::Reopen { .. } = &event {
            show_main(app);   // unminimize + show + set_focus
        }
    }
    // ...existing Exit handling...
});

Reopen is macOS-only, so the arm is #[cfg(target_os = "macos")]. The { .. } pattern intentionally ignores the has_visible_windows field: reshowing/focusing on any reopen is the desired behavior and is a harmless no-op when a window is already up.

Verification

I confirmed the API against the pinned tauri 2.11.5 source in the cargo cache:

// tauri-2.11.5/src/app.rs
#[non_exhaustive]
#[cfg(target_os = "macos")]
Reopen {
    /// Indicates whether the NSApplication object found any visible windows in your application.
    has_visible_windows: bool,
}

So the variant is macOS-gated (the #[cfg] wrapper is required), it's a non-exhaustive struct variant (the { .. } pattern is correct), and it's emitted exactly for the Dock-icon reopen this issue is about. show_main(&AppHandle) is already called identically by the tray menu handler.

I did not run a full cargo build locally: generate_context!() embeds frontendDist (../dist), so compiling requires a complete npm run build + Tauri toolchain pass. The change is a 3-line addition that reuses an existing helper and mirrors the existing RunEvent matching in the same closure; happy to adjust if CI surfaces anything.

Close-to-tray hides the main window instead of quitting (on_window_event's
CloseRequested handler), but the RunEvent handler only dealt with Exit. macOS
fires RunEvent::Reopen when the Dock icon is clicked, and Tauri does nothing
with it by default — so the app activates (menu bar switches) but no window
appears, and the user has no way to get it back short of relaunching.

Handle RunEvent::Reopen by reshowing the main window via the existing show_main
helper (unminimize + show + set_focus) — the same path the tray "Open" item
already uses. Reopen is macOS-only, so the arm is #[cfg(target_os = "macos")].

Verified against the pinned tauri 2.11.5 source: RunEvent::Reopen is a
macOS-gated non_exhaustive struct variant emitted for applicationShouldHandleReopen
(the Dock-icon click).
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.

Main window cannot be restored after prolonged idle period on macOS

1 participant