[fix] show system tray icon on application startup

This commit is contained in:
dijunkun
2026-09-07 17:42:25 +08:00
parent 5aedb09c55
commit d381cbaa9b
3 changed files with 31 additions and 4 deletions
@@ -392,7 +392,10 @@ struct LinuxTrayImpl {
exit_menu_label(GetExitMenuLabel(language_index_value)),
exit_menu_ascii_label(IsAsciiPrintable(exit_menu_label)
? exit_menu_label
: "Exit") {}
: "Exit") {
EnsureTrayIcon();
}
explicit LinuxTrayImpl(std::function<void()> show_window_callback,
std::function<void()> hide_window_callback,
@@ -416,7 +419,9 @@ struct LinuxTrayImpl {
exit_menu_label(GetExitMenuLabel(language_index_value)),
exit_menu_ascii_label(IsAsciiPrintable(exit_menu_label)
? exit_menu_label
: "Exit") {}
: "Exit") {
EnsureTrayIcon();
}
~LinuxTrayImpl() { RemoveTrayIcon(); }
@@ -60,6 +60,8 @@ WinTray::WinTray(HWND app_hwnd, HICON icon, const std::wstring& tooltip,
nid_.uCallbackMessage = WM_TRAY_CALLBACK;
nid_.hIcon = icon_;
wcsncpy_s(nid_.szTip, tip_.c_str(), _TRUNCATE);
EnsureTrayIcon();
}
WinTray::WinTray(std::function<void()> show_window,
@@ -80,7 +82,7 @@ WinTray::~WinTray() {
}
void WinTray::MinimizeToTray() {
Shell_NotifyIcon(NIM_ADD, &nid_);
EnsureTrayIcon();
if (hide_window_) {
hide_window_();
} else if (app_hwnd_) {
@@ -88,7 +90,25 @@ void WinTray::MinimizeToTray() {
}
}
void WinTray::RemoveTrayIcon() { Shell_NotifyIcon(NIM_DELETE, &nid_); }
bool WinTray::EnsureTrayIcon() {
if (tray_icon_added_) {
// NIM_MODIFY also detects an icon lost when Explorer was restarted.
if (Shell_NotifyIcon(NIM_MODIFY, &nid_)) {
return true;
}
tray_icon_added_ = false;
}
tray_icon_added_ = Shell_NotifyIcon(NIM_ADD, &nid_) != FALSE;
return tray_icon_added_;
}
void WinTray::RemoveTrayIcon() {
if (tray_icon_added_) {
Shell_NotifyIcon(NIM_DELETE, &nid_);
tray_icon_added_ = false;
}
}
void WinTray::ShowApplicationWindow() {
if (show_window_) {
@@ -33,6 +33,7 @@ class WinTray {
bool HandleTrayMessage(MSG* msg);
private:
bool EnsureTrayIcon();
void ShowApplicationWindow();
void OpenSettings();
@@ -42,6 +43,7 @@ class WinTray {
std::wstring tip_;
int language_index_;
NOTIFYICONDATA nid_;
bool tray_icon_added_ = false;
std::function<void()> show_window_;
std::function<void()> hide_window_;
std::function<void()> open_settings_;