yew_stdweb/services/dialog.rs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
//! This module contains the implementation of a service
//! to show alerts and confirm dialogs in a browser.
//!
//! If you call these methods repeatably browsers tend to disable these options to give users
//! a better experience.
use cfg_if::cfg_if;
use cfg_match::cfg_match;
cfg_if! {
if #[cfg(feature = "std_web")] {
use stdweb::Value;
#[allow(unused_imports)]
use stdweb::{_js_impl, js};
} else if #[cfg(feature = "web_sys")] {
use crate::utils;
}
}
/// A dialog service.
#[derive(Default, Debug)]
pub struct DialogService {}
impl DialogService {
/// Calls [alert](https://developer.mozilla.org/en-US/docs/Web/API/Window/alert)
/// function.
pub fn alert(message: &str) {
cfg_match! {
feature = "std_web" => js! { @(no_return) alert(@{message}); },
feature = "web_sys" => utils::window().alert_with_message(message).unwrap(),
};
}
/// Calls [confirm](https://developer.mozilla.org/en-US/docs/Web/API/Window/confirm)
/// function.
pub fn confirm(message: &str) -> bool {
cfg_match! {
feature = "std_web" => ({
let value: Value = js! { return confirm(@{message}); };
match value {
Value::Bool(result) => result,
_ => false,
}
}),
feature = "web_sys" => utils::window().confirm_with_message(message).unwrap(),
}
}
/// Prompts the user to input a message. In most browsers this will open an alert box with
/// an input field where the user can input a message.
#[cfg_attr(
feature = "web_sys",
doc = "A default value can be supplied which will be returned if the user doesn't input anything."
)]
///
/// [MDN Documentation](https://developer.mozilla.org/en-US/docs/Web/API/Window/prompt)
///
/// This method will `panic!` if there is an error in the process of trying to carry out this
/// operation.
///
/// Note that this function is blocking; no other code can be run on the thread while
/// the user inputs their message which means that the page will appear to have 'frozen'
/// while the user types in their message.
///
#[cfg_attr(
feature = "web_sys",
doc = "This function will return `None` if the value of `default` is `None` and the user \
cancels the operation. (normally a 'cancel' button will be displayed to the user, \
clicking which cancels the operation)."
)]
#[cfg_attr(
feature = "std_web",
doc = "This function will return `None` if the user cancels the operation (normally a \
'cancel' button will be displayed to the user, clicking which cancels the operation)."
)]
pub fn prompt(
message: &str,
#[cfg(feature = "web_sys")] default: Option<&str>,
) -> Option<String> {
cfg_if! {
if #[cfg(feature="web_sys")] {
if let Some(default) = default {
utils::window()
.prompt_with_message_and_default(message, default)
.expect("Couldn't read input.")
}
else {
utils::window()
.prompt_with_message(message)
.expect("Couldn't read input.")
}
} else if #[cfg(feature="std_web")] {
let value: Value = js! { return prompt(@{message}); };
match value {
Value::String(result) => Some(result),
_ => None,
}
}
}
}
}