mirror of
https://github.com/electron/electron.git
synced 2026-04-10 03:01:51 -04:00
feat: show toast dismissal reason on Windows (#50030)
* feat: show toast dismissal reason on Windows Co-authored-by: Shelley Vohr <shelley.vohr@gmail.com> * Update docs/api/notification.md Co-authored-by: David Sanders <dsanders11@ucsbalum.com> Co-authored-by: Shelley Vohr <shelley.vohr@gmail.com> --------- Co-authored-by: trop[bot] <37223003+trop[bot]@users.noreply.github.com> Co-authored-by: Shelley Vohr <shelley.vohr@gmail.com>
This commit is contained in:
@@ -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.
|
||||
|
||||
|
||||
@@ -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() {
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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);
|
||||
|
||||
|
||||
@@ -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.
|
||||
|
||||
@@ -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:
|
||||
|
||||
@@ -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());
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user