Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: notifications crate #61

Draft
wants to merge 7 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,11 @@ version = "0.1.0"
[dependencies]
gloo-timers = { version = "0.1.0", path = "crates/timers" }
gloo-console-timer = { version = "0.1.0", path = "crates/console-timer" }
gloo-notifications = { version = "0.1.0", path = "crates/notifications" }
gloo-events = { version = "0.1.0", path = "crates/events" }

[features]
default = []
futures = ["gloo-timers/futures"]
futures = ["gloo-timers/futures", "gloo-notifications/futures"]

[workspace]
36 changes: 36 additions & 0 deletions crates/notifications/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
[package]
name = "gloo-notifications"
version = "0.1.0"
authors = ["Rust and WebAssembly Working Group"]
edition = "2018"

[dependencies]
wasm-bindgen = "0.2.40"
js-sys = "0.3.17"

[dependencies.futures_rs]
package = "futures"
version = "0.1.25"
optional = true

[dependencies.wasm-bindgen-futures]
version = "0.3.17"
optional = true

[dependencies.web-sys]
version = "0.3.17"
features = [
"Notification",
"NotificationOptions",
"NotificationDirection",
"NotificationPermission",
"GetNotificationOptions",
]

[features]
default = []
futures = ["futures_rs", "wasm-bindgen-futures"]


[dev-dependencies]
wasm-bindgen-test = "0.2.40"
115 changes: 115 additions & 0 deletions crates/notifications/src/builder.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
use crate::Notification;
use wasm_bindgen::JsValue;
use web_sys::{NotificationDirection, NotificationOptions};

/// A builder for a `Notification`.
///
/// The builder is turned into a `Notification` by calling `.show()`,
/// which displays the notifcation on the screen.
///
/// Example:
///
/// ```rust
/// use gloo_notifications::Notification;
///
/// Notification::request_permission()
/// .map(|mut builder| {
/// let _notification = builder
/// .title("Notification title")
/// .body("Notification body")
/// .show();
/// })
/// ```
#[derive(Debug)]
pub struct NotificationBuilder<'a> {
title: &'a str,
sys_builder: NotificationOptions,
}

impl<'a> NotificationBuilder<'a> {
#[inline]
pub(crate) fn new() -> Self {
NotificationBuilder {
title: "",
sys_builder: NotificationOptions::new(),
}
}

#[inline]
pub(crate) fn get_inner(&self) -> (&str, &NotificationOptions) {
(self.title, &self.sys_builder)
}

/// Sets the title of the notification
#[inline]
#[must_use = "You have to call .show() to display the notification"]
pub fn title(&mut self, title: &'a str) -> &mut Self {
self.title = title;
self
}

/// Sets the body text of the notification
#[inline]
#[must_use = "You have to call .show() to display the notification"]
pub fn body(&mut self, body: &str) -> &mut Self {
self.sys_builder.body(body);
self
}

/// Sets the data of the notification
#[inline]
#[must_use = "You have to call .show() to display the notification"]
pub fn data(&mut self, val: &JsValue) -> &mut Self {
self.sys_builder.data(val);
self
}

/// Sets the direction of the notification, which is either Auto, Ltr or Rtl.
#[inline]
#[must_use = "You have to call .show() to display the notification"]
pub fn dir(&mut self, dir: NotificationDirection) -> &mut Self {
self.sys_builder.dir(dir);
self
}

/// Sets the icon of the notification
#[inline]
#[must_use = "You have to call .show() to display the notification"]
pub fn icon(&mut self, val: &str) -> &mut Self {
self.sys_builder.icon(val);
self
}

/// Sets the language of the notification
#[inline]
#[must_use = "You have to call .show() to display the notification"]
pub fn lang(&mut self, val: &str) -> &mut Self {
self.sys_builder.lang(val);
self
}

/// Sets the requireInteraction property.
///
/// If set to `true`, the notification stays visible until the user activates or closes it.
#[inline]
#[must_use = "You have to call .show() to display the notification"]
pub fn require_interaction(&mut self, val: bool) -> &mut Self {
self.sys_builder.require_interaction(val);
self
}

/// Sets the tag of the notification
#[inline]
#[must_use = "You have to call .show() to display the notification"]
pub fn tag(&mut self, val: &str) -> &mut Self {
self.sys_builder.tag(val);
self
}

/// Returns a new Notification from this builder
/// and displays it in the browser, if the permission is granted
#[inline]
pub fn show(&self) -> Notification {
Notification::new(self)
}
}
34 changes: 34 additions & 0 deletions crates/notifications/src/future.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
//! This module provides the `Notification::request_permission()` function,
//! which returns a `futures_rs::Future`.

extern crate futures_rs as futures;

use futures::Future;
use wasm_bindgen::{JsValue, UnwrapThrowExt};
use wasm_bindgen_futures::JsFuture;

use crate::{Notification, NotificationBuilder};

impl Notification {
/// ```rust
/// use gloo_notifications::Notification;
///
/// Notification::request_permission()
/// .map(|mut builder| {
/// let _notification = builder
/// .title("Hello World")
/// .show();
/// })
/// .map_err(|_| {
/// // in case the permission is denied
/// })
/// ```
///
#[must_use = "futures do nothing unless polled"]
pub fn request_permission<'a>() -> impl Future<Item = NotificationBuilder<'a>, Error = JsValue>
{
let promise = web_sys::Notification::request_permission().unwrap_throw();

JsFuture::from(promise).map(|_| NotificationBuilder::new())
}
}
Loading