From 83ad2068e0c188c947f1e3dd178e474e2bed8084 Mon Sep 17 00:00:00 2001 From: Joakim Hulthe Date: Thu, 12 Jun 2025 20:38:33 +0200 Subject: [PATCH] Make it run in wasm --- .cargo/config.toml | 2 ++ Cargo.lock | 4 ++++ Cargo.toml | 3 +++ src/app.rs | 5 +++-- src/painting.rs | 11 +++++++---- 5 files changed, 19 insertions(+), 6 deletions(-) create mode 100644 .cargo/config.toml diff --git a/.cargo/config.toml b/.cargo/config.toml new file mode 100644 index 0000000..2e07606 --- /dev/null +++ b/.cargo/config.toml @@ -0,0 +1,2 @@ +[target.wasm32-unknown-unknown] +rustflags = ['--cfg', 'getrandom_backend="wasm_js"'] diff --git a/Cargo.lock b/Cargo.lock index 81c139c..7b2cb7d 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -953,9 +953,11 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "73fea8450eea4bac3940448fb7ae50d91f034f941199fcd9d909a5a07aa455f0" dependencies = [ "cfg-if", + "js-sys", "libc", "r-efi", "wasi 0.14.2+wasi-0.2.4", + "wasm-bindgen", ] [[package]] @@ -1350,6 +1352,7 @@ dependencies = [ "egui_glow 0.31.1 (registry+https://github.com/rust-lang/crates.io-index)", "env_logger", "eyre", + "getrandom 0.3.2", "half", "insta", "log", @@ -1357,6 +1360,7 @@ dependencies = [ "rfd", "serde", "wasm-bindgen-futures", + "web-sys", "zerocopy 0.8.25", ] diff --git a/Cargo.toml b/Cargo.toml index 5bcbf74..d38fa8c 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -30,9 +30,12 @@ base64 = "0.22.1" [target.'cfg(not(target_arch = "wasm32"))'.dependencies] env_logger = "0.11.8" +rand = "0.9.1" [target.'cfg(target_arch = "wasm32")'.dependencies] +getrandom = { version = "0.3", features = ["wasm_js"] } wasm-bindgen-futures = "0.4.50" +web-sys = "0.3.77" [patch.crates-io] egui = { git = "https://github.com/emilk/egui", rev = "f2ce6424f3a32f47308fb9871d540c01377b2cd9" } diff --git a/src/app.rs b/src/app.rs index 80431f9..ca8d386 100644 --- a/src/app.rs +++ b/src/app.rs @@ -2,7 +2,6 @@ use std::{ fs, path::PathBuf, sync::{Arc, mpsc}, - thread, }; use crate::{file_editor::FileEditor, preferences::Preferences, util::GuiSender}; @@ -204,9 +203,10 @@ impl eframe::App for App { self.open_tab(Tab::File(file)); } + #[cfg(not(target_arch = "wasm32"))] if ui.button("Open File").clicked() { let actions_tx = self.actions_tx(ui.ctx()); - thread::spawn(move || { + std::thread::spawn(move || { let file = rfd::FileDialog::new().pick_file(); let Some(file_path) = file else { return }; @@ -263,6 +263,7 @@ impl eframe::App for App { } } + #[cfg(not(target_arch = "wasm32"))] if ui .add_enabled(open_file.is_some(), Button::new("Save As")) .clicked() diff --git a/src/painting.rs b/src/painting.rs index 3080d7a..83ea913 100644 --- a/src/painting.rs +++ b/src/painting.rs @@ -3,7 +3,6 @@ use std::{ iter, mem, str::FromStr, sync::Arc, - time::Instant, }; use base64::{Engine, prelude::BASE64_STANDARD}; @@ -436,7 +435,8 @@ impl Handwriting { self.refresh_texture = false; - let start_time = Instant::now(); + #[cfg(not(target_arch = "wasm32"))] + let start_time = std::time::Instant::now(); let mut tesselator = Tessellator::new( mesh_context.pixels_per_point, @@ -468,8 +468,11 @@ impl Handwriting { self.image = rasterize::(px_x, px_y, point_to_pixel, triangles); texture.set(self.image.clone(), Default::default()); - let elapsed = start_time.elapsed(); - println!("refreshed mesh in {:.3}s", elapsed.as_secs_f32()); + #[cfg(not(target_arch = "wasm32"))] + { + let elapsed = start_time.elapsed(); + println!("refreshed mesh in {:.3}s", elapsed.as_secs_f32()); + } } pub fn ui(&mut self, style: &HandwritingStyle, ui: &mut Ui) {