From 9f7501a8f6bf523351ef7b6232994da64ea1a31a Mon Sep 17 00:00:00 2001 From: dotCooCoo Date: Sun, 29 Nov 2026 11:24:48 -0800 Subject: [PATCH] Status bar: scrolling the view no longer lays out the zoom and artboard controls again The status bar's Zoom field, Zoom Presets menu, Rotation menu and artboard navigator were one memoized view keyed on the whole canvas view, so every frame of a wheel pan changed the key and ran all four again. The Zoom field reads only the view's zoom, and the status bar reads only the zoom of the view the field commits. - The Zoom field and Zoom Presets menu are their own memoized view, keyed on the zoom instead of the canvas view, so a pan replays them. - The Rotation menu and the artboard navigator are a second memoized view, so a zoom change runs only the zoom controls. The new test counts the runs of each view over ten pan frames and four zoom changes; before the change the pan ran the navigation controls ten times. --- crates/ui-egui/src/chrome/status_bar.rs | 130 +++++++++++++++--------- 1 file changed, 84 insertions(+), 46 deletions(-) diff --git a/crates/ui-egui/src/chrome/status_bar.rs b/crates/ui-egui/src/chrome/status_bar.rs index 1a3df7737b..b7446defb4 100644 --- a/crates/ui-egui/src/chrome/status_bar.rs +++ b/crates/ui-egui/src/chrome/status_bar.rs @@ -1,5 +1,5 @@ use super::{ROTATE_VIEW_ANGLES, Ui, VectorcraftApp, json}; -use craft::behavior::kurbo::Size; +use craft::behavior::kurbo::{Point, Size}; use craft::behavior::{CanvasView, IconId}; use craft::ui::bars::{Bar, BarKind, StatusMessage, StatusProgress}; use craft::ui::buttons::{Button, IconButton}; @@ -31,7 +31,7 @@ pub fn status_bar(app: &mut VectorcraftApp, ui: &mut Ui) { let view = app.view(); let rotation = view.map_or(0.0, |v| v.rotation); let lang = crate::i18n::current(); - let canvas_view = app.canvas_view(); + let canvas_zoom = app.canvas_view().map(|v| v.zoom); // PLAN: assumed Artboard::rect has `width()` and `height()`. let document = st.and_then(|d| d.doc.artboards.get(cur)).map_or(Size::new(1.0, 1.0), |a| Size::new(a.rect.width(), a.rect.height())); let scaling = app.ui_scaling(); @@ -92,21 +92,10 @@ pub fn status_bar(app: &mut VectorcraftApp, ui: &mut Ui) { }; // PLAN: assumed VectorcraftApp::canvas_view() -> Option (cv.S4) and the label msgid "Status Bar". Bar::new(tl!("Status Bar"), BarKind::Status).trailing(readouts).show(ui, |ui, _| { - let navigation = Navigation { - view: canvas_view, - document, - scaling, - print, - shown_zoom, - has_view: view.is_some(), - turned, - degrees, - cur, - revision, - artboards, - lang, - picked: Outbox::new(&mut picks), - }; + let zoom = + ZoomControls { zoom: canvas_zoom, document, scaling, print, shown_zoom, has_view: view.is_some(), lang, picked: Outbox::new(&mut picks) }; + let _ = MemoView::new("status-bar-zoom", zoom, show_zoom).show(ui); + let navigation = Navigation { has_view: view.is_some(), turned, degrees, cur, revision, artboards, lang, picked: Outbox::new(&mut picks) }; let _ = MemoView::new("status-bar-navigation", navigation, show_navigation).show(ui); Divider::vertical().show(ui); // An empty readout shows a dash. @@ -187,13 +176,12 @@ pub fn status_bar(app: &mut VectorcraftApp, ui: &mut Ui) { } } -/// What the status bar's navigation controls read: the Zoom field, the Rotation menu and the artboard navigator. The -/// bar replays them while these hold still and nothing reaches them (`MemoView`), as most frames only move the -/// readouts. +/// What the status bar's Zoom field and Zoom Presets menu read. The bar replays them while these hold still and +/// nothing reaches them (`MemoView`). The canvas view's center is not among them, so a pan replays them. #[derive(MemoProps)] -struct Navigation<'a> { - /// The canvas view the Zoom field shows and edits. - view: Option, +struct ZoomControls<'a> { + /// The canvas view's zoom (`CanvasView::zoom`), which the Zoom field shows and edits. + zoom: Option, /// The size of the artboard the navigator shows, which the Zoom field fits. document: Size, /// UI Scaling, which divides the canvas view's zoom. @@ -202,6 +190,45 @@ struct Navigation<'a> { print: Option, /// The view's zoom percentage, which the Zoom Presets menu checks. shown_zoom: Option, + /// Whether the active document has a view to zoom. + has_view: bool, + #[memo_props(hash = "an interface language: an index into the languages offered")] + lang: crate::i18n::Lang, + /// What the controls chose in the pass. + picked: Outbox<'a, NavigationPick>, +} + +/// The Zoom field and the Zoom Presets menu of the status bar, from [`ZoomControls`]. +fn show_zoom(p: &mut ZoomControls<'_>, ui: &mut Ui) { + #[cfg(test)] + tests::ZOOM_RUNS.with(|n| n.set(n.get() + 1)); + // The field reads only the view's zoom, and the bar reads only the zoom of the view it commits. + let view = p.zoom.map(|zoom| CanvasView::new(egui::Rect::ZERO, Point::ZERO, zoom)); + let document = p.document; + let zoom = Field::new(tl!("Zoom")) + .side(SideLabel::new().kind(LabelKind::Hidden)) + .show(ui, |ui, _| ZoomField::new(tl!("Zoom"), view, document).scrub(true).status_bar().show(ui)); + // The field edits the canvas view, whose zoom is divided by UI Scaling; `view.setZoom` takes the view's zoom. + if let Some(zoom) = zoom.inner { + if let Some(view) = zoom.view.committed() { + let percent = crate::state::zoom_display_percent(crate::state::View::zoom_of_canvas(view, p.scaling), p.print); + p.picked.send(NavigationPick::Zoom(percent)); + } + if let Some(invalid) = zoom.invalid_zoom { + p.picked.send(NavigationPick::InvalidZoom(invalid.message(&Env::of(ui)))); + } + } + let presets = zoom_presets(p.shown_zoom, p.print); + let open_presets = IconButton::new(IconId::new("chevron_down")).label(tl!("Zoom Presets")).enabled(p.has_view).show(ui); + if let Some(preset) = Menu::new(tl!("Zoom Presets")).values().opened_by(open_presets.response(), Trigger::Press).pick(ui, &presets).picked { + p.picked.send(NavigationPick::Preset(preset)); + } +} + +/// What the status bar's Rotation menu and artboard navigator read. The bar replays them while these hold still and +/// nothing reaches them (`MemoView`), as most frames only move the readouts, and a pan or a zoom leaves them alone. +#[derive(MemoProps)] +struct Navigation<'a> { /// Whether the active document has a view to turn. has_view: bool, /// The Rotate View preset the view is turned to, else `usize::MAX`. @@ -234,29 +261,10 @@ enum NavigationPick { Artboard(serde_json::Value), } -/// The Zoom field, the Rotation menu and the artboard navigator of the status bar, from [`Navigation`]. +/// The Rotation menu and the artboard navigator of the status bar, from [`Navigation`]. fn show_navigation(p: &mut Navigation<'_>, ui: &mut Ui) { #[cfg(test)] tests::NAVIGATION_RUNS.with(|n| n.set(n.get() + 1)); - let (view, document) = (p.view, p.document); - let zoom = Field::new(tl!("Zoom")) - .side(SideLabel::new().kind(LabelKind::Hidden)) - .show(ui, |ui, _| ZoomField::new(tl!("Zoom"), view, document).scrub(true).status_bar().show(ui)); - // The field edits the canvas view, whose zoom is divided by UI Scaling; `view.setZoom` takes the view's zoom. - if let Some(zoom) = zoom.inner { - if let Some(view) = zoom.view.committed() { - let percent = crate::state::zoom_display_percent(crate::state::View::zoom_of_canvas(view, p.scaling), p.print); - p.picked.send(NavigationPick::Zoom(percent)); - } - if let Some(invalid) = zoom.invalid_zoom { - p.picked.send(NavigationPick::InvalidZoom(invalid.message(&Env::of(ui)))); - } - } - let presets = zoom_presets(p.shown_zoom, p.print); - let open_presets = IconButton::new(IconId::new("chevron_down")).label(tl!("Zoom Presets")).enabled(p.has_view).show(ui); - if let Some(preset) = Menu::new(tl!("Zoom Presets")).values().opened_by(open_presets.response(), Trigger::Press).pick(ui, &presets).picked { - p.picked.send(NavigationPick::Preset(preset)); - } let angles: Vec> = ROTATE_VIEW_ANGLES.iter().enumerate().map(|(i, (label, _))| Entry::new(i, *label)).collect(); let (turned, angle, has_view) = (p.turned, format!("{}°", p.degrees), p.has_view); let mut rotate_to = None; @@ -388,13 +396,15 @@ mod tests { thread_local! { /// How many times this thread ran the status bar's navigation controls ([`show_navigation`]). pub(super) static NAVIGATION_RUNS: std::cell::Cell = const { std::cell::Cell::new(0) }; + /// How many times this thread ran the status bar's Zoom field and Zoom Presets menu ([`show_zoom`]). + pub(super) static ZOOM_RUNS: std::cell::Cell = const { std::cell::Cell::new(0) }; } - /// Frames that change nothing the navigation controls show replay them instead of laying them out again; a new - /// zoom or a new artboard runs them once, and they show it. + /// Frames that change nothing the zoom and navigation controls show replay them instead of laying them out again; + /// a new zoom or a new artboard runs them once, and they show it. #[test] fn idle_frames_replay_the_navigation_controls() { - let runs = || NAVIGATION_RUNS.with(std::cell::Cell::get); + let runs = || ZOOM_RUNS.with(std::cell::Cell::get) + NAVIGATION_RUNS.with(std::cell::Cell::get); let mut app = VectorcraftApp::new(vectorcraft_engine::Session::new(), Default::default()); app.run("file.new", json!({"width": 300, "height": 200})).unwrap(); app.view_mut().unwrap().rect = egui::Rect::from_min_size(egui::Pos2::ZERO, egui::vec2(600.0, 400.0)); @@ -423,6 +433,34 @@ mod tests { assert!(panel.enabled("Next Artboard") || panel.enabled("Previous Artboard"), "the navigator counts the new artboard"); } + /// Scrolling the view moves its center and nothing the navigation controls show, so frames of a wheel pan replay + /// them; a new zoom runs only the Zoom field and its presets, not the Rotation menu and the artboard navigator. + #[test] + fn wheel_frames_run_only_the_controls_that_show_what_changed() { + let runs = || (ZOOM_RUNS.with(std::cell::Cell::get), NAVIGATION_RUNS.with(std::cell::Cell::get)); + let mut app = VectorcraftApp::new(vectorcraft_engine::Session::new(), Default::default()); + app.run("file.new", json!({"width": 300, "height": 200})).unwrap(); + app.view_mut().unwrap().rect = egui::Rect::from_min_size(egui::Pos2::ZERO, egui::vec2(600.0, 400.0)); + let mut panel = Panel::new(app, status_bar); + panel.room(egui::vec2(1200.0, 300.0)); + panel.frame(); + panel.frame(); + let before = runs(); + for i in 0..10 { + panel.app().view_mut().unwrap().center.y += if i % 2 == 0 { 40.0 } else { -25.0 }; + panel.frame(); + } + assert_eq!(runs(), before, "a pan runs neither the zoom controls nor the navigation controls"); + for i in 0..4 { + panel.app().run("view.setZoom", json!({"zoom": 150 + 25 * i})).unwrap(); + panel.frame(); + } + let after = runs(); + assert!(after.0 > before.0, "a new zoom runs the zoom controls"); + assert_eq!(after.1, before.1, "a new zoom does not run the Rotation menu and the artboard navigator"); + assert_eq!(panel.value("Zoom").as_deref(), Some("225%"), "the field shows the last zoom"); + } + #[test] fn the_zoom_presets_menu_sets_a_stop_and_fits_the_view() { let mut app = VectorcraftApp::new(vectorcraft_engine::Session::new(), Default::default());