mirror of
https://github.com/storytold/craft-libs.git
synced 2026-11-18 03:42:58 +00:00
ui: strengthen number presentation and overflow coverage
This commit is contained in:
committed by
skylar
parent
6008abdb96
commit
2f9f4f368b
@@ -253,9 +253,10 @@ impl<'a, V: NumberValue + 'static> NumberField<'a, V> {
|
||||
self
|
||||
}
|
||||
|
||||
/// The face the value is set in: monospace, whose digits share one advance so values keep their places as they
|
||||
/// change (the default), or the UI body face. The face is the one the field measures its value in, paints it in and
|
||||
/// edits it in, so the room a value takes is the room its text and caret fill.
|
||||
/// The type role the value is set in: the monospace role, whose digits share one advance so values keep their places
|
||||
/// as they change (the default), or the UI body role, whose face **and** size the value then takes. The role is the
|
||||
/// one the field measures its value in, paints it in and edits it in, so the room a value takes is the room its text
|
||||
/// and caret fill.
|
||||
#[must_use]
|
||||
pub fn mono(mut self, mono: bool) -> Self {
|
||||
self.options.mono = mono;
|
||||
|
||||
@@ -109,8 +109,8 @@ fn number_field(ui: &mut Ui, data: &mut StoryData) -> StoryOutput {
|
||||
out.inner.flatten().map(StoryOutput::from).unwrap_or_default()
|
||||
}
|
||||
|
||||
/// The two presentation choices on their own and together: the value centred or started at the reading start, set in the
|
||||
/// monospace face or the UI body face, over the same range.
|
||||
/// The two presentation choices on their own and together: the value centred or started at the logical reading start, set
|
||||
/// in the monospace role or the UI body role (face and size), over the same range.
|
||||
#[story(NumberField<'static>, covers = [], drive = Drive::Type("42"))]
|
||||
fn number_field_presentation(ui: &mut Ui, data: &mut StoryData) -> StoryOutput {
|
||||
let out = FieldGroup::new("Number field presentation").show(ui, |ui, _| {
|
||||
|
||||
@@ -1,108 +1,162 @@
|
||||
//! Where a number field puts its value and the face it sets it in: `centred` and `mono`, either of them off, on their
|
||||
//! own and together, in measure, paint and editing.
|
||||
//! Where a number field puts its value and the role it sets it in: `centred` and `mono`, either of them off, on their own
|
||||
//! and together, in measure, paint and editing.
|
||||
|
||||
use craft_behavior::Mixed;
|
||||
use craft_i18n::Direction;
|
||||
use craft_ui::contract::{Bind, Props as _, Show as _, Sizing};
|
||||
use craft_ui::display::Text;
|
||||
use craft_ui::runtime::env::UnitKind;
|
||||
use craft_ui::style::TypeRole;
|
||||
use craft_ui::testing::{Fixture, trial};
|
||||
use craft_ui::text::NumberField;
|
||||
use egui::{FontFamily, Key, Modifiers, Rect, Shape};
|
||||
use egui::{FontFamily, FontId, Key, Modifiers, Rect, Shape};
|
||||
use std::cell::RefCell;
|
||||
use std::rc::Rc;
|
||||
|
||||
/// The texts a frame painted, each with where it lies and the face it is set in.
|
||||
fn painted(shapes: &[Shape]) -> Vec<(String, Rect, FontFamily, f32)> {
|
||||
shapes
|
||||
.iter()
|
||||
/// The label of the reference run set in the monospace role.
|
||||
const MONO_ROLE: &str = "mono role";
|
||||
/// The label of the reference run set in the UI body role.
|
||||
const BODY_ROLE: &str = "body role";
|
||||
|
||||
/// A text run a frame painted: its text, where it lies and the font it is set in.
|
||||
struct Run {
|
||||
text: String,
|
||||
rect: Rect,
|
||||
font: FontId,
|
||||
}
|
||||
|
||||
/// The runs the next frame paints, with their fonts.
|
||||
fn painted_runs(fixture: &mut Fixture) -> Vec<Run> {
|
||||
fixture
|
||||
.frame_shapes()
|
||||
.into_iter()
|
||||
.filter_map(|shape| match shape {
|
||||
Shape::Text(text) => {
|
||||
let font = text.galley.job.sections.first()?.format.font_id.clone();
|
||||
let rect = text.galley.rect.translate(text.pos.to_vec2());
|
||||
Some((text.galley.job.text.clone(), rect, font.family, font.size))
|
||||
Some(Run { text: text.galley.job.text.clone(), rect: text.galley.rect.translate(text.pos.to_vec2()), font })
|
||||
}
|
||||
_ => None,
|
||||
})
|
||||
.collect()
|
||||
}
|
||||
|
||||
/// The painted run of `text` that lies inside `field`, from the frame's shapes.
|
||||
fn run<'a>(runs: &'a [(String, Rect, FontFamily, f32)], field: Rect, text: &str) -> Option<&'a (String, Rect, FontFamily, f32)> {
|
||||
runs.iter().find(|run| run.0 == text && field.expand(1.0).contains_rect(run.1))
|
||||
/// The run showing `text` inside `field`.
|
||||
fn run_in<'a>(runs: &'a [Run], field: Rect, text: &str) -> Option<&'a Run> {
|
||||
runs.iter().find(|run| run.text == text && field.expand(1.0).contains_rect(run.rect))
|
||||
}
|
||||
|
||||
/// The rect of the value `text` a frame of `fixture` painted inside `field`.
|
||||
fn value_in(fixture: &Fixture, field: Rect, text: &str) -> Rect {
|
||||
let found = fixture.texts().iter().find(|run| run.text == text && field.expand(1.0).contains_rect(run.rect));
|
||||
match found {
|
||||
Some(run) => run.rect,
|
||||
None => panic!("{field:?} paints no {text}: {:?}", fixture.texts().iter().map(|run| run.text.clone()).collect::<Vec<_>>()),
|
||||
/// The run showing `text` inside `field`, or a panic naming what the frame painted.
|
||||
fn shown<'a>(runs: &'a [Run], field: Rect, text: &str) -> &'a Run {
|
||||
match run_in(runs, field, text) {
|
||||
Some(run) => run,
|
||||
None => panic!("{field:?} paints no {text}: {:?}", runs.iter().map(|run| run.text.clone()).collect::<Vec<_>>()),
|
||||
}
|
||||
}
|
||||
|
||||
/// The rect of the one run inside `field` that is not the value `text`: the field's unit.
|
||||
fn beside_in(fixture: &Fixture, field: Rect, text: &str) -> Rect {
|
||||
let found = fixture.texts().iter().find(|run| run.text != text && field.expand(1.0).contains_rect(run.rect));
|
||||
match found {
|
||||
Some(run) => run.rect,
|
||||
None => panic!("{field:?} paints no unit beside {text}: {:?}", fixture.texts().iter().map(|run| run.text.clone()).collect::<Vec<_>>()),
|
||||
/// The one run inside `field` (the dash of a mixed field).
|
||||
fn only<'a>(runs: &'a [Run], field: Rect) -> &'a Run {
|
||||
match runs.iter().find(|run| field.expand(1.0).contains_rect(run.rect)) {
|
||||
Some(run) => run,
|
||||
None => panic!("{field:?} paints nothing: {:?}", runs.iter().map(|run| run.text.clone()).collect::<Vec<_>>()),
|
||||
}
|
||||
}
|
||||
|
||||
/// The unit a field paints: the one run inside it that is not the value `text`.
|
||||
fn unit_of<'a>(runs: &'a [Run], field: Rect, text: &str) -> &'a Run {
|
||||
match runs.iter().find(|run| run.text != text && field.expand(1.0).contains_rect(run.rect)) {
|
||||
Some(run) => run,
|
||||
None => panic!("{field:?} paints no unit beside {text}: {:?}", runs.iter().map(|run| run.text.clone()).collect::<Vec<_>>()),
|
||||
}
|
||||
}
|
||||
|
||||
/// How far `run` sits from the edge the text reads from: its reading start when `start`, else its reading end; the left
|
||||
/// edge in a left-to-right interface, the right one under right-to-left.
|
||||
fn inset(run: Rect, field: Rect, start: bool, rtl: bool) -> f32 {
|
||||
if start == rtl { field.right() - run.right() } else { run.left() - field.left() }
|
||||
}
|
||||
|
||||
/// Two settled fields of one presentation: "5" and "100" over one range and of one width, so where the fields put them
|
||||
/// says where that presentation puts values of other widths.
|
||||
struct Pair {
|
||||
fixture: Fixture,
|
||||
fields: Vec<Rect>,
|
||||
fn twins(started: bool, direction: Option<Direction>) -> (Vec<Rect>, Fixture) {
|
||||
let boxes: Rc<RefCell<Vec<Rect>>> = Rc::default();
|
||||
let b = boxes.clone();
|
||||
let mut fixture = Fixture::new(move |ui| {
|
||||
let mut v = b.borrow_mut();
|
||||
v.clear();
|
||||
for (at, value) in [5.0, 100.0].into_iter().enumerate() {
|
||||
v.push(NumberField::new("N", Bind::given(Some(value))).range(0.0..=100.0).centred(!started).id_salt(at).show(ui).response.rect);
|
||||
}
|
||||
});
|
||||
if let Some(direction) = direction {
|
||||
fixture.direction(direction);
|
||||
}
|
||||
fixture.settle();
|
||||
let fields = boxes.borrow().clone();
|
||||
(fields, fixture)
|
||||
}
|
||||
|
||||
impl Pair {
|
||||
fn new(started: bool, direction: Option<Direction>) -> Pair {
|
||||
let boxes: Rc<RefCell<Vec<Rect>>> = Rc::default();
|
||||
let b = boxes.clone();
|
||||
let mut fixture = Fixture::new(move |ui| {
|
||||
let mut v = b.borrow_mut();
|
||||
v.clear();
|
||||
for (at, value) in [5.0, 100.0].into_iter().enumerate() {
|
||||
v.push(NumberField::new("N", Bind::given(Some(value))).range(0.0..=100.0).centred(!started).id_salt(at).show(ui).response.rect);
|
||||
}
|
||||
});
|
||||
if let Some(direction) = direction {
|
||||
fixture.direction(direction);
|
||||
/// Four settled fields with a unit: "5" and "100" started, then "5" and "100" centred, all over one range and of one
|
||||
/// width, so each presentation is the other's twin.
|
||||
fn unit_twins(direction: Option<Direction>) -> (Vec<Rect>, Fixture) {
|
||||
let boxes: Rc<RefCell<Vec<Rect>>> = Rc::default();
|
||||
let b = boxes.clone();
|
||||
let mut fixture = Fixture::new(move |ui| {
|
||||
let mut v = b.borrow_mut();
|
||||
v.clear();
|
||||
for (at, (value, centred)) in [(5.0, false), (100.0, false), (5.0, true), (100.0, true)].into_iter().enumerate() {
|
||||
let field = NumberField::new("N", Bind::given(Some(value))).range(0.0..=100.0).unit_kind(UnitKind::Percent).centred(centred);
|
||||
v.push(field.id_salt(at).show(ui).response.rect);
|
||||
}
|
||||
fixture.settle();
|
||||
let fields = boxes.borrow().clone();
|
||||
Pair { fixture, fields }
|
||||
}
|
||||
|
||||
/// The rects of the two values, "5" and "100".
|
||||
fn values(&self) -> [Rect; 2] {
|
||||
[value_in(&self.fixture, self.fields[0], "5"), value_in(&self.fixture, self.fields[1], "100")]
|
||||
});
|
||||
if let Some(direction) = direction {
|
||||
fixture.direction(direction);
|
||||
}
|
||||
fixture.settle();
|
||||
let fields = boxes.borrow().clone();
|
||||
(fields, fixture)
|
||||
}
|
||||
|
||||
/// The presentation every number field has had: the value centred in the room the field leaves for it, in the monospace
|
||||
/// face. A value of another width keeps its centre, and `centred(false)` starts it at the reading start instead, which in
|
||||
/// a left-to-right interface is the field's left edge.
|
||||
/// role. A value of another width keeps its centre; `centred(false)` starts it at the logical reading start instead, the
|
||||
/// left edge in a left-to-right interface and the right edge under right-to-left.
|
||||
#[trial]
|
||||
fn a_field_centres_its_value_in_the_mono_face_unless_told_to_start_it() {
|
||||
let mut centred = Pair::new(false, None);
|
||||
let started = Pair::new(true, None);
|
||||
let (c, s) = (centred.values(), started.values());
|
||||
assert!((c[0].center().x - c[1].center().x).abs() <= 0.5, "centred values of other widths share a centre: {c:?}");
|
||||
assert!((c[0].left() - c[1].left()).abs() > 2.0, "centred values of other widths start at other places: {c:?}");
|
||||
assert!((s[0].left() - s[1].left()).abs() <= 0.5, "started values share the reading start: {s:?}");
|
||||
assert!(s[1].left() < started.fields[1].center().x - 2.0, "a started value sits at the field's start, not its centre: {:?}", s[1]);
|
||||
// The default presentation sets the value in the monospace face: both fields' values are mono runs.
|
||||
let runs = painted(¢red.fixture.frame_shapes());
|
||||
let values: Vec<&(String, Rect, FontFamily, f32)> = runs.iter().filter(|run| run.0 == "5" || run.0 == "100").collect();
|
||||
assert_eq!(values.len(), 2, "the two fields paint their two values: {runs:?}");
|
||||
assert!(values.iter().all(|run| run.2 == FontFamily::Monospace), "a value is set in the mono face by default: {runs:?}");
|
||||
for direction in [None, Some(Direction::Rtl)] {
|
||||
let rtl = direction.is_some();
|
||||
let what = if rtl { "under right-to-left" } else { "under left-to-right" };
|
||||
let (centred_fields, mut centred) = twins(false, direction);
|
||||
let (started_fields, mut started) = twins(true, direction);
|
||||
let (centred_runs, started_runs) = (painted_runs(&mut centred), painted_runs(&mut started));
|
||||
let c = [shown(¢red_runs, centred_fields[0], "5"), shown(¢red_runs, centred_fields[1], "100")];
|
||||
let s = [shown(&started_runs, started_fields[0], "5"), shown(&started_runs, started_fields[1], "100")];
|
||||
assert!(
|
||||
(c[0].rect.center().x - c[1].rect.center().x).abs() <= 0.5,
|
||||
"centred values of other widths share a centre {what}: {:?}",
|
||||
(c[0].rect, c[1].rect)
|
||||
);
|
||||
assert!(
|
||||
(c[0].rect.left() - c[1].rect.left()).abs() > 2.0,
|
||||
"centred values of other widths start at other places {what}: {:?}",
|
||||
(c[0].rect, c[1].rect)
|
||||
);
|
||||
let (s5, s100) = (inset(s[0].rect, started_fields[0], true, rtl), inset(s[1].rect, started_fields[1], true, rtl));
|
||||
assert!((s5 - s100).abs() <= 0.5, "started values share the logical reading start {what}: {s5} beside {s100}");
|
||||
// The centred twin of the same field says how far in a centred value sits: a started one is at the near edge, not
|
||||
// merely left of where a wider centred value would reach.
|
||||
let c5 = inset(c[0].rect, centred_fields[0], true, rtl);
|
||||
assert!(s5 < c5 / 2.0, "a started value sits at the reading start, not at a centred offset {what}: {s5} beside {c5}");
|
||||
// The default presentation sets the value in the monospace role.
|
||||
assert!(
|
||||
c.iter().chain(&s).all(|run| run.font.family == FontFamily::Monospace),
|
||||
"a value is set in the monospace role by default {what}: {:?}",
|
||||
(c[0].font.clone(), s[0].font.clone())
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/// `mono(false)` sets the value in the UI body face, and the face is the one the field measures its value in too: a
|
||||
/// content-sized field is its value, its caret and its padding wide, so the room the field takes is the room its text
|
||||
/// fills in either face.
|
||||
/// `mono(false)` sets the value in the UI body role, and the field measures the role it paints: a content-sized field is
|
||||
/// its value, its caret and its padding wide in either role, so the room it takes is the room its text fills. The
|
||||
/// reference runs are the roles' own fonts, size included, so this pins the whole painted font and not only the family.
|
||||
#[trial]
|
||||
fn the_mono_choice_sets_the_face_the_field_paints_and_measures() {
|
||||
let boxes: Rc<RefCell<Vec<Rect>>> = Rc::default();
|
||||
@@ -113,65 +167,74 @@ fn the_mono_choice_sets_the_face_the_field_paints_and_measures() {
|
||||
for (at, mono) in [true, false].into_iter().enumerate() {
|
||||
v.push(NumberField::new("N", Bind::given(Some(111.0))).mono(mono).sizing(Sizing::Content).id_salt(at).show(ui).response.rect);
|
||||
}
|
||||
Text::new(MONO_ROLE).role(TypeRole::Mono).show(ui);
|
||||
Text::new(BODY_ROLE).role(TypeRole::Body).show(ui);
|
||||
});
|
||||
fixture.settle();
|
||||
let fields = boxes.borrow().clone();
|
||||
let runs = painted(&fixture.frame_shapes());
|
||||
let family = |field: Rect| run(&runs, field, "111").map(|run| run.2.clone());
|
||||
assert_eq!(family(fields[0]), Some(FontFamily::Monospace), "a field sets its value in the mono face by default: {runs:?}");
|
||||
assert_eq!(family(fields[1]), Some(FontFamily::Proportional), "mono(false) sets the value in the UI body face: {runs:?}");
|
||||
let room = |field: Rect| {
|
||||
let value = value_in(&fixture, field, "111");
|
||||
assert!(field.expand(1.0).contains_rect(value), "the field is as wide as the value it measured: {value:?} in {field:?}");
|
||||
// The padding and the caret's room are the field's own in either face; the value is all that the face changes.
|
||||
field.width() - value.width()
|
||||
let runs = painted_runs(&mut fixture);
|
||||
let mono = shown(&runs, fields[0], "111");
|
||||
let proportional = shown(&runs, fields[1], "111");
|
||||
let reference = |text: &str| runs.iter().find(|run| run.text == text).map(|run| run.font.clone());
|
||||
let (mono_role, body_role) = (reference(MONO_ROLE), reference(BODY_ROLE));
|
||||
assert!(
|
||||
mono_role.is_some() && body_role.is_some() && mono_role != body_role,
|
||||
"the two roles are set in other fonts: {mono_role:?} {body_role:?}"
|
||||
);
|
||||
assert_eq!(Some(mono.font.clone()), mono_role, "a field sets its value in the monospace role, face and size");
|
||||
assert_eq!(Some(proportional.font.clone()), body_role, "mono(false) sets the value in the UI body role, face and size");
|
||||
let room = |field: Rect, run: &Run| {
|
||||
assert!(field.expand(1.0).contains_rect(run.rect), "the field is as wide as the value it measured: {:?} in {field:?}", run.rect);
|
||||
// The padding and the caret's room are the field's own in either role; the value is all that the role changes.
|
||||
field.width() - run.rect.width()
|
||||
};
|
||||
let (mono, proportional) = (room(fields[0]), room(fields[1]));
|
||||
// The box and the text are laid out at the points the field's measure gives them, so a face the measure missed would
|
||||
// leave a different room around the value by the whole of an advance.
|
||||
assert!((mono - proportional).abs() <= 1.0, "the field measures the face it paints: {mono} beside {proportional}");
|
||||
let (measured, painted) = (room(fields[0], mono), room(fields[1], proportional));
|
||||
// The value "111" is what this unbounded field measures (the current value, not a fixed sample), and the monospace
|
||||
// role is 6.7 points wider on it than the body role, so a measure that named the face itself leaves the room apart.
|
||||
assert!((measured - painted).abs() <= 1.0, "the field measures the role it paints: {measured} beside {painted}");
|
||||
}
|
||||
|
||||
/// A started value keeps the field's unit where the field always put it: at the end, apart from the number, so a column
|
||||
/// of fields still lines its units up.
|
||||
/// A started value keeps the field's unit where the field always put it: at the logical reading end, apart from the
|
||||
/// number, and at the same inset the value keeps from the reading start. The centred twin of each value shows the unit
|
||||
/// is the same either way.
|
||||
#[trial]
|
||||
fn a_started_value_keeps_its_unit_at_the_end_of_the_field() {
|
||||
let boxes: Rc<RefCell<Vec<Rect>>> = Rc::default();
|
||||
let b = boxes.clone();
|
||||
let mut fixture = Fixture::new(move |ui| {
|
||||
let mut v = b.borrow_mut();
|
||||
v.clear();
|
||||
for (at, value) in [5.0, 100.0].into_iter().enumerate() {
|
||||
let field = NumberField::new("N", Bind::given(Some(value))).range(0.0..=100.0).unit_kind(UnitKind::Percent).centred(false).id_salt(at);
|
||||
v.push(field.show(ui).response.rect);
|
||||
fn a_started_value_keeps_its_unit_at_the_reading_end() {
|
||||
for direction in [None, Some(Direction::Rtl)] {
|
||||
let rtl = direction.is_some();
|
||||
let what = if rtl { "under right-to-left" } else { "under left-to-right" };
|
||||
let (fields, mut fixture) = unit_twins(direction);
|
||||
let runs = painted_runs(&mut fixture);
|
||||
let texts = ["5", "100", "5", "100"];
|
||||
let numbers: Vec<&Run> = fields.iter().zip(texts).map(|(field, text)| shown(&runs, *field, text)).collect();
|
||||
let units: Vec<&Run> = fields.iter().zip(texts).map(|(field, text)| unit_of(&runs, *field, text)).collect();
|
||||
// Started values share the reading start, and it is the inset the unit keeps from the other end.
|
||||
let (s5, s100) = (inset(numbers[0].rect, fields[0], true, rtl), inset(numbers[1].rect, fields[1], true, rtl));
|
||||
assert!((s5 - s100).abs() <= 0.5, "started values share the reading start beside a unit {what}: {s5} beside {s100}");
|
||||
let unit_end = inset(units[0].rect, fields[0], false, rtl);
|
||||
assert!((s5 - unit_end).abs() <= 0.5, "the value starts at the inset the unit keeps from the reading end {what}: {s5} beside {unit_end}");
|
||||
// The unit is the same whichever the presentation: all four fields end it at one inset.
|
||||
let ends: Vec<f32> = units.iter().zip(&fields).map(|(unit, field)| inset(unit.rect, *field, false, rtl)).collect();
|
||||
assert!(ends.iter().all(|end| (end - ends[0]).abs() <= 0.5), "the units end at one place {what}: {ends:?}");
|
||||
// The centred twins sit further in, and share their centre.
|
||||
let (c5, c100) = (inset(numbers[2].rect, fields[2], true, rtl), inset(numbers[3].rect, fields[3], true, rtl));
|
||||
assert!(c5 > s5 + 2.0 && c100 > s100 + 2.0, "a centred value sits further in than a started one {what}: {c5} {c100} beside {s5} {s100}");
|
||||
assert!(
|
||||
(numbers[2].rect.center().x - numbers[3].rect.center().x).abs() <= 0.5,
|
||||
"centred values share a centre {what}: {:?}",
|
||||
(numbers[2].rect, numbers[3].rect)
|
||||
);
|
||||
for (number, unit) in numbers.iter().zip(&units) {
|
||||
let apart = if rtl { number.rect.left() - unit.rect.right() } else { unit.rect.left() - number.rect.right() };
|
||||
assert!(apart > 0.0, "the unit stands apart from the number {what}: {:?} beside {:?}", number.rect, unit.rect);
|
||||
}
|
||||
});
|
||||
fixture.settle();
|
||||
let fields = boxes.borrow().clone();
|
||||
let numbers = [value_in(&fixture, fields[0], "5"), value_in(&fixture, fields[1], "100")];
|
||||
let units = [beside_in(&fixture, fields[0], "5"), beside_in(&fixture, fields[1], "100")];
|
||||
assert!((numbers[0].left() - numbers[1].left()).abs() <= 0.5, "started values share the reading start beside a unit: {numbers:?}");
|
||||
assert!((units[0].right() - units[1].right()).abs() <= 0.5, "the units end at one place from the field's end: {units:?}");
|
||||
assert!(numbers[1].right() < units[1].left(), "the unit stands apart from the number: {numbers:?} {units:?}");
|
||||
}
|
||||
|
||||
/// A started value follows a right-to-left interface: the reading start is the field's right edge there, and a centred
|
||||
/// value keeps the field's centre whatever the direction.
|
||||
#[trial]
|
||||
fn a_started_value_follows_a_right_to_left_interface() {
|
||||
let centred = Pair::new(false, Some(Direction::Rtl));
|
||||
let started = Pair::new(true, Some(Direction::Rtl));
|
||||
let (c, s) = (centred.values(), started.values());
|
||||
assert!((c[0].center().x - c[1].center().x).abs() <= 0.5, "centred values share a centre under right-to-left: {c:?}");
|
||||
assert!((s[0].right() - s[1].right()).abs() <= 0.5, "started values share the reading start, the right edge, under right-to-left: {s:?}");
|
||||
assert!(s[1].right() > started.fields[1].center().x + 2.0, "a started value sits at the field's right edge: {:?}", s[1]);
|
||||
}
|
||||
}
|
||||
|
||||
/// The caret sits beside the value wherever the field puts it: after the last digit at End and before the first at Home,
|
||||
/// inside the field's own box, centred or started, mono or not. An editing field lays its text out as its measure did.
|
||||
/// inside the field's own box, in all four presentations. An editing field lays its text out as its measure did.
|
||||
#[trial]
|
||||
fn the_caret_follows_the_value_wherever_the_field_puts_it() {
|
||||
for (centred, mono) in [(true, true), (false, true), (false, false)] {
|
||||
for (centred, mono) in [(true, true), (true, false), (false, true), (false, false)] {
|
||||
let boxes: Rc<RefCell<Vec<Rect>>> = Rc::default();
|
||||
let b = boxes.clone();
|
||||
let mut fixture = Fixture::new(move |ui| {
|
||||
@@ -190,7 +253,8 @@ fn the_caret_follows_the_value_wherever_the_field_puts_it() {
|
||||
});
|
||||
fixture.settle();
|
||||
let field = boxes.borrow()[0];
|
||||
let value = value_in(&fixture, field, "100");
|
||||
let runs = painted_runs(&mut fixture);
|
||||
let value = shown(&runs, field, "100").rect;
|
||||
fixture.key(Key::Tab, Modifiers::NONE);
|
||||
fixture.settle();
|
||||
fixture.key(Key::End, Modifiers::NONE);
|
||||
@@ -207,8 +271,48 @@ fn the_caret_follows_the_value_wherever_the_field_puts_it() {
|
||||
}
|
||||
}
|
||||
|
||||
/// A mixed field's dash and a disabled field's value are presented like any other value: `centred(false)` starts them at
|
||||
/// the reading start too, so a field keeps its look when it holds no value or cannot be edited.
|
||||
/// A started value wider than the room the field gives it: it begins at the reading start rather than at a centred
|
||||
/// offset, and in edit the field scrolls so that Home and End both keep the caret inside its box.
|
||||
#[trial]
|
||||
fn a_long_started_value_starts_at_the_reading_start_and_keeps_its_caret_in_view() {
|
||||
const LONG: f64 = 123_456_789_012_345.0;
|
||||
const LONG_TEXT: &str = "123456789012345";
|
||||
let boxes: Rc<RefCell<Vec<Rect>>> = Rc::default();
|
||||
let b = boxes.clone();
|
||||
let mut fixture = Fixture::new(move |ui| {
|
||||
let mut v = b.borrow_mut();
|
||||
v.clear();
|
||||
v.push(NumberField::new("N", Bind::given(Some(LONG))).centred(false).id_salt("long").show(ui).response.rect);
|
||||
});
|
||||
fixture.settle();
|
||||
let field = boxes.borrow()[0];
|
||||
let runs = painted_runs(&mut fixture);
|
||||
// This fixture shows one field, so the value is its one run of that text: it is found by text alone, since a value
|
||||
// wider than its room is painted beyond the field's box and no containment test would find it.
|
||||
let value = match runs.iter().find(|run| run.text == LONG_TEXT) {
|
||||
Some(run) => run.rect,
|
||||
None => panic!("the field paints no {LONG_TEXT}: {:?}", runs.iter().map(|run| run.text.clone()).collect::<Vec<_>>()),
|
||||
};
|
||||
// The overflow is what the trial is for: the ordinary layout keeps the field at one number cell, so the value is
|
||||
// wider than the field it is drawn across (were the field grown to fit, this would fail rather than test nothing).
|
||||
assert!(value.width() > field.width(), "the value is wider than the field it overflows: {value:?} in {field:?}");
|
||||
let start = inset(value, field, true, false);
|
||||
assert!(start >= -0.5, "a started value begins inside the field, not left of it as a centred one would: {value:?} in {field:?}");
|
||||
assert!(start < field.width() / 4.0, "and beside the field's start, not at a centred offset: {start} in {field:?}");
|
||||
// In edit the value is wider than the room, so the field scrolls: the caret stays inside the box at both ends.
|
||||
fixture.key(Key::Tab, Modifiers::NONE);
|
||||
fixture.settle();
|
||||
fixture.key(Key::Home, Modifiers::NONE);
|
||||
let home = fixture.ime().expect("a field in edit reports its caret").caret;
|
||||
fixture.key(Key::End, Modifiers::NONE);
|
||||
let end = fixture.ime().expect("a field in edit reports its caret").caret;
|
||||
assert!(field.expand(1.5).contains_rect(home), "the caret at Home stays in the field: {home:?} in {field:?}");
|
||||
assert!(field.expand(1.5).contains_rect(end), "the field scrolls so the caret at End stays in it: {end:?} in {field:?}");
|
||||
assert!(end.min.x > home.min.x, "End is past Home: {home:?} {end:?}");
|
||||
}
|
||||
|
||||
/// A mixed field's dash and a disabled field's value are presented like any other value, and each has its centred twin
|
||||
/// in the same fixture: the twin proves it is centred, and the started ones must sit at the start the twin rules out.
|
||||
#[trial]
|
||||
fn a_mixed_or_disabled_field_keeps_the_presentation_it_is_given() {
|
||||
let boxes: Rc<RefCell<Vec<Rect>>> = Rc::default();
|
||||
@@ -217,21 +321,51 @@ fn a_mixed_or_disabled_field_keeps_the_presentation_it_is_given() {
|
||||
let mut v = b.borrow_mut();
|
||||
v.clear();
|
||||
let mut mixed = Mixed::<Option<f64>>::Mixed;
|
||||
v.push(NumberField::new("Mixed", Bind::bound_mixed(&mut mixed)).range(0.0..=100.0).centred(false).id_salt("mixed").show(ui).response.rect);
|
||||
v.push(
|
||||
NumberField::new("Off", Bind::given(Some(100.0))).range(0.0..=100.0).centred(false).enabled(false).id_salt("off").show(ui).response.rect,
|
||||
NumberField::new("Mixed", Bind::bound_mixed(&mut mixed))
|
||||
.range(0.0..=100.0)
|
||||
.centred(false)
|
||||
.id_salt("mixed_started")
|
||||
.show(ui)
|
||||
.response
|
||||
.rect,
|
||||
);
|
||||
let mut mixed = Mixed::<Option<f64>>::Mixed;
|
||||
v.push(NumberField::new("Mixed", Bind::bound_mixed(&mut mixed)).range(0.0..=100.0).id_salt("mixed_centred").show(ui).response.rect);
|
||||
v.push(
|
||||
NumberField::new("Off", Bind::given(Some(100.0)))
|
||||
.range(0.0..=100.0)
|
||||
.centred(false)
|
||||
.enabled(false)
|
||||
.id_salt("off_started")
|
||||
.show(ui)
|
||||
.response
|
||||
.rect,
|
||||
);
|
||||
v.push(NumberField::new("Off", Bind::given(Some(100.0))).range(0.0..=100.0).enabled(false).id_salt("off_centred").show(ui).response.rect);
|
||||
});
|
||||
fixture.settle();
|
||||
let fields = boxes.borrow().clone();
|
||||
let dash = {
|
||||
let found = fixture.texts().iter().find(|run| fields[0].expand(1.0).contains_rect(run.rect)).map(|run| run.rect);
|
||||
match found {
|
||||
Some(rect) => rect,
|
||||
None => panic!("a mixed field paints its dash: {:?}", fixture.texts().iter().map(|run| run.text.clone()).collect::<Vec<_>>()),
|
||||
}
|
||||
};
|
||||
assert!(dash.left() < fields[0].center().x - 2.0, "the mixed dash starts at the field's start: {dash:?} in {:?}", fields[0]);
|
||||
let value = value_in(&fixture, fields[1], "100");
|
||||
assert!(value.left() < fields[1].center().x - 2.0, "a disabled value starts at the field's start: {value:?} in {:?}", fields[1]);
|
||||
let runs = painted_runs(&mut fixture);
|
||||
let (dash_started, dash_centred) = (only(&runs, fields[0]), only(&runs, fields[1]));
|
||||
let (value_started, value_centred) = (shown(&runs, fields[2], "100"), shown(&runs, fields[3], "100"));
|
||||
// Each twin really is centred: the run's centre is its field's.
|
||||
assert!(
|
||||
(dash_centred.rect.center().x - fields[1].center().x).abs() <= 0.5,
|
||||
"the centred twin centres the mixed dash {:?} in {:?}",
|
||||
dash_centred.rect,
|
||||
fields[1]
|
||||
);
|
||||
assert!(
|
||||
(value_centred.rect.center().x - fields[3].center().x).abs() <= 0.5,
|
||||
"the centred twin centres the disabled value {:?} in {:?}",
|
||||
value_centred.rect,
|
||||
fields[3]
|
||||
);
|
||||
// The started ones sit at the field's start the twin rules out: both at the same inset, under half the twin's.
|
||||
let (dash_in, value_in) = (dash_started.rect.left() - fields[0].left(), value_started.rect.left() - fields[2].left());
|
||||
let (dash_twin, value_twin) = (dash_centred.rect.left() - fields[1].left(), value_centred.rect.left() - fields[3].left());
|
||||
assert!((dash_in - value_in).abs() <= 0.5, "a mixed field's dash starts where a value starts: {dash_in} beside {value_in}");
|
||||
assert!(dash_in < dash_twin / 2.0, "the mixed dash starts at the start, not at a centred offset: {dash_in} beside {dash_twin}");
|
||||
assert!(value_in < value_twin / 2.0, "a disabled value starts at the start, not at a centred offset: {value_in} beside {value_twin}");
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user