mirror of
https://github.com/storytold/craft-libs.git
synced 2026-11-26 02:09:00 +00:00
Percent number fields read one decimal and the sign beside the number (L-308: 100.0%)
This commit is contained in:
@@ -423,6 +423,7 @@ pub(crate) fn run(state: &mut TextState, mut input: AdapterIn<'_>) -> AdapterOut
|
||||
preedit: preedit.clone(),
|
||||
rtl: input.rtl,
|
||||
centred: input.options.centred,
|
||||
end_aligned: input.options.end_aligned,
|
||||
caret_visible: input.caret_visible,
|
||||
underline: input.underline,
|
||||
galley: None,
|
||||
@@ -548,6 +549,7 @@ pub(crate) fn show_selectable(ui: &mut Ui, text: Selectable<'_>) {
|
||||
preedit: None,
|
||||
rtl: false,
|
||||
centred: false,
|
||||
end_aligned: false,
|
||||
caret_visible: false,
|
||||
underline: 0.0,
|
||||
galley: Some(layout.galley.clone()),
|
||||
|
||||
@@ -84,6 +84,9 @@ pub struct TextOptions {
|
||||
pub mixed: bool,
|
||||
/// The text sits in the middle of the part instead of at its start (numbers).
|
||||
pub centred: bool,
|
||||
/// The text sits at the part's end edge, against what follows it (a number and its attached unit sign). Ignored
|
||||
/// while `centred`.
|
||||
pub end_aligned: bool,
|
||||
/// A plain Left or Right with the whole text selected starts from the end of the text, not from the selection's
|
||||
/// start: a field whose last digits are what the user edits (a timecode's frames).
|
||||
pub arrows_from_end: bool,
|
||||
@@ -139,6 +142,7 @@ impl Default for TextOptions {
|
||||
submits: false,
|
||||
mixed: false,
|
||||
centred: false,
|
||||
end_aligned: false,
|
||||
arrows_from_end: false,
|
||||
unparsed: None,
|
||||
own_menu: false,
|
||||
|
||||
@@ -34,6 +34,8 @@ pub(crate) struct Edit<'a> {
|
||||
pub rtl: bool,
|
||||
/// The text sits in the middle of the rect.
|
||||
pub centred: bool,
|
||||
/// The text sits at the end edge of the rect (unless `centred`).
|
||||
pub end_aligned: bool,
|
||||
/// Whether the caret is drawn in this pass.
|
||||
pub caret_visible: bool,
|
||||
/// The width of the preedit underline.
|
||||
@@ -84,6 +86,7 @@ pub(crate) fn show(ui: &mut Ui, edit: Edit<'_>) -> Shown {
|
||||
preedit,
|
||||
rtl,
|
||||
centred,
|
||||
end_aligned,
|
||||
caret_visible,
|
||||
underline,
|
||||
galley,
|
||||
@@ -202,8 +205,10 @@ pub(crate) fn show(ui: &mut Ui, edit: Edit<'_>) -> Shown {
|
||||
.align(match (centred, rtl) {
|
||||
_ if galley.is_some() => egui::Align2::LEFT_TOP,
|
||||
(true, _) => egui::Align2::CENTER_CENTER,
|
||||
(false, true) => egui::Align2::RIGHT_CENTER,
|
||||
(false, false) => egui::Align2::LEFT_CENTER,
|
||||
(false, rtl) => match (end_aligned, rtl) {
|
||||
(true, false) | (false, true) => egui::Align2::RIGHT_CENTER,
|
||||
(true, true) | (false, false) => egui::Align2::LEFT_CENTER,
|
||||
},
|
||||
});
|
||||
if let Some(hint) = hint_text.clone() {
|
||||
edit = edit.hint_text(hint);
|
||||
|
||||
@@ -273,6 +273,11 @@ 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.
|
||||
fn is_percent(&self) -> bool {
|
||||
self.codec.is_none() && self.unit_kind == Some(UnitKind::Percent)
|
||||
}
|
||||
|
||||
/// The field's codec: the one set, else the env's locale and the spellings of the unit kind (a length field
|
||||
/// reads every length spelling).
|
||||
fn number_codec(&self, units: &Units) -> NumberCodec {
|
||||
@@ -282,7 +287,7 @@ impl<'a, V: NumberValue + 'static> NumberField<'a, V> {
|
||||
None => units.reader(self.unit_kind),
|
||||
};
|
||||
let codec = NumberCodec::new(reader, self.decimals());
|
||||
if self.fixed_decimals.is_some() { codec.fixed() } else { codec }
|
||||
if self.fixed_decimals.is_some() || self.is_percent() { codec.fixed() } else { codec }
|
||||
})
|
||||
}
|
||||
|
||||
@@ -290,6 +295,8 @@ 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,
|
||||
(None, Some(fixed), _, _) => fixed..=fixed,
|
||||
(None, None, Some(max), _) => 0..=max.min(12),
|
||||
(None, None, None, Some(kind)) => kind.decimals(),
|
||||
@@ -483,7 +490,9 @@ impl<V: NumberValue + 'static> Component for NumberField<'_, V> {
|
||||
cx.label_part(*slot);
|
||||
}
|
||||
let text = placed.text;
|
||||
self.options.centred = true;
|
||||
// A percentage reads "100.0%": the number stands against its sign, not in the middle of the room.
|
||||
self.options.centred = !self.is_percent();
|
||||
self.options.end_aligned = self.is_percent();
|
||||
self.options.mono = true;
|
||||
self.options.steps_value = self.presets.is_none() && self.popup_slider.is_none();
|
||||
let shown = cx.value();
|
||||
|
||||
@@ -122,7 +122,7 @@ fn a_bar_number_field_sets_its_digits_and_unit_on_the_line_of_a_select() {
|
||||
});
|
||||
});
|
||||
fixture.settle();
|
||||
let (face, digits, unit) = (baseline_of(&fixture, "Normal"), baseline_of(&fixture, "100"), baseline_of(&fixture, "%"));
|
||||
let (face, digits, unit) = (baseline_of(&fixture, "Normal"), baseline_of(&fixture, "100.0"), baseline_of(&fixture, "%"));
|
||||
assert!((digits - face).abs() < 0.5, "the digits sit on the select's baseline: digits {digits}, select {face}");
|
||||
assert!((unit - face).abs() < 0.5, "the unit sits on the select's baseline: unit {unit}, select {face}");
|
||||
}
|
||||
|
||||
@@ -351,7 +351,7 @@ fn the_unit_keeps_its_place_at_the_end_of_the_field() {
|
||||
v.clear();
|
||||
for (at, value) in [5.0, 100.0].into_iter().enumerate() {
|
||||
let mut shown = Some(value);
|
||||
let field = NumberField::new("N", Bind::bound(&mut shown)).range(0.0..=100.0).unit_kind(UnitKind::Percent).id_salt(at);
|
||||
let field = NumberField::new("N", Bind::bound(&mut shown)).range(0.0..=100.0).unit_kind(UnitKind::Points).id_salt(at);
|
||||
v.push(field.show(ui).response.rect);
|
||||
}
|
||||
});
|
||||
@@ -718,3 +718,17 @@ fn the_wheel_over_an_open_presets_list_scrolls_the_list_while_the_field_keeps_fo
|
||||
assert!(first_row(&fixture).is_none_or(|row| row.top() < before.top()), "the list scrolled");
|
||||
assert_eq!(value.get(), committed, "the field's value stays");
|
||||
}
|
||||
|
||||
/// A percent field shows one decimal and its sign right after the number ("100.0%"), not the sign apart at the end of
|
||||
/// the field.
|
||||
#[trial]
|
||||
fn a_percent_field_reads_one_decimal_then_the_sign() {
|
||||
let mut fixture = craft_ui::testing::Fixture::new(|ui| {
|
||||
NumberField::new("Scale", Bind::bound(&mut Some(100.0f64))).unit_kind(UnitKind::Percent).range(1.0..=1000.0).show(ui);
|
||||
});
|
||||
fixture.settle();
|
||||
let painted = |text: &str| fixture.texts().iter().find(|run| run.text == text).map(|run| run.rect);
|
||||
let number = painted("100.0").unwrap_or_else(|| panic!("the value shows one decimal: {:?}", fixture.texts().iter().map(|r| r.text.clone()).collect::<Vec<_>>()));
|
||||
let sign = painted("%").unwrap_or_else(|| panic!("the field shows the sign"));
|
||||
assert!(sign.min.x - number.max.x <= 5.0, "the sign follows the number closely: {number:?} {sign:?}");
|
||||
}
|
||||
|
||||
@@ -62,7 +62,7 @@ fn a_small_percent_field_shows_its_value_whole_beside_the_chevron() {
|
||||
fixture.settle();
|
||||
let chevron = fixture.by_role(Role::BUTTON).first().unwrap_or_else(|| panic!("the field shows its chevron")).rect;
|
||||
let painted = |text: &str| fixture.texts().iter().find(|run| run.text == text).map(|run| run.rect);
|
||||
let value = painted("100").unwrap_or_else(|| panic!("the field shows its value"));
|
||||
let value = painted("100.0").unwrap_or_else(|| panic!("the field shows its value"));
|
||||
let unit = painted("%").unwrap_or_else(|| panic!("the field shows its unit"));
|
||||
assert!(value.max.x <= unit.min.x, "the value ends before its unit: {value:?} {unit:?}");
|
||||
assert!(unit.max.x <= chevron.min.x, "the unit ends before the chevron: {unit:?} {chevron:?}");
|
||||
|
||||
Reference in New Issue
Block a user