Skip to content

feat(tray): add option to minimize to system tray on window close,portable mode and singleton - #7

Closed
heartacker wants to merge 3 commits into
AtomUICommunity:masterfrom
heartacker:feat/tray-minimize-on-close
Closed

heartacker wants to merge 3 commits into
AtomUICommunity:masterfrom
heartacker:feat/tray-minimize-on-close

Conversation

@heartacker

Copy link
Copy Markdown
Contributor

Summary

Add an application setting to minimize CxShell to the system tray instead of exiting when closing the main window.

Included Commits

  • feat(tray): 新增关闭主窗口时最小化到通知栏设置

- 新增 ApplicationSettings.MinimizeToTrayOnClose 配置项(默认开启),用于控制关闭主窗口时是否最小化到通知栏
- MainWindow 重写 OnClosing,依据设置与关闭原因(排除应用退出/系统关机)拦截关闭并隐藏窗口到托盘,新增 PrepareForExit 允许显式退出真正关闭窗口
- App 托盘菜单文案接入 LocalizationService 并随语言切换动态刷新;"退出"菜单与 ShutdownRequested 时先调用 PrepareForExit 再关闭;新增 HasActiveTrayIcon 判断托盘可用状态
- ShowMainWindow 及窗口激活逻辑优化:窗口隐藏时先 Show 再激活,恢复正常显示
- ApplicationSettingsPage/ApplicationSettingsWindow 设置页新增"窗口"分组及"关闭主窗口时最小化到通知栏"复选框(仅 Windows 显示)
- ApplicationSettingsViewModel 新增 MinimizeToTrayOnClose 可观察属性,变更即持久化,并补充 IsMinimizeToTrayOnCloseSupported 平台判断
- LocalizationService 新增中英文文案:ApplicationSettings.Window、ApplicationSettings.MinimizeToTrayOnClose、Tray.ShowWindow、Tray.Exit
- OnLoaded 中的启动逻辑(RDP 冒烟、命令行启动处理、会话管理器显示、自动更新检查)改为仅首次加载执行,避免重复触发;CommandLineHandoffServer 释放移至 Closed 事件
- 补充单元测试:MinimizeToTrayOnClose 默认值与持久化行为、ShouldMinimizeToTrayOnClose 针对不同关闭原因与守卫条件的判定逻辑
@heartacker heartacker changed the title feat(tray): add option to minimize to system tray on window close feat(tray): add option to minimize to system tray on window close,portable mode and singleton Nov 27, 2026
@xiaochengzjc

Copy link
Copy Markdown
Collaborator

Thanks — the tray, portable and single-instance work all landed. I cherry-picked the three commits onto master with your authorship and then added one follow-up commit with four fixes found in review. Details:

Merged as-is (dcafd65, 8f40ee8, 1241e20)

All three cherry-picked with zero conflicts, each builds on its own with 0 warnings. ShouldMinimizeToTrayOnClose being a pure static with theory tests made this easy to verify, and the AppDataPaths extraction is clean — I checked all eleven call sites and the installed paths are byte-identical to before, so no migration is needed for existing users.

Follow-up commit 8571b34 — what I changed and why

1. One-time balloon when the window hides. Avalonia.TrayIcon has no balloon API, so there is a small borderless Views/TrayBalloonWindow that sits above the notification area for 7 s and restores the window on click. It fires once per profile (ApplicationSettings.TrayMinimizeHintShown), because otherwise "close" looks like "the app quit" for everyone upgrading with sessions still running.

2. A failed hand-off no longer exits silently. TryAcquireSingleInstanceOrForward dropped the return value of SendToFirstInstance, so when both the lock and the pipe failed the process just disappeared — no window, no message. It now logs the pipe name and lock path through a new StartupReporter (writes Logs/startup.log, shows a native message box on Windows).

3. Portable mode requires a data/.portable marker. Triggering on the folder alone meant that any unrelated folder named data next to the executable would silently switch to an empty profile — indistinguishable from "all my sessions are gone". The marker makes it explicit, and the release archives can ship an empty one.

4. The lock directory is probed for real writability. Directory.CreateDirectory succeeds on an existing read-only folder, so the old code fell through to TryAcquireLock → null and started unlocked by accident. It now writes a probe file, falls back to %TEMP%/cxshell.<scope>.lock, and as a last resort starts without the lock rather than failing. sftp-drag.log moved onto AppDataPaths too, so it follows portable mode.

Behaviour note for the release: launching the executable a second time now raises the running window instead of opening a second instance. --newwin still bypasses it.

Verification

Full suite is 767 tests (your 765 plus one for the marker requirement and one for the %TEMP% fallback). Release build is 0 warnings. I also exercised it on Windows: portable mode writes cxshell.db/Logs//process.lock inside data/, the second launch exits in 0.39 s with one process left and the window restored, and holding process.lock with an exclusive handle produces the expected dialog.

Nothing else from this PR is outstanding — feel free to close it, or tell me if you would rather I squash the follow-up into your commits.

@xiaochengzjc

Copy link
Copy Markdown
Collaborator

这个 PR 的内容已经全部在 master 上了,而且是补丁等价地进来的,不是被丢弃:

$ git cherry -v master pr7
- fd878af feat(tray): 新增关闭主窗口时最小化到通知栏设置
- 97869b3 feat(portable): 支持同目录 data 文件夹自动启用可移动模式
- 5f5f9ca feat(instance): 支持按数据目录隔离的单例启动与窗口唤醒

三条都是 -,说明对应提交已通过 cherry-pick 落在 v0.1.82。逐项核对:

  • Services/AppDataPaths.cs、Services/SingleInstanceChannel.cs 都在 master 里;
  • Models/SessionInfo.cs 的 MinimizeToTrayOnClose(默认 true)、TrayMinimizeHintShown 都在;
  • 设置页与设置窗口两处都接了 IsMinimizeToTraySupported / MinimizeToTrayOnClose 的复选框;
  • .portable 标记与按数据目录隔离的单实例也都在用。

页面显示 dirty 是因为分支停在 11 月 27 日的 master 上(相对当前 master 落后约 6800 行),不是有内容冲突。

所以这个 PR 按「已完成」关闭。如果当时还有哪一部分没被 cherry-pick 覆盖到、你这边还能指出来,我立刻重开或者补一条补丁。感谢这套托盘 / 便携 / 单例的实现,一直在用。

@heartacker
heartacker deleted the feat/tray-minimize-on-close branch December 1, 2026 00:00
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.

2 participants