feat: add notification module
This commit is contained in:
parent
45bd3650ea
commit
aec31b7e88
8 changed files with 125 additions and 4 deletions
|
@ -16,6 +16,8 @@ pub mod process;
|
|||
pub mod tauri;
|
||||
#[cfg(feature = "window")]
|
||||
pub mod window;
|
||||
#[cfg(feature = "notification")]
|
||||
pub mod notification;
|
||||
|
||||
#[derive(Debug, thiserror::Error)]
|
||||
pub enum Error {
|
||||
|
|
84
src/notification.rs
Normal file
84
src/notification.rs
Normal file
|
@ -0,0 +1,84 @@
|
|||
use log::debug;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use wasm_bindgen::JsValue;
|
||||
|
||||
pub async fn is_permission_granted() -> crate::Result<bool> {
|
||||
let raw = inner::isPermissionGranted().await?;
|
||||
|
||||
Ok(serde_wasm_bindgen::from_value(raw)?)
|
||||
}
|
||||
|
||||
pub async fn request_permission() -> crate::Result<Permission> {
|
||||
let raw = inner::requestPermission().await?;
|
||||
|
||||
Ok(serde_wasm_bindgen::from_value(raw)?)
|
||||
}
|
||||
|
||||
#[derive(Debug, Default, Clone, Copy, PartialEq, Eq)]
|
||||
pub enum Permission {
|
||||
#[default]
|
||||
Default,
|
||||
Granted,
|
||||
Denied,
|
||||
}
|
||||
|
||||
impl<'de> Deserialize<'de> for Permission {
|
||||
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
|
||||
where
|
||||
D: serde::Deserializer<'de>,
|
||||
{
|
||||
match String::deserialize(deserializer)?.as_str() {
|
||||
"default" => Ok(Permission::Default),
|
||||
"granted" => Ok(Permission::Granted),
|
||||
"denied" => Ok(Permission::Denied),
|
||||
_ => Err(serde::de::Error::custom(
|
||||
"expected one of default, granted, denied",
|
||||
)),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Default, Serialize)]
|
||||
pub struct Notification<'a> {
|
||||
body: Option<&'a str>,
|
||||
title: Option<&'a str>,
|
||||
icon: Option<&'a str>
|
||||
}
|
||||
|
||||
impl<'a> Notification<'a> {
|
||||
pub fn new() -> Self {
|
||||
Self::default()
|
||||
}
|
||||
|
||||
pub fn set_title(&mut self, title: &'a str) {
|
||||
self.title = Some(title);
|
||||
}
|
||||
|
||||
pub fn set_body(&mut self, body: &'a str) {
|
||||
self.body = Some(body);
|
||||
}
|
||||
|
||||
pub fn set_icon(&mut self, icon: &'a str) {
|
||||
self.icon = Some(icon);
|
||||
}
|
||||
|
||||
pub fn show(&self) -> crate::Result<()> {
|
||||
inner::sendNotification(serde_wasm_bindgen::to_value(&self)?)?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
mod inner {
|
||||
use wasm_bindgen::{prelude::wasm_bindgen, JsValue};
|
||||
|
||||
#[wasm_bindgen(module = "/src/notification.js")]
|
||||
extern "C" {
|
||||
#[wasm_bindgen(catch)]
|
||||
pub async fn isPermissionGranted() -> Result<JsValue, JsValue>;
|
||||
#[wasm_bindgen(catch)]
|
||||
pub async fn requestPermission() -> Result<JsValue, JsValue>;
|
||||
#[wasm_bindgen(catch)]
|
||||
pub fn sendNotification(notification: JsValue) -> Result<(), JsValue>;
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue