From e838bb23f8251c7b2d5b0ebfca820f38680cc022 Mon Sep 17 00:00:00 2001 From: Johannes Hofmann Date: Tue, 27 Oct 2026 14:01:38 +0100 Subject: web: guarantee initial fit with a ResizeObserver on the canvas The fit at load time (and the 120 ms nudge) could still run before the canvas had a valid layout size, leaving the first image invisible until 'Fit to window' was pressed. A ResizeObserver attached when the first bitmap arrives fires once with the element's real size immediately after observing, then on every later resize; it re-fits automatically unless the user has panned or zoomed by hand since the last load (user_moved flag, reset on each image load). Window resizes now re-fit the image as well instead of only redrawing it. --- crates/wipfel/Cargo.toml | 1 + crates/wipfel/src/app.rs | 48 ++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 49 insertions(+) diff --git a/crates/wipfel/Cargo.toml b/crates/wipfel/Cargo.toml index e1e39e1..fad31ce 100644 --- a/crates/wipfel/Cargo.toml +++ b/crates/wipfel/Cargo.toml @@ -36,6 +36,7 @@ features = [ "PointerEvent", "Request", "Response", + "ResizeObserver", "TextMetrics", "Window", "WheelEvent", diff --git a/crates/wipfel/src/app.rs b/crates/wipfel/src/app.rs index eeb82d6..879ed91 100644 --- a/crates/wipfel/src/app.rs +++ b/crates/wipfel/src/app.rs @@ -162,6 +162,9 @@ pub fn app() -> Html { let img_size = use_state(|| (0u32, 0u32)); let find_peak = use_state(|| Option::::None); let show_hidden = use_state(|| false); + // set once the user pans or zooms by hand; auto-fit is then suppressed + // until the next image load + let user_moved = use_mut_ref(|| false); // force a redraw when the window is resized so the canvas backing // store tracks its CSS size (listener lives as long as the app) @@ -225,6 +228,44 @@ pub fn app() -> Html { }) }; + // Keep the fit correct even when layout was not ready at load time: + // a ResizeObserver fires once with the element's initial size right + // after it starts observing, then on every later resize. Auto-fit + // whenever the user has not panned or zoomed by hand since the last + // image load — this is what makes the very first image appear. + { + let canvas_ref = canvas_ref.clone(); + let fit = fit.clone(); + let bitmap = bitmap.clone(); + let user_moved = user_moved.clone(); + use_effect_with((*bitmap).is_some(), move |has_image| { + let observer = if *has_image { + canvas_ref + .cast::() + .and_then(|c| { + let fit2 = fit.clone(); + let um = user_moved.clone(); + let closure = wasm_bindgen::closure::Closure::wrap(Box::new( + move |_entries: js_sys::Array, _obs: web_sys::ResizeObserver| { + if !*um.borrow() { + fit2.emit(MouseEvent::new("fit").unwrap()); + } + }, + ) as Box); + let ro = + web_sys::ResizeObserver::new(closure.as_ref().unchecked_ref()).ok()?; + let _ = ro.observe(&c); + // the observer does not own the closure; keep both + // alive until this effect is torn down + Some((ro, closure)) + }) + } else { + None + }; + move || drop(observer) + }); + } + // refit the projection to the given markers and update the status line let do_refit = { let pan = pan.clone(); @@ -276,12 +317,14 @@ pub fn app() -> Html { let vp = vp.clone(); let img_size = img_size.clone(); let find_peak = find_peak.clone(); + let user_moved = user_moved.clone(); Callback::from(move |(bytes, name): (Vec, String)| { let (bitmap, status, fit, pan, markers, fit_info) = (bitmap.clone(), status.clone(), fit.clone(), pan.clone(), markers.clone(), fit_info.clone()); let vp = vp.clone(); let img_size = img_size.clone(); let find_peak = find_peak.clone(); + let user_moved = user_moved.clone(); spawn_local(async move { match bridge::decode_bitmap(&bytes).await { Ok(bmp) => { @@ -315,6 +358,7 @@ pub fn app() -> Html { markers.set(Vec::new()); fit_info.set(String::new()); find_peak.set(None); + *user_moved.borrow_mut() = false; bitmap.set(Some(bmp)); fit.emit(MouseEvent::new("fit").unwrap()); // same nudge as the desktop app: re-fit shortly @@ -537,7 +581,9 @@ pub fn app() -> Html { let callbacks = ViewCallbacks { pan: { let view = 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; @@ -546,7 +592,9 @@ pub fn app() -> Html { }, zoom: { let view = 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; -- cgit v1.2.3