dioxus_sdk_notification/
lib.rs1#![deny(missing_docs)]
5
6use std::{
7 error::Error,
8 fmt::{self, Display},
9 path::{Path, PathBuf},
10};
11#[derive(Debug, Clone, Default)]
27pub struct Notification {
28 app_name: String,
29 summary: String,
30 body: String,
31 icon_path: PathBuf,
32 timeout: NotificationTimeout,
33}
34
35#[derive(Debug, PartialEq, Clone, Default)]
37pub enum NotificationTimeout {
38 #[default]
40 Default,
41 Never,
43 Duration(std::time::Duration),
45}
46
47cfg_if::cfg_if! {
48 if #[cfg(any(target_os = "windows", target_os = "macos", target_os = "linux"))] {
49 use notify_rust::Timeout;
50 impl From<NotificationTimeout> for Timeout {
51 fn from(value: NotificationTimeout) -> Self {
52 match value {
53 NotificationTimeout::Default => Timeout::Default,
54 NotificationTimeout::Never => Timeout::Never,
55 NotificationTimeout::Duration(dur) => Timeout::Milliseconds(dur.as_millis().try_into().unwrap()),
56 }
57 }
58 }
59 }
60}
61
62impl Notification {
63 pub fn new() -> Self {
65 Self::default()
66 }
67
68 pub fn show(&self) -> Result<(), NotificationError> {
70 self.show_inner()
71 }
72
73 #[cfg(not(any(target_os = "windows", target_os = "macos", target_os = "linux")))]
75 fn show_inner(&self) -> Result<(), NotificationError> {
76 Err(NotificationError::Unsupported)
77 }
78
79 #[cfg(any(target_os = "windows", target_os = "macos", target_os = "linux"))]
81 fn show_inner(&self) -> Result<(), NotificationError> {
82 let icon_path =
83 self.icon_path
84 .as_os_str()
85 .to_str()
86 .ok_or(NotificationError::FailedToShow(
87 "failed to convert icon path into str".into(),
88 ))?;
89
90 notify_rust::Notification::new()
91 .appname(&self.app_name)
92 .summary(&self.summary)
93 .body(&self.body)
94 .icon(icon_path)
95 .timeout(self.timeout.clone())
96 .show()
97 .map_err(|e| NotificationError::FailedToShow(e.into()))?;
98
99 Ok(())
100 }
101
102 pub fn app_name(&mut self, value: impl ToString) -> &mut Self {
104 self.app_name = value.to_string();
105 self
106 }
107
108 pub fn summary(&mut self, value: impl ToString) -> &mut Self {
110 self.summary = value.to_string();
111 self
112 }
113
114 pub fn body(&mut self, value: impl ToString) -> &mut Self {
116 self.body = value.to_string();
117 self
118 }
119
120 pub fn icon_path(&mut self, value: impl AsRef<Path>) -> &mut Self {
124 self.icon_path = value.as_ref().to_path_buf();
125 self
126 }
127
128 pub fn timeout(&mut self, value: NotificationTimeout) -> &mut Self {
130 self.timeout = value;
131 self
132 }
133}
134
135#[test]
136#[cfg(any(target_os = "windows", target_os = "macos", target_os = "linux"))]
137fn test_notification() {
138 Notification::new()
139 .app_name("dioxus test".to_string())
140 .summary("hi, this is dioxus test".to_string())
141 .body("lorem ipsum??".to_string())
142 .show()
143 .unwrap();
144}
145
146#[derive(Debug)]
148pub enum NotificationError {
149 Unsupported,
151 FailedToShow(Box<dyn Error>),
153}
154
155impl Error for NotificationError {}
156impl Display for NotificationError {
157 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
158 match self {
159 Self::Unsupported => write!(f, "notification is not supported on this platform"),
160 Self::FailedToShow(err) => write!(f, "failed to show notification: {err}"),
161 }
162 }
163}