mirror of
https://github.com/storytold/craft-libs.git
synced 2026-11-26 00:57:26 +00:00
ui-widgets: NumberPrecision::Measured lets an app show whole percentages (60%) where the standard precision writes one decimal
This commit is contained in:
@@ -19,8 +19,8 @@ pub use chips::{ChipCodec, ChipField, ChipFieldOutput, ChipFieldStyle, ChipWords
|
||||
pub use hex::{HexCodec, HexField};
|
||||
pub use linked::{LinkMode, LinkedFields, LinkedFieldsStyle, LinkedLayout, LinkedOutput, LinkedWords, follow_equal, follow_ratio, link_toggle};
|
||||
pub use number::{
|
||||
NamedOr, NumberField, NumberLook, NumberName, NumberOutput, NumberPick, NumberStepper, NumberStyle, NumberValue, RangeAlert, StepArrows,
|
||||
UnitPlacement,
|
||||
NamedOr, NumberField, NumberLook, NumberName, NumberOutput, NumberPick, NumberPrecision, NumberStepper, NumberStyle, NumberValue, RangeAlert,
|
||||
StepArrows, UnitPlacement,
|
||||
};
|
||||
pub use selectable_text::{SelectableStyle, SelectableText};
|
||||
pub use text_field::TextField;
|
||||
|
||||
@@ -85,6 +85,19 @@ pub enum NumberLook {
|
||||
Inline,
|
||||
}
|
||||
|
||||
/// How many decimals a number field without a codec, fixed or maximum decimals of its own shows. An app sets it once
|
||||
/// at its root (`RootEnv::values`); the default is the standard precision.
|
||||
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash, crate::runtime::env::EnvValue, crate::contract::MemoProps)]
|
||||
#[env(style)]
|
||||
#[non_exhaustive]
|
||||
pub enum NumberPrecision {
|
||||
/// A percentage reads one decimal, written in full ("100.0%"); other numbers their unit kind's decimals.
|
||||
#[default]
|
||||
Standard,
|
||||
/// A percentage reads whole ("60%"): a drawing app's opacity and scale.
|
||||
Measured,
|
||||
}
|
||||
|
||||
/// The placeholder of a mixed field.
|
||||
const BLANK: &str = "\u{2014}";
|
||||
|
||||
@@ -207,6 +220,8 @@ pub struct NumberField<'a, V: NumberValue + 'static = Option<f64>> {
|
||||
scale: f64,
|
||||
/// The scope's look, read from the env in measure and parts.
|
||||
look: NumberLook,
|
||||
/// The scope's precision, read from the env in measure and parts.
|
||||
precision: NumberPrecision,
|
||||
options: TextOptions,
|
||||
positive_only: bool,
|
||||
scrub: bool,
|
||||
@@ -286,6 +301,7 @@ impl<'a, V: NumberValue + 'static> NumberField<'a, V> {
|
||||
lengths: None,
|
||||
scale: 1.0,
|
||||
look: NumberLook::Centred,
|
||||
precision: NumberPrecision::Standard,
|
||||
options: TextOptions::default(),
|
||||
positive_only: false,
|
||||
scrub: false,
|
||||
@@ -344,10 +360,11 @@ impl<'a, V: NumberValue + 'static> NumberField<'a, V> {
|
||||
Rule::new(range.as_ref(), self.step)
|
||||
}
|
||||
|
||||
/// Reads the scope's look and a length field's scale from stored to shown from the env. The status bar's zoom
|
||||
/// Reads the scope's look and precision and a length field's scale from stored to shown from the env. The status bar's zoom
|
||||
/// well keeps its own look.
|
||||
fn settle(&mut self, env: &EnvView<'_>) {
|
||||
self.look = if self.status_zoom { NumberLook::Centred } else { env.get() };
|
||||
self.precision = env.get();
|
||||
if let Some(lengths) = self.lengths {
|
||||
self.scale = env.units().length_scale(lengths).unwrap_or(1.0);
|
||||
}
|
||||
@@ -423,7 +440,7 @@ impl<'a, V: NumberValue + 'static> NumberField<'a, V> {
|
||||
value.and_then(NumberValue::number).map(|stored| self.to_shown(stored))
|
||||
}
|
||||
|
||||
/// Whether the field counts percent: it shows one decimal and its sign right after the number.
|
||||
/// Whether the field counts percent: it writes its precision's decimals in full, its sign right after the number.
|
||||
fn is_percent(&self) -> bool {
|
||||
self.codec.is_none() && self.unit_kind == Some(UnitKind::Percent)
|
||||
}
|
||||
@@ -450,8 +467,11 @@ impl<'a, V: NumberValue + 'static> NumberField<'a, V> {
|
||||
fn shown_decimals(&self) -> RangeInclusive<usize> {
|
||||
match (&self.codec, self.fixed_decimals, self.max_decimals, self.unit_kind) {
|
||||
(Some(codec), _, _, _) => 0..=codec.decimals(),
|
||||
// A percentage reads "100.0%": one decimal, written in full.
|
||||
(None, _, _, Some(UnitKind::Percent)) => 1..=1,
|
||||
// A percentage reads "100.0%": one decimal, written in full; whole at the measured precision.
|
||||
(None, _, _, Some(UnitKind::Percent)) => match self.precision {
|
||||
NumberPrecision::Standard => 1..=1,
|
||||
NumberPrecision::Measured => 0..=0,
|
||||
},
|
||||
(None, Some(fixed), _, _) => fixed..=fixed,
|
||||
(None, None, Some(max), _) => 0..=max.min(12),
|
||||
(None, None, None, Some(kind)) => kind.decimals(),
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
//! Stories of `NumberField`.
|
||||
|
||||
use super::{NamedOr, NumberField, NumberLook, NumberName, NumberPick, StepArrows, UnitPlacement};
|
||||
use super::{NamedOr, NumberField, NumberLook, NumberName, NumberPick, NumberPrecision, StepArrows, UnitPlacement};
|
||||
use crate::contract::{Bind, Entry, Props as _, Show as _, ShowContainer as _, Validity};
|
||||
use crate::display::Text;
|
||||
use crate::form::{Field, FieldGroup};
|
||||
@@ -255,3 +255,25 @@ fn focus_first(fixture: &mut Fixture) {
|
||||
fn number_field_inline_focused(ui: &mut Ui, data: &mut StoryData) -> StoryOutput {
|
||||
inline_fields(ui, data)
|
||||
}
|
||||
|
||||
/// Fields at the measured precision ([`NumberPrecision::Measured`]): a percentage reads whole.
|
||||
#[story(NumberField<'static>, covers = [], drive = Drive::Never, title = "Measured precision")]
|
||||
fn number_field_measured(ui: &mut Ui, data: &mut StoryData) -> StoryOutput {
|
||||
let out = with_env(
|
||||
ui,
|
||||
|overrides| {
|
||||
overrides.values.set(NumberPrecision::Measured);
|
||||
},
|
||||
|ui| {
|
||||
FieldGroup::new("Measured precision")
|
||||
.show(ui, |ui, _| {
|
||||
let first = row("Percent").show(ui, |ui, _| {
|
||||
NumberField::new("Opacity", data.bind("opacity", Some(60.0))).range(RANGE).unit_kind(UnitKind::Percent).show(ui).edit
|
||||
});
|
||||
first.inner
|
||||
})
|
||||
.inner
|
||||
},
|
||||
);
|
||||
out.flatten().map(StoryOutput::from).unwrap_or_default()
|
||||
}
|
||||
|
||||
@@ -1,10 +1,11 @@
|
||||
//! The number field's inline look (`NumberLook::Inline`): the number and its unit are one run of the text part, so
|
||||
//! editing reaches the unit's text as it reaches the number's.
|
||||
//! The number field's looks an app sets at its root: the inline look (`NumberLook::Inline`), where the number and its
|
||||
//! unit are one run of the text part, so editing reaches the unit's text as it reaches the number's; and the measured
|
||||
//! precision (`NumberPrecision::Measured`).
|
||||
|
||||
use craft_ui::contract::{Bind, Props as _, Show as _};
|
||||
use craft_ui::runtime::env::{LengthUnits, UnitKind};
|
||||
use craft_ui::testing::{Fixture, trial};
|
||||
use craft_ui::text::{NumberField, NumberLook};
|
||||
use craft_ui::text::{NumberField, NumberLook, NumberPrecision};
|
||||
use egui::{Id, Key, Modifiers, Rect};
|
||||
use std::cell::Cell;
|
||||
use std::rc::Rc;
|
||||
@@ -70,3 +71,22 @@ fn an_inline_field_edits_its_whole_run_and_commits_the_number() {
|
||||
fixture.settle();
|
||||
assert_eq!(value.get(), Some(46.0), "the number edited inside the run commits without its unit");
|
||||
}
|
||||
|
||||
/// At the measured precision a percentage reads whole where the standard precision writes one decimal: the same
|
||||
/// field shows fewer digits.
|
||||
#[trial]
|
||||
fn a_percent_at_the_measured_precision_reads_whole() {
|
||||
let number = |precision: Option<NumberPrecision>| {
|
||||
let mut fixture = Fixture::new(|ui| {
|
||||
NumberField::new("Opacity", Bind::bound(&mut Some(60.0f64))).unit_kind(UnitKind::Percent).range(0.0..=100.0).show(ui);
|
||||
});
|
||||
if let Some(precision) = precision {
|
||||
fixture.root_value(precision);
|
||||
}
|
||||
fixture.settle();
|
||||
fixture.texts().iter().map(|run| run.text.clone()).find(|text| text.starts_with("60")).unwrap_or_default()
|
||||
};
|
||||
let (standard, measured) = (number(None), number(Some(NumberPrecision::Measured)));
|
||||
assert!(standard.starts_with("60") && measured.starts_with("60"), "both show the value: {standard:?} {measured:?}");
|
||||
assert!(measured.len() < standard.len() && !measured.contains(['.', ',']), "the measured percent is whole: {measured:?}, standard {standard:?}");
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user