diff --git a/crates/ui-contract/src/runtime/env/units.rs b/crates/ui-contract/src/runtime/env/units.rs index 5d1e992a19..3f1b20cf72 100644 --- a/crates/ui-contract/src/runtime/env/units.rs +++ b/crates/ui-contract/src/runtime/env/units.rs @@ -408,11 +408,17 @@ impl Units { /// 66.67 %), with zoom typing (`a/b` as a ratio, a second separator cutting the text), so every zoom field reads /// typed text by the same rules. Under [`ZoomPrecision::Whole`] it writes whole percents. pub fn zoom_codec(&self) -> NumberCodec { - let decimals = match self.zoom { + NumberCodec::new(self.reader(Some(UnitKind::Percent)), self.zoom_decimals()).zoom_typing() + } +} + +impl Units { + /// The most decimals a zoom shows: the env's [`ZoomPrecision`], shared by `zoom_codec` and `zoom_percent`. + fn zoom_decimals(&self) -> usize { + match self.zoom { ZoomPrecision::Fractional => ZOOM_DECIMALS, ZoomPrecision::Whole => 0, - }; - NumberCodec::new(self.reader(Some(UnitKind::Percent)), decimals).zoom_typing() + } } } @@ -426,9 +432,8 @@ impl Units { } /// The same units with `zoom` as the decimals a zoom shows: the env's [`ZoomPrecision`]. - #[doc(hidden)] #[must_use] - pub fn with_zoom_precision(mut self, zoom: ZoomPrecision) -> Self { + pub(crate) fn with_zoom_precision(mut self, zoom: ZoomPrecision) -> Self { self.zoom = zoom; self } @@ -543,9 +548,9 @@ impl Units { } /// A zoom of `n` percent: `100%`, `12.5%`, `66.67%`. The fewest digits that show it to two decimals, so the - /// ladder's thirds read as Photoshop shows them. + /// ladder's thirds read as Photoshop shows them. Under [`ZoomPrecision::Whole`] a whole percent. pub fn zoom_percent(&self, n: f64) -> String { - self.text(UnitKind::Percent, n, 0..=2) + self.text(UnitKind::Percent, n, 0..=self.zoom_decimals()) } /// `n` degrees: `12°`. diff --git a/crates/ui-contract/tests/it/runtime/env.rs b/crates/ui-contract/tests/it/runtime/env.rs index 0923dfab5d..54af6885de 100644 --- a/crates/ui-contract/tests/it/runtime/env.rs +++ b/crates/ui-contract/tests/it/runtime/env.rs @@ -68,3 +68,22 @@ fn the_hit_reach_is_one_read_in_a_scope_and_from_the_context() { assert_eq!(Env::root_hit_reach(ui.ctx()), reach); }); } + +/// A read-only zoom text follows the env's zoom precision: whole under `Whole`, with its fraction by default. +#[trial] +fn zoom_percent_follows_the_zoom_precision() { + use craft_ui::runtime::env::ZoomPrecision; + let mut frames = Frames::new(); + frames.frame(vec![], |ui| { + let fractional = Env::of(ui).units().zoom_percent(94.15); + let whole = with_env( + ui, + |o| { + o.values.set(ZoomPrecision::Whole); + }, + |ui| Env::of(ui).units().zoom_percent(94.15), + ); + assert!(fractional.contains('.') || fractional.contains(','), "the default keeps a fraction: {fractional}"); + assert!(!whole.contains('.') && !whole.contains(','), "whole has no decimal mark: {whole}"); + }); +} diff --git a/docs/contract-api.md b/docs/contract-api.md index 3128890a65..2f09ff91f3 100644 --- a/docs/contract-api.md +++ b/docs/contract-api.md @@ -4119,6 +4119,7 @@ One-shot screenshot slots and the facts a screenshot is measured against. - `craft_ui::runtime::env::WordTemplate`: re-export of `craft_i18n::WordTemplate` (outside these crates). - `craft_ui::runtime::env::Words`: re-export of `craft_i18n::Words` (outside these crates). - `craft_ui::runtime::env::WordsOverride`: `pub enum WordsOverride { Replace(Arc), Overlay(WordRows) }`. How a scope changes the built-in text: the only override path for words. +- `craft_ui::runtime::env::ZoomPrecision`: `pub enum ZoomPrecision { Fractional, Whole }`. How many decimals a zoom shows. An app sets it once on its root env with `RootEnv::values`; [`Units::zoom_codec`] - `craft_ui::runtime::env::in_room`: `pub fn in_room(ui: &mut Ui, rect: Rect, body: impl FnOnce(&mut Ui) -> R) -> R`. Runs `body` in a child `Ui` held to `rect` and clipped to it, as a layout room of its own: the flow grid, content - `craft_ui::runtime::env::system_appearance`: `pub fn system_appearance(ctx: &Context) -> SystemState`. What the OS reports on `ctx` now: the host's reader (`InstallSpec::system`), with the colour scheme egui's - `craft_ui::runtime::env::system_text_scale`: `pub fn system_text_scale(ctx: &Context) -> Option`. The desktop text scale from the installed [`SystemReader`], `None` where the OS gives none.