cleanup & polish

This commit is contained in:
Jonas Kruckenberg 2022-11-17 18:13:39 +01:00
parent ee68fe6b5b
commit d5fa1c0397
15 changed files with 657 additions and 470 deletions

View file

@ -1,5 +1,17 @@
use serde::{Deserialize, Serialize};
/// Checks if the permission to send notifications is granted.
///
/// # Example
///
/// ```rust,no_run
/// use tauri_sys::notification;
///
/// # async fn main() -> Result<(), Box<dyn std::error::Error>> {
/// let is_granted = notification::is_permission_granted().await?;
/// # Ok(())
/// # }
/// ```
#[inline(always)]
pub async fn is_permission_granted() -> crate::Result<bool> {
let raw = inner::isPermissionGranted().await?;
@ -7,6 +19,18 @@ pub async fn is_permission_granted() -> crate::Result<bool> {
Ok(serde_wasm_bindgen::from_value(raw)?)
}
/// Requests the permission to send notifications.
///
/// # Example
///
/// ```rust,no_run
/// use tauri_sys::notification;
///
/// # async fn main() -> Result<(), Box<dyn std::error::Error>> {
/// let perm = notification::request_permission().await?;
/// # Ok(())
/// # }
/// ```
#[inline(always)]
pub async fn request_permission() -> crate::Result<Permission> {
let raw = inner::requestPermission().await?;
@ -14,6 +38,7 @@ pub async fn request_permission() -> crate::Result<Permission> {
Ok(serde_wasm_bindgen::from_value(raw)?)
}
/// Possible permission values.
#[derive(Debug, Deserialize, Default, Clone, Copy, PartialEq, Eq)]
pub enum Permission {
#[default]
@ -25,34 +50,55 @@ pub enum Permission {
Denied,
}
/// The desktop notification definition.
///
/// Allows you to construct a Notification data and send it.
#[derive(Debug, Default, Serialize)]
pub struct Notification<'a> {
body: Option<&'a str>,
title: Option<&'a str>,
icon: Option<&'a str>
}
icon: Option<&'a str>,
}
impl<'a> Notification<'a> {
pub fn new() -> Self {
Self::default()
}
/// Sets the notification title.
pub fn set_title(&mut self, title: &'a str) {
self.title = Some(title);
}
/// Sets the notification body.
pub fn set_body(&mut self, body: &'a str) {
self.body = Some(body);
}
/// Sets the notification icon.
pub fn set_icon(&mut self, icon: &'a str) {
self.icon = Some(icon);
}
/// Shows the notification.
///
/// # Example
///
/// ```rust,no_run
/// use tauri_sys::notification::Notification;
///
/// # fn main() -> Result<(), Box<dyn std::error::Error>> {
/// Notification::new()
/// .set_title("Tauri")
/// .set_body("Tauri is awesome!")
/// .show()?;
/// # Ok(())
/// # }
/// ```
#[inline(always)]
pub fn show(&self) -> crate::Result<()> {
inner::sendNotification(serde_wasm_bindgen::to_value(&self)?)?;
Ok(())
}
}