feat: add notification module

This commit is contained in:
Jonas Kruckenberg 2022-11-15 15:57:02 +01:00
parent 45bd3650ea
commit aec31b7e88
8 changed files with 125 additions and 4 deletions

1
Cargo.lock generated
View file

@ -2963,6 +2963,7 @@ name = "tauri-sys"
version = "0.1.0"
dependencies = [
"js-sys",
"log",
"semver 1.0.14",
"serde",
"serde-wasm-bindgen",

View file

@ -14,6 +14,7 @@ wasm-bindgen-futures = "0.4.32"
url = { version = "2.3.1", optional = true, features = ["serde"] }
thiserror = "1.0.37"
semver = { version = "1.0.14", optional = true, features = ["serde"] }
log = "0.4.17"
[dev-dependencies]
wasm-bindgen-test = "0.3.33"
@ -24,7 +25,7 @@ tauri-sys = { path = ".", features = ["all"] }
all-features = true
[features]
all = ["app", "clipboard", "event", "mocks", "tauri", "window", "process", "dialog"]
all = ["app", "clipboard", "event", "mocks", "tauri", "window", "process", "dialog", "notification"]
app = ["dep:semver"]
clipboard = []
dialog = []
@ -33,6 +34,7 @@ mocks = []
tauri = ["dep:url"]
window = []
process = []
notification = []
[workspace]
members = ["examples/test", "examples/test/src-tauri"]

View file

@ -64,7 +64,7 @@ These API bindings are not completely on-par with `@tauri-apps/api` yet, but her
- [ ] `notification`
- [ ] `os`
- [ ] `path`
- [ ] `process`
- [x] `process`
- [ ] `shell`
- [x] `tauri`
- [ ] `updater`

View file

@ -5,7 +5,7 @@
use std::sync::atomic::{AtomicBool, Ordering};
use tauri::{Manager, State};
use tauri::{Manager, State, api::notification::Notification};
struct Received(AtomicBool);
// Learn more about Tauri commands at https://tauri.app/v1/guides/features/command

View file

@ -3,6 +3,7 @@ mod clipboard;
mod event;
mod window;
mod dialog;
mod notification;
extern crate console_error_panic_hook;
use std::future::Future;
@ -153,7 +154,10 @@ fn main() {
InteractiveTest(name="dialog::pick_folder",test=dialog::pick_folder())
InteractiveTest(name="dialog::pick_folders",test=dialog::pick_folders())
InteractiveTest(name="dialog::save",test=dialog::save())
Test(name="notification::is_permission_granted",test=notification::is_permission_granted())
Test(name="notification::request_permission",test=notification::request_permission())
InteractiveTest(name="notification::show_notification",test=notification::show_notification())
// Test(name="window::WebviewWindow::new",test=window::create_window())
Terminate

View file

@ -0,0 +1,28 @@
use anyhow::ensure;
use tauri_sys::notification::{self, Permission};
pub async fn is_permission_granted() -> anyhow::Result<()> {
let granted = notification::is_permission_granted().await?;
ensure!(granted);
Ok(())
}
pub async fn request_permission() -> anyhow::Result<()> {
let permission = notification::request_permission().await?;
ensure!(permission == Permission::Granted);
Ok(())
}
pub async fn show_notification() -> anyhow::Result<()> {
let mut n = notification::Notification::default();
n.set_title("TAURI");
n.set_body("Tauri is awesome!");
n.show()?;
Ok(())
}

View file

@ -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
View 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>;
}
}