Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions src-tauri/src/app_runtime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,11 @@ fn configure_setup(builder: Builder<tauri::Wry>) -> Builder<tauri::Wry> {

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);
}
app_runtime_events::RunEventAction::HandleExitRequested => {
if let RunEvent::ExitRequested { api, .. } = event {
lifecycle::events::handle_exit_requested(app_handle, &api);
Expand Down
26 changes: 26 additions & 0 deletions src-tauri/src/app_runtime_events.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ pub(crate) enum PageLoadAction {
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) enum RunEventAction {
None,
#[cfg(target_os = "macos")]
ShowMainWindow,
HandleExitRequested,
HandleExit,
}
Expand Down Expand Up @@ -63,8 +65,22 @@ 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
} else {
RunEventAction::ShowMainWindow
}
}
Comment on lines +69 to +75
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

medium

在 macOS 上,点击 Dock 图标的标准行为是确保主窗口可见并获得焦点。目前的实现使用了 has_visible_windows 来判断是否执行 ShowMainWindow,这在多窗口场景下可能不够健壮:如果用户打开了辅助窗口(如设置窗口)但隐藏了主窗口,点击 Dock 图标将不会恢复主窗口。建议考虑移除此判断,让 show_main_window 始终执行(该函数内部通常是幂等的,且会自动处理聚焦),或者明确这是否是针对单窗口场景的有意简化。


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,
Expand All @@ -79,6 +95,9 @@ mod tests {
};
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!(
Expand Down Expand Up @@ -119,6 +138,13 @@ 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);
assert_eq!(reopen_event_action(true), RunEventAction::None);
}

#[test]
fn run_event_action_maps_exit_events() {
assert_eq!(
Expand Down