summaryrefslogtreecommitdiff
path: root/crates/wipfel
diff options
context:
space:
mode:
authorJohannes Hofmann <Johannes.Hofmann@gmx.de>2026-10-27 18:01:44 +0100
committerJohannes Hofmann <Johannes.Hofmann@gmx.de>2026-10-27 18:01:44 +0100
commit60b07b483f49adfee51480b029f23f7c4913886f (patch)
tree087570295f00a2787c06bedae97574b6a24c254d /crates/wipfel
parentd7142ca2c39c3ca2c58240558abcac9a0e8f8e09 (diff)
web: fix pinch zoom — stale use_state snapshots clobbered each other
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).
Diffstat (limited to 'crates/wipfel')
-rw-r--r--crates/wipfel/src/app.rs77
-rw-r--r--crates/wipfel/src/canvas_view.rs62
2 files changed, 63 insertions, 76 deletions
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::<web_sys::ImageBitmap>);
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<dyn Fn(&dyn Fn(&mut ViewState))> = {
+ 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::<HtmlCanvasElement>()
@@ -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
diff --git a/crates/wipfel/src/canvas_view.rs b/crates/wipfel/src/canvas_view.rs
index f6c575f..c5777e3 100644
--- a/crates/wipfel/src/canvas_view.rs
+++ b/crates/wipfel/src/canvas_view.rs
@@ -63,8 +63,11 @@ pub struct ViewCallbacks {
pub hover: Callback<(f64, f64)>,
/// pointer left the canvas
pub hover_end: Callback<()>,
- /// diagnostic text for the status line (pinch debugging on mobile)
- pub debug: Callback<String>,
+ /// two-finger pinch: (center x, center y, factor, pan dx, pan dy).
+ /// Zoom and pan must land in ONE state update: two consecutive
+ /// read-modify-writes clobber each other, because a UseStateHandle
+ /// cloned into a callback derefs to its render-time snapshot.
+ pub pinch: Callback<(f64, f64, f64, f64, f64)>,
}
#[derive(Properties, PartialEq)]
@@ -114,9 +117,6 @@ pub fn canvas_view(props: &Props) -> Html {
pts: Vec::new(),
pinch: None,
});
- // event counters for pinch diagnostics: (pointerdown, pinch-f, seeds)
- let evcount = use_mut_ref(|| (0u32, 0u32, 0u32));
-
// redraw on every view/bitmap/marker change and on resize ticks
{
let bitmap = props.bitmap.clone();
@@ -379,8 +379,6 @@ pub fn canvas_view(props: &Props) -> Html {
let markers = props.markers.clone();
let view = props.view.clone();
let bitmap = props.bitmap.clone();
- let debug = props.callbacks.debug.clone();
- let evcount = evcount.clone();
Callback::from(move |e: PointerEvent| {
e.prevent_default();
let rect = canvas_ref
@@ -431,28 +429,17 @@ pub fn canvas_view(props: &Props) -> Html {
let _ = c.set_pointer_capture(e.pointer_id());
}
}
- debug.emit(format!(
- "pd#{} id={} pts={n}",
- {
- let mut c = evcount.borrow_mut();
- c.0 += 1;
- c.0
- },
- e.pointer_id()
- ));
})
};
let on_pointermove = {
let drag = drag.clone();
let pan_cb = props.callbacks.pan.clone();
- let zoom_cb = props.callbacks.zoom.clone();
+ let pinch_cb = props.callbacks.pinch.clone();
let marker_drag = props.callbacks.marker_drag.clone();
let hover = props.callbacks.hover.clone();
let to_img = to_img.clone();
let canvas_ref = props.canvas_ref.clone();
- let debug = props.callbacks.debug.clone();
- let evcount = evcount.clone();
let scale = props.view.scale;
Callback::from(move |e: PointerEvent| {
// update the pointer table and handle two-finger pinch
@@ -490,40 +477,15 @@ pub fn canvas_view(props: &Props) -> Html {
};
d.pinch = Some((dist, cx, cy));
d.moved = true;
- let dbg = match ev {
- Some((f, ..)) => {
- let (pd, mv, sd) = {
- let mut c = evcount.borrow_mut();
- c.1 += 1;
- (c.0, c.1, c.2)
- };
- format!(
- "pinch d={dist:.0} f={f:.3} pd={pd} mv={mv} sd={sd} p1=({:.0},{:.0}) p2=({:.0},{:.0})",
- x1, y1, x2, y2
- )
- }
- None => {
- let sd = {
- let mut c = evcount.borrow_mut();
- c.2 += 1;
- c.2
- };
- format!(
- "pinch d={dist:.0} seed#{sd} p1=({:.0},{:.0}) p2=({:.0},{:.0})",
- x1, y1, x2, y2
- )
- }
- };
- (ev, dbg)
+ (ev, true)
} else {
- (None, String::new())
+ (None, false)
}
};
- if !pinch_ev.1.is_empty() {
- debug.emit(pinch_ev.1);
+ let in_pinch = pinch_ev.1;
+ if in_pinch {
if let Some((factor, cx, cy, dx, dy)) = pinch_ev.0 {
- zoom_cb.emit((cx, cy, factor));
- pan_cb.emit((dx, dy));
+ pinch_cb.emit((cx, cy, factor, dx, dy));
}
return;
}
@@ -599,7 +561,6 @@ pub fn canvas_view(props: &Props) -> Html {
let on_pointercancel = {
let drag = drag.clone();
- let debug = props.callbacks.debug.clone();
Callback::from(move |e: PointerEvent| {
let mut d = drag.borrow_mut();
// do NOT clear the whole table: when a mobile browser
@@ -612,7 +573,6 @@ pub fn canvas_view(props: &Props) -> Html {
if d.pts.is_empty() {
d.mode = None;
}
- debug.emit(format!("pointercancel id={} pts={}", e.pointer_id(), d.pts.len()));
})
};