diff --git a/crates/ui-egui/src/app.rs b/crates/ui-egui/src/app.rs index 00a8b9cd61..14329e8f0f 100644 --- a/crates/ui-egui/src/app.rs +++ b/crates/ui-egui/src/app.rs @@ -707,14 +707,18 @@ impl VectorcraftApp { } } +/// Use the same supported scale for the interface and the canvas. Invalid saved values fall back to 100%. +fn applied_ui_scaling(saved: f64) -> f32 { + if saved.is_finite() && saved > 0.0 { saved.clamp(0.75, 2.0) as f32 } else { 1.0 } +} + /// The root values the preferences give every frame: the theme, the interface scale (UI Scaling /// over the display's `native_ppp`), Show Tool Tips, the zoom stops (100% at `print` screen points /// to an inch, [`crate::state::print_ppi`]) and which drags scrub a value (Scrub Numeric Fields: the /// side label only, or nothing). pub(crate) fn root_env(prefs: &vectorcraft_engine::Prefs, native_ppp: f32, print: Option) -> RootEnv { let (theme, mode, accent) = theme::root_theme(prefs); - // A hand-edited preferences file must not scale the interface to nothing. - let scaling = if prefs.ui_scaling.is_finite() && prefs.ui_scaling > 0.0 { prefs.ui_scaling as f32 } else { 1.0 }; + let scaling = applied_ui_scaling(prefs.ui_scaling); let mut root = RootEnv::new().theme(theme).mode(mode).accent(accent).scale(Scale::new(scaling)).tooltips(prefs.show_tool_tips); root.sizes = theme::app_sizes(); root.values.set(view::zoom_scheme(print, native_ppp)); diff --git a/crates/ui-egui/src/app/view.rs b/crates/ui-egui/src/app/view.rs index 1dbe830af4..fcd71714d3 100644 --- a/crates/ui-egui/src/app/view.rs +++ b/crates/ui-egui/src/app/view.rs @@ -327,7 +327,7 @@ impl VectorcraftApp { /// Preferences › User Interface › UI Scaling, as the interface applies it. pub(crate) fn ui_scaling(&self) -> f32 { - self.session.prefs.ui_scaling.clamp(0.75, 2.0) as f32 + super::applied_ui_scaling(self.session.prefs.ui_scaling) } /// Ask the active view's canvas to run `request` in its next pass; `bounds` are the layers' bounds diff --git a/crates/ui-egui/src/canvas/tests_ui_scaling.rs b/crates/ui-egui/src/canvas/tests_ui_scaling.rs index 0ccc63be2c..64dca524b3 100644 --- a/crates/ui-egui/src/canvas/tests_ui_scaling.rs +++ b/crates/ui-egui/src/canvas/tests_ui_scaling.rs @@ -92,3 +92,20 @@ fn pixel_preview_compares_a_document_pixel_with_a_device_pixel() { } } } + +#[test] +fn out_of_range_saved_ui_scaling_keeps_actual_size_correct() { + let (a, b) = (Point::new(100.0, 100.0), Point::new(228.0, 100.0)); + for (saved, expected) in [(0.5, 0.75), (3.0, 2.0), (0.0, 1.0), (f64::NAN, 1.0)] { + let (mut app, ctx, jobs) = shown(1.0, 1.5); + app.run("view.actualSize", json!({})).unwrap(); + frame(&mut app, &ctx, (1.0, 1.5), vec![]); + let before = device_length(rendered(&jobs), a, b); + app.session.prefs.ui_scaling = saved; + let scale = crate::app::root_env(&app.session.prefs, 1.0, None).scale.0; + assert_eq!(scale, expected, "interface scale from saved value {saved}"); + frame(&mut app, &ctx, (1.0, scale), vec![]); + let length = device_length(rendered(&jobs), a, b); + assert!((length - before).abs() < 1e-4, "saved scale {saved} changes 128 pt from {before} to {length} pixels"); + } +}