mirror of
https://github.com/storytold/craft-libs.git
synced 2026-11-26 00:57:26 +00:00
behavior: NumberCodec::share_of_start reads a lone percentage (Reader::share) as that share of the value the edit started from, on every pass and on commit only: a size field's 50% could only be read against the value of the moment, which the numbers typed before the % had already applied
This commit is contained in:
@@ -167,6 +167,8 @@ pub struct NumberCodec {
|
||||
custom: Option<Custom>,
|
||||
/// Typing rules of a zoom field ([`NumberCodec::zoom_typing`]).
|
||||
zoom: bool,
|
||||
/// A lone percentage is that share of the start value ([`NumberCodec::share_of_start`]).
|
||||
share: bool,
|
||||
}
|
||||
|
||||
impl fmt::Debug for NumberCodec {
|
||||
@@ -176,6 +178,7 @@ impl fmt::Debug for NumberCodec {
|
||||
.field("decimals", &self.decimals)
|
||||
.field("fixed", &self.fixed)
|
||||
.field("zoom", &self.zoom)
|
||||
.field("share", &self.share)
|
||||
.field("custom", &self.custom.is_some())
|
||||
.finish()
|
||||
}
|
||||
@@ -188,7 +191,12 @@ impl PartialEq for NumberCodec {
|
||||
(Some(a), Some(b)) => Arc::ptr_eq(a, b),
|
||||
_ => false,
|
||||
};
|
||||
self.reader == other.reader && self.decimals == other.decimals && self.fixed == other.fixed && self.zoom == other.zoom && same_custom
|
||||
self.reader == other.reader
|
||||
&& self.decimals == other.decimals
|
||||
&& self.fixed == other.fixed
|
||||
&& self.zoom == other.zoom
|
||||
&& self.share == other.share
|
||||
&& same_custom
|
||||
}
|
||||
}
|
||||
|
||||
@@ -222,7 +230,7 @@ const NUMBER_SAMPLE: &str = "0000.00";
|
||||
impl NumberCodec {
|
||||
/// A codec reading with `reader` and writing at most `decimals` decimal places.
|
||||
pub fn new(reader: Reader, decimals: usize) -> Self {
|
||||
NumberCodec { reader, decimals, fixed: false, custom: None, zoom: false }
|
||||
NumberCodec { reader, decimals, fixed: false, custom: None, zoom: false, share: false }
|
||||
}
|
||||
|
||||
/// The same codec writing every decimal place, trailing zeros included ("1.00"), as a field of a quantity shown
|
||||
@@ -262,6 +270,14 @@ impl NumberCodec {
|
||||
Ok((number, ParseNote { cut, ..ParseNote::default() }))
|
||||
}
|
||||
|
||||
/// The same codec reading a lone percentage (`50%`, [`Reader::share`]) as that share of the value the edit
|
||||
/// started from, as a size field does: every pass of the edit reads the same value. With no start value it is no
|
||||
/// value, so it never applies as it is typed; it applies on commit.
|
||||
pub fn share_of_start(mut self) -> Self {
|
||||
self.share = true;
|
||||
self
|
||||
}
|
||||
|
||||
/// The same codec writing at most `decimals` decimal places.
|
||||
pub fn at_most(mut self, decimals: usize) -> Self {
|
||||
self.decimals = self.decimals.min(decimals);
|
||||
@@ -342,6 +358,11 @@ impl Codec for NumberCodec {
|
||||
if self.zoom {
|
||||
return self.read_zoom(trimmed).map(|(number, note)| (Some(number), note));
|
||||
}
|
||||
if self.share
|
||||
&& let Some(share) = self.reader.share(trimmed)
|
||||
{
|
||||
return start.map(|start| (Some(start * share), ParseNote::default())).ok_or(NotAValue);
|
||||
}
|
||||
match self.reader.read_tagged(text, *start) {
|
||||
Ok((number, tag)) => Ok((Some(number), ParseNote { unit: tag, ..ParseNote::default() })),
|
||||
Err(rejected) => {
|
||||
|
||||
@@ -130,6 +130,16 @@ impl Reader {
|
||||
};
|
||||
(parse.at == chars.len() && value.is_finite()).then_some((value, parse.tag)).ok_or(NotAValue)
|
||||
}
|
||||
|
||||
/// The fraction a lone percentage writes (`50%` is 0.5): a number and `%`, with no sign, unit or operator;
|
||||
/// `None` for any other text.
|
||||
pub fn share(&self, text: &str) -> Option<f64> {
|
||||
let chars: Vec<char> = text.chars().filter(|c| !c.is_whitespace()).collect();
|
||||
let number = chars.strip_suffix(&['%'])?;
|
||||
let mut parse = Parse { reader: self, chars: number, at: 0, tag: None };
|
||||
let value = parse.number().ok()?;
|
||||
(parse.at == number.len()).then_some(value / 100.0)
|
||||
}
|
||||
}
|
||||
|
||||
struct Parse<'a> {
|
||||
|
||||
@@ -1,7 +1,8 @@
|
||||
//! A length field keeps the document's unit and shows the user's.
|
||||
|
||||
use craft_behavior::NumberCodec;
|
||||
use craft_ui::contract::{Bind, Props as _, Show as _};
|
||||
use craft_ui::runtime::env::{LengthUnits, UnitKind};
|
||||
use craft_ui::runtime::env::{Env, LengthUnits, UnitKind};
|
||||
use craft_ui::testing::input::key_press;
|
||||
use craft_ui::testing::pointer::click;
|
||||
use craft_ui::testing::{Frames, trial};
|
||||
@@ -90,3 +91,33 @@ fn picas_with_points_and_a_percentage_factor_read_as_print_apps_write_them() {
|
||||
let half = in_lengths(mm, START, type_and_commit("1.5cm"));
|
||||
assert!((share - half).abs() < SAME && share != START, "3cm*50% is half of 3 cm: {share} against {half}");
|
||||
}
|
||||
|
||||
/// With `NumberCodec::share_of_start`, a lone percentage typed key by key is that share of the length the edit began
|
||||
/// on, applied on Enter: the numbers typed before the `%` apply as typed, and the share does not read them.
|
||||
#[trial]
|
||||
fn a_lone_percentage_is_that_share_of_the_length_the_edit_began_on() {
|
||||
let mm = LengthUnits::new(UnitKind::Points, UnitKind::Millimetres, START);
|
||||
let mut frames = Frames::new();
|
||||
let value = RefCell::new(Some(START));
|
||||
let at = Cell::new(Pos2::ZERO);
|
||||
let mut show = |ui: &mut Ui| {
|
||||
let codec = NumberCodec::new(Env::of(ui).units().length_reader(mm), 2).share_of_start();
|
||||
let mut guard = value.borrow_mut();
|
||||
let field = NumberField::length("Width", Bind::bound(&mut *guard), mm).codec(codec).id_salt("width");
|
||||
at.set(field.show(ui).response.rect.center());
|
||||
};
|
||||
let half = |v: Option<f64>| v.is_some_and(|v| (v - START / 2.0).abs() < SAME);
|
||||
frames.idle(3, &mut show);
|
||||
click(&mut frames, at.get(), &mut show);
|
||||
frames.frame(vec![key_press(Key::A, Modifiers::COMMAND)], &mut show);
|
||||
for typed in ["5", "0", "%"] {
|
||||
frames.frame(vec![Event::Text(typed.into())], &mut show);
|
||||
frames.idle(1, &mut show);
|
||||
}
|
||||
let typed = *value.borrow();
|
||||
assert!(typed != Some(START) && !half(typed), "the numbers apply as typed, the share waits for Enter: {typed:?}");
|
||||
frames.frame(vec![key_press(Key::Enter, Modifiers::NONE)], &mut show);
|
||||
frames.idle(3, &mut show);
|
||||
let committed = value.into_inner();
|
||||
assert!(half(committed), "50% is half the length the edit began on: {committed:?}");
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user