From 93e97d5f80ff818364e3f912b27b87d059f405da Mon Sep 17 00:00:00 2001 From: Aster Date: Thu, 9 Apr 2026 00:39:23 +0800 Subject: [PATCH 1/2] fix: handle macOS application reopen event to show main window --- src-tauri/src/app_runtime.rs | 4 ++++ src-tauri/src/app_runtime_events.rs | 24 ++++++++++++++++++++++-- 2 files changed, 26 insertions(+), 2 deletions(-) diff --git a/src-tauri/src/app_runtime.rs b/src-tauri/src/app_runtime.rs index f9ff9ad..d0f23cf 100644 --- a/src-tauri/src/app_runtime.rs +++ b/src-tauri/src/app_runtime.rs @@ -125,6 +125,10 @@ fn configure_setup(builder: Builder) -> Builder { fn handle_run_event(app_handle: &tauri::AppHandle, event: RunEvent) { match app_runtime_events::run_event_action(&event) { + app_runtime_events::RunEventAction::ShowMainWindow => { + append_desktop_log("application reopen requested, showing main window"); + window::actions::show_main_window(app_handle, DEFAULT_SHELL_LOCALE, append_desktop_log); + } app_runtime_events::RunEventAction::HandleExitRequested => { if let RunEvent::ExitRequested { api, .. } = event { lifecycle::events::handle_exit_requested(app_handle, &api); diff --git a/src-tauri/src/app_runtime_events.rs b/src-tauri/src/app_runtime_events.rs index 8d1e5a9..07d9206 100644 --- a/src-tauri/src/app_runtime_events.rs +++ b/src-tauri/src/app_runtime_events.rs @@ -17,6 +17,7 @@ pub(crate) enum PageLoadAction { #[derive(Debug, Clone, Copy, PartialEq, Eq)] pub(crate) enum RunEventAction { None, + ShowMainWindow, HandleExitRequested, HandleExit, } @@ -63,8 +64,21 @@ pub(crate) fn page_load_action( } } +pub(crate) fn reopen_event_action(has_visible_windows: bool) -> RunEventAction { + if has_visible_windows { + RunEventAction::None + } else { + RunEventAction::ShowMainWindow + } +} + pub(crate) fn run_event_action(event: &RunEvent) -> RunEventAction { match event { + #[cfg(target_os = "macos")] + RunEvent::Reopen { + has_visible_windows, + .. + } => reopen_event_action(*has_visible_windows), RunEvent::ExitRequested { .. } => RunEventAction::HandleExitRequested, RunEvent::Exit => RunEventAction::HandleExit, _ => RunEventAction::None, @@ -74,8 +88,8 @@ pub(crate) fn run_event_action(event: &RunEvent) -> RunEventAction { #[cfg(test)] mod tests { use super::{ - main_window_action, page_load_action, run_event_action, MainWindowAction, PageLoadAction, - RunEventAction, + main_window_action, page_load_action, reopen_event_action, run_event_action, + MainWindowAction, PageLoadAction, RunEventAction, }; use tauri::{webview::PageLoadEvent, RunEvent}; @@ -119,6 +133,12 @@ mod tests { ); } + #[test] + fn reopen_event_action_shows_main_window_only_when_no_window_is_visible() { + assert_eq!(reopen_event_action(false), RunEventAction::ShowMainWindow); + assert_eq!(reopen_event_action(true), RunEventAction::None); + } + #[test] fn run_event_action_maps_exit_events() { assert_eq!( From 757a405a81d72bf1912d91abafa03d9d41abd9ea Mon Sep 17 00:00:00 2001 From: Aster Date: Thu, 9 Apr 2026 12:11:56 +0800 Subject: [PATCH 2/2] fix: Guard macOS reopen logic with cfg --- src-tauri/src/app_runtime.rs | 1 + src-tauri/src/app_runtime_events.rs | 10 ++++++++-- 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/src-tauri/src/app_runtime.rs b/src-tauri/src/app_runtime.rs index d0f23cf..1aa8286 100644 --- a/src-tauri/src/app_runtime.rs +++ b/src-tauri/src/app_runtime.rs @@ -125,6 +125,7 @@ fn configure_setup(builder: Builder) -> Builder { fn handle_run_event(app_handle: &tauri::AppHandle, event: RunEvent) { match app_runtime_events::run_event_action(&event) { + #[cfg(target_os = "macos")] app_runtime_events::RunEventAction::ShowMainWindow => { append_desktop_log("application reopen requested, showing main window"); window::actions::show_main_window(app_handle, DEFAULT_SHELL_LOCALE, append_desktop_log); diff --git a/src-tauri/src/app_runtime_events.rs b/src-tauri/src/app_runtime_events.rs index 07d9206..d932706 100644 --- a/src-tauri/src/app_runtime_events.rs +++ b/src-tauri/src/app_runtime_events.rs @@ -17,6 +17,7 @@ pub(crate) enum PageLoadAction { #[derive(Debug, Clone, Copy, PartialEq, Eq)] pub(crate) enum RunEventAction { None, + #[cfg(target_os = "macos")] ShowMainWindow, HandleExitRequested, HandleExit, @@ -64,6 +65,7 @@ pub(crate) fn page_load_action( } } +#[cfg(target_os = "macos")] pub(crate) fn reopen_event_action(has_visible_windows: bool) -> RunEventAction { if has_visible_windows { RunEventAction::None @@ -88,11 +90,14 @@ pub(crate) fn run_event_action(event: &RunEvent) -> RunEventAction { #[cfg(test)] mod tests { use super::{ - main_window_action, page_load_action, reopen_event_action, run_event_action, - MainWindowAction, PageLoadAction, RunEventAction, + main_window_action, page_load_action, run_event_action, MainWindowAction, PageLoadAction, + RunEventAction, }; use tauri::{webview::PageLoadEvent, RunEvent}; + #[cfg(target_os = "macos")] + use super::reopen_event_action; + #[test] fn main_window_action_ignores_non_main_windows() { assert_eq!( @@ -133,6 +138,7 @@ mod tests { ); } + #[cfg(target_os = "macos")] #[test] fn reopen_event_action_shows_main_window_only_when_no_window_is_visible() { assert_eq!(reopen_event_action(false), RunEventAction::ShowMainWindow);