yew_stdweb/services/
dialog.rs1use cfg_if::cfg_if;
8use cfg_match::cfg_match;
9cfg_if! {
10 if #[cfg(feature = "std_web")] {
11 use stdweb::Value;
12 #[allow(unused_imports)]
13 use stdweb::{_js_impl, js};
14 } else if #[cfg(feature = "web_sys")] {
15 use crate::utils;
16 }
17}
18
19#[derive(Default, Debug)]
21pub struct DialogService {}
22
23impl DialogService {
24 pub fn alert(message: &str) {
27 cfg_match! {
28 feature = "std_web" => js! { @(no_return) alert(@{message}); },
29 feature = "web_sys" => utils::window().alert_with_message(message).unwrap(),
30 };
31 }
32
33 pub fn confirm(message: &str) -> bool {
36 cfg_match! {
37 feature = "std_web" => ({
38 let value: Value = js! { return confirm(@{message}); };
39 match value {
40 Value::Bool(result) => result,
41 _ => false,
42 }
43 }),
44 feature = "web_sys" => utils::window().confirm_with_message(message).unwrap(),
45 }
46 }
47
48 #[cfg_attr(
51 feature = "web_sys",
52 doc = "A default value can be supplied which will be returned if the user doesn't input anything."
53 )]
54 #[cfg_attr(
65 feature = "web_sys",
66 doc = "This function will return `None` if the value of `default` is `None` and the user \
67 cancels the operation. (normally a 'cancel' button will be displayed to the user, \
68 clicking which cancels the operation)."
69 )]
70 #[cfg_attr(
71 feature = "std_web",
72 doc = "This function will return `None` if the user cancels the operation (normally a \
73 'cancel' button will be displayed to the user, clicking which cancels the operation)."
74 )]
75 pub fn prompt(
76 message: &str,
77 #[cfg(feature = "web_sys")] default: Option<&str>,
78 ) -> Option<String> {
79 cfg_if! {
80 if #[cfg(feature="web_sys")] {
81 if let Some(default) = default {
82 utils::window()
83 .prompt_with_message_and_default(message, default)
84 .expect("Couldn't read input.")
85 }
86 else {
87 utils::window()
88 .prompt_with_message(message)
89 .expect("Couldn't read input.")
90 }
91 } else if #[cfg(feature="std_web")] {
92 let value: Value = js! { return prompt(@{message}); };
93 match value {
94 Value::String(result) => Some(result),
95 _ => None,
96 }
97 }
98 }
99 }
100}