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

#128 fix #129

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
22 changes: 18 additions & 4 deletions src/backend/wasm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ pub struct WasmDialog {
card: Element,
input: HtmlInputElement,
button: HtmlButtonElement,

style: Element,
no_overlay: bool,
}

impl WasmDialog {
Expand Down Expand Up @@ -75,6 +75,7 @@ impl WasmDialog {
input,

style,
no_overlay: opt.hide_wasm_file_dialog,
}
}

Expand All @@ -85,15 +86,28 @@ impl WasmDialog {

let overlay = self.overlay.clone();
let button = self.button.clone();

let input = self.input.clone();
let no_overlay = self.no_overlay;
let promise = js_sys::Promise::new(&mut move |res, _rej| {
let closure = Closure::wrap(Box::new(move || {
res.call0(&JsValue::undefined()).unwrap();
}) as Box<dyn FnMut()>);

button.set_onclick(Some(closure.as_ref().unchecked_ref()));
closure.forget();
if no_overlay {
overlay.set_class_name("hidden");
}else {
button.set_onclick(Some(closure.as_ref().unchecked_ref()));
}

body.append_child(&overlay).ok();
if no_overlay {
//INFO: When the file picker is closed it will focus the last focused element.
input.focus();
input.click();
input.set_onchange(Some(closure.as_ref().unchecked_ref()));
input.set_onfocus(Some(closure.as_ref().unchecked_ref()));
}
closure.forget();
});
let future = wasm_bindgen_futures::JsFuture::from(promise);
future.await.unwrap();
Expand Down
7 changes: 7 additions & 0 deletions src/backend/wasm/style.css
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,13 @@
justify-content: center;
align-items: center;
}

#rfd-overlay.hidden {
width: 0;
height: 0;
overflow: hidden;
}

#rfd-card {
padding: 20px;
background-color: white;
Expand Down
16 changes: 16 additions & 0 deletions src/file_dialog.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ pub struct FileDialog {
pub(crate) file_name: Option<String>,
pub(crate) title: Option<String>,
pub(crate) parent: Option<RawWindowHandle>,
pub hide_wasm_file_dialog: bool,
}

// Oh god, I don't like sending RawWindowHandle between threads but here we go anyways...
Expand Down Expand Up @@ -53,6 +54,13 @@ impl FileDialog {
self
}

/// Sets if the dialog html overlay should be hidden. Supported platforms:
/// * WASM
pub fn set_hide_overlay(mut self, hide: bool) -> Self {
self.hide_wasm_file_dialog = hide;
self
}

/// Set starting directory of the dialog. Supported platforms:
/// * Linux ([GTK only](https://github.com/PolyMeilex/rfd/issues/42))
/// * Windows
Expand Down Expand Up @@ -177,6 +185,14 @@ impl AsyncFileDialog {
self
}


/// Sets if the dialog html overlay should be hidden. Supported platforms:
/// * WASM
pub fn set_hide_overlay(mut self, hide: bool) -> Self {
self.file_dialog = self.file_dialog.set_hide_overlay(hide);
self
}

/// Set starting file name of the dialog. Supported platforms:
/// * Windows
/// * Linux
Expand Down