mirror of
https://github.com/storytold/craft-libs.git
synced 2026-11-26 12:32:10 +01:00
ui-contract: zoom_percent follows ZoomPrecision with zoom_codec
One zoom_decimals helper serves both, so the read-only zoom texts (Navigator slider, full-window title, zoom bar entries) agree with the zoom fields. with_zoom_precision is pub(crate). Regenerates docs/contract-api.md. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
43ae21e05e
commit
e173ef092e
3 files changed
+32
-7
No files matched your search
+12
-7
@@ -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°`.
|
||||
|
||||
@@ -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}");
|
||||
});
|
||||
}
|
||||
@@ -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<Words>), 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<R>(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<f32>`. The desktop text scale from the installed [`SystemReader`], `None` where the OS gives none.
|
||||
|
||||
Reference in new issue
Block a user