|
| 1 | +// Copyright (c) Microsoft Corporation. |
| 2 | +// Licensed under the MIT license. |
| 3 | + |
| 4 | +#include "pch.h" |
| 5 | +#include "DesktopNotification.h" |
| 6 | + |
| 7 | +#include <WtExeUtils.h> |
| 8 | + |
| 9 | +using namespace winrt::Windows::UI::Notifications; |
| 10 | +using namespace winrt::Windows::Data::Xml::Dom; |
| 11 | + |
| 12 | +namespace winrt::TerminalApp::implementation |
| 13 | +{ |
| 14 | + std::atomic<uint64_t> DesktopNotification::_lastNotificationTime{ 0 }; |
| 15 | + |
| 16 | + // Method Description: |
| 17 | + // - Rate-limits toast notifications so we don't spam the user. |
| 18 | + // Return Value: |
| 19 | + // - Returns true if a notification is allowed, false if too recent. |
| 20 | + bool DesktopNotification::ShouldSendNotification() |
| 21 | + { |
| 22 | + const auto now = GetTickCount64(); |
| 23 | + auto last = _lastNotificationTime.load(std::memory_order_relaxed); |
| 24 | + |
| 25 | + // Subtraction wraps cleanly modulo 2^64, so the delta is correct even |
| 26 | + // across the (~584 million year) GetTickCount64 rollover. |
| 27 | + if (now - last < MinNotificationIntervalMs) |
| 28 | + { |
| 29 | + return false; |
| 30 | + } |
| 31 | + |
| 32 | + // Attempt to update; if another thread beat us, that's fine — we'll skip this one. |
| 33 | + return _lastNotificationTime.compare_exchange_strong(last, now, std::memory_order_relaxed); |
| 34 | + } |
| 35 | + |
| 36 | + // Method Description: |
| 37 | + // - Sends a toast notification with the given title and message. |
| 38 | + // - When the user clicks the toast, the `Activated` callback fires |
| 39 | + // with the tabIndex that was passed in, so the caller can switch |
| 40 | + // to the correct tab and summon the window. |
| 41 | + // Arguments: |
| 42 | + // - args: The title, message, and tab index to include in the notification. |
| 43 | + // - activated: A callback invoked on the background thread when the |
| 44 | + // toast is clicked. The uint32_t parameter is the tab index. |
| 45 | + void DesktopNotification::SendNotification(const DesktopNotificationArgs& args, std::function<void()> activatedFunc) |
| 46 | + { |
| 47 | + try |
| 48 | + { |
| 49 | + if (!ShouldSendNotification()) |
| 50 | + { |
| 51 | + return; |
| 52 | + } |
| 53 | + |
| 54 | + // Build the toast XML. We use a simple template with a title and body text. |
| 55 | + // |
| 56 | + // <toast launch="--from-toast"> |
| 57 | + // <visual> |
| 58 | + // <binding template="ToastGeneric"> |
| 59 | + // <text>Title</text> |
| 60 | + // <text>Message</text> |
| 61 | + // </binding> |
| 62 | + // </visual> |
| 63 | + // </toast> |
| 64 | + auto toastXml = ToastNotificationManager::GetTemplateContent(ToastTemplateType::ToastText02); |
| 65 | + auto textNodes = toastXml.GetElementsByTagName(L"text"); |
| 66 | + |
| 67 | + // First <text> is the title |
| 68 | + textNodes.Item(0).InnerText(args.Title); |
| 69 | + // Second <text> is the body |
| 70 | + textNodes.Item(1).InnerText(args.Message); |
| 71 | + |
| 72 | + auto toastElement = toastXml.DocumentElement(); |
| 73 | + |
| 74 | + // When a toast is clicked, Windows launches a new instance of the app |
| 75 | + // with the "launch" attribute as command-line arguments. We handle |
| 76 | + // toast activation in-process via the Activated event below, so the |
| 77 | + // new instance should do nothing. "--from-toast" is recognized by |
| 78 | + // AppCommandlineArgs::ParseArgs as a no-op sentinel. |
| 79 | + toastElement.SetAttribute(L"launch", L"--from-toast"); |
| 80 | + |
| 81 | + toastElement.SetAttribute(L"scenario", L"default"); |
| 82 | + |
| 83 | + auto toast = ToastNotification{ toastXml }; |
| 84 | + |
| 85 | + // Set the tag and group to enable notification replacement. |
| 86 | + // Repeated notifications with the same tag replace the previous one |
| 87 | + // rather than stacking in the notification center. |
| 88 | + toast.Tag(args.Tag); |
| 89 | + toast.Group(L"WindowsTerminal"); |
| 90 | + |
| 91 | + // When the user activates (clicks) the toast, fire the callback. |
| 92 | + if (activatedFunc) |
| 93 | + { |
| 94 | + toast.Activated([activatedFunc](const auto& /*sender*/, const auto& /*eventArgs*/) { |
| 95 | + activatedFunc(); |
| 96 | + }); |
| 97 | + } |
| 98 | + |
| 99 | + // For packaged apps, CreateToastNotifier() uses the package identity automatically. |
| 100 | + // For unpackaged apps, we must pass the explicit AUMID that was registered |
| 101 | + // at startup via SetCurrentProcessExplicitAppUserModelID. |
| 102 | + winrt::Windows::UI::Notifications::ToastNotifier notifier{ nullptr }; |
| 103 | + if (IsPackaged()) |
| 104 | + { |
| 105 | + notifier = ToastNotificationManager::CreateToastNotifier(); |
| 106 | + } |
| 107 | + else |
| 108 | + { |
| 109 | + // Retrieve the AUMID that was set by WindowEmperor at startup. |
| 110 | + wil::unique_cotaskmem_string aumid; |
| 111 | + if (SUCCEEDED(GetCurrentProcessExplicitAppUserModelID(&aumid))) |
| 112 | + { |
| 113 | + notifier = ToastNotificationManager::CreateToastNotifier(aumid.get()); |
| 114 | + } |
| 115 | + } |
| 116 | + if (notifier) |
| 117 | + { |
| 118 | + notifier.Show(toast); |
| 119 | + } |
| 120 | + } |
| 121 | + catch (...) |
| 122 | + { |
| 123 | + // Toast notification is a best-effort feature. If it fails (e.g., notifications |
| 124 | + // are disabled, or the app is unpackaged without proper AUMID setup), we silently |
| 125 | + // ignore the error. |
| 126 | + LOG_CAUGHT_EXCEPTION(); |
| 127 | + } |
| 128 | + } |
| 129 | +} |
0 commit comments