Skip to content

Commit

Permalink
Fix notifications (whoops!)
Browse files Browse the repository at this point in the history
  • Loading branch information
simonbuchan committed Apr 1, 2019
1 parent e5a139d commit db74a4d
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 16 deletions.
36 changes: 33 additions & 3 deletions src/icon-object.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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<uint32_t> id;
Expand All @@ -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<LPWSTR*>(lParam) = lpName;
*reinterpret_cast<ResourceId*>(lParam) = lpName;
return FALSE;
},
reinterpret_cast<LONG_PTR>(&resource));
Expand Down
4 changes: 2 additions & 2 deletions src/notify-icon-object.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down Expand Up @@ -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();
Expand Down
23 changes: 12 additions & 11 deletions test/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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" },
Expand Down Expand Up @@ -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, {
Expand All @@ -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",

Expand Down

0 comments on commit db74a4d

Please sign in to comment.