Skip to content

Commit c6ca243

Browse files
fix: handle macOS application reopen event to show main window (AstrBotDevs#113)
* fix: handle macOS application reopen event to show main window * fix: Guard macOS reopen logic with cfg
1 parent 09ab410 commit c6ca243

2 files changed

Lines changed: 31 additions & 0 deletions

File tree

src-tauri/src/app_runtime.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,11 @@ fn configure_setup(builder: Builder<tauri::Wry>) -> Builder<tauri::Wry> {
125125

126126
fn handle_run_event(app_handle: &tauri::AppHandle, event: RunEvent) {
127127
match app_runtime_events::run_event_action(&event) {
128+
#[cfg(target_os = "macos")]
129+
app_runtime_events::RunEventAction::ShowMainWindow => {
130+
append_desktop_log("application reopen requested, showing main window");
131+
window::actions::show_main_window(app_handle, DEFAULT_SHELL_LOCALE, append_desktop_log);
132+
}
128133
app_runtime_events::RunEventAction::HandleExitRequested => {
129134
if let RunEvent::ExitRequested { api, .. } = event {
130135
lifecycle::events::handle_exit_requested(app_handle, &api);

src-tauri/src/app_runtime_events.rs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ pub(crate) enum PageLoadAction {
1717
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
1818
pub(crate) enum RunEventAction {
1919
None,
20+
#[cfg(target_os = "macos")]
21+
ShowMainWindow,
2022
HandleExitRequested,
2123
HandleExit,
2224
}
@@ -63,8 +65,22 @@ pub(crate) fn page_load_action(
6365
}
6466
}
6567

68+
#[cfg(target_os = "macos")]
69+
pub(crate) fn reopen_event_action(has_visible_windows: bool) -> RunEventAction {
70+
if has_visible_windows {
71+
RunEventAction::None
72+
} else {
73+
RunEventAction::ShowMainWindow
74+
}
75+
}
76+
6677
pub(crate) fn run_event_action(event: &RunEvent) -> RunEventAction {
6778
match event {
79+
#[cfg(target_os = "macos")]
80+
RunEvent::Reopen {
81+
has_visible_windows,
82+
..
83+
} => reopen_event_action(*has_visible_windows),
6884
RunEvent::ExitRequested { .. } => RunEventAction::HandleExitRequested,
6985
RunEvent::Exit => RunEventAction::HandleExit,
7086
_ => RunEventAction::None,
@@ -79,6 +95,9 @@ mod tests {
7995
};
8096
use tauri::{webview::PageLoadEvent, RunEvent};
8197

98+
#[cfg(target_os = "macos")]
99+
use super::reopen_event_action;
100+
82101
#[test]
83102
fn main_window_action_ignores_non_main_windows() {
84103
assert_eq!(
@@ -119,6 +138,13 @@ mod tests {
119138
);
120139
}
121140

141+
#[cfg(target_os = "macos")]
142+
#[test]
143+
fn reopen_event_action_shows_main_window_only_when_no_window_is_visible() {
144+
assert_eq!(reopen_event_action(false), RunEventAction::ShowMainWindow);
145+
assert_eq!(reopen_event_action(true), RunEventAction::None);
146+
}
147+
122148
#[test]
123149
fn run_event_action_maps_exit_events() {
124150
assert_eq!(

0 commit comments

Comments
 (0)