Skip to content

fix: handle macOS application reopen event to show main window#113

Merged
zouyonghe merged 2 commits intoAstrBotDevs:mainfrom
Aster-amellus:fix/mac-reopen
Apr 9, 2026
Merged

fix: handle macOS application reopen event to show main window#113
zouyonghe merged 2 commits intoAstrBotDevs:mainfrom
Aster-amellus:fix/mac-reopen

Conversation

@Aster-amellus
Copy link
Copy Markdown
Contributor

@Aster-amellus Aster-amellus commented Apr 8, 2026

动机/Motivation

AstrBotDevs/AstrBot#7336

issue 中提到的 Mac 下无法 Reopen 的问题,我在本地环境下也成功复现:
点击左上角红色关闭按钮后,主窗口被隐藏,但应用进程仍然常驻;此时再次点击 Dock
图标无法重新打开主窗口,只能通过顶部状态栏/托盘恢复。

Summary / 改动概述

  • 在桌面运行时事件决策层中新增 ShowMainWindow 语义,避免把平台事件名直接泄漏到业务层。
  • 针对 macOS 的 RunEvent::Reopen { has_visible_windows, .. } 增加处理:当应用当前没有可见窗口时,恢复主窗口显示。
  • 保持现有行为不变:点击左上角红色关闭按钮仍然是“隐藏窗口并常驻”,右键 Quit 仍然是真正退出应用。
  • 补充对应单元测试,覆盖 “无可见窗口时允许 reopen,有可见窗口时不重复处理” 的行为。

Verification / 验证方式

本地复现与验证流程:

  1. 在 macOS 上启动 AstrBot Desktop。
  2. 点击左上角红色关闭按钮,确认主窗口隐藏但应用仍驻留。
  3. 点击 Dock 图标。
  4. 修复前:主窗口不会恢复显示。
  5. 修复后:主窗口可正常恢复显示。
  6. 右键 Quit 仍然可以正常退出应用。
image image

———

Checklist / 检查清单

  • This change is not a breaking change. / 此改动不是破坏性变更。
  • I have verified the change locally (and provided logs/screenshots
    above). / 我已在本地验证并在上方提供日志/截图。
  • If workflows are changed, I attached at least one related Actions run
    link. / 如果修改了工作流,我已附上至少一个相关 Actions 运行链接。
  • If dependencies are updated, lockfiles are updated accordingly (src-
    tauri/Cargo.lock, lockfiles under changed package dirs). / 如果引入或升级依
    赖,已同步更新相关 lock 文件(如 src-tauri/Cargo.lock、对应包目录中的 lock
    文件)。
  • This PR does not include malicious code. / 此 PR 不包含恶意代码。

Summary by Sourcery

Handle macOS application reopen events to restore the main window when no window is visible.

Bug Fixes:

  • Restore the main window on macOS when the app is reopened from the Dock after the main window was previously closed/hidden.

Enhancements:

  • Introduce a platform-agnostic run event action to represent showing the main window instead of leaking platform-specific event names into higher layers.

Tests:

  • Add unit test coverage for the reopen event decision logic, ensuring it only shows the main window when no window is currently visible.

Copilot AI review requested due to automatic review settings April 8, 2026 16:48
Copy link
Copy Markdown

@sourcery-ai sourcery-ai bot left a comment

Choose a reason for hiding this comment

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

Hey - I've reviewed your changes and they look great!


Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

Copy link
Copy Markdown

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

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

Code Review

This pull request introduces logic to handle application reopen events on macOS, such as clicking the Dock icon. It adds a new ShowMainWindow action to the RunEventAction enum and implements a reopen_event_action helper to trigger this action when no windows are currently visible. Feedback indicates that the current implementation might be too restrictive; using has_visible_windows to guard the action could prevent the main window from appearing if only auxiliary windows (like settings) are open. It is suggested to ensure the main window is consistently focused or restored regardless of other visible windows.

Comment on lines +67 to +73
pub(crate) fn reopen_event_action(has_visible_windows: bool) -> RunEventAction {
if has_visible_windows {
RunEventAction::None
} else {
RunEventAction::ShowMainWindow
}
}
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 始终执行(该函数内部通常是幂等的,且会自动处理聚焦),或者明确这是否是针对单窗口场景的有意简化。

Copy link
Copy Markdown

Copilot AI left a comment

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 fixes macOS behavior where clicking the Dock icon after closing (hiding) the main window does not reopen/show the window, by mapping Tauri’s macOS RunEvent::Reopen into an internal runtime action and handling it in the desktop runtime event loop.

Changes:

  • Add a new runtime semantic action RunEventAction::ShowMainWindow to avoid leaking platform event names into higher-level logic.
  • Handle macOS RunEvent::Reopen { has_visible_windows, .. } by showing the main window only when no windows are currently visible.
  • Add unit test coverage for the reopen decision logic (reopen_event_action).

Reviewed changes

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

File Description
src-tauri/src/app_runtime.rs Handles the new ShowMainWindow runtime action by calling window::actions::show_main_window.
src-tauri/src/app_runtime_events.rs Introduces ShowMainWindow, adds macOS Reopen mapping, and adds unit tests for the reopen decision.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@zouyonghe
Copy link
Copy Markdown
Member

把代码格式化一下就可以合了

@zouyonghe zouyonghe merged commit c6ca243 into AstrBotDevs:main Apr 9, 2026
4 checks passed
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.

3 participants