From 48185c380133735446fed7e9e2ea5e5bd3fc48b2 Mon Sep 17 00:00:00 2001 From: dotCooCoo Date: Sat, 28 Nov 2026 11:36:15 -0800 Subject: [PATCH 1/2] Status bar: the zoom field's presets menu lists the zoom stops, Fit on Screen and Fit All again The move to the craft UI framework left the status bar's zoom field without its presets menu. A Zoom Presets button beside the field opens a menu of the zoom stops, the current one checked, then Fit on Screen and Fit All; each entry runs view.setZoom, view.fitArtboard or view.fitAll. --- crates/ui-egui/src/chrome/status_bar.rs | 90 +++++++++++++++++++++++++ crates/ui-egui/src/state.rs | 5 ++ 2 files changed, 95 insertions(+) diff --git a/crates/ui-egui/src/chrome/status_bar.rs b/crates/ui-egui/src/chrome/status_bar.rs index 9848d86f1a..02f95bfb3e 100644 --- a/crates/ui-egui/src/chrome/status_bar.rs +++ b/crates/ui-egui/src/chrome/status_bar.rs @@ -46,8 +46,10 @@ pub fn status_bar(app: &mut VectorcraftApp, ui: &mut Ui) { let render = crate::i18n::tr_ctx(lang, "diagnostics", "render"); let ui_name = crate::i18n::tr_ctx(lang, "diagnostics", "ui"); let busy = busy_labels(app); + let presets = zoom_presets(view.map(|v| crate::state::zoom_display_percent(v.zoom, print)), print); let (mut zoom_to, mut rotate_to, mut go, mut show_pick, mut stop) = (None, None, None, None, false); + let mut preset_pick = None; let mut invalid_zoom = None; let mut snapping = None; // Convenience controls leave first: direct toggles, then the menu. The existing timings and pointer position @@ -100,6 +102,8 @@ pub fn status_bar(app: &mut VectorcraftApp, ui: &mut Ui) { } invalid_zoom = zoom.invalid_zoom.map(|invalid| invalid.message(&Env::of(ui))); } + let open_presets = IconButton::new(IconId::new("chevron_down")).label(tl!("Zoom Presets")).enabled(view.is_some()).show(ui); + preset_pick = Menu::new(tl!("Zoom Presets")).values().opened_by(open_presets.response(), Trigger::Press).pick(ui, &presets).picked; Field::new(tl!("Rotation")).side(SideLabel::new().kind(LabelKind::Hidden)).show(ui, |ui, _| { // Entries are keyed by index: an angle is not a hashable value. rotate_to = Select::new(tl!("Rotation"), Bind::given(turned), &angles) @@ -178,6 +182,18 @@ pub fn status_bar(app: &mut VectorcraftApp, ui: &mut Ui) { if let Some(zoom) = zoom_to { app.run("view.setZoom", json!({"zoom": zoom})).ok(); } + match preset_pick { + Some(ZoomPreset::Percent(zoom)) => { + app.run("view.setZoom", json!({ "zoom": zoom })).ok(); + } + Some(ZoomPreset::FitOnScreen) => { + app.run("view.fitArtboard", json!({})).ok(); + } + Some(ZoomPreset::FitAll) => { + app.run("view.fitAll", json!({})).ok(); + } + None => {} + } if let Some(&(_, angle)) = rotate_to.and_then(|i| ROTATE_VIEW_ANGLES.get(i)) { app.run("view.rotate", json!({"angle": angle})).ok(); } @@ -200,6 +216,7 @@ pub fn status_bar(app: &mut VectorcraftApp, ui: &mut Ui) { mod tests { use super::*; use crate::tests::panel_harness::Panel; + use craft::ui::canvas::ViewCommands; use craft::ui::contract::Role; #[test] @@ -272,6 +289,51 @@ mod tests { } } + #[test] + fn the_zoom_presets_menu_sets_a_stop_and_fits_the_view() { + 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)); + app.run("view.setZoom", json!({"zoom": 800})).unwrap(); + app.view_mut().unwrap().request = None; + // The canvas applies a fit on its next frame; the status bar alone leaves the request queued. + let requested = |panel: &Panel| panel.app().view().and_then(|v| v.request.map(|(r, _)| r.command())); + let document = app.session.active().unwrap().doc.clone(); + let undo = app.session.active().unwrap().history.undo.len(); + let mut panel = Panel::new(app, status_bar); + // Room for the whole menu, so the entries at its foot are on screen. + panel.room(egui::vec2(1200.0, 1600.0)); + + panel.click_role("Zoom Presets", Role::BUTTON); + panel.click_role("200%", Role::MENU_ITEM_RADIO); + let print = panel.app().print_ppi(); + assert!((crate::state::zoom_display_percent(panel.app().view().unwrap().zoom, print) - 200.0).abs() < 1e-6, "a stop sets its zoom"); + + panel.click_role("Zoom Presets", Role::BUTTON); + panel.click_role("Fit on Screen", Role::MENU_ITEM_RADIO); + assert_eq!(requested(&panel), Some(ViewCommands::FitArtboard), "Fit on Screen runs view.fitArtboard"); + panel.app().view_mut().unwrap().request = None; + + panel.click_role("Zoom Presets", Role::BUTTON); + panel.click_role("Fit All", Role::MENU_ITEM_RADIO); + assert_eq!(requested(&panel), Some(ViewCommands::FitOnScreen), "Fit All runs view.fitAll"); + + let app = panel.app(); + assert_eq!(app.session.active().unwrap().doc, document); + assert_eq!(app.session.active().unwrap().history.undo.len(), undo, "zooming records no undo step"); + } + + #[test] + fn the_zoom_presets_list_the_stops_from_the_highest_and_check_the_shown_zoom() { + let presets = zoom_presets(Some(200.0), None); + let labels: Vec = presets.iter().filter(|e| e.is_item()).map(|e| e.label().as_str().to_owned()).collect(); + assert_eq!(labels.first().map(String::as_str), Some("64000%")); + assert_eq!(labels.iter().rev().take(3).rev().map(String::as_str).collect::>(), ["3.13%", "Fit on Screen", "Fit All"]); + let checked: Vec<_> = presets.iter().filter(|e| e.is_checked()).filter_map(|e| e.value().copied()).collect(); + assert_eq!(checked, [ZoomPreset::Percent(200.0)]); + assert!(zoom_presets(None, None).iter().all(|e| !e.is_checked()), "without a view no stop is checked"); + } + #[test] fn with_no_document_the_artboard_navigator_shows_a_dash() { let app = VectorcraftApp::new(vectorcraft_engine::Session::new(), Default::default()); @@ -295,6 +357,34 @@ fn busy_labels(app: &VectorcraftApp) -> Vec<&str> { .collect() } +/// An entry of the Zoom Presets menu beside the status bar's zoom field. +#[derive(Clone, Copy, Debug, PartialEq)] +enum ZoomPreset { + /// A zoom stop, in the percent `view.setZoom` takes. + Percent(f64), + /// Fit on Screen (`view.fitArtboard`). + FitOnScreen, + /// Fit All (`view.fitAll`). + FitAll, +} + +/// The Zoom Presets menu: the zoom stops from the highest, the one at the `shown` percent checked, then Fit on Screen +/// and Fit All. +fn zoom_presets(shown: Option, print: Option) -> Vec> { + let stops = crate::state::zoom_stop_percents(); + let mut entries: Vec> = stops + .rev() + .map(|pct| { + let label = crate::state::zoom_label(crate::state::zoom_from_display_percent(pct, print), print); + Entry::new(ZoomPreset::Percent(pct), label).checked(shown.is_some_and(|s| (s - pct).abs() < 0.01)) + }) + .collect(); + entries.push(Entry::separator()); + entries.push(Entry::new(ZoomPreset::FitOnScreen, tl!("Fit on Screen"))); + entries.push(Entry::new(ZoomPreset::FitAll, tl!("Fit All"))); + entries +} + /// What the status bar's info field shows, chosen from the Show menu that a click on the field /// opens (`view.statusBarShow`). Kept with the UI state ([`crate::UiState::status_bar_show`]). #[derive(Clone, Copy, Debug, Default, PartialEq, Eq, serde::Serialize)] diff --git a/crates/ui-egui/src/state.rs b/crates/ui-egui/src/state.rs index 4265351f6a..5ac49daeb9 100644 --- a/crates/ui-egui/src/state.rs +++ b/crates/ui-egui/src/state.rs @@ -230,6 +230,11 @@ fn zoom_stops(print: Option) -> ZoomScheme { ZoomScheme::ILLUSTRATOR.with_hundred(zoom_baseline(print)) } +/// The zoom stops in the percent the status bar shows, lowest first. +pub fn zoom_stop_percents() -> impl DoubleEndedIterator { + zoom_stops(None).rungs.iter().map(|rung| rung * 100.0) +} + /// The zoom `z` as the status bar shows it, in percent of [`zoom_baseline`]. pub fn zoom_display_percent(z: f64, print: Option) -> f64 { zoom_stops(print).percent(z) From 46e61230e2b1ea6856a7d838558c6feb2b9e17a9 Mon Sep 17 00:00:00 2001 From: dotCooCoo Date: Sat, 28 Nov 2026 13:20:28 -0800 Subject: [PATCH 2/2] Status bar: the zoom presets menu leaves out stops outside the zoom range With Display Print Size on, 100% is not one screen point per document point, so a stop at either end of the list can fall outside the view's zoom range (WindowView::ZOOM_RANGE). Picking it set the nearest end of the range instead: at 96 PPI the 64000% entry showed 48000%. The menu now lists only the stops that view.setZoom sets exactly. --- crates/ui-egui/src/chrome/status_bar.rs | 42 ++++++++++++++++++++++++- 1 file changed, 41 insertions(+), 1 deletion(-) diff --git a/crates/ui-egui/src/chrome/status_bar.rs b/crates/ui-egui/src/chrome/status_bar.rs index 02f95bfb3e..7849b117ec 100644 --- a/crates/ui-egui/src/chrome/status_bar.rs +++ b/crates/ui-egui/src/chrome/status_bar.rs @@ -12,6 +12,7 @@ use craft::ui::pickers::{Select, SelectLook}; use craft::ui::runtime::env::Env; use craft::ui::style::TypeRole; use craft::ui::text::ZoomField; +use vectorcraft_engine::WindowView; /// Shows the status bar: zoom, rotation, artboard navigator, the info field with its Show menu, the readouts, Stop, /// the background-work progress and last the message, so a narrow bar elides the message before anything else. @@ -334,6 +335,38 @@ mod tests { assert!(zoom_presets(None, None).iter().all(|e| !e.is_checked()), "without a view no stop is checked"); } + /// With Display Print Size on, 100% is more or less than one screen point per document point, so the stops at + /// the ends of the list can fall outside the view's zoom range; the menu lists only the stops a pick sets exactly. + #[test] + fn the_zoom_presets_leave_out_stops_outside_the_zoom_range_at_print_size() { + let percents = |print: Option| -> Vec { + zoom_presets(None, print) + .iter() + .filter_map(|e| match e.value() { + Some(ZoomPreset::Percent(pct)) => Some(*pct), + _ => None, + }) + .collect() + }; + let stops: Vec = crate::state::zoom_stop_percents().collect(); + let n = stops.len(); + assert_eq!(percents(None).len(), n, "at 1:1 every stop is in range"); + let dense = crate::state::print_ppi(true, Some(96.0)); + assert_eq!(percents(dense).first(), Some(&stops[n - 2]), "64000% at 96 PPI is past the highest zoom, 25600% is not"); + assert_eq!(percents(dense).last(), Some(&stops[0]), "3.13% at 96 PPI is in range"); + let sparse = crate::state::print_ppi(true, Some(40.0)); + assert_eq!(percents(sparse).first(), Some(&stops[n - 1]), "64000% at 40 PPI is in range"); + assert_eq!(percents(sparse).last(), Some(&stops[2]), "3.13% and 4.17% at 40 PPI are below the lowest zoom, 6.25% is not"); + let (min, max) = WindowView::ZOOM_RANGE; + for print in [None, dense, sparse] { + for pct in percents(print) { + let zoom = crate::state::zoom_from_display_percent(pct, print); + let set = crate::state::zoom_display_percent(zoom.clamp(min, max), print); + assert!((set - pct).abs() < 1e-6, "{pct}% at {print:?} sets {set}%"); + } + } + } + #[test] fn with_no_document_the_artboard_navigator_shows_a_dash() { let app = VectorcraftApp::new(vectorcraft_engine::Session::new(), Default::default()); @@ -369,11 +402,18 @@ enum ZoomPreset { } /// The Zoom Presets menu: the zoom stops from the highest, the one at the `shown` percent checked, then Fit on Screen -/// and Fit All. +/// and Fit All. A stop whose zoom falls outside the view's zoom range ([`WindowView::ZOOM_RANGE`]) at the `print` +/// setting is left out, since `view.setZoom` would set the nearest end of the range instead. fn zoom_presets(shown: Option, print: Option) -> Vec> { + let (min, max) = WindowView::ZOOM_RANGE; + let reachable = |pct: &f64| { + let zoom = crate::state::zoom_from_display_percent(*pct, print); + zoom >= min * (1.0 - 1e-9) && zoom <= max * (1.0 + 1e-9) + }; let stops = crate::state::zoom_stop_percents(); let mut entries: Vec> = stops .rev() + .filter(reachable) .map(|pct| { let label = crate::state::zoom_label(crate::state::zoom_from_display_percent(pct, print), print); Entry::new(ZoomPreset::Percent(pct), label).checked(shown.is_some_and(|s| (s - pct).abs() < 0.01))