From 60b07b483f49adfee51480b029f23f7c4913886f Mon Sep 17 00:00:00 2001 From: Johannes Hofmann Date: Tue, 27 Oct 2026 18:01:44 +0100 Subject: web: fix pinch zoom — stale use_state snapshots clobbered each other MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit A UseStateHandle cloned into a callback derefs to its render-time snapshot (documented Yew behaviour). Pinch emitted zoom and pan as two separate read-modify-writes per event: both read the same stale snapshot, so the pan write reverted the zoom write on every event and the scale never moved. Wheel zoom survived because it emits alone with a render between events. Fix: keep the authoritative ViewState in a use_mut_ref mirror with an apply_view helper (read live, mutate, write mirror + use_state), and fold pinch zoom+pan into a single combined update. Removes all the pinch diagnostic probes (debug callback, event counters, build stamp). --- crates/wipfel/src/app.rs | 77 ++++++++++++++++++++++++++++++++---------------- 1 file changed, 52 insertions(+), 25 deletions(-) (limited to 'crates/wipfel/src/app.rs') diff --git a/crates/wipfel/src/app.rs b/crates/wipfel/src/app.rs index 7d8eef6..1843021 100644 --- a/crates/wipfel/src/app.rs +++ b/crates/wipfel/src/app.rs @@ -149,7 +149,25 @@ fn find_peak_panel(p: &FpProps) -> Html { pub fn app() -> Html { let bitmap = use_state(|| None::); let view = use_state(ViewState::default); - let status = use_state(|| "No image loaded · build 27k".to_string()); + // Live view state for the event callbacks. A UseStateHandle cloned + // into a callback derefs to its render-time snapshot (documented + // Yew behaviour), so consecutive read-modify-writes within one + // frame clobber each other — this silently killed pinch zoom, where + // the pan emit reverted the zoom emit of the same event. The + // mirror is the live source of truth; every write goes through + // apply_view, which also pushes to the use_state for rendering. + let view_live = use_mut_ref(ViewState::default); + let apply_view: Rc = { + let view = view.clone(); + let view_live = view_live.clone(); + Rc::new(move |f: &dyn Fn(&mut ViewState)| { + let mut v = (*view_live.borrow()).clone(); + f(&mut v); + *view_live.borrow_mut() = v.clone(); + view.set(v); + }) + }; + let status = use_state(|| "No image loaded".to_string()); let db_status = use_state(|| "Loading hill database…".to_string()); let fit_info = use_state(String::new); let tick = use_state(|| 0u32); @@ -223,11 +241,9 @@ pub fn app() -> Html { // Fit the current bitmap into the canvas, centered. let fit = { let bitmap = bitmap.clone(); - let view = view.clone(); let canvas_ref = canvas_ref.clone(); - let status = status.clone(); + let apply = apply_view.clone(); Callback::from(move |_: MouseEvent| { - status.set("FIT ran".to_string()); let Some(bmp) = bitmap.as_ref() else { return }; let (cw, ch) = canvas_ref .cast::() @@ -238,10 +254,10 @@ pub fn app() -> Html { return; } let scale = (cw / iw).min(ch / ih); - view.set(ViewState { - scale, - ox: (cw - iw * scale) / 2.0, - oy: (ch - ih * scale) / 2.0, + apply(&move |v| { + v.scale = scale; + v.ox = (cw - iw * scale) / 2.0; + v.oy = (ch - ih * scale) / 2.0; }); }) }; @@ -659,28 +675,43 @@ pub fn app() -> Html { let callbacks = ViewCallbacks { pan: { - let view = view.clone(); + let apply = apply_view.clone(); let user_moved = user_moved.clone(); Callback::from(move |(dx, dy): (f64, f64)| { *user_moved.borrow_mut() = true; - let mut v = (*view).clone(); - v.ox += dx; - v.oy += dy; - view.set(v); + apply(&move |v| { + v.ox += dx; + v.oy += dy; + }); }) }, zoom: { - let view = view.clone(); + let apply = apply_view.clone(); let user_moved = user_moved.clone(); Callback::from(move |(cx, cy, factor): (f64, f64, f64)| { *user_moved.borrow_mut() = true; - let v = &*view; - let ns = (v.scale * factor).clamp(0.02, 64.0); - let k = ns / v.scale; - view.set(ViewState { - scale: ns, - ox: cx - (cx - v.ox) * k, - oy: cy - (cy - v.oy) * k, + apply(&move |v| { + let ns = (v.scale * factor).clamp(0.02, 64.0); + let k = ns / v.scale; + v.ox = cx - (cx - v.ox) * k; + v.oy = cy - (cy - v.oy) * k; + v.scale = ns; + }); + }) + }, + pinch: { + let apply = apply_view.clone(); + let user_moved = user_moved.clone(); + Callback::from(move |(cx, cy, factor, dx, dy): (f64, f64, f64, f64, f64)| { + *user_moved.borrow_mut() = true; + // zoom and pan in ONE update: two separate updates would + // each start from the same stale snapshot and clobber + apply(&move |v| { + let ns = (v.scale * factor).clamp(0.02, 64.0); + let k = ns / v.scale; + v.ox = cx - (cx - v.ox) * k + dx; + v.oy = cy - (cy - v.oy) * k + dy; + v.scale = ns; }); }) }, @@ -771,10 +802,6 @@ pub fn app() -> Html { let hover = hover.clone(); Callback::from(move |_: ()| hover.set(None)) }, - debug: { - let status = status.clone(); - Callback::from(move |s: String| status.set(s)) - }, }; // Lens FOV visibility limit, port of the desktop bump(): the angle at -- cgit v1.2.3