user_notify/
lib.rs

1//! Show desktop notifications to end users on linux, macOS and windows.
2//!
3//! If you want to see the platform implementation and view this on docs.rs, then make sure to select the correct platform in the header bar.
4
5#![deny(
6    unused,
7    clippy::correctness,
8    missing_debug_implementations,
9    missing_docs,
10    clippy::all,
11    clippy::doc_broken_link,
12    clippy::wildcard_imports,
13    clippy::needless_borrow,
14    clippy::cast_lossless,
15    clippy::unused_async,
16    clippy::explicit_iter_loop,
17    clippy::explicit_into_iter_loop,
18    clippy::cloned_instead_of_copied
19)]
20mod error;
21mod notification;
22mod platform_impl;
23mod xdg_category;
24
25use std::sync::Arc;
26
27pub use error::Error;
28pub use notification::*;
29pub use platform_impl::*;
30pub use xdg_category::*;
31
32/// Get the notification manager for the platform
33///
34/// `app_id` and `notification_protocol` are only used on windows
35#[allow(unused_variables)]
36pub fn get_notification_manager(
37    app_id: String,
38    notification_protocol: Option<String>,
39) -> Arc<dyn NotificationManager> {
40    #[cfg(target_os = "macos")]
41    {
42        use objc2_foundation::NSBundle;
43        if NSBundle::mainBundle().bundleIdentifier().is_none() {
44            return Arc::new(platform_impl::mock::NotificationManagerMock::new())
45                as Arc<dyn NotificationManager>;
46        }
47        Arc::new(platform_impl::mac_os::NotificationManagerMacOS::new())
48            as Arc<dyn NotificationManager>
49    }
50    #[cfg(target_os = "windows")]
51    {
52        use ::windows::core::HSTRING;
53        match ::windows::UI::Notifications::ToastNotificationManager::CreateToastNotifierWithId(
54            &HSTRING::from(&app_id),
55        ) {
56            Ok(_tf) => Arc::new(platform_impl::windows::NotificationManagerWindows::new(
57                app_id.clone(),
58                notification_protocol,
59            )) as Arc<dyn NotificationManager>,
60            Err(err) => {
61                log::error!(
62                    "failed to get toast notifier for {app_id}, falling back to mock notifification manager: {err:?}"
63                );
64                Arc::new(platform_impl::mock::NotificationManagerMock::new())
65                    as Arc<dyn NotificationManager>
66            }
67        }
68    }
69    #[cfg(any(
70        target_os = "linux",
71        target_os = "dragonfly",
72        target_os = "freebsd",
73        target_os = "openbsd",
74        target_os = "netbsd"
75    ))]
76    {
77        Arc::new(platform_impl::xdg::NotificationManagerXdg::new()) as Arc<dyn NotificationManager>
78    }
79}