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

Add target_hint to BrowserOptions #45

Merged
merged 3 commits into from
Apr 22, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
4 changes: 3 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -141,12 +141,13 @@ 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 {
Expand All @@ -162,6 +163,7 @@ impl std::default::Default for BrowserOptions {
fn default() -> Self {
BrowserOptions {
suppress_output: true,
target_hint: String::from("_blank"),
amodm marked this conversation as resolved.
Show resolved Hide resolved
}
}
}
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