| 1 | //! Cross-platform notification response types. |
| 2 | //! |
| 3 | //! These types describe the outcome of a shown notification — whether the |
| 4 | //! user interacted with it or it was closed by the platform. |
| 5 | //! They are shared between all backends so consumer code does not need |
| 6 | //! a `cfg` switch to read responses. |
| 7 | |
| 8 | /// Reason a notification was closed without an action being invoked. |
| 9 | /// |
| 10 | /// ### Platform notes |
| 11 | /// |
| 12 | /// **XDG (Linux/BSD):** maps directly to `NotificationClosed` D-Bus signal reasons. |
| 13 | /// |
| 14 | /// **macOS:** the system does not distinguish close reasons, so all closes are |
| 15 | /// reported as [`CloseReason::Dismissed`]. |
| 16 | /// |
| 17 | /// **Windows:** `UserCanceled` → [`Dismissed`](CloseReason::Dismissed), |
| 18 | /// `TimedOut` → [`Expired`](CloseReason::Expired), |
| 19 | /// `ApplicationHidden` → [`CloseAction`](CloseReason::CloseAction). |
| 20 | // #[non_exhaustive] // TODO: mark in 5.0 |
| 21 | #[deriverust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/derive:(Copyrust-analyzer cargo core https://github.com/rust-lang/rust/library/core marker/Copy:, Clonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core clone/Clone:, Debugrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/macros/Debug:, PartialEqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/PartialEq:, Eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/Eq:, Hashrust-analyzer cargo core https://github.com/rust-lang/rust/library/core hash/macros/Hash:)] |
| 22 | pub enum CloseReasonrust-analyzer cargo notify-rust 4.18.0 response/CloseReason# { |
| 23 | /// The notification expired (timed out). |
| 24 | Expiredrust-analyzer cargo notify-rust 4.18.0 response/CloseReason#Expired#, |
| 25 | |
| 26 | /// The notification was dismissed by the user. |
| 27 | Dismissedrust-analyzer cargo notify-rust 4.18.0 response/CloseReason#Dismissed#, |
| 28 | |
| 29 | /// The notification was closed programmatically. |
| 30 | CloseActionrust-analyzer cargo notify-rust 4.18.0 response/CloseReason#CloseAction#, |
| 31 | |
| 32 | /// An unrecognised or reserved reason was reported by the platform. |
| 33 | Otherrust-analyzer cargo notify-rust 4.18.0 response/CloseReason#Other#(u32), |
| 34 | } |
| 35 | |
| 36 | impl Fromrust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/From#<u32> for CloseReasonrust-analyzer cargo notify-rust 4.18.0 response/CloseReason# { |
| 37 | fn fromrust-analyzer cargo notify-rust 4.18.0 response/impl#[CloseReason][`From<u32>`]from().(raw_reason: u32) -> Selfrust-analyzer cargo notify-rust 4.18.0 response/impl#[CloseReason][`From<u32>`] { |
| 38 | match raw_reason { |
| 39 | 1 => CloseReasonrust-analyzer cargo notify-rust 4.18.0 response/CloseReason#::Expiredrust-analyzer cargo notify-rust 4.18.0 response/CloseReason#Expired#, |
| 40 | 2 => CloseReasonrust-analyzer cargo notify-rust 4.18.0 response/CloseReason#::Dismissedrust-analyzer cargo notify-rust 4.18.0 response/CloseReason#Dismissed#, |
| 41 | 3 => CloseReasonrust-analyzer cargo notify-rust 4.18.0 response/CloseReason#::CloseActionrust-analyzer cargo notify-rust 4.18.0 response/CloseReason#CloseAction#, |
| 42 | other => CloseReasonrust-analyzer cargo notify-rust 4.18.0 response/CloseReason#::Otherrust-analyzer cargo notify-rust 4.18.0 response/CloseReason#Other#(other), |
| 43 | } |
| 44 | } |
| 45 | } |
| 46 | |
| 47 | /// The outcome of a shown notification. |
| 48 | /// |
| 49 | /// Returned by [`wait_for_response`](crate::NotificationHandle::wait_for_response). |
| 50 | /// |
| 51 | /// Match on this to handle every possible outcome: |
| 52 | /// |
| 53 | /// ```no_run |
| 54 | /// # use notify_rust::{NotificationResponse, CloseReason}; |
| 55 | /// # let response = NotificationResponse::Closed(CloseReason::Dismissed); |
| 56 | /// match response { |
| 57 | /// NotificationResponse::Default => println!("body clicked"), |
| 58 | /// NotificationResponse::Action(ref key) => println!("button '{key}' clicked"), |
| 59 | /// NotificationResponse::Reply(ref text) => println!("user replied: {text}"), |
| 60 | /// NotificationResponse::Closed(reason) => println!("closed: {reason:?}"), |
| 61 | /// } |
| 62 | /// ``` |
| 63 | #[deriverust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/derive:(Clonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core clone/Clone:, Debugrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/macros/Debug:, PartialEqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/PartialEq:, Eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/Eq:, Hashrust-analyzer cargo core https://github.com/rust-lang/rust/library/core hash/macros/Hash:)] |
| 64 | pub enum NotificationResponserust-analyzer cargo notify-rust 4.18.0 response/NotificationResponse# { |
| 65 | /// The default action was invoked — the user activated the notification without |
| 66 | /// choosing a specific button (e.g. clicked the body, tapped the banner). |
| 67 | /// |
| 68 | /// Corresponds to the D-Bus `"default"` action key, |
| 69 | /// Apple's `UNNotificationDefaultActionIdentifier`, and a body-click on Windows. |
| 70 | Defaultrust-analyzer cargo notify-rust 4.18.0 response/NotificationResponse#Default#, |
| 71 | |
| 72 | /// The user invoked a named action button. |
| 73 | Actionrust-analyzer cargo notify-rust 4.18.0 response/NotificationResponse#Action#(Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#), |
| 74 | |
| 75 | /// The user submitted an inline text reply. |
| 76 | /// |
| 77 | /// Only produced by the `preview-macos-un` backend (macOS `UNUserNotificationCenter` |
| 78 | /// with an inline reply action). On all other backends this variant is never emitted. |
| 79 | Replyrust-analyzer cargo notify-rust 4.18.0 response/NotificationResponse#Reply#(Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#), |
| 80 | |
| 81 | /// The notification was closed without any action being taken. |
| 82 | Closedrust-analyzer cargo notify-rust 4.18.0 response/NotificationResponse#Closed#(CloseReasonrust-analyzer cargo notify-rust 4.18.0 response/CloseReason#), |
| 83 | } |
| 84 | |
| 85 | impl NotificationResponserust-analyzer cargo notify-rust 4.18.0 response/NotificationResponse# { |
| 86 | /// Returns `true` if this response is the [`Default`](NotificationResponse::Default) variant. |
| 87 | pub fn is_default_actionrust-analyzer cargo notify-rust 4.18.0 response/impl#[NotificationResponse]is_default_action().(&self) -> bool { |
| 88 | matchesrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/matches!!(self, NotificationResponserust-analyzer cargo notify-rust 4.18.0 response/NotificationResponse#::Defaultrust-analyzer cargo notify-rust 4.18.0 response/NotificationResponse#Default#) |
| 89 | } |
| 90 | } |
| 91 | |
| 92 | impl Fromrust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/From#<Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#> for NotificationResponserust-analyzer cargo notify-rust 4.18.0 response/NotificationResponse# { |
| 93 | fn fromrust-analyzer cargo notify-rust 4.18.0 response/impl#[NotificationResponse][`From<String>`]from().(key: Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#) -> Selfrust-analyzer cargo notify-rust 4.18.0 response/impl#[NotificationResponse][`From<String>`] { |
| 94 | Selfrust-analyzer cargo notify-rust 4.18.0 response/impl#[NotificationResponse][`From<String>`]::Actionrust-analyzer cargo notify-rust 4.18.0 response/NotificationResponse#Action#(key) |
| 95 | } |
| 96 | } |
| 97 | |
| 98 | impl Fromrust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/From#<&str> for NotificationResponserust-analyzer cargo notify-rust 4.18.0 response/NotificationResponse# { |
| 99 | fn fromrust-analyzer cargo notify-rust 4.18.0 response/impl#[NotificationResponse][`From<&str>`]from().(key: &str) -> Selfrust-analyzer cargo notify-rust 4.18.0 response/impl#[NotificationResponse][`From<&str>`] { |
| 100 | Selfrust-analyzer cargo notify-rust 4.18.0 response/impl#[NotificationResponse][`From<&str>`]::Actionrust-analyzer cargo notify-rust 4.18.0 response/NotificationResponse#Action#(key.to_ownedrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc str/impl#[str][ToOwned]to_owned().()) |
| 101 | } |
| 102 | } |
| 103 | |
| 104 | /// Response to an action, a backward-compatible facade. |
| 105 | /// |
| 106 | /// This type is preserved for source compatibility with existing match arms and type signatures. |
| 107 | /// Prefer [`NotificationResponse`] for new code, which owns its data and covers more cases. |
| 108 | /// |
| 109 | /// **Deprecated since 4.18.0** — use [`NotificationResponse`] instead. |
| 110 | #[deriverust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/derive:(Clonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core clone/Clone:, Debugrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/macros/Debug:)] |
| 111 | pub enum ActionResponserust-analyzer cargo notify-rust 4.18.0 response/ActionResponse#<'a> { |
| 112 | /// The user clicked a named action button (or the notification body, key `"default"`). |
| 113 | Customrust-analyzer cargo notify-rust 4.18.0 response/ActionResponse#Custom#(&'a str), |
| 114 | /// The notification was closed without any action being taken. |
| 115 | Closedrust-analyzer cargo notify-rust 4.18.0 response/ActionResponse#Closed#(CloseReasonrust-analyzer cargo notify-rust 4.18.0 response/CloseReason#), |
| 116 | } |
| 117 | |
| 118 | impl<'a> Fromrust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/From#<&'a str> for ActionResponserust-analyzer cargo notify-rust 4.18.0 response/ActionResponse#<'a> { |
| 119 | fn fromrust-analyzer cargo notify-rust 4.18.0 response/impl#[`ActionResponse<'a>`][`From<&'a str>`]from().(raw: &'a str) -> Selfrust-analyzer cargo notify-rust 4.18.0 response/impl#[`ActionResponse<'a>`][`From<&'a str>`] { |
| 120 | Selfrust-analyzer cargo notify-rust 4.18.0 response/impl#[`ActionResponse<'a>`][`From<&'a str>`]::Customrust-analyzer cargo notify-rust 4.18.0 response/ActionResponse#Custom#(raw) |
| 121 | } |
| 122 | } |
| 123 | |
| 124 | /// Helper trait implemented by closures used with [`NotificationHandle::wait_for_response`](crate::NotificationHandle::wait_for_response). |
| 125 | /// |
| 126 | /// Any `FnOnce(&NotificationResponse)` closure automatically implements this trait. |
| 127 | pub trait ResponseHandlerrust-analyzer cargo notify-rust 4.18.0 response/ResponseHandler# { |
| 128 | /// Invoke the handler with the given response. |
| 129 | fn callrust-analyzer cargo notify-rust 4.18.0 response/ResponseHandler#call().(self, response: &NotificationResponserust-analyzer cargo notify-rust 4.18.0 response/NotificationResponse#); |
| 130 | } |
| 131 | |
| 132 | impl<F> ResponseHandlerrust-analyzer cargo notify-rust 4.18.0 response/ResponseHandler# for F |
| 133 | where |
| 134 | F: FnOncerust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/function/FnOnce#(&NotificationResponserust-analyzer cargo notify-rust 4.18.0 response/NotificationResponse#), |
| 135 | { |
| 136 | fn callrust-analyzer cargo notify-rust 4.18.0 response/impl#[F][ResponseHandler]call().(self, response: &NotificationResponserust-analyzer cargo notify-rust 4.18.0 response/NotificationResponse#) { |
| 137 | (self)(response); |
| 138 | } |
| 139 | } |
| 140 | |
| 141 | /// Callback for the close signal of a notification. |
| 142 | /// |
| 143 | /// Implemented for both `Fn(CloseReason)` and `Fn()`, so there is rarely |
| 144 | /// a good reason to implement this manually. |
| 145 | pub trait CloseHandlerrust-analyzer cargo notify-rust 4.18.0 response/CloseHandler#<T> { |
| 146 | /// Called with the [`CloseReason`]. |
| 147 | fn callrust-analyzer cargo notify-rust 4.18.0 response/CloseHandler#call().(&self, reason: CloseReasonrust-analyzer cargo notify-rust 4.18.0 response/CloseReason#); |
| 148 | } |
| 149 | |
| 150 | impl<F> CloseHandlerrust-analyzer cargo notify-rust 4.18.0 response/CloseHandler#<CloseReasonrust-analyzer cargo notify-rust 4.18.0 response/CloseReason#> for F |
| 151 | where |
| 152 | F: Fnrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/function/Fn#(CloseReasonrust-analyzer cargo notify-rust 4.18.0 response/CloseReason#), |
| 153 | { |
| 154 | fn callrust-analyzer cargo notify-rust 4.18.0 response/impl#[F][`CloseHandler<CloseReason>`]call().(&self, reason: CloseReasonrust-analyzer cargo notify-rust 4.18.0 response/CloseReason#) { |
| 155 | self(reason); |
| 156 | } |
| 157 | } |
| 158 | |
| 159 | impl<F> CloseHandlerrust-analyzer cargo notify-rust 4.18.0 response/CloseHandler#<()> for F |
| 160 | where |
| 161 | F: Fnrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/function/Fn#(), |
| 162 | { |
| 163 | fn callrust-analyzer cargo notify-rust 4.18.0 response/impl#[F][`CloseHandler<()>`]call().(&self, _reason: CloseReasonrust-analyzer cargo notify-rust 4.18.0 response/CloseReason#) { |
| 164 | self(); |
| 165 | } |
| 166 | } |