| |
| |
| |
|
|
| use serde::de::DeserializeOwned; |
| use tauri::{ |
| plugin::{PermissionState, PluginApi}, |
| AppHandle, Runtime, |
| }; |
|
|
| use crate::NotificationBuilder; |
|
|
| pub fn init<R: Runtime, C: DeserializeOwned>( |
| app: &AppHandle<R>, |
| _api: PluginApi<R, C>, |
| ) -> crate::Result<Notification<R>> { |
| Ok(Notification(app.clone())) |
| } |
|
|
| |
| |
| |
| pub struct Notification<R: Runtime>(AppHandle<R>); |
|
|
| impl<R: Runtime> crate::NotificationBuilder<R> { |
| pub fn show(self) -> crate::Result<()> { |
| let mut notification = imp::Notification::new(self.app.config().identifier.clone()); |
|
|
| if let Some(title) = self |
| .data |
| .title |
| .or_else(|| self.app.config().product_name.clone()) |
| { |
| notification = notification.title(title); |
| } |
| if let Some(body) = self.data.body { |
| notification = notification.body(body); |
| } |
| if let Some(icon) = self.data.icon { |
| notification = notification.icon(icon); |
| } |
| if let Some(sound) = self.data.sound { |
| notification = notification.sound(sound); |
| } |
| #[cfg(feature = "windows7-compat")] |
| { |
| notification.notify(&self.app)?; |
| } |
| #[cfg(not(feature = "windows7-compat"))] |
| notification.show()?; |
|
|
| Ok(()) |
| } |
| } |
|
|
| impl<R: Runtime> Notification<R> { |
| pub fn builder(&self) -> NotificationBuilder<R> { |
| NotificationBuilder::new(self.0.clone()) |
| } |
|
|
| pub fn request_permission(&self) -> crate::Result<PermissionState> { |
| Ok(PermissionState::Granted) |
| } |
|
|
| pub fn permission_state(&self) -> crate::Result<PermissionState> { |
| Ok(PermissionState::Granted) |
| } |
| } |
|
|
| mod imp { |
| |
|
|
| #[cfg(windows)] |
| use std::path::MAIN_SEPARATOR as SEP; |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| #[allow(dead_code)] |
| #[derive(Debug, Default)] |
| pub struct Notification { |
| |
| body: Option<String>, |
| |
| title: Option<String>, |
| |
| icon: Option<String>, |
| |
| sound: Option<String>, |
| |
| identifier: String, |
| } |
|
|
| impl Notification { |
| |
| pub fn new(identifier: impl Into<String>) -> Self { |
| Self { |
| identifier: identifier.into(), |
| ..Default::default() |
| } |
| } |
|
|
| |
| #[must_use] |
| pub fn body(mut self, body: impl Into<String>) -> Self { |
| self.body = Some(body.into()); |
| self |
| } |
|
|
| |
| #[must_use] |
| pub fn title(mut self, title: impl Into<String>) -> Self { |
| self.title = Some(title.into()); |
| self |
| } |
|
|
| |
| #[must_use] |
| pub fn icon(mut self, icon: impl Into<String>) -> Self { |
| self.icon = Some(icon.into()); |
| self |
| } |
|
|
| |
| #[must_use] |
| pub fn sound(mut self, sound: impl Into<String>) -> Self { |
| self.sound = Some(sound.into()); |
| self |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| #[cfg_attr( |
| all(not(docsrs), feature = "windows7-compat"), |
| deprecated = "This function does not work on Windows 7. Use `Self::notify` instead." |
| )] |
| pub fn show(self) -> crate::Result<()> { |
| let mut notification = notify_rust::Notification::new(); |
| if let Some(body) = self.body { |
| notification.body(&body); |
| } |
| if let Some(title) = self.title { |
| notification.summary(&title); |
| } |
| if let Some(icon) = self.icon { |
| notification.icon(&icon); |
| } else { |
| notification.auto_icon(); |
| } |
| if let Some(sound) = self.sound { |
| notification.sound_name(&sound); |
| } |
| #[cfg(windows)] |
| { |
| let exe = tauri::utils::platform::current_exe()?; |
| let exe_dir = exe.parent().expect("failed to get exe directory"); |
| let curr_dir = exe_dir.display().to_string(); |
| |
| if !(curr_dir.ends_with(format!("{SEP}target{SEP}debug").as_str()) |
| || curr_dir.ends_with(format!("{SEP}target{SEP}release").as_str())) |
| { |
| notification.app_id(&self.identifier); |
| } |
| } |
| #[cfg(target_os = "macos")] |
| { |
| let _ = notify_rust::set_application(if tauri::is_dev() { |
| "com.apple.Terminal" |
| } else { |
| &self.identifier |
| }); |
| } |
|
|
| tauri::async_runtime::spawn(async move { |
| let _ = notification.show(); |
| }); |
|
|
| Ok(()) |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| #[cfg(feature = "windows7-compat")] |
| #[cfg_attr(docsrs, doc(cfg(feature = "windows7-compat")))] |
| #[allow(unused_variables)] |
| pub fn notify<R: tauri::Runtime>(self, app: &tauri::AppHandle<R>) -> crate::Result<()> { |
| #[cfg(windows)] |
| { |
| fn is_windows_7() -> bool { |
| let v = windows_version::OsVersion::current(); |
| |
| v.major == 6 && v.minor == 1 |
| } |
|
|
| if is_windows_7() { |
| self.notify_win7(app) |
| } else { |
| #[allow(deprecated)] |
| self.show() |
| } |
| } |
| #[cfg(not(windows))] |
| { |
| #[allow(deprecated)] |
| self.show() |
| } |
| } |
|
|
| |
| #[cfg(all(windows, feature = "windows7-compat"))] |
| fn notify_win7<R: tauri::Runtime>(self, app: &tauri::AppHandle<R>) -> crate::Result<()> { |
| let app_ = app.clone(); |
| let _ = app.clone().run_on_main_thread(move || { |
| let mut notification = win7_notifications::Notification::new(); |
| if let Some(body) = self.body { |
| notification.body(&body); |
| } |
| if let Some(title) = self.title { |
| notification.summary(&title); |
| } |
| if let Some(icon) = app_.default_window_icon() { |
| notification.icon(icon.rgba().to_vec(), icon.width(), icon.height()); |
| } |
| let _ = notification.show(); |
| }); |
|
|
| Ok(()) |
| } |
| } |
| } |
|
|