Skip to main content

notify_rust/
lib.rs

1//! Desktop Notifications for Rust.
2//!
3//! Desktop notifications are popup messages generated to notify the user of certain events.
4//!
5//! ## Platform Support
6//!
7//! This library was originally conceived with the [XDG](https://en.wikipedia.org/wiki/XDG) notification specification in mind.
8//! Since version 3.3 this crate also builds on macOS, however the semantics of the [XDG](https://en.wikipedia.org/wiki/XDG) specification and macOS `NSNotifications`
9//! are quite different.
10//! Therefore only a very small subset of functions is supported on macOS.
11//! Certain methods don't have any effect there, others will explicitly fail to compile,
12//! in these cases you will have to add platform specific toggles to your code.
13//! For more see [platform differences](#platform-differences)
14//!
15//! # Examples
16//!
17//! ## Example 1: Simple Notification
18//!
19//! ```no_run
20//! # use notify_rust::*;
21//! Notification::new()
22//!     .summary("Firefox News")
23//!     .body("This will almost look like a real firefox notification.")
24//!     .icon("firefox")
25//!     .timeout(Timeout::Milliseconds(6000)) //milliseconds
26//!     .show().unwrap();
27//! ```
28//!
29//! ## Example 2: Persistent Notification
30//!
31//! ```no_run
32//! # use notify_rust::*;
33//! Notification::new()
34//!     .summary("Category:email")
35//!     .body("This has nothing to do with emails.\nIt should not go away until you acknowledge it.")
36//!     .icon("thunderbird")
37//!     .appname("thunderbird")
38//!     .hint(Hint::Category("email".to_owned()))
39//!     .hint(Hint::Resident(true)) // this is not supported by all implementations
40//!     .timeout(Timeout::Never) // this however is
41//!     .show().unwrap();
42//! ```
43//!
44//! Careful! There are no checks whether you use hints twice.
45//! It is possible to set `urgency=Low` AND `urgency=Critical`, in which case the behavior of the server is undefined.
46//!
47//! ## Example 3: Ask the user to do something
48//!
49//! ```no_run
50//! # use notify_rust::*;
51//! # #[cfg(all(unix, not(target_os = "macos")))]
52//! Notification::new().summary("click me")
53//!                    .action("default", "default")
54//!                    .action("clicked", "click here")
55//!                    .hint(Hint::Resident(true))
56//!                    .show()
57//!                    .unwrap()
58//!                    .wait_for_action(|action| match action {
59//!                                         "default" => println!("you clicked \"default\""),
60//!                                         "clicked" => println!("that was correct"),
61//!                                         // here "__closed" is a hard coded keyword
62//!                                         "__closed" => println!("the notification was closed"),
63//!                                         _ => ()
64//!                                     });
65//! ```
66//!
67//! ## Minimal Example
68//!
69//! You can omit almost everything
70//!
71//! ```no_run
72//! # use notify_rust::Notification;
73//! Notification::new().show();
74//! ```
75//!
76//! more [examples](https://github.com/hoodie/notify-rust/tree/main/examples) in the repository.
77//!
78//! # Platform Differences
79//! <details>
80//! ✔︎ = works <br/>
81//! ❌ = will not compile
82//!
83//! ## `Notification`
84//! | method              | XDG   | macOS | windows |
85//! |---------------------|-------|-------|---------|
86//! |  `fn appname(...)`  |  ✔︎    |       |        |
87//! |  `fn summary(...)`  |  ✔︎    | ✔︎     |  ✔︎    |
88//! |  `fn subtitle(...)` |       | ✔︎     |  ✔︎    |
89//! |  `fn body(...)`     |  ✔︎    | ✔︎     |  ✔︎    |
90//! |  `fn icon(...)`     |  ✔︎    |       |        |
91//! |  `fn image_path(...)`|  ✔︎   | ✔︎     |  ✔︎    |
92//! |  `fn auto_icon(...)`|  ✔︎    |       |        |
93//! |  `fn hint(...)`     |  ✔︎    | ❌    | ❌    |
94//! |  `fn timeout(...)`  |  ✔︎    |       |  ✔︎    |
95//! |  `fn urgency(...)`  |  ✔︎    | ❌    |  ✔︎    |
96//! |  `fn action(...)`   |  ✔︎    |       |        |
97//! |  `fn id(...)`       |  ✔︎    |       |        |
98//! |  `fn finalize(...)` |  ✔︎    | ✔︎     |  ✔︎    |
99//! |  `fn show(...)`     |  ✔︎    | ✔︎     |  ✔︎    |
100//!
101//! ## `NotificationHandle`
102//!
103//! | method                   | XDG | macOS | windows |
104//! |--------------------------|-----|-------|---------|
105//! | `fn wait_for_action(...)`|  ✔︎  |  ❌  |   ❌   |
106//! | `fn close(...)`          |  ✔︎  |  ❌  |   ❌   |
107//! | `fn on_close(...)`       |  ✔︎  |  ❌  |   ❌   |
108//! | `fn update(...)`         |  ✔︎  |  ❌  |   ❌   |
109//! | `fn id(...)`             |  ✔︎  |  ❌  |   ❌   |
110//!
111//! ## Functions
112//!
113//! |                                            | XDG | macOS | windows |
114//! |--------------------------------------------|-----|-------|---------|
115//! | `fn get_capabilities(...)`                 | ✔︎   |   ❌ |  ❌    |
116//! | `fn get_server_information(...)`           | ✔︎   |   ❌ |  ❌    |
117//! | `fn set_application(...)`                  | ❌  |   ✔︎  |  ❌    |
118//! | `fn get_bundle_identifier_or_default(...)` | ❌  |   ✔︎  |  ❌    |
119//!
120//!
121//! ### Toggles
122//!
123//! Please use `target_os` toggles if you plan on using methods labeled with ❌.
124//!
125//! ```ignore
126//! #[cfg(target_os = "macos")]
127//! // or
128//! // #### #[cfg(all(unix, not(target_os = "macos")))]
129//! ```
130//! </details>
131//!
132
133#![deny(
134    missing_copy_implementations,
135    trivial_casts,
136    trivial_numeric_casts,
137    unsafe_code,
138    unused_import_braces,
139    unused_qualifications
140)]
141#![warn(
142    missing_docs,
143    clippy::doc_markdown,
144    clippy::semicolon_if_nothing_returned,
145    clippy::single_match_else,
146    clippy::inconsistent_struct_constructor,
147    clippy::map_unwrap_or,
148    clippy::match_same_arms
149)]
150
151#[cfg(all(feature = "dbus", unix, not(target_os = "macos")))]
152extern crate dbus;
153
154#[cfg(target_os = "macos")]
155extern crate mac_notification_sys;
156
157#[cfg(target_os = "windows")]
158extern crate winrt_notification;
159
160#[macro_use]
161#[cfg(all(feature = "images", unix, not(target_os = "macos")))]
162extern crate lazy_static;
163
164pub mod error;
165mod hints;
166mod miniver;
167mod notification;
168mod timeout;
169pub(crate) mod urgency;
170
171#[cfg(target_os = "macos")]
172mod macos;
173
174#[cfg(target_os = "windows")]
175mod windows;
176
177#[cfg(all(unix, not(target_os = "macos")))]
178mod xdg;
179
180#[cfg(all(feature = "images", unix, not(target_os = "macos")))]
181mod image;
182
183#[cfg(target_os = "macos")]
184pub use mac_notification_sys::{get_bundle_identifier_or_default, set_application};
185
186#[cfg(target_os = "macos")]
187pub use macos::NotificationHandle;
188
189#[cfg(all(
190    any(feature = "dbus", feature = "zbus"),
191    unix,
192    not(target_os = "macos")
193))]
194pub use crate::xdg::{
195    dbus_stack, get_capabilities, get_server_information, handle_action, ActionResponse,
196    CloseHandler, CloseReason, DbusStack, NotificationHandle,
197};
198
199// #[cfg(all(feature = "server", unix, not(target_os = "macos")))]
200// pub use crate::xdg::stop_server;
201
202pub use crate::hints::Hint;
203
204#[cfg(all(feature = "images", unix, not(target_os = "macos")))]
205pub use crate::image::{Image, ImageError};
206
207#[cfg_attr(
208    target_os = "macos",
209    deprecated(note = "Urgency is not supported on macOS")
210)]
211pub use crate::urgency::Urgency;
212
213pub use crate::{notification::Notification, timeout::Timeout};
214
215#[cfg(all(feature = "images", unix, not(target_os = "macos")))]
216lazy_static! {
217    /// Read once at runtime. Needed for Images
218    pub static ref SPEC_VERSION: miniver::Version =
219        get_server_information()
220        .and_then(|info| info.spec_version.parse::<miniver::Version>())
221        .unwrap_or_else(|_| miniver::Version::new(1,1));
222}
223/// Return value of `get_server_information()`.
224#[derive(Debug)]
225pub struct ServerInformation {
226    /// The product name of the server.
227    pub name: String,
228    /// The vendor name.
229    pub vendor: String,
230    /// The server's version string.
231    pub version: String,
232    /// The specification version the server is compliant with.
233    pub spec_version: String,
234}