diff --git a/Cargo.lock b/Cargo.lock index ba8bcad..3bfbaee 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -227,7 +227,7 @@ dependencies = [ "clipboard-win", "image", "log", - "objc2 0.6.1", + "objc2 0.6.4", "objc2-app-kit 0.3.1", "objc2-core-foundation", "objc2-core-graphics", @@ -288,6 +288,17 @@ dependencies = [ "slab", ] +[[package]] +name = "async-fs" +version = "2.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8034a681df4aed8b8edbd7fbe472401ecf009251c8b40556b304567052e294c5" +dependencies = [ + "async-lock", + "blocking", + "futures-lite", +] + [[package]] name = "async-io" version = "2.4.1" @@ -318,6 +329,17 @@ dependencies = [ "pin-project-lite", ] +[[package]] +name = "async-net" +version = "2.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b948000fad4873c1c9339d60f2623323a0cfd3816e5181033c6a5cb68b2accf7" +dependencies = [ + "async-io", + "blocking", + "futures-lite", +] + [[package]] name = "async-process" version = "2.3.1" @@ -490,6 +512,15 @@ dependencies = [ "objc2 0.5.2", ] +[[package]] +name = "block2" +version = "0.6.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cdeb9d870516001442e364c5220d3574d2da8dc765554b4a617230d33fa58ef5" +dependencies = [ + "objc2 0.6.4", +] + [[package]] name = "blocking" version = "1.6.2" @@ -511,7 +542,9 @@ dependencies = [ "egui", "env_logger", "log", + "rfd", "serde", + "smol", "tinytemplate", "wasm-bindgen-futures", "web-sys", @@ -752,7 +785,9 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "89a09f22a6c6069a18470eb92d2298acf25463f14256d24778e1230d789a2aec" dependencies = [ "bitflags 2.9.4", - "objc2 0.6.1", + "block2 0.6.2", + "libc", + "objc2 0.6.4", ] [[package]] @@ -1252,7 +1287,7 @@ dependencies = [ "glutin_glx_sys", "glutin_wgl_sys", "libloading", - "objc2 0.6.1", + "objc2 0.6.4", "objc2-app-kit 0.3.1", "objc2-core-foundation", "objc2-foundation 0.3.1", @@ -1810,9 +1845,9 @@ dependencies = [ [[package]] name = "objc2" -version = "0.6.1" +version = "0.6.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "88c6597e14493ab2e44ce58f2fdecf095a51f12ca57bec060a11c57332520551" +checksum = "3a12a8ed07aefc768292f076dc3ac8c48f3781c8f2d5851dd3d98950e8c5a89f" dependencies = [ "objc2-encode", ] @@ -1824,7 +1859,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e4e89ad9e3d7d297152b17d39ed92cd50ca8063a89a9fa569046d41568891eff" dependencies = [ "bitflags 2.9.4", - "block2", + "block2 0.5.1", "libc", "objc2 0.5.2", "objc2-core-data", @@ -1840,7 +1875,8 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e6f29f568bec459b0ddff777cec4fe3fd8666d82d5a40ebd0ff7e66134f89bcc" dependencies = [ "bitflags 2.9.4", - "objc2 0.6.1", + "block2 0.6.2", + "objc2 0.6.4", "objc2-core-foundation", "objc2-core-graphics", "objc2-foundation 0.3.1", @@ -1853,7 +1889,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "74dd3b56391c7a0596a295029734d3c1c5e7e510a4cb30245f8221ccea96b009" dependencies = [ "bitflags 2.9.4", - "block2", + "block2 0.5.1", "objc2 0.5.2", "objc2-core-location", "objc2-foundation 0.2.2", @@ -1865,7 +1901,7 @@ version = "0.2.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a5ff520e9c33812fd374d8deecef01d4a840e7b41862d849513de77e44aa4889" dependencies = [ - "block2", + "block2 0.5.1", "objc2 0.5.2", "objc2-foundation 0.2.2", ] @@ -1877,7 +1913,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "617fbf49e071c178c0b24c080767db52958f716d9eabdf0890523aeae54773ef" dependencies = [ "bitflags 2.9.4", - "block2", + "block2 0.5.1", "objc2 0.5.2", "objc2-foundation 0.2.2", ] @@ -1890,7 +1926,7 @@ checksum = "1c10c2894a6fed806ade6027bcd50662746363a9589d3ec9d9bef30a4e4bc166" dependencies = [ "bitflags 2.9.4", "dispatch2", - "objc2 0.6.1", + "objc2 0.6.4", ] [[package]] @@ -1901,7 +1937,7 @@ checksum = "989c6c68c13021b5c2d6b71456ebb0f9dc78d752e86a98da7c716f4f9470f5a4" dependencies = [ "bitflags 2.9.4", "dispatch2", - "objc2 0.6.1", + "objc2 0.6.4", "objc2-core-foundation", "objc2-io-surface", ] @@ -1912,7 +1948,7 @@ version = "0.2.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "55260963a527c99f1819c4f8e3b47fe04f9650694ef348ffd2227e8196d34c80" dependencies = [ - "block2", + "block2 0.5.1", "objc2 0.5.2", "objc2-foundation 0.2.2", "objc2-metal", @@ -1924,7 +1960,7 @@ version = "0.2.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "000cfee34e683244f284252ee206a27953279d370e309649dc3ee317b37e5781" dependencies = [ - "block2", + "block2 0.5.1", "objc2 0.5.2", "objc2-contacts", "objc2-foundation 0.2.2", @@ -1943,7 +1979,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "0ee638a5da3799329310ad4cfa62fbf045d5f56e3ef5ba4149e7452dcf89d5a8" dependencies = [ "bitflags 2.9.4", - "block2", + "block2 0.5.1", "dispatch", "libc", "objc2 0.5.2", @@ -1956,7 +1992,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "900831247d2fe1a09a683278e5384cfb8c80c79fe6b166f9d14bfdde0ea1b03c" dependencies = [ "bitflags 2.9.4", - "objc2 0.6.1", + "objc2 0.6.4", "objc2-core-foundation", ] @@ -1967,7 +2003,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "7282e9ac92529fa3457ce90ebb15f4ecbc383e8338060960760fa2cf75420c3c" dependencies = [ "bitflags 2.9.4", - "objc2 0.6.1", + "objc2 0.6.4", "objc2-core-foundation", ] @@ -1977,7 +2013,7 @@ version = "0.2.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a1a1ae721c5e35be65f01a03b6d2ac13a54cb4fa70d8a5da293d7b0020261398" dependencies = [ - "block2", + "block2 0.5.1", "objc2 0.5.2", "objc2-app-kit 0.2.2", "objc2-foundation 0.2.2", @@ -1990,7 +2026,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "dd0cba1276f6023976a406a14ffa85e1fdd19df6b0f737b063b95f6c8c7aadd6" dependencies = [ "bitflags 2.9.4", - "block2", + "block2 0.5.1", "objc2 0.5.2", "objc2-foundation 0.2.2", ] @@ -2002,7 +2038,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e42bee7bff906b14b167da2bac5efe6b6a07e6f7c0a21a7308d40c960242dc7a" dependencies = [ "bitflags 2.9.4", - "block2", + "block2 0.5.1", "objc2 0.5.2", "objc2-foundation 0.2.2", "objc2-metal", @@ -2025,7 +2061,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b8bb46798b20cd6b91cbd113524c490f1686f4c4e8f49502431415f3512e2b6f" dependencies = [ "bitflags 2.9.4", - "block2", + "block2 0.5.1", "objc2 0.5.2", "objc2-cloud-kit", "objc2-core-data", @@ -2045,7 +2081,7 @@ version = "0.2.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "44fa5f9748dbfe1ca6c0b79ad20725a11eca7c2218bceb4b005cb1be26273bfe" dependencies = [ - "block2", + "block2 0.5.1", "objc2 0.5.2", "objc2-foundation 0.2.2", ] @@ -2057,7 +2093,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "76cfcbf642358e8689af64cee815d139339f3ed8ad05103ed5eaf73db8d84cb3" dependencies = [ "bitflags 2.9.4", - "block2", + "block2 0.5.1", "objc2 0.5.2", "objc2-core-location", "objc2-foundation 0.2.2", @@ -2215,6 +2251,12 @@ dependencies = [ "windows-sys 0.59.0", ] +[[package]] +name = "pollster" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2f3a9f18d041e6d0e102a0a46750538147e5e8992d3b4873aaafee2520b00ce3" + [[package]] name = "portable-atomic" version = "1.11.1" @@ -2356,6 +2398,33 @@ version = "1.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "19b30a45b0cd0bcca8037f3d0dc3421eaf95327a17cad11964fb8179b4fc4832" +[[package]] +name = "rfd" +version = "0.17.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "20dafead71c16a34e1ff357ddefc8afc11e7d51d6d2b9fbd07eaa48e3e540220" +dependencies = [ + "block2 0.6.2", + "dispatch2", + "js-sys", + "libc", + "log", + "objc2 0.6.4", + "objc2-app-kit 0.3.1", + "objc2-core-foundation", + "objc2-foundation 0.3.1", + "percent-encoding", + "pollster", + "raw-window-handle", + "wasm-bindgen", + "wasm-bindgen-futures", + "wayland-backend", + "wayland-client", + "wayland-protocols", + "web-sys", + "windows-sys 0.61.2", +] + [[package]] name = "ron" version = "0.11.0" @@ -2566,6 +2635,23 @@ dependencies = [ "wayland-backend", ] +[[package]] +name = "smol" +version = "2.0.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a33bd3e260892199c3ccfc487c88b2da2265080acb316cd920da72fdfd7c599f" +dependencies = [ + "async-channel", + "async-executor", + "async-fs", + "async-io", + "async-lock", + "async-net", + "async-process", + "blocking", + "futures-lite", +] + [[package]] name = "smol_str" version = "0.2.2" @@ -3043,7 +3129,7 @@ dependencies = [ "jni", "log", "ndk-context", - "objc2 0.6.1", + "objc2 0.6.4", "objc2-foundation 0.3.1", "url", "web-sys", @@ -3596,7 +3682,7 @@ dependencies = [ "android-activity", "atomic-waker", "bitflags 2.9.4", - "block2", + "block2 0.5.1", "bytemuck", "calloop", "cfg_aliases", diff --git a/Cargo.toml b/Cargo.toml index 4fdbd72..70f57c6 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -26,10 +26,12 @@ log = "0.4.27" serde = { version = "1.0.219", features = ["derive"] } tinytemplate = "1.2.1" +rfd = "0.17.2" # native: [target.'cfg(not(target_arch = "wasm32"))'.dependencies] env_logger = "0.11.8" +smol = "2.0.2" # web: [target.'cfg(target_arch = "wasm32")'.dependencies] diff --git a/src/app.rs b/src/app.rs index c2a4385..3eadf7f 100644 --- a/src/app.rs +++ b/src/app.rs @@ -1,4 +1,15 @@ use std::fs::DirEntry; +use std::ops::DerefMut; +use std::sync::Arc; + +use egui::mutex::Mutex; +#[cfg(target_arch = "wasm32")] +use wasm_bindgen_futures; +#[cfg(target_arch = "wasm32")] +use web_sys; + +#[cfg(not(target_arch = "wasm32"))] +use smol; use tinytemplate; @@ -16,6 +27,10 @@ pub struct TemplateApp { value: f32, #[serde(skip)] templates: Vec, + + #[serde(skip)] + cont_mux: Arc>, + content: String, } impl Default for TemplateApp { @@ -26,6 +41,8 @@ impl Default for TemplateApp { label: "Hello World!".to_owned(), value: 2.7, templates: Vec::new(), + cont_mux: Arc::new(Mutex::new(String::new())), + content: "No File selected".to_string(), } } } @@ -85,10 +102,17 @@ impl eframe::App for TemplateApp { }); ui.add(egui::Slider::new(&mut self.value, 0.0..=10.0).text("value")); - if ui.button("Increment").clicked() { + if ui.button("test").clicked() { self.value += 1.0; } + if ui.button("rfd").clicked() { + let file = test_async(self.cont_mux.clone()); + } + self.content = self.cont_mux.lock().clone(); + + ui.text_edit_multiline(&mut self.content); + ui.separator(); ui.add(egui::github_link_file!( @@ -96,6 +120,7 @@ impl eframe::App for TemplateApp { "Source code." )); + ui.with_layout(egui::Layout::bottom_up(egui::Align::LEFT), |ui| { powered_by_egui_and_eframe(ui); egui::warn_if_debug_build(ui); @@ -117,3 +142,47 @@ fn powered_by_egui_and_eframe(ui: &mut egui::Ui) { ui.label("."); }); } + +fn test_async(test: Arc>) { + // Spawn dialog on main thread + let task = rfd::AsyncFileDialog::new().pick_file(); + + // Await somewhere else + execute(async move { + + let file = task.await; + + if let Some(file) = file { + // If you care about wasm support you just read() the file + let output = file.read().await; + + let string_data = match String::from_utf8(output) { + Ok(t) => t, + Err(e) => e.to_string(), + }; + + #[cfg(target_arch = "wasm32")] + web_sys::console::log_1(&string_data.clone().into()); + + let mut t = test.lock(); + let a = t.deref_mut(); + *a = string_data; + } + }); +} + +#[cfg(not(target_arch = "wasm32"))] +fn execute + Send + 'static>(f: F) { + std::thread::spawn(move || smol::block_on(f)); +} + +#[cfg(target_arch = "wasm32")] +fn execute + 'static>(f: F) { + web_sys::console::log_1(&"Async Start".into()); + wasm_bindgen_futures::spawn_local(f); + web_sys::console::log_1(&"Async End".into()); +} + +enum FileError { + Unknown, +} \ No newline at end of file