Skip to content

Commit 0c655a1

Browse files
feat: restore icon and context menu when Explorer restarts if necessary (#71)
* [windows] feat: Add onWindowsTaskbarCreated Method. * refactor: make taskbar-related code comply to current code style * feat: restore icon and context menu when Explorer restarts if possible - restore the icon with the existing resource. - do create pop-up menu only once. * feat: restore icon and context menu when Explorer restarts if necessary Correct the message of the previous commit. --------- Co-authored-by: l619534951 <[email protected]>
1 parent 532e808 commit 0c655a1

File tree

1 file changed

+18
-8
lines changed

1 file changed

+18
-8
lines changed

packages/tray_manager/windows/tray_manager_plugin.cpp

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -48,13 +48,16 @@ class TrayManagerPlugin : public flutter::Plugin {
4848
flutter::PluginRegistrarWindows* registrar;
4949
NOTIFYICONDATA nid;
5050
NOTIFYICONIDENTIFIER niif;
51-
HMENU hMenu;
51+
// do create pop-up menu only once.
52+
HMENU hMenu = CreatePopupMenu();
5253
bool tray_icon_setted = false;
54+
UINT windows_taskbar_created_message_id = 0;
5355

5456
// The ID of the WindowProc delegate registration.
5557
int window_proc_id = -1;
5658

5759
void TrayManagerPlugin::_CreateMenu(HMENU menu, flutter::EncodableMap args);
60+
void TrayManagerPlugin::_ApplyIcon();
5861

5962
// Called for top-level WindowProc delegation.
6063
std::optional<LRESULT> TrayManagerPlugin::HandleWindowProc(HWND hwnd,
@@ -109,6 +112,7 @@ TrayManagerPlugin::TrayManagerPlugin(flutter::PluginRegistrarWindows* registrar)
109112
[this](HWND hwnd, UINT message, WPARAM wparam, LPARAM lparam) {
110113
return HandleWindowProc(hwnd, message, wparam, lparam);
111114
});
115+
windows_taskbar_created_message_id = RegisterWindowMessage(L"TaskbarCreated");
112116
}
113117

114118
TrayManagerPlugin::~TrayManagerPlugin() {
@@ -195,6 +199,12 @@ std::optional<LRESULT> TrayManagerPlugin::HandleWindowProc(HWND hWnd,
195199
default:
196200
return DefWindowProc(hWnd, message, wParam, lParam);
197201
};
202+
} else if (message == windows_taskbar_created_message_id) {
203+
if (windows_taskbar_created_message_id != 0 && tray_icon_setted) {
204+
// restore the icon with the existing resource.
205+
tray_icon_setted = false;
206+
_ApplyIcon();
207+
}
198208
}
199209
return result;
200210
}
@@ -224,22 +234,25 @@ void TrayManagerPlugin::SetIcon(
224234

225235
std::wstring_convert<std::codecvt_utf8_utf16<wchar_t>> converter;
226236

227-
HICON hIcon = static_cast<HICON>(
237+
nid.hIcon = static_cast<HICON>(
228238
LoadImage(nullptr, (LPCWSTR)(converter.from_bytes(iconPath).c_str()),
229239
IMAGE_ICON, GetSystemMetrics(SM_CXSMICON),
230240
GetSystemMetrics(SM_CYSMICON), LR_LOADFROMFILE));
231241

242+
_ApplyIcon();
243+
244+
result->Success(flutter::EncodableValue(true));
245+
}
246+
247+
void TrayManagerPlugin::_ApplyIcon() {
232248
if (tray_icon_setted) {
233-
nid.hIcon = hIcon;
234249
Shell_NotifyIcon(NIM_MODIFY, &nid);
235250
} else {
236251
nid.cbSize = sizeof(NOTIFYICONDATA);
237252
nid.hWnd = GetMainWindow();
238253
nid.uCallbackMessage = WM_MYMESSAGE;
239-
nid.hIcon = hIcon;
240254
nid.uFlags = NIF_MESSAGE | NIF_ICON;
241255
Shell_NotifyIcon(NIM_ADD, &nid);
242-
hMenu = CreatePopupMenu();
243256
}
244257

245258
niif.cbSize = sizeof(NOTIFYICONIDENTIFIER);
@@ -248,8 +261,6 @@ void TrayManagerPlugin::SetIcon(
248261
niif.guidItem = GUID_NULL;
249262

250263
tray_icon_setted = true;
251-
252-
result->Success(flutter::EncodableValue(true));
253264
}
254265

255266
void TrayManagerPlugin::SetToolTip(
@@ -276,7 +287,6 @@ void TrayManagerPlugin::SetContextMenu(
276287
const flutter::EncodableMap& args =
277288
std::get<flutter::EncodableMap>(*method_call.arguments());
278289

279-
hMenu = CreatePopupMenu();
280290
_CreateMenu(hMenu, std::get<flutter::EncodableMap>(
281291
args.at(flutter::EncodableValue("menu"))));
282292

0 commit comments

Comments
 (0)