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 auto_icon(...)`| ✔︎ | | |
92//! | `fn hint(...)` | ✔︎ | ❌ | ❌ |
93//! | `fn timeout(...)` | ✔︎ | | ✔︎ |
94//! | `fn urgency(...)` | ✔︎ | ❌ | ❌ |
95//! | `fn action(...)` | ✔︎ | | |
96//! | `fn id(...)` | ✔︎ | | |
97//! | `fn finalize(...)` | ✔︎ | ✔︎ | ✔︎ |
98//! | `fn show(...)` | ✔︎ | ✔︎ | ✔︎ |
99//!
100//! ## `NotificationHandle`
101//!
102//! | method | XDG | macOS | windows |
103//! |--------------------------|-----|-------|---------|
104//! | `fn wait_for_action(...)`| ✔︎ | ❌ | ❌ |
105//! | `fn close(...)` | ✔︎ | ❌ | ❌ |
106//! | `fn on_close(...)` | ✔︎ | ❌ | ❌ |
107//! | `fn update(...)` | ✔︎ | ❌ | ❌ |
108//! | `fn id(...)` | ✔︎ | ❌ | ❌ |
109//!
110//! ## Functions
111//!
112//! | | XDG | macOS | windows |
113//! |--------------------------------------------|-----|-------|---------|
114//! | `fn get_capabilities(...)` | ✔︎ | ❌ | ❌ |
115//! | `fn get_server_information(...)` | ✔︎ | ❌ | ❌ |
116//! | `fn set_application(...)` | ❌ | ✔︎ | ❌ |
117//! | `fn get_bundle_identifier_or_default(...)` | ❌ | ✔︎ | ❌ |
118//!
119//!
120//! ### Toggles
121//!
122//! Please use `target_os` toggles if you plan on using methods labeled with ❌.
123//!
124//! ```ignore
125//! #[cfg(target_os = "macos")]
126//! // or
127//! // #### #[cfg(all(unix, not(target_os = "macos")))]
128//! ```
129//! </details>
130//!
131
132#![deny(
133 missing_copy_implementations,
134 trivial_casts,
135 trivial_numeric_casts,
136 unsafe_code,
137 unused_import_braces,
138 unused_qualifications
139)]
140#![warn(
141 missing_docs,
142 clippy::doc_markdown,
143 clippy::semicolon_if_nothing_returned,
144 clippy::single_match_else,
145 clippy::inconsistent_struct_constructor,
146 clippy::map_unwrap_or,
147 clippy::match_same_arms
148)]
149
150#[cfg(all(feature = "dbus", unix, not(target_os = "macos")))]
151extern crate dbus;
152
153#[cfg(target_os = "macos")]
154extern crate mac_notification_sys;
155
156#[cfg(target_os = "windows")]
157extern crate winrt_notification;
158
159#[macro_use]
160#[cfg(all(feature = "images", unix, not(target_os = "macos")))]
161extern crate lazy_static;
162
163pub mod error;
164mod hints;
165mod miniver;
166mod notification;
167mod timeout;
168pub(crate) mod urgency;
169
170#[cfg(target_os = "macos")]
171mod macos;
172
173#[cfg(target_os = "windows")]
174mod windows;
175
176#[cfg(all(unix, not(target_os = "macos")))]
177mod xdg;
178
179#[cfg(all(feature = "images", unix, not(target_os = "macos")))]
180mod image;
181
182#[cfg(target_os = "macos")]
183pub use mac_notification_sys::{get_bundle_identifier_or_default, set_application};
184
185#[cfg(target_os = "macos")]
186pub use macos::NotificationHandle;
187
188#[cfg(all(
189 any(feature = "dbus", feature = "zbus"),
190 unix,
191 not(target_os = "macos")
192))]
193pub use crate::xdg::{
194 dbus_stack, get_capabilities, get_server_information, handle_action, ActionResponse,
195 CloseHandler, CloseReason, DbusStack, NotificationHandle,
196};
197
198// #[cfg(all(feature = "server", unix, not(target_os = "macos")))]
199// pub use crate::xdg::stop_server;
200
201pub use crate::hints::Hint;
202
203#[cfg(all(feature = "images", unix, not(target_os = "macos")))]
204pub use crate::image::{Image, ImageError};
205
206#[cfg_attr(
207 target_os = "macos",
208 deprecated(note = "Urgency is not supported on macOS")
209)]
210pub use crate::urgency::Urgency;
211
212pub use crate::{notification::Notification, timeout::Timeout};
213
214#[cfg(all(feature = "images", unix, not(target_os = "macos")))]
215lazy_static! {
216 /// Read once at runtime. Needed for Images
217 pub static ref SPEC_VERSION: miniver::Version =
218 get_server_information()
219 .and_then(|info| info.spec_version.parse::<miniver::Version>())
220 .unwrap_or_else(|_| miniver::Version::new(1,1));
221}
222/// Return value of `get_server_information()`.
223#[derive(Debug)]
224pub struct ServerInformation {
225 /// The product name of the server.
226 pub name: String,
227 /// The vendor name.
228 pub vendor: String,
229 /// The server's version string.
230 pub version: String,
231 /// The specification version the server is compliant with.
232 pub spec_version: String,
233}