diff --git a/src/lib.rs b/src/lib.rs index 0d8420f..bcf55fb 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -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 @@ -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() { /// // ... /// } /// ``` diff --git a/src/wasm.rs b/src/wasm.rs index 078708d..8721301 100644 --- a/src/wasm.rs +++ b/src/wasm.rs @@ -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",