diff options
| author | Johannes Hofmann <Johannes.Hofmann@gmx.de> | 2026-10-28 20:18:01 +0100 |
|---|---|---|
| committer | Johannes Hofmann <Johannes.Hofmann@gmx.de> | 2026-10-28 20:18:01 +0100 |
| commit | 34377db6b5c8e563dca18f72cc8174a5df2dd92e (patch) | |
| tree | 429b4c68c33d861c313a39b400e230c63f427c3e /crates/wipfel/src/app.rs | |
| parent | 969297d6658bb801de13cba132af774197c62ca8 (diff) | |
web: fix redraw tick stalling in persistent resize closures
A UseStateHandle cloned into a long-lived closure derefs to its
render-time snapshot (same root cause as the pinch bug), so
tick.set(*tick + 1) wrote the same value on every resize after the
first: no state change, no redraw, stale stretched canvas backing
store — the distortion thought fixed by d465654 returned on the second
resize (the window-resize listener, created once at startup, never
worked at all). Yew 0.21 has no functional-update setter, so the tick
now has a use_mut_ref mirror as the live counter.
Diffstat (limited to 'crates/wipfel/src/app.rs')
| -rw-r--r-- | crates/wipfel/src/app.rs | 24 |
1 files changed, 21 insertions, 3 deletions
diff --git a/crates/wipfel/src/app.rs b/crates/wipfel/src/app.rs index 09f3ac3..96325eb 100644 --- a/crates/wipfel/src/app.rs +++ b/crates/wipfel/src/app.rs @@ -197,11 +197,23 @@ pub fn app() -> Html { 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) + // store tracks its CSS size (listener lives as long as the app). + // The live counter lives in a RefCell: a UseStateHandle cloned into + // these persistent closures would deref to its render-time snapshot + // and stall the bump after the first resize. + let tick_live = use_mut_ref(|| 0u32); { let tick = tick.clone(); + let live = tick_live.clone(); use_effect_with((), move |_| { - let closure = Closure::wrap(Box::new(move || tick.set(*tick + 1)) as Box<dyn Fn()>); + let closure = Closure::wrap(Box::new(move || { + let v = { + let mut l = live.borrow_mut(); + *l += 1; + *l + }; + tick.set(v); + }) as Box<dyn Fn()>); let _ = bridge::window() .add_event_listener_with_callback("resize", closure.as_ref().unchecked_ref()); // the listener is never removed (app lifetime); leak the closure @@ -284,9 +296,15 @@ pub fn app() -> Html { let fit2 = fit.clone(); let um = user_moved.clone(); let tick2 = tick.clone(); + let live2 = tick_live.clone(); let closure = wasm_bindgen::closure::Closure::wrap(Box::new( move |_entries: js_sys::Array, _obs: web_sys::ResizeObserver| { - tick2.set(*tick2 + 1); + let v = { + let mut l = live2.borrow_mut(); + *l += 1; + *l + }; + tick2.set(v); if !*um.borrow() { fit2.emit(MouseEvent::new("fit").unwrap()); } |
