Skip to content
Merged
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
27 changes: 23 additions & 4 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -141,31 +141,50 @@ impl FromStr for Browser {
}
}

#[derive(Debug, Eq, PartialEq, Copy, Clone, Hash)]
#[derive(Debug, Eq, PartialEq, Clone, Hash)]
/// BrowserOptions to override certain default behaviour
///
/// e.g. by default, we suppress stdout/stderr, but that behaviour can be overridden here
pub struct BrowserOptions {
pub suppress_output: bool,
pub target_hint: String,
}

impl fmt::Display for BrowserOptions {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
f.write_fmt(format_args!(
"BrowserOptions(supress_output={})",
self.suppress_output
"BrowserOptions(supress_output={}, target_hint={})",
self.suppress_output, self.target_hint,
))
}
}

impl std::default::Default for BrowserOptions {
fn default() -> Self {
let target_hint = String::from(option_env!("WEBBROWSER_WASM_TARGET").unwrap_or("_blank"));
BrowserOptions {
suppress_output: true,
target_hint,
}
}
}

impl BrowserOptions {
pub fn new() -> Self {
Self::default()
}

pub fn with_suppress_output(&mut self, suppress_output: bool) -> &mut Self {
self.suppress_output = suppress_output;
self
}

pub fn with_target_hint(&mut self, target_hint: &str) -> &mut Self {
self.target_hint = target_hint.to_owned();
self
}
}

/// Opens the URL on the default browser of this platform
///
/// Returns Ok(..) so long as the browser invocation was successful. An Err(..) is returned only if
Expand Down Expand Up @@ -215,7 +234,7 @@ pub fn open_browser(browser: Browser, url: &str) -> Result<()> {
/// ```no_run
/// use webbrowser::{open_browser_with_options, Browser, BrowserOptions};
///
/// if open_browser_with_options(Browser::Default, "http://github.com", &BrowserOptions { suppress_output: false }).is_ok() {
/// if open_browser_with_options(Browser::Default, "http://github.com", BrowserOptions::new().with_suppress_output(false)).is_ok() {
/// // ...
/// }
/// ```
Expand Down
32 changes: 11 additions & 21 deletions src/wasm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,31 +4,21 @@ use crate::{Browser, BrowserOptions, Error, ErrorKind, Result};
/// and always opens URLs in the same browser where wasm32 vm is running.
#[inline]
pub fn open_browser_internal(_: Browser, url: &str, options: &BrowserOptions) -> Result<()> {
// we can override the target by the env var WEBBROWSER_WASM_TARGET at compile time
let configured_target = option_env!("WEBBROWSER_WASM_TARGET");
let window = web_sys::window();
match window {
Some(w) => {
let target = configured_target.unwrap_or_else(|| "_blank");
wasm_console_log(
&format!("target for url {} detected as {}", url, target),
options,
);

match w.open_with_url_and_target(url, target) {
Ok(x) => match x {
Some(_) => Ok(()),
None => {
wasm_console_log(POPUP_ERR_MSG, options);
Err(Error::new(ErrorKind::Other, POPUP_ERR_MSG))
}
},
Err(_) => {
wasm_console_log("window error while opening url", options);
Err(Error::new(ErrorKind::Other, "error opening url"))
Some(w) => match w.open_with_url_and_target(url, &options.target_hint) {
Ok(x) => match x {
Some(_) => Ok(()),
None => {
wasm_console_log(POPUP_ERR_MSG, options);
Err(Error::new(ErrorKind::Other, POPUP_ERR_MSG))
}
},
Err(_) => {
wasm_console_log("window error while opening url", options);
Err(Error::new(ErrorKind::Other, "error opening url"))
}
}
},
None => Err(Error::new(
ErrorKind::Other,
"should have a window in this context",
Expand Down