diff --git a/crates/ui-egui/src/chrome/status_bar.rs b/crates/ui-egui/src/chrome/status_bar.rs index 7a70dcd4c7..04a7240cf7 100644 --- a/crates/ui-egui/src/chrome/status_bar.rs +++ b/crates/ui-egui/src/chrome/status_bar.rs @@ -71,9 +71,9 @@ pub fn status_bar(app: &mut VectorcraftApp, ui: &mut Ui) { let zoom = Field::new(tl!("Zoom")) .side(SideLabel::new().kind(LabelKind::Hidden)) .show(ui, |ui, _| ZoomField::new(tl!("Zoom"), app.canvas_view(), document).scrub(true).show(ui)); - // PLAN: assumed CanvasView::zoom and crate::state::zoom_display_percent(zoom, print) through the scheme (cv.S4). + // The field edits the canvas view, whose zoom is divided by UI Scaling; `view.setZoom` takes the view's zoom. if let Some(view) = zoom.inner.and_then(|z| z.view.committed().copied()) { - zoom_to = Some(crate::state::zoom_display_percent(view.zoom, print)); + zoom_to = Some(crate::state::zoom_display_percent(crate::state::View::zoom_of_canvas(&view, app.ui_scaling()), print)); } 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. diff --git a/crates/ui-egui/src/chrome/tests.rs b/crates/ui-egui/src/chrome/tests.rs index 2838bbdc33..086424276b 100644 --- a/crates/ui-egui/src/chrome/tests.rs +++ b/crates/ui-egui/src/chrome/tests.rs @@ -833,6 +833,28 @@ fn set_zoom_100_matches_actual_size_with_display_print_size() { assert!((app.view().unwrap().zoom - 96.0 / 72.0).abs() < 1e-12); } +/// At UI Scaling 150% the zoom field shows the zoom `view.setZoom` set, and a zoom typed into it is the zoom the view +/// gets, as at 100%: 200% gives the view the zoom `view.setZoom` gives for 200%. +#[test] +fn the_zoom_field_sets_the_typed_zoom_at_any_ui_scaling() { + let mut app = crate::VectorcraftApp::new(Session::new(), Default::default()); + app.session.execute("prefs.set", &json!({"key": "displayPrintSize", "value": false})).unwrap(); + app.run("file.new", json!({"width": 200, "height": 100})).unwrap(); + app.session.prefs.ui_scaling = 1.5; + app.view_mut().unwrap().rect = egui::Rect::from_min_size(egui::Pos2::ZERO, egui::vec2(460.0, 260.0)); + app.run("view.setZoom", json!({"zoom": 200})).unwrap(); + let wanted = app.view().unwrap().zoom; + app.run("view.setZoom", json!({"zoom": 100})).unwrap(); + let mut bar = crate::tests::panel_harness::Panel::new(app, super::status_bar); + assert!((bar.ctx().zoom_factor() - 1.5).abs() < 1e-6, "the interface is zoomed by UI Scaling"); + let shown = |bar: &crate::tests::panel_harness::Panel| bar.value("Zoom").unwrap_or_default(); + assert!(shown(&bar).starts_with("100"), "the field shows the zoom view.setZoom set: {}", shown(&bar)); + bar.type_into("Zoom", "200"); + let zoom = bar.app().view().unwrap().zoom; + assert!((zoom - wanted).abs() < 1e-9, "typing 200% gives the zoom of view.setZoom 200%: {zoom}, not {wanted}"); + assert!(shown(&bar).starts_with("200"), "the field shows the typed zoom: {}", shown(&bar)); +} + /// Read the painted coordinates while checking that drawing never changes the source /// position, document, selection or either branch of its edit history. fn assert_status_coordinates(bar: &mut Shown, expected: Option<&str>) { diff --git a/crates/ui-egui/src/panels/navigator.rs b/crates/ui-egui/src/panels/navigator.rs index e2096de5d5..31bb7ebd9f 100644 --- a/crates/ui-egui/src/panels/navigator.rs +++ b/crates/ui-egui/src/panels/navigator.rs @@ -230,11 +230,12 @@ fn shifted(mut view: CanvasView, region: DRect) -> CanvasView { view } -/// The `view.setZoom` params for `edited`, a view of the region's frame: the zoom as the status bar -/// shows it (with `print` screen points to an inch at 100%, [`crate::state::print_ppi`]), and the -/// centre back in document units. -fn view_params(edited: &CanvasView, region: DRect, print: Option) -> Value { - json!({"zoom": crate::state::zoom_display_percent(edited.zoom, print), "center": [edited.center.x + region.x0, edited.center.y + region.y0]}) +/// The `view.setZoom` params for `edited`, a canvas view of the region's frame at `ui_scaling`: the +/// zoom as the status bar shows it (with `print` screen points to an inch at 100%, +/// [`crate::state::print_ppi`]), and the centre back in document units. +fn view_params(edited: &CanvasView, region: DRect, print: Option, ui_scaling: f32) -> Value { + let zoom = crate::state::View::zoom_of_canvas(edited, ui_scaling); + json!({"zoom": crate::state::zoom_display_percent(zoom, print), "center": [edited.center.x + region.x0, edited.center.y + region.y0]}) } pub fn show(app: &mut VectorcraftApp, ui: &mut Ui) { @@ -280,7 +281,8 @@ pub fn show(app: &mut VectorcraftApp, ui: &mut Ui) { && *edited != given { let print = app.print_ppi(); - app.run("view.setZoom", view_params(edited, region, print)).ok(); + let scaling = app.ui_scaling(); + app.run("view.setZoom", view_params(edited, region, print, scaling)).ok(); } } @@ -299,7 +301,29 @@ mod tests { assert_eq!((given.center.x, given.center.y), (210.0, 170.0), "the centre minus the region's corner"); let mut edited = given; edited.center = (100.0, 100.0).into(); - assert_eq!(view_params(&edited, region, None)["center"], json!([90.0, 80.0]), "the edit plus the region's corner"); + assert_eq!(view_params(&edited, region, None, 1.0)["center"], json!([90.0, 80.0]), "the edit plus the region's corner"); + } + + /// At UI Scaling 150% the panel's zoom field shows the zoom `view.setZoom` set, and a zoom typed into it is the + /// zoom the view gets, as at 100%: 200% gives the view the zoom `view.setZoom` gives for 200%. + #[test] + fn the_zoom_field_sets_the_typed_zoom_at_any_ui_scaling() { + let mut app = VectorcraftApp::new(vectorcraft_engine::Session::new(), Default::default()); + app.session.execute("prefs.set", &json!({"key": "displayPrintSize", "value": false})).unwrap(); + app.run("file.new", json!({"width": 200, "height": 100})).unwrap(); + app.session.prefs.ui_scaling = 1.5; + app.view_mut().unwrap().rect = Rect::from_min_size(pos2(0.0, 0.0), egui::vec2(460.0, 260.0)); + app.run("view.setZoom", json!({"zoom": 200})).unwrap(); + let wanted = app.view().unwrap().zoom; + app.run("view.setZoom", json!({"zoom": 100})).unwrap(); + let mut panel = crate::tests::panel_harness::Panel::new(app, show); + assert!((panel.ctx().zoom_factor() - 1.5).abs() < 1e-6, "the interface is zoomed by UI Scaling"); + let shown = |panel: &crate::tests::panel_harness::Panel| panel.value("Zoom").unwrap_or_default(); + assert!(shown(&panel).starts_with("100"), "the field shows the zoom view.setZoom set: {}", shown(&panel)); + panel.type_into("Zoom", "200"); + let zoom = panel.app().view().unwrap().zoom; + assert!((zoom - wanted).abs() < 1e-9, "typing 200% gives the zoom of view.setZoom 200%: {zoom}, not {wanted}"); + assert!(shown(&panel).starts_with("200"), "the field shows the typed zoom: {}", shown(&panel)); } /// The panel menu's rows are titled, checked and enabled as the option says, and each row's diff --git a/crates/ui-egui/src/state.rs b/crates/ui-egui/src/state.rs index b37805eb57..1ed56d8faa 100644 --- a/crates/ui-egui/src/state.rs +++ b/crates/ui-egui/src/state.rs @@ -156,10 +156,16 @@ impl View { /// Takes the view the canvas reports back, the inverse of [`Self::canvas`]. pub fn set_canvas(&mut self, view: &CanvasView, ui_scaling: f32) { self.center = view.center; - self.zoom = view.zoom * f64::from(ui_scaling); + self.zoom = Self::zoom_of_canvas(view, ui_scaling); self.rotation = view.rotation; self.rect = view.rect; } + + /// The zoom [`Self::zoom`] holds for the canvas view `view` at `ui_scaling`, the inverse of the + /// zoom in [`Self::canvas`]: the zoom `view.setZoom` and the View menu set and the status bar shows. + pub fn zoom_of_canvas(view: &CanvasView, ui_scaling: f32) -> f64 { + view.zoom * f64::from(ui_scaling) + } } /// The status bar's angle menu and View › Rotate View list these preset view angles, each as