Skip to content

Commit

Permalink
FIXUP: review comments #1
Browse files Browse the repository at this point in the history
  • Loading branch information
yuyoyuppe committed Feb 4, 2020
1 parent ef7b84e commit bc77c09
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 14 deletions.
4 changes: 2 additions & 2 deletions doc/devdocs/common.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,8 @@ To use UWP-style toast notifications, simply include the header and call one of
void show_toast(std::wstring_view message); // #1

void show_toast_background_activated( // #2
std::wstring_view background_handler_id,
std::wstring_view message,
std::wstring_view background_handler_id,
std::vector<std::wstring_view> button_labels);
```
We might add more functions in the future if the need arises, e.g. `show_toast_xml` which will accept raw XML for rich customization.
Expand All @@ -74,8 +74,8 @@ Implement a toast activation handler/callback as a function in [handler_function
void some_func() {
// ...
notifications::show_toast_background_activated(
L"awesome_toast", // activation handler id
L"Toast message!", // text displayed in a toast
L"awesome_toast", // activation handler id
{L"Press me!", L"Also could press me!", L"I'm here to be pressed!"} // buttons in a toast
);
```
Expand Down
10 changes: 5 additions & 5 deletions src/common/notifications.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ void notifications::register_background_toast_handler()
{
try
{
// Re-request access to clean up from our previous versions
// Re-request access to clean up from previous PowerToys installations
BackgroundExecutionManager::RemoveAccess();
BackgroundExecutionManager::RequestAccessAsync().get();

Expand Down Expand Up @@ -54,10 +54,10 @@ void notifications::register_background_toast_handler()
void notifications::show_toast(std::wstring_view message)
{
// The toast won't be actually activated in the background, since it doesn't have any buttons
show_toast_background_activated({}, message, {});
show_toast_background_activated(message, {}, {});
}

void notifications::show_toast_background_activated(std::wstring_view background_handler_id, std::wstring_view message, std::vector<std::wstring_view> button_labels)
void notifications::show_toast_background_activated(std::wstring_view message, std::wstring_view background_handler_id, std::vector<std::wstring_view> button_labels)
{
// DO NOT LOCALIZE any string in this function, because they're XML tags and a subject to
// https://docs.microsoft.com/en-us/windows/uwp/design/shell/tiles-and-notifications/toast-xml-schema
Expand All @@ -71,8 +71,8 @@ void notifications::show_toast_background_activated(std::wstring_view background
for (size_t i = 0; i < size(button_labels); ++i)
{
toast_xml += LR"(<action activationType="background" arguments=")";
toast_xml += std::to_wstring(i); // pass the button ID
toast_xml += L';';
toast_xml += L"button_id=" + std::to_wstring(i); // pass the button ID
toast_xml += L"&amp;handler=";
toast_xml += background_handler_id;
toast_xml += LR"(" content=")";
toast_xml += button_labels[i];
Expand Down
2 changes: 1 addition & 1 deletion src/common/notifications.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,5 @@ namespace notifications

// Make sure your plaintext_message argument is properly XML-escaped
void show_toast(std::wstring_view plaintext_message);
void show_toast_background_activated(std::wstring_view background_handler_id, std::wstring_view plaintext_message, std::vector<std::wstring_view> plaintext_button_labels);
void show_toast_background_activated(std::wstring_view plaintext_message, std::wstring_view background_handler_id, std::vector<std::wstring_view> plaintext_button_labels);
}
13 changes: 7 additions & 6 deletions src/common/notifications_winrt/BackgroundHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ namespace winrt::PowerToysNotifications::implementation
{
using Windows::ApplicationModel::Background::IBackgroundTaskInstance;
using Windows::UI::Notifications::ToastNotificationActionTriggerDetail;
using Windows::Foundation::WwwFormUrlDecoder;

void BackgroundHandler::Run(IBackgroundTaskInstance bti)
{
Expand All @@ -16,11 +17,11 @@ namespace winrt::PowerToysNotifications::implementation
{
return;
}
std::wstring button_id_and_handler_data{ details.Argument() };
std::wstring_view button_id_and_handler{ button_id_and_handler_data };
const auto first_semicolon_pos = button_id_and_handler.find(';');
const size_t button_id = std::stoi(button_id_and_handler_data.substr(0, first_semicolon_pos));
const auto handler = button_id_and_handler.substr(first_semicolon_pos + 1);
dispatch_to_backround_handler(handler, std::move(bti), button_id);

WwwFormUrlDecoder decoder{details.Argument()};

const size_t button_id = std::stoi(decoder.GetFirstValueByName(L"button_id").c_str());
auto handler = decoder.GetFirstValueByName(L"handler");
dispatch_to_backround_handler(std::move(handler), std::move(bti), button_id);
}
}

0 comments on commit bc77c09

Please sign in to comment.