-
Notifications
You must be signed in to change notification settings - Fork 6.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
MSIX: initial implement notifications library
- Loading branch information
Showing
27 changed files
with
723 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
#include "pch.h" | ||
#include "notifications.h" | ||
|
||
#include <unknwn.h> | ||
#include <winrt/base.h> | ||
#include <winrt/Windows.Foundation.h> | ||
#include <winrt/Windows.Data.Xml.Dom.h> | ||
#include <winrt/Windows.Foundation.Collections.h> | ||
#include <winrt/Windows.UI.Notifications.h> | ||
#include <winrt/Windows.ApplicationModel.Background.h> | ||
|
||
using namespace winrt::Windows::ApplicationModel::Background; | ||
using winrt::Windows::Data::Xml::Dom::XmlDocument; | ||
using winrt::Windows::UI::Notifications::ToastNotification; | ||
using winrt::Windows::UI::Notifications::ToastNotificationManager; | ||
|
||
namespace | ||
{ | ||
constexpr std::wstring_view TASK_NAME = L"PowerToysBackgroundNotificationsHandler"; | ||
constexpr std::wstring_view TASK_ENTRYPOINT = L"PowerToysNotifications.BackgroundHandler"; | ||
} | ||
|
||
void notif::register_background_toast_handler() | ||
{ | ||
// Re-request access to clean up from our previous versions | ||
BackgroundExecutionManager::RemoveAccess(); | ||
BackgroundExecutionManager::RequestAccessAsync().get(); | ||
|
||
BackgroundTaskBuilder builder; | ||
ToastNotificationActionTrigger trigger{ L"PowerToys" }; | ||
builder.SetTrigger(trigger); | ||
builder.TaskEntryPoint(TASK_ENTRYPOINT); | ||
builder.Name(TASK_NAME); | ||
|
||
const auto tasks = BackgroundTaskRegistration::AllTasks(); | ||
const bool already_registered = std::any_of(begin(tasks), end(tasks), [=](const auto& task) { | ||
return task.Value().Name() == TASK_NAME; | ||
}); | ||
if (already_registered) | ||
{ | ||
return; | ||
} | ||
try | ||
{ | ||
(void)builder.Register(); | ||
} | ||
catch (...) | ||
{ | ||
// Couldn't register the task, nothing we can do | ||
} | ||
} | ||
|
||
void notif::show_toast(std::wstring_view message) | ||
{ | ||
show_toast_background_activated(L"Unknown", message, {}); | ||
} | ||
|
||
void notif::show_toast_background_activated(std::wstring_view background_handler_id, std::wstring_view message, std::vector<std::wstring_view> button_labels) | ||
{ | ||
std::wstring toast_xml; | ||
toast_xml.reserve(1024); | ||
toast_xml += LR"(<?xml version="1.0"?><toast><visual><binding template="ToastGeneric"><text>PowerToys</text><text>)"; | ||
toast_xml += message; | ||
toast_xml += L"</text></binding></visual><actions>"; | ||
|
||
for (size_t i = 0; i < size(button_labels); ++i) | ||
{ | ||
toast_xml += LR"(<action activationType="background" arguments=")"; | ||
toast_xml += std::to_wstring(i); // pass button ID | ||
toast_xml += L';'; | ||
toast_xml += background_handler_id; | ||
toast_xml += LR"(" content=")"; | ||
toast_xml += button_labels[i]; | ||
toast_xml += LR"("/>)"; | ||
} | ||
toast_xml += L"</actions></toast>"; | ||
|
||
XmlDocument toast_xml_doc; | ||
toast_xml_doc.LoadXml(toast_xml); | ||
ToastNotification notification{ toast_xml_doc }; | ||
|
||
const auto notifier = ToastNotificationManager::ToastNotificationManager::CreateToastNotifier(); | ||
notifier.Show(notification); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
#pragma once | ||
|
||
#include <string_view> | ||
#include <vector> | ||
|
||
namespace notif | ||
{ | ||
void register_background_toast_handler(); | ||
|
||
void show_toast(std::wstring_view message); | ||
void show_toast_background_activated(std::wstring_view background_handler_id, std::wstring_view message, std::vector<std::wstring_view> button_labels); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
#define NOTIFICATIONSDLL_API __declspec(dllexport) | ||
#define NOTIFICATIONSDLL_API __declspec(dllimport) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
#include "pch.h" | ||
|
||
STDAPI DllRegisterServer() | ||
{ | ||
return S_OK; | ||
} | ||
|
||
STDAPI DllUnregisterServer() | ||
{ | ||
return S_OK; | ||
} | ||
|
||
BOOL APIENTRY DllMain(HMODULE, | ||
DWORD ul_reason_for_call, | ||
LPVOID) | ||
{ | ||
switch (ul_reason_for_call) | ||
{ | ||
case DLL_PROCESS_ATTACH: | ||
case DLL_THREAD_ATTACH: | ||
case DLL_THREAD_DETACH: | ||
case DLL_PROCESS_DETACH: | ||
break; | ||
} | ||
return TRUE; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
#pragma once | ||
|
||
#define WIN32_LEAN_AND_MEAN // Exclude rarely-used stuff from Windows headers | ||
#include <windows.h> | ||
|
||
#include <winrt/Windows.ApplicationModel.Background.h> | ||
#include <winrt/Windows.Foundation.h> | ||
#include <winrt/Windows.UI.Notifications.h> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
EXPORTS | ||
DllCanUnloadNow = WINRT_CanUnloadNow PRIVATE | ||
DllGetActivationFactory = WINRT_GetActivationFactory PRIVATE | ||
DllRegisterServer PRIVATE | ||
DllUnregisterServer PRIVATE |
Oops, something went wrong.