Merge pull request #9974 from dotCooCoo/fix/status-bar-zoom-presets

Status bar: the zoom field's presets menu lists the zoom stops, Fit on Screen and Fit All again
This commit is contained in:
.CooCoo authored and GitHub committed 2026-11-28 14:18:21 -08:00
commit d76abce927
2 files changed
+144 -1

No files matched your search

+139 -1
View File
@@ -14,6 +14,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.
@@ -47,6 +48,7 @@ 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 shown_zoom = view.map(|v| crate::state::zoom_display_percent(v.zoom, print));
let (mut show_pick, mut stop) = (None, false);
let mut snapping = None;
@@ -95,6 +97,7 @@ pub fn status_bar(app: &mut VectorcraftApp, ui: &mut Ui) {
document,
scaling,
print,
shown_zoom,
has_view: view.is_some(),
turned,
degrees,
@@ -140,11 +143,12 @@ pub fn status_bar(app: &mut VectorcraftApp, ui: &mut Ui) {
crate::menus::invoke(app, id, params);
}
// The last choice of each kind counts, as the controls report one choice of each in a pass.
let (mut zoom_to, mut invalid_zoom, mut rotate_to, mut go) = (None, None, None, None);
let (mut zoom_to, mut invalid_zoom, mut preset_pick, mut rotate_to, mut go) = (None, None, None, None, None);
for pick in picks {
match pick {
NavigationPick::Zoom(zoom) => zoom_to = Some(zoom),
NavigationPick::InvalidZoom(message) => invalid_zoom = Some(message),
NavigationPick::Preset(preset) => preset_pick = Some(preset),
NavigationPick::Rotate(i) => rotate_to = Some(i),
NavigationPick::Artboard(to) => go = Some(to),
}
@@ -153,6 +157,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();
}
@@ -184,6 +200,8 @@ struct Navigation<'a> {
scaling: f32,
/// The density zoom percentages count from ([`VectorcraftApp::print_ppi`]).
print: Option<f64>,
/// The view's zoom percentage, which the Zoom Presets menu checks.
shown_zoom: Option<f64>,
/// Whether the active document has a view to turn.
has_view: bool,
/// The Rotate View preset the view is turned to, else `usize::MAX`.
@@ -208,6 +226,8 @@ enum NavigationPick {
Zoom(f64),
/// The message of a zoom the Zoom field could not read.
InvalidZoom(String),
/// The entry chosen in the Zoom Presets menu.
Preset(ZoomPreset),
/// The Rotate View preset chosen (an index into [`ROTATE_VIEW_ANGLES`]).
Rotate(usize),
/// The `index` of `view.goToArtboard`: an artboard's index, or first, previous, next or last.
@@ -232,6 +252,11 @@ fn show_navigation(p: &mut Navigation<'_>, ui: &mut Ui) {
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<Entry<usize>> = 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;
@@ -287,6 +312,7 @@ fn show_navigation(p: &mut Navigation<'_>, ui: &mut Ui) {
mod tests {
use super::*;
use crate::tests::panel_harness::Panel;
use craft::ui::canvas::ViewCommands;
use craft::ui::contract::Role;
#[test]
@@ -397,6 +423,83 @@ mod tests {
assert!(panel.enabled("Next Artboard") || panel.enabled("Previous Artboard"), "the navigator counts the new artboard");
}
#[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<String> = 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::<Vec<_>>(), ["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");
}
/// 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<f64>| -> Vec<f64> {
zoom_presets(None, print)
.iter()
.filter_map(|e| match e.value() {
Some(ZoomPreset::Percent(pct)) => Some(*pct),
_ => None,
})
.collect()
};
let stops: Vec<f64> = 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());
@@ -420,6 +523,41 @@ 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. 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<f64>, print: Option<f64>) -> Vec<Entry<ZoomPreset>> {
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<Entry<ZoomPreset>> = 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))
})
.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)]
+5
View File
@@ -230,6 +230,11 @@ fn zoom_stops(print: Option<f64>) -> 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<Item = f64> {
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>) -> f64 {
zoom_stops(print).percent(z)