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

web-sys services conversion #827

Merged
merged 11 commits into from
Jan 15, 2020
54 changes: 54 additions & 0 deletions src/services/console.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
//! This module contains a service implementation to use browser's console.

#[cfg(feature = "std_web")]
#[allow(unused_imports)]
use stdweb::{_js_impl, js};
#[cfg(feature = "web_sys")]
use ::{wasm_bindgen::JsValue, web_sys::console};

/// A service to use methods of a
/// [Console](https://developer.mozilla.org/en-US/docs/Web/API/Console).
Expand All @@ -17,101 +20,152 @@ impl ConsoleService {
/// [console.log](https://developer.mozilla.org/en-US/docs/Web/API/Console/log)
/// method implementation.
pub fn log(&mut self, message: &str) {
#[cfg(feature = "std_web")]
js! { @(no_return) console.log(@{message}); }
#[cfg(feature = "web_sys")]
console::log_1(&JsValue::from_str(message));
}

/// [console.warn](https://developer.mozilla.org/en-US/docs/Web/API/Console/warn)
/// method implementation.
pub fn warn(&mut self, message: &str) {
#[cfg(feature = "std_web")]
js! { @(no_return) console.warn(@{message}); }
#[cfg(feature = "web_sys")]
console::warn_1(&JsValue::from_str(message));
}

/// [console.info](https://developer.mozilla.org/en-US/docs/Web/API/Console/info)
/// method implementation.
pub fn info(&mut self, message: &str) {
#[cfg(feature = "std_web")]
js! { @(no_return) console.info(@{message}); }
#[cfg(feature = "web_sys")]
console::info_1(&JsValue::from_str(message));
}

/// [console.error](https://developer.mozilla.org/en-US/docs/Web/API/Console/error)
/// method implementation.
pub fn error(&mut self, message: &str) {
#[cfg(feature = "std_web")]
js! { @(no_return) console.error(@{message}); }
#[cfg(feature = "web_sys")]
console::error_1(&JsValue::from_str(message));
}

/// [console.debug](https://developer.mozilla.org/en-US/docs/Web/API/Console/debug)
/// method implementation.
pub fn debug(&mut self, message: &str) {
#[cfg(feature = "std_web")]
js! { @(no_return) console.debug(@{message}); }
#[cfg(feature = "web_sys")]
console::debug_1(&JsValue::from_str(message));
}

/// [console.count_named](https://developer.mozilla.org/en-US/docs/Web/API/Console/count_named)
/// method implementation.
pub fn count_named(&mut self, name: &str) {
#[cfg(feature = "std_web")]
js! { @(no_return) console.count(@{name}); }
#[cfg(feature = "web_sys")]
console::count_with_label(name);
}

/// [console.count](https://developer.mozilla.org/en-US/docs/Web/API/Console/count)
/// method implementation.
pub fn count(&mut self) {
#[cfg(feature = "std_web")]
js! { @(no_return) console.count(); }
#[cfg(feature = "web_sys")]
console::count();
}

/// [console.time_named](https://developer.mozilla.org/en-US/docs/Web/API/Console/time_named)
/// method implementation.
pub fn time_named(&mut self, name: &str) {
#[cfg(feature = "std_web")]
js! { @(no_return) console.time(@{name}); }
#[cfg(feature = "web_sys")]
console::time_with_label(name);
}

/// [console.time_named_end](https://developer.mozilla.org/en-US/docs/Web/API/Console/time_named_end)
/// method implementation.
pub fn time_named_end(&mut self, name: &str) {
#[cfg(feature = "std_web")]
js! { @(no_return) console.timeEnd(@{name}); }
#[cfg(feature = "web_sys")]
console::time_end_with_label(name);
}

/// [console.time](https://developer.mozilla.org/en-US/docs/Web/API/Console/time)
/// method implementation.
pub fn time(&mut self) {
#[cfg(feature = "std_web")]
js! { @(no_return) console.time(); }
#[cfg(feature = "web_sys")]
console::time();
}
/// [console.time_end](https://developer.mozilla.org/en-US/docs/Web/API/Console/time_end)
/// method implementation.
pub fn time_end(&mut self) {
#[cfg(feature = "std_web")]
js! { @(no_return) console.timeEnd(); }
#[cfg(feature = "web_sys")]
console::time_end();
}

/// [console.clear](https://developer.mozilla.org/en-US/docs/Web/API/Console/clear)
/// method implementation.
pub fn clear(&mut self) {
#[cfg(feature = "std_web")]
js! { @(no_return) console.clear(); }
#[cfg(feature = "web_sys")]
console::clear();
}

/// [console.group](https://developer.mozilla.org/en-US/docs/Web/API/Console/group)
/// method implementation.
pub fn group(&mut self) {
#[cfg(feature = "std_web")]
js! { @(no_return) console.group(); }
#[cfg(feature = "web_sys")]
console::group_0();
}

/// [console.group_collapsed](https://developer.mozilla.org/en-US/docs/Web/API/Console/group_collapsed)
/// method implementation.
pub fn group_collapsed(&mut self) {
#[cfg(feature = "std_web")]
js! { @(no_return) console.groupCollapsed(); }
#[cfg(feature = "web_sys")]
console::group_collapsed_0();
}

/// [console.group_end](https://developer.mozilla.org/en-US/docs/Web/API/Console/group_end)
/// method implementation.
pub fn group_end(&mut self) {
#[cfg(feature = "std_web")]
js! { @(no_return) console.groupEnd(); }
#[cfg(feature = "web_sys")]
console::group_end();
}

/// [console.trace](https://developer.mozilla.org/en-US/docs/Web/API/Console/trace)
/// method implementation.
pub fn trace(&mut self) {
#[cfg(feature = "std_web")]
js! { @(no_return) console.trace(); }
#[cfg(feature = "web_sys")]
console::trace_0();
}

/// [console.assert](https://developer.mozilla.org/en-US/docs/Web/API/Console/assert)
/// method implementation.
pub fn assert(&mut self, condition: bool, message: &str) {
#[cfg(feature = "std_web")]
js! { @(no_return) console.assert(@{condition}, @{message}); }
#[cfg(feature = "web_sys")]
console::assert_with_condition_and_data_1(condition, &String::from(message).into());
}
}
24 changes: 20 additions & 4 deletions src/services/dialog.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
//! This module contains the implementation of a service
//! to show alerts and confirm dialogs in a browser.

#[cfg(feature = "std_web")]
use stdweb::Value;
#[cfg(feature = "std_web")]
#[allow(unused_imports)]
use stdweb::{_js_impl, js};

Expand All @@ -18,16 +20,30 @@ impl DialogService {
/// Calls [alert](https://developer.mozilla.org/en-US/docs/Web/API/Window/alert)
/// function.
pub fn alert(&mut self, message: &str) {
#[cfg(feature = "std_web")]
js! { @(no_return) alert(@{message}); }
#[cfg(feature = "web_sys")]
web_sys::window()
.unwrap()
.alert_with_message(message)
.unwrap();
daxpedda marked this conversation as resolved.
Show resolved Hide resolved
}

/// Calls [confirm](https://developer.mozilla.org/en-US/docs/Web/API/Window/confirm)
/// function.
pub fn confirm(&mut self, message: &str) -> bool {
let value: Value = js! { return confirm(@{message}); };
match value {
Value::Bool(result) => result,
_ => false,
#[cfg(feature = "std_web")]
{
let value: Value = js! { return confirm(@{message}); };
match value {
Value::Bool(result) => result,
_ => false,
}
}
#[cfg(feature = "web_sys")]
web_sys::window()
.unwrap()
.confirm_with_message(message)
.unwrap()
}
}
Loading