Compare commits

...

2 Commits

Author SHA1 Message Date
Shelley Vohr
0e824e14b9 Update docs/api/notification.md
Co-authored-by: David Sanders <dsanders11@ucsbalum.com>
2026-02-25 15:59:24 +01:00
Shelley Vohr
2e81b564f2 feat: show toast dismissal reason on Windows 2026-02-23 13:10:03 +01:00
8 changed files with 43 additions and 10 deletions

View File

@@ -111,7 +111,8 @@ app.whenReady().then(() => {
Returns:
* `event` Event
* `details` Event\<\>
* `reason` _Windows_ string (optional) - The reason the notification was closed. This can be 'userCanceled', 'applicationHidden', or 'timedOut'.
Emitted when the notification is closed by manual intervention from the user.

View File

@@ -189,8 +189,23 @@ void Notification::NotificationFailed(const std::string& error) {
void Notification::NotificationDestroyed() {}
void Notification::NotificationClosed() {
Emit("close");
void Notification::NotificationClosed(const std::string& reason) {
if (reason.empty()) {
Emit("close");
} else {
v8::Isolate* isolate = JavascriptEnvironment::GetIsolate();
v8::HandleScope handle_scope(isolate);
gin_helper::internal::Event* event =
gin_helper::internal::Event::New(isolate);
v8::Local<v8::Object> event_object =
event->GetWrapper(isolate).ToLocalChecked();
gin_helper::Dictionary dict(isolate, event_object);
dict.Set("reason", reason);
EmitWithoutEvent("close", event_object);
}
}
void Notification::Close() {

View File

@@ -50,7 +50,7 @@ class Notification final : public gin_helper::DeprecatedWrappable<Notification>,
void NotificationReplied(const std::string& reply) override;
void NotificationDisplayed() override;
void NotificationDestroyed() override;
void NotificationClosed() override;
void NotificationClosed(const std::string& reason) override;
void NotificationFailed(const std::string& error) override;
// gin_helper::Wrappable

View File

@@ -46,9 +46,10 @@ void Notification::NotificationClicked() {
Destroy();
}
void Notification::NotificationDismissed(bool should_destroy) {
void Notification::NotificationDismissed(bool should_destroy,
const std::string& close_reason) {
if (delegate())
delegate()->NotificationClosed();
delegate()->NotificationClosed(close_reason);
set_is_dismissed(true);

View File

@@ -76,7 +76,8 @@ class Notification {
// Should be called by derived classes.
void NotificationClicked();
void NotificationDismissed(bool should_destroy = true);
void NotificationDismissed(bool should_destroy = true,
const std::string& close_reason = "");
void NotificationFailed(const std::string& error = "");
// delete this.

View File

@@ -24,7 +24,7 @@ class NotificationDelegate {
virtual void NotificationAction(int action_index, int selection_index = -1) {}
virtual void NotificationClick() {}
virtual void NotificationClosed() {}
virtual void NotificationClosed(const std::string& reason = "") {}
virtual void NotificationDisplayed() {}
protected:

View File

@@ -60,7 +60,7 @@ class NotificationDelegateImpl final : public electron::NotificationDelegate {
->DispatchNonPersistentClickEvent(notification_id_, base::DoNothing());
}
void NotificationClosed() override {
void NotificationClosed(const std::string& reason) override {
content::NotificationEventDispatcher::GetInstance()
->DispatchNonPersistentCloseEvent(notification_id_, base::DoNothing());
}

View File

@@ -800,9 +800,24 @@ IFACEMETHODIMP ToastEventHandler::Invoke(
IFACEMETHODIMP ToastEventHandler::Invoke(
winui::Notifications::IToastNotification* sender,
winui::Notifications::IToastDismissedEventArgs* e) {
winui::Notifications::ToastDismissalReason reason;
std::string reason_string;
if (SUCCEEDED(e->get_Reason(&reason))) {
switch (reason) {
case winui::Notifications::ToastDismissalReason_UserCanceled:
reason_string = "userCanceled";
break;
case winui::Notifications::ToastDismissalReason_ApplicationHidden:
reason_string = "applicationHidden";
break;
case winui::Notifications::ToastDismissalReason_TimedOut:
reason_string = "timedOut";
break;
}
}
content::GetUIThreadTaskRunner({})->PostTask(
FROM_HERE, base::BindOnce(&Notification::NotificationDismissed,
notification_, false));
notification_, false, reason_string));
DebugLog("Notification dismissed");
return S_OK;
}