Skip to content

Commit

Permalink
updating: remove previous progress toasts
Browse files Browse the repository at this point in the history
toast notifications:
- add method for removing existing toasts
- add method for updating existing toast contents/title
- refactoring
  • Loading branch information
yuyoyuppe committed Oct 22, 2020
1 parent 5593f81 commit 5e77234
Show file tree
Hide file tree
Showing 9 changed files with 251 additions and 190 deletions.
4 changes: 2 additions & 2 deletions installer/PowerToysBootstrapper/bootstrapper/bootstrapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -300,7 +300,7 @@ int bootstrapper()
break;
}
progressParams.progress = std::min(0.99f, progressParams.progress + 0.001f);
notifications::update_progress_bar_toast(TOAST_TAG, progressParams);
notifications::update_toast_progress_bar(TOAST_TAG, progressParams);
}
} }.detach();
}
Expand All @@ -313,7 +313,7 @@ int bootstrapper()
std::scoped_lock lock{ progressLock };
progressParams.progress = value;
progressParams.progress_title = title;
notifications::update_progress_bar_toast(TOAST_TAG, progressParams);
notifications::update_toast_progress_bar(TOAST_TAG, progressParams);
};

spdlog::debug("Extracting embedded MSI installer");
Expand Down
62 changes: 52 additions & 10 deletions src/common/notifications.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,10 @@
#include <winrt/Windows.Data.Xml.Dom.h>
#include <winrt/Windows.Foundation.Collections.h>
#include <winrt/Windows.UI.Notifications.h>
#include <winrt/Windows.UI.Notifications.Management.h>
#include <winrt/Windows.ApplicationModel.Background.h>
#include <winrt/Windows.System.h>
#include <winrt/Windows.System.UserProfile.h>
#include <wil/com.h>
#include <propvarutil.h>
#include <propkey.h>
Expand Down Expand Up @@ -39,6 +42,7 @@ namespace
constexpr std::wstring_view APPIDS_REGISTRY = LR"(Software\Classes\AppUserModelId\)";

std::wstring APPLICATION_ID = L"Microsoft.PowerToysWin32";
constexpr std::wstring_view DEFAULT_TOAST_GROUP = L"PowerToysToastTag";
}

namespace localized_strings
Expand Down Expand Up @@ -275,11 +279,10 @@ void notifications::show_toast_with_activations(std::wstring message,
std::wstring toast_xml;
toast_xml.reserve(2048);

toast_xml += LR"(<?xml version="1.0"?><toast><visual><binding template="ToastGeneric"><text id="1">)";
toast_xml += title;
toast_xml += LR"(</text><text id="2">)";
toast_xml += message;
toast_xml += L"</text>";
toast_xml += LR"(<?xml version="1.0"?><toast><visual><binding template="ToastGeneric">)";
toast_xml += LR"(<text id="1">{title}</text>)";
toast_xml += LR"(<text id="2">{message}</text>)";

if (params.progress_bar.has_value())
{
toast_xml += LR"(<progress title="{progressTitle}" value="{progressValue}" valueStringOverride="{progressValueString}" status="" />)";
Expand Down Expand Up @@ -373,17 +376,20 @@ void notifications::show_toast_with_activations(std::wstring message,
xml_escape(toast_xml);
toast_xml_doc.LoadXml(toast_xml);
ToastNotification notification{ toast_xml_doc };
notification.Group(DEFAULT_TOAST_GROUP);

winrt::Windows::Foundation::Collections::StringMap map;
map.Insert(L"message", std::move(message));
map.Insert(L"title", std::move(title));
if (params.progress_bar.has_value())
{
float progress = std::clamp(params.progress_bar->progress, 0.0f, 1.0f);
winrt::Windows::Foundation::Collections::StringMap map;
map.Insert(L"progressValue", std::to_wstring(progress));
map.Insert(L"progressValueString", std::to_wstring(static_cast<int>(progress * 100)) + std::wstring(L"%"));
map.Insert(L"progressTitle", params.progress_bar->progress_title);
winrt::Windows::UI::Notifications::NotificationData data(map);
notification.Data(data);
}
winrt::Windows::UI::Notifications::NotificationData data{ map };
notification.Data(std::move(data));

const auto notifier = winstore::running_as_packaged() ? ToastNotificationManager::ToastNotificationManager::CreateToastNotifier() :
ToastNotificationManager::ToastNotificationManager::CreateToastNotifier(APPLICATION_ID);
Expand Down Expand Up @@ -412,7 +418,7 @@ void notifications::show_toast_with_activations(std::wstring message,
}
}

void notifications::update_progress_bar_toast(std::wstring_view tag, progress_bar_params params)
void notifications::update_toast_progress_bar(std::wstring_view tag, progress_bar_params params)
{
const auto notifier = winstore::running_as_packaged() ?
ToastNotificationManager::ToastNotificationManager::CreateToastNotifier() :
Expand All @@ -425,5 +431,41 @@ void notifications::update_progress_bar_toast(std::wstring_view tag, progress_ba
map.Insert(L"progressTitle", params.progress_title);

winrt::Windows::UI::Notifications::NotificationData data(map);
winrt::Windows::UI::Notifications::NotificationUpdateResult res = notifier.Update(data, tag);
winrt::Windows::UI::Notifications::NotificationUpdateResult res = notifier.Update(data, tag, DEFAULT_TOAST_GROUP);
}

void notifications::update_toast_contents(std::wstring_view tag, std::wstring plaintext_message, std::wstring title)
{
const auto notifier = winstore::running_as_packaged() ?
ToastNotificationManager::ToastNotificationManager::CreateToastNotifier() :
ToastNotificationManager::ToastNotificationManager::CreateToastNotifier(APPLICATION_ID);

winrt::Windows::Foundation::Collections::StringMap map;

map.Insert(L"title", std::move(title));
map.Insert(L"message", std::move(plaintext_message));

winrt::Windows::UI::Notifications::NotificationData data(map);
winrt::Windows::UI::Notifications::NotificationUpdateResult res = notifier.Update(data, tag, DEFAULT_TOAST_GROUP);
}

void notifications::remove_toasts(std::wstring_view tag)
{
using namespace winrt::Windows::System;

try
{
User currentUser{ *User::FindAllAsync(UserType::LocalUser, UserAuthenticationStatus::LocallyAuthenticated).get().First() };
if (!currentUser)
{
return;
}
currentUser.GetPropertyAsync(KnownUserProperties::AccountName());
auto toastHistory = ToastNotificationManager::GetForUser(currentUser).History();
toastHistory.Remove(tag, DEFAULT_TOAST_GROUP, APPLICATION_ID);
}
catch (...)
{
// Couldn't get the current user or problem removing the toast => nothing we can do
}
}
5 changes: 4 additions & 1 deletion src/common/notifications.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
namespace notifications
{
constexpr inline const wchar_t TOAST_ACTIVATED_LAUNCH_ARG[] = L"-ToastActivated";
constexpr inline const wchar_t UPDATING_PROCESS_TOAST_TAG[] = L"PTUpdateNotifyTag";

void override_application_id(const std::wstring_view appID);
void register_background_toast_handler();
Expand Down Expand Up @@ -59,5 +60,7 @@ namespace notifications

void show_toast(std::wstring plaintext_message, std::wstring title, toast_params params = {});
void show_toast_with_activations(std::wstring plaintext_message, std::wstring title, std::wstring_view background_handler_id, std::vector<action_t> actions, toast_params params = {});
void update_progress_bar_toast(std::wstring_view tag, progress_bar_params params);
void update_toast_progress_bar(std::wstring_view tag, progress_bar_params params);
void update_toast_contents(std::wstring_view tag, std::wstring plaintext_message, std::wstring title);
void remove_toasts(std::wstring_view tag);
}

0 comments on commit 5e77234

Please sign in to comment.