Skip to content
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
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ gtk-sys = { version = "0.18.0", features = ["v3_24"], optional = true }
glib-sys = { version = "0.18.0", optional = true }
gobject-sys = { version = "0.18.0", optional = true }

[target.'cfg(target_arch = "wasm32")'.dependencies]
[target.'cfg(target_family = "wasm")'.dependencies]
wasm-bindgen = "0.2.69"
js-sys = "0.3.46"
web-sys = { version = "0.3.46", features = [
Expand Down
11 changes: 7 additions & 4 deletions build.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
fn main() {
let target_os = std::env::var("CARGO_CFG_TARGET_OS").expect("target OS not detected");
let target_arch = std::env::var("CARGO_CFG_TARGET_ARCH").expect("target ARCH not detected");
let target_family =
std::env::var("CARGO_CFG_TARGET_FAMILY").expect("target FAMILY not detected");
let is_wasm = target_family.split(',').any(|f| f == "wasm");

match (target_arch.as_str(), target_os.as_str()) {
(_, "macos") => println!("cargo:rustc-link-lib=framework=AppKit"),
(_, "windows") => {}
("wasm32", _) => {}
match target_os.as_str() {
"macos" => println!("cargo:rustc-link-lib=framework=AppKit"),
"windows" => {}
_ if is_wasm => {}
_ => {
let gtk = std::env::var_os("CARGO_FEATURE_GTK3").is_some();
let xdg = std::env::var_os("CARGO_FEATURE_XDG_PORTAL").is_some();
Expand Down
6 changes: 3 additions & 3 deletions examples/async.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ fn main() {

if let Some(file) = file {
// If you are on native platform you can just get the path
#[cfg(not(target_arch = "wasm32"))]
#[cfg(not(target_family = "wasm"))]
println!("{:?}", file.path());

// If you care about wasm support you just read() the file
Expand All @@ -21,12 +21,12 @@ fn main() {

use std::future::Future;

#[cfg(not(target_arch = "wasm32"))]
#[cfg(not(target_family = "wasm"))]
fn execute<F: Future<Output = ()> + Send + 'static>(f: F) {
// this is stupid... use any executor of your choice instead
std::thread::spawn(move || futures::executor::block_on(f));
}
#[cfg(target_arch = "wasm32")]
#[cfg(target_family = "wasm")]
fn execute<F: Future<Output = ()> + 'static>(f: F) {
wasm_bindgen_futures::spawn_local(f);
}
4 changes: 2 additions & 2 deletions examples/save.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#[cfg(not(target_arch = "wasm32"))]
#[cfg(not(target_family = "wasm"))]
fn main() {
let path = std::env::current_dir().unwrap();

Expand All @@ -10,7 +10,7 @@ fn main() {
println!("The user choose: {:#?}", res);
}

#[cfg(target_arch = "wasm32")]
#[cfg(target_family = "wasm")]
fn main() {
// On wasm only async dialogs are possible
}
4 changes: 2 additions & 2 deletions examples/simple.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#[cfg(not(target_arch = "wasm32"))]
#[cfg(not(target_family = "wasm"))]
fn main() {
let path = std::env::current_dir().unwrap();

Expand All @@ -11,7 +11,7 @@ fn main() {
println!("The user choose: {:#?}", res);
}

#[cfg(target_arch = "wasm32")]
#[cfg(target_family = "wasm")]
fn main() {
// On wasm only async dialogs are possible
}
14 changes: 7 additions & 7 deletions examples/winit-example/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ fn main() {

let window = builder.build(&event_loop).unwrap();

#[cfg(target_arch = "wasm32")]
#[cfg(target_family = "wasm")]
{
use winit::platform::web::WindowExtWebSys;

Expand All @@ -41,9 +41,9 @@ fn main() {
event_loop
.run(move |event, target| match event {
event::Event::UserEvent(name) => {
#[cfg(target_arch = "wasm32")]
#[cfg(target_family = "wasm")]
alert(&name);
#[cfg(not(target_arch = "wasm32"))]
#[cfg(not(target_family = "wasm"))]
println!("{}", name);
}
event::Event::WindowEvent { event, .. } => match event {
Expand Down Expand Up @@ -205,23 +205,23 @@ fn main() {
use std::future::Future;

struct Executor {
#[cfg(not(target_arch = "wasm32"))]
#[cfg(not(target_family = "wasm"))]
pool: futures::executor::ThreadPool,
}

impl Executor {
fn new() -> Self {
Self {
#[cfg(not(target_arch = "wasm32"))]
#[cfg(not(target_family = "wasm"))]
pool: futures::executor::ThreadPool::new().unwrap(),
}
}

#[cfg(not(target_arch = "wasm32"))]
#[cfg(not(target_family = "wasm"))]
fn execut<F: Future<Output = ()> + Send + 'static>(&self, f: F) {
self.pool.spawn_ok(f);
}
#[cfg(target_arch = "wasm32")]
#[cfg(target_family = "wasm")]
fn execut<F: Future<Output = ()> + 'static>(&self, f: F) {
wasm_bindgen_futures::spawn_local(f);
}
Expand Down
16 changes: 8 additions & 8 deletions src/backend.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::message_dialog::MessageDialogResult;
use crate::FileHandle;
use std::future::Future;
#[cfg(not(target_arch = "wasm32"))]
#[cfg(not(target_family = "wasm"))]
use std::path::PathBuf;
use std::pin::Pin;

Expand Down Expand Up @@ -30,7 +30,7 @@ mod linux;
mod gtk3;
#[cfg(target_os = "macos")]
mod macos;
#[cfg(target_arch = "wasm32")]
#[cfg(target_family = "wasm")]
mod wasm;
#[cfg(target_os = "windows")]
mod win_cid;
Expand All @@ -51,20 +51,20 @@ mod xdg_desktop_portal;
//

/// Dialog used to pick file/files
#[cfg(not(target_arch = "wasm32"))]
#[cfg(not(target_family = "wasm"))]
pub trait FilePickerDialogImpl {
fn pick_file(self) -> Option<PathBuf>;
fn pick_files(self) -> Option<Vec<PathBuf>>;
}

/// Dialog used to save file
#[cfg(not(target_arch = "wasm32"))]
#[cfg(not(target_family = "wasm"))]
pub trait FileSaveDialogImpl {
fn save_file(self) -> Option<PathBuf>;
}

/// Dialog used to pick folder
#[cfg(not(target_arch = "wasm32"))]
#[cfg(not(target_family = "wasm"))]
pub trait FolderPickerDialogImpl {
fn pick_folder(self) -> Option<PathBuf>;
fn pick_folders(self) -> Option<Vec<PathBuf>>;
Expand All @@ -86,9 +86,9 @@ pub trait MessageDialogImpl {
//

// Return type of async dialogs:
#[cfg(not(target_arch = "wasm32"))]
#[cfg(not(target_family = "wasm"))]
pub type DialogFutureType<T> = Pin<Box<dyn Future<Output = T> + Send>>;
#[cfg(target_arch = "wasm32")]
#[cfg(target_family = "wasm")]
pub type DialogFutureType<T> = Pin<Box<dyn Future<Output = T>>>;

/// Dialog used to pick file/files
Expand All @@ -98,7 +98,7 @@ pub trait AsyncFilePickerDialogImpl {
}

/// Dialog used to pick folder
#[cfg(not(target_arch = "wasm32"))]
#[cfg(not(target_family = "wasm"))]
pub trait AsyncFolderPickerDialogImpl {
fn pick_folder_async(self) -> DialogFutureType<Option<FileHandle>>;
fn pick_folders_async(self) -> DialogFutureType<Option<Vec<FileHandle>>>;
Expand Down
12 changes: 6 additions & 6 deletions src/file_dialog.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ unsafe impl Sync for FileDialog {}

impl FileDialog {
/// New file dialog builder
#[cfg(not(target_arch = "wasm32"))]
#[cfg(not(target_family = "wasm"))]
pub fn new() -> Self {
Default::default()
}
Expand Down Expand Up @@ -121,13 +121,13 @@ impl FileDialog {
}
}

#[cfg(not(target_arch = "wasm32"))]
#[cfg(not(target_family = "wasm"))]
use crate::backend::{FilePickerDialogImpl, FileSaveDialogImpl, FolderPickerDialogImpl};

#[cfg(target_os = "macos")]
use crate::backend::FileOrFolderPickerDialogImpl;

#[cfg(not(target_arch = "wasm32"))]
#[cfg(not(target_family = "wasm"))]
impl FileDialog {
/// Pick one file
pub fn pick_file(self) -> Option<PathBuf> {
Expand Down Expand Up @@ -279,7 +279,7 @@ impl AsyncFileDialog {
use crate::backend::AsyncFileOrFolderPickerDialogImpl;
use crate::backend::AsyncFilePickerDialogImpl;
use crate::backend::AsyncFileSaveDialogImpl;
#[cfg(not(target_arch = "wasm32"))]
#[cfg(not(target_family = "wasm"))]
use crate::backend::AsyncFolderPickerDialogImpl;

use std::future::Future;
Expand All @@ -295,15 +295,15 @@ impl AsyncFileDialog {
AsyncFilePickerDialogImpl::pick_files_async(self.file_dialog)
}

#[cfg(not(target_arch = "wasm32"))]
#[cfg(not(target_family = "wasm"))]
/// Pick one folder
///
/// Does not exist in `WASM32`
pub fn pick_folder(self) -> impl Future<Output = Option<FileHandle>> {
AsyncFolderPickerDialogImpl::pick_folder_async(self.file_dialog)
}

#[cfg(not(target_arch = "wasm32"))]
#[cfg(not(target_family = "wasm"))]
/// Pick multiple folders
///
/// Does not exist in `WASM32`
Expand Down
12 changes: 6 additions & 6 deletions src/file_handle/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,16 @@
//!
//! It should allow a user to treat web browser files same way as native files

#[cfg(not(target_arch = "wasm32"))]
#[cfg(not(target_family = "wasm"))]
mod native;
#[cfg(not(target_arch = "wasm32"))]
#[cfg(not(target_family = "wasm"))]
pub use native::FileHandle;

#[cfg(target_arch = "wasm32")]
#[cfg(target_family = "wasm")]
mod web;
#[cfg(target_arch = "wasm32")]
#[cfg(target_family = "wasm")]
pub use web::FileHandle;
#[cfg(target_arch = "wasm32")]
#[cfg(target_family = "wasm")]
pub(crate) use web::WasmFileHandleKind;

#[cfg(test)]
Expand All @@ -27,7 +27,7 @@ mod tests {
let _ = FileHandle::read;
#[cfg(feature = "file-handle-inner")]
let _ = FileHandle::inner;
#[cfg(not(target_arch = "wasm32"))]
#[cfg(not(target_family = "wasm"))]
let _ = FileHandle::path;
}
}
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ pub use file_handle::FileHandle;
mod file_dialog;
mod oneshot;

#[cfg(not(target_arch = "wasm32"))]
#[cfg(not(target_family = "wasm"))]
pub use file_dialog::FileDialog;

pub use file_dialog::AsyncFileDialog;
Expand Down