Skip to content

PhotoCraft: An open-source, photo editor written by scratch in pure Rust

fix: use wasm-safe timing for Liquify (#393)

Nauman Ahmed870741a5cda1
diff --git a/crates/engine/src/distort_cmds.rs b/crates/engine/src/distort_cmds.rs
index c88f040fd..3f25c5d6a 100644
--- a/crates/engine/src/distort_cmds.rs
+++ b/crates/engine/src/distort_cmds.rs
@@ -22,6 +22,7 @@ use photocraft_raster::Surface;
use serde_json::{Value, json};
use crate::commands::CommandSpec;
+use crate::photo_cmds::Stopwatch;
use crate::{EngineError, Result, Session};
pub const LIQUIFY: &str = "filter.liquify";
@@ -278,7 +279,7 @@ fn liquify(s: &mut Session, p: &Value) -> Result<Value> {
if strokes.is_empty() {
return Ok(json!({"layer": id.0, "changed": false}));
}
- let t0 = std::time::Instant::now();
+ let t0 = Stopwatch::start();
let field = LiquifyField::from_strokes(canvas, cell, &strokes);
let smart = matches!(s.active().and_then(|d| d.doc.layer(id)).map(|l| &l.content), Some(LayerContent::Smart(_)));
if field.is_identity() && !smart {
@@ -300,7 +301,7 @@ fn liquify(s: &mut Session, p: &Value) -> Result<Value> {
None,
)?;
let mut r = r;
- r["ms"] = json!(t0.elapsed().as_secs_f64() * 1000.0);
+ r["ms"] = json!(t0.ms());
r["maxDisplacement"] = json!(field.max_displacement());
Ok(r)
}
diff --git a/crates/engine/src/distort_cmds/tests.rs b/crates/engine/src/distort_cmds/tests.rs
index fca77926c..96fe10187 100644
--- a/crates/engine/src/distort_cmds/tests.rs
+++ b/crates/engine/src/distort_cmds/tests.rs
@@ -91,6 +91,17 @@ fn liquify_respects_the_selection() {
assert_ne!(after.read_region(Rect::new(10, 10, 35, 40)), before.read_region(Rect::new(10, 10, 35, 40)));
}
+#[test]
+fn liquify_elapsed_time_is_wasm_safe() {
+ let mut s = session(8);
+ let r = s.execute(LIQUIFY, json!({"strokes": [{"tool": "forwardWarp", "size": 30, "points": [[30, 25], [42, 25]]}]})).unwrap();
+ assert_eq!(r["changed"], true);
+ let ms = r["ms"].as_f64().unwrap();
+ assert!(ms.is_finite() && ms >= 0.0);
+ #[cfg(target_arch = "wasm32")]
+ assert_eq!(ms, 0.0, "the web build uses the wasm-safe stopwatch fallback");
+}
+
#[test]
fn puppet_identity_translation_and_undo() {
for depth in [8u64, 16, 32] {