From db74a4d89e90d33ae022dc80819dc95fbdee8cd6 Mon Sep 17 00:00:00 2001 From: Simon Buchan Date: Tue, 2 Apr 2019 12:58:45 +1300 Subject: [PATCH] Fix notifications (whoops!) --- src/icon-object.cc | 36 +++++++++++++++++++++++++++++++++--- src/notify-icon-object.cc | 4 ++-- test/index.ts | 23 ++++++++++++----------- 3 files changed, 47 insertions(+), 16 deletions(-) diff --git a/src/icon-object.cc b/src/icon-object.cc index 4747e11..70bafa3 100644 --- a/src/icon-object.cc +++ b/src/icon-object.cc @@ -45,6 +45,36 @@ napi_value export_Icon_loadBuiltin(napi_env env, napi_callback_info info) { return result; } +struct ResourceId { + LPWSTR id; + std::wstring storage; + + ResourceId& operator=(uint32_t new_id) { + if (IS_INTRESOURCE(new_id)) { + id = MAKEINTRESOURCE(new_id); + storage.clear(); + } else { + storage.assign(MAKEINTRESOURCE(new_id)); + id = storage.data(); + } + return *this; + } + + ResourceId& operator=(LPWSTR new_id) { + if (IS_INTRESOURCE(new_id)) { + id = new_id; + } else { + storage.assign(new_id); + id = storage.data(); + } + return *this; + } + + operator LPWSTR() const { + return id; + } +}; + napi_value export_Icon_loadResource(napi_env env, napi_callback_info info) { icon_size_t size; std::optional id; @@ -65,15 +95,15 @@ napi_value export_Icon_loadResource(napi_env env, napi_callback_info info) { } } - LPWSTR resource; + ResourceId resource; if (id) { - resource = MAKEINTRESOURCEW(id.value()); + resource = id.value(); } else { // Use first RT_GROUP_ICON, the same as Windows uses for an .exe icon. EnumResourceNamesW( hinstance, RT_GROUP_ICON, [](HMODULE hModule, LPCWSTR lpType, LPWSTR lpName, LONG_PTR lParam) { - *reinterpret_cast(lParam) = lpName; + *reinterpret_cast(lParam) = lpName; return FALSE; }, reinterpret_cast(&resource)); diff --git a/src/notify-icon-object.cc b/src/notify-icon-object.cc index 5ef9925..72fc5eb 100644 --- a/src/notify-icon-object.cc +++ b/src/notify-icon-object.cc @@ -151,7 +151,7 @@ napi_status get_icon_options_common(napi_env env, napi_value value, // Deliberate slicing. This is a bit clumsy, but it's the simplest solution // that still clearly separates the notify_icon stuff from the N-API // ownership stuff. - options->notification = options->notification; + options->notification = options->object_notification; } NAPI_RETURN_IF_NOT_OK(napi_get_named_property(env, value, "onSelect", &options->select_callback)); @@ -197,7 +197,7 @@ void apply_options(NotifyIconObject* this_object, if (options.object_notification) { if (options.object_notification->icon_ref) { - this_object->notification_icon_ref = std::move(options.icon_ref.value()); + this_object->notification_icon_ref = std::move(options.object_notification->icon_ref.value()); } else { // Since the notification is being replaced, we don't need to keep // the old notification icon. this_object->notification_icon_ref.clear(); diff --git a/test/index.ts b/test/index.ts index c7a6fa9..6b806a9 100644 --- a/test/index.ts +++ b/test/index.ts @@ -24,7 +24,7 @@ catchErrors(() => { { separator: true }, { id: 103, text: "Time", disabled: true }, { id: 124, text: "Counter" }, - { text: "Notification" }, + { id: 125, text: "Notification" }, { text: "Submenu", items: [ { id: 456, text: "Use warning" }, @@ -89,6 +89,16 @@ catchErrors(() => { text: `Counter: ${++count}`, }); return; + case 125: + notifyIcon.update({ + notification: { + icon: notificationIcon, + sound: false, + title: "Some notification", + text: `You selected: "${item.text}"\nThe time is: ${new Date().toLocaleTimeString()}` + }, + }); + return; case 456: const useWarning = !item.checked; contextMenu.update(itemId, { @@ -99,19 +109,10 @@ catchErrors(() => { }); return; } - - notifyIcon.update({ - notification: { - icon: notificationIcon, - sound: false, - title: "Annoying Message", - text: `You selected: "${item.text}"\nThe time is: ${new Date().toLocaleTimeString()}` - }, - }); }); const notifyIcon = new NotifyIcon({ - guid, + // guid, icon, tooltip: "Example Tooltip Text",