Use typed alignment and type roles for number fields

This commit is contained in:
skylar taylor-barrick
2026-11-17 22:32:31 +01:00
committed by skylar
parent 64a376b2af
commit 9c563f7337
7 changed files with 219 additions and 358 deletions
+1 -1
View File
@@ -45,7 +45,7 @@ impl Wrap {
}
}
/// Where the lines of a multi-line run sit within the run's width.
/// Horizontal placement in reading order: within a text run's width, or a text input's room.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Default)]
pub enum HAlign {
/// At the start edge: left, right in a right-to-left interface.
@@ -3,7 +3,7 @@
mod completion;
use crate::primitives::{TextLayout, TextOptions};
use crate::primitives::{HAlign, TextLayout, TextOptions};
use crate::runtime::access::text::{Line, Run};
use crate::runtime::egui::{clipboard, focus as egui_focus, input as egui_input, text_edit};
use crate::runtime::focus::{EditSet, KeepKeys, KeyUse};
@@ -420,7 +420,7 @@ pub(crate) fn run(state: &mut TextState, mut input: AdapterIn<'_>) -> AdapterOut
char_limit: input.options.limit,
preedit: preedit.clone(),
rtl: input.rtl,
centred: input.options.centred,
align: input.options.align,
caret_visible: input.caret_visible,
underline: input.underline,
galley: None,
@@ -535,7 +535,7 @@ pub(crate) fn show_selectable(ui: &mut Ui, text: Selectable<'_>) {
char_limit: None,
preedit: None,
rtl: false,
centred: false,
align: HAlign::Start,
caret_visible: false,
underline: 0.0,
galley: Some(layout.galley.clone()),
@@ -1,6 +1,6 @@
//! The types of text parts and editors (18.2, 18.4): keys, options, completion and the own-caret editor state.
use crate::primitives::TextLayout;
use crate::primitives::{HAlign, TextLayout};
use crate::runtime::env::{Label, Message};
use crate::style::Ink;
use craft_behavior::{PartKey, TextPos};
@@ -80,8 +80,8 @@ pub struct TextOptions {
/// The value handed to the part is the start the codec parses against; committing empty text that the codec
/// rejects leaves the value mixed.
pub mixed: bool,
/// The text sits in the middle of the part instead of at its start (numbers).
pub centred: bool,
/// Where the text sits in its part, relative to the interface's reading direction.
pub align: HAlign,
/// 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,
@@ -121,7 +121,7 @@ impl Default for TextOptions {
select_on_focus: false,
submits: false,
mixed: false,
centred: false,
align: HAlign::Start,
arrows_from_end: false,
unparsed: None,
own_menu: false,
@@ -1,5 +1,6 @@
//! Building `TextEdit` and its state: the one caller of egui's text editing.
use crate::primitives::HAlign;
use craft_theme::TypeRole;
use egui::text::{CCursor, CCursorRange, LayoutJob, TextFormat};
use egui::text_edit::TextEditState;
@@ -31,8 +32,8 @@ pub(crate) struct Edit<'a> {
/// The bytes of `text` that are IME preedit, underlined.
pub preedit: Option<Range<usize>>,
pub rtl: bool,
/// The text sits in the middle of the rect.
pub centred: bool,
/// Where the text sits in the rect, relative to the interface's reading direction.
pub align: HAlign,
/// Whether the caret is drawn in this pass.
pub caret_visible: bool,
/// The width of the preedit underline.
@@ -80,7 +81,7 @@ pub(crate) fn show(ui: &mut Ui, edit: Edit<'_>) -> Shown {
char_limit,
preedit,
rtl,
centred,
align,
caret_visible,
underline,
galley,
@@ -189,11 +190,11 @@ pub(crate) fn show(ui: &mut Ui, edit: Edit<'_>) -> Shown {
.return_key(if multiline { Some(egui::KeyboardShortcut::new(egui::Modifiers::NONE, egui::Key::Enter)) } else { None })
.layouter(&mut layouter)
// A laid-out galley sits at the rect's corner, as a painted run does.
.align(match (centred, rtl) {
.align(match (align, 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,
(HAlign::Center, _) => egui::Align2::CENTER_CENTER,
(HAlign::Start, true) | (HAlign::End, false) => egui::Align2::RIGHT_CENTER,
(HAlign::Start, false) | (HAlign::End, true) => egui::Align2::LEFT_CENTER,
});
if let Some(hint) = hint_text.clone() {
edit = edit.hint_text(hint);
+15 -17
View File
@@ -15,7 +15,7 @@ use crate::contract::{
Access, Bind, BindMut, Common, Component, Cx, Edit as _, Entry, Focus as _, Input, MeasureCx, Numeric, OverlayUi, Overlays as _, PaintUi,
PartsCx, Pointer as _, Props as _, Role, Scrub, Sizing, Styled as _, WordSetCx as _,
};
use crate::primitives::{Span, TextOptions, TextRun};
use crate::primitives::{HAlign, TextOptions, TextRun};
use crate::runtime::env::{Label, LengthUnits, UnitKind, Units};
use crate::style::{ComponentStyle, StyleColor, StyleCx, SurfaceLook, TextLook, TypeRole};
use craft_behavior::nav::{LARGE_STEPS, StepResult, ValueAxes, ValueSteps};
@@ -208,9 +208,7 @@ impl<'a, V: NumberValue + 'static> NumberField<'a, V> {
fixed_decimals: None,
lengths: None,
scale: 1.0,
// The value sits in the middle of its room in the monospace face, the presentation every number field has
// had; `centred` and `mono` let a caller choose either independently.
options: TextOptions { centred: true, mono: true, ..TextOptions::default() },
options: TextOptions { align: HAlign::Center, role: TypeRole::Mono, ..TextOptions::default() },
scrub: false,
track: 0.0,
picked: None,
@@ -243,24 +241,19 @@ impl<'a, V: NumberValue + 'static> NumberField<'a, V> {
self
}
/// Where the value sits in the room the field leaves for it: in the middle (the default), or at the logical reading
/// start, the edge the text is read from — the left edge in a left-to-right interface, the right edge in a
/// right-to-left one. The unit keeps its place at the end of the field either way, so a column of fields lines its
/// units up whichever presentation its values take. Values wider than the available room retain the existing
/// single-line clipping and scrolling behaviour.
/// Where the value sits in its room: centred by default, or at its logical start or end. The unit keeps its
/// place at the reading end. The same alignment places the resting text and its editor, including the caret.
#[must_use]
pub fn centred(mut self, centred: bool) -> Self {
self.options.centred = centred;
pub fn align(mut self, align: HAlign) -> Self {
self.options.align = align;
self
}
/// 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.
/// The value's type role, including its face and size, in measurement, paint and editing. Defaults to
/// [`TypeRole::Mono`]; use [`TypeRole::Body`] for the interface's body text.
#[must_use]
pub fn mono(mut self, mono: bool) -> Self {
self.options.mono = mono;
pub fn role(mut self, role: TypeRole) -> Self {
self.options.role = role;
self
}
@@ -400,6 +393,11 @@ impl<'a, V: NumberValue + 'static> NumberField<'a, V> {
place(rect, &style.field, &self.leading, &self.trailing, rtl)
}
/// Measure the same type role the text adapter paints and edits.
fn value_run<'s>(&self, text: &'s str) -> TextRun<'s> {
TextRun::new(text).role(self.options.role)
}
/// The alert for `typed`, a typed number the rule held to its range, in shown units; `kept` is the committed
/// value, in shown units too.
fn range_alert(&self, typed: f64, kept: Option<f64>) -> RangeAlert {
+17 -7
View File
@@ -5,10 +5,11 @@ use crate::contract::{Bind, Entry, Props as _, Show as _, ShowContainer as _, Va
use crate::display::Text;
use crate::form::{Field, FieldGroup};
use crate::layout_components::Row;
use crate::primitives::HAlign;
use crate::runtime::env::{Env, Message, UnitKind};
use crate::testing::{Drive, Fixture, StoryData, StoryOutput, story};
use craft_behavior::{Mixed, NumberCodec, Reader};
use craft_theme::ControlSize;
use craft_theme::{ControlSize, TypeRole};
use egui::{Key, Modifiers, Ui};
const SIZE: f64 = 12.5;
@@ -109,24 +110,33 @@ 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 logical reading start, set
/// in the monospace role or the UI body role (face and size), over the same range.
/// The logical start, centre and end alignments in the monospace and UI body roles, 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, _| {
let first = row("Centred, mono")
.show(ui, |ui, _| NumberField::new("Centred, mono", Bind::bound(data.value("centred_mono", Some(SIZE)))).range(RANGE).show(ui).edit);
row("Started, mono").show(ui, |ui, _| {
NumberField::new("Started, mono", Bind::bound(data.value("started_mono", Some(SIZE)))).range(RANGE).centred(false).show(ui);
NumberField::new("Started, mono", Bind::bound(data.value("started_mono", Some(SIZE)))).range(RANGE).align(HAlign::Start).show(ui);
});
row("Centred, proportional").show(ui, |ui, _| {
NumberField::new("Centred, proportional", Bind::bound(data.value("centred_ui", Some(SIZE)))).range(RANGE).mono(false).show(ui);
NumberField::new("Centred, proportional", Bind::bound(data.value("centred_ui", Some(SIZE)))).range(RANGE).role(TypeRole::Body).show(ui);
});
row("Started, proportional").show(ui, |ui, _| {
NumberField::new("Started, proportional", Bind::bound(data.value("started_ui", Some(SIZE))))
.range(RANGE)
.centred(false)
.mono(false)
.align(HAlign::Start)
.role(TypeRole::Body)
.show(ui);
});
row("Ended, mono").show(ui, |ui, _| {
NumberField::new("Ended, mono", Bind::bound(data.value("ended_mono", Some(SIZE)))).range(RANGE).align(HAlign::End).show(ui);
});
row("Ended, proportional").show(ui, |ui, _| {
NumberField::new("Ended, proportional", Bind::bound(data.value("ended_ui", Some(SIZE))))
.range(RANGE)
.align(HAlign::End)
.role(TypeRole::Body)
.show(ui);
});
first.inner
@@ -1,375 +1,227 @@
//! 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.
//! Number-field alignment and type roles agree across measurement, resting 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::primitives::HAlign;
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, FontId, Key, Modifiers, Rect, Shape};
use egui::{FontId, Key, Modifiers, Rect, Shape};
use std::cell::RefCell;
use std::rc::Rc;
/// 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";
const ALIGNS: [HAlign; 3] = [HAlign::Start, HAlign::Center, HAlign::End];
const ROLES: [TypeRole; 3] = [TypeRole::Mono, TypeRole::Body, TypeRole::Caption];
/// 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();
Some(Run { text: text.galley.job.text.clone(), rect: text.galley.rect.translate(text.pos.to_vec2()), font })
}
Shape::Text(text) => Some(Run {
text: text.galley.job.text.clone(),
rect: text.galley.rect.translate(text.pos.to_vec2()),
font: text.galley.job.sections.first()?.format.font_id.clone(),
}),
_ => None,
})
.collect()
}
/// 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 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<_>>()),
runs.iter()
.find(|run| run.text == text && field.expand(1.0).contains_rect(run.rect))
.unwrap_or_else(|| panic!("{field:?} paints no {text}: {:?}", runs.iter().map(|run| &run.text).collect::<Vec<_>>()))
}
fn edge(run: Rect, field: Rect, align: HAlign, rtl: bool) -> f32 {
match (align, rtl) {
(HAlign::Center, _) => run.center().x - field.center().x,
(HAlign::Start, false) | (HAlign::End, true) => run.left() - field.left(),
(HAlign::Start, true) | (HAlign::End, false) => field.right() - run.right(),
}
}
/// The one run inside `field` (the dash of a mixed field).
fn only(runs: &[Run], field: Rect) -> &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.
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)
}
/// 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);
}
});
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
/// 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() {
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(&centred_runs, centred_fields[0], "5"), shown(&centred_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 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();
let b = boxes.clone();
let mut fixture = Fixture::new(move |ui| {
let mut v = b.borrow_mut();
v.clear();
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);
fn alignment_places_values_at_the_reading_edges_and_keeps_units_separate() {
for direction in [Direction::Ltr, Direction::Rtl] {
let rtl = direction == Direction::Rtl;
for with_unit in [false, true] {
let boxes: Rc<RefCell<Vec<Rect>>> = Rc::default();
let b = boxes.clone();
let mut fixture = Fixture::new(move |ui| {
let mut boxes = b.borrow_mut();
boxes.clear();
for (at, align) in ALIGNS.into_iter().enumerate() {
for (which, value) in [5.0, 100.0].into_iter().enumerate() {
let mut field = NumberField::new("N", Bind::given(Some(value))).range(0.0..=100.0);
// The centre case also checks the default, not only its setter.
if align != HAlign::Center {
field = field.align(align);
}
if with_unit {
field = field.unit_kind(UnitKind::Percent);
}
boxes.push(field.id_salt((at, which)).show(ui).response.rect);
}
}
});
fixture.direction(direction);
fixture.settle();
let boxes = boxes.borrow();
let runs = painted_runs(&mut fixture);
let mut unit_ends = Vec::new();
for (at, align) in ALIGNS.into_iter().enumerate() {
let fields = &boxes[at * 2..at * 2 + 2];
let values = [shown(&runs, fields[0], "5"), shown(&runs, fields[1], "100")];
let positions = [edge(values[0].rect, fields[0], align, rtl), edge(values[1].rect, fields[1], align, rtl)];
assert!((positions[0] - positions[1]).abs() <= 0.5, "{align:?} aligns values of different widths under {direction:?}: {positions:?}");
if with_unit {
for (field, value) in fields.iter().zip(values) {
let unit = runs
.iter()
.find(|run| run.text != value.text && field.expand(1.0).contains_rect(run.rect))
.expect("a unit beside the value");
unit_ends.push(edge(unit.rect, *field, HAlign::End, rtl));
let gap = if rtl { value.rect.left() - unit.rect.right() } else { unit.rect.left() - value.rect.right() };
assert!(gap > 0.0, "the unit stays apart from a {align:?} value under {direction:?}");
}
}
}
// Different edge choices must actually move the short value, not all silently centre it.
let offsets: Vec<_> = [0, 2, 4].into_iter().map(|at| shown(&runs, boxes[at], "5").rect.left() - boxes[at].left()).collect();
assert!(
if rtl {
offsets[0] > offsets[1] + 2.0 && offsets[1] > offsets[2] + 2.0
} else {
offsets[0] + 2.0 < offsets[1] && offsets[1] + 2.0 < offsets[2]
},
"the three alignments differ under {direction:?}: {offsets:?}"
);
assert!(unit_ends.iter().all(|end| (end - unit_ends[0]).abs() <= 0.5), "changing alignment leaves the unit in place: {unit_ends:?}");
}
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_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 (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 its digits are
// set at clearly different advances in the two roles, 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 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_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);
// The field's horizontal padding is symmetric at this preset, so the value keeps at the reading start the inset the
// unit keeps at the reading end: coverage of the default preset's padding, not a claim that every theme's is.
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);
}
}
}
/// 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, in all four presentations. The field is its ordinary cell width rather than sized to its
/// content, so a value placed by the wrong alignment moves across the whole band (tens of points), not within the caret's
/// own room. This compares the editable caret with the resting painted value.
#[trial]
fn the_caret_follows_the_value_wherever_the_field_puts_it() {
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| {
let mut v = b.borrow_mut();
v.clear();
v.push(NumberField::new("N", Bind::given(Some(100.0))).centred(centred).mono(mono).id_salt("n").show(ui).response.rect);
let mut boxes = b.borrow_mut();
boxes.clear();
for (at, align) in ALIGNS.into_iter().enumerate() {
let mut mixed = Mixed::<Option<f64>>::Mixed;
boxes.push(
NumberField::new("Mixed", Bind::bound_mixed(&mut mixed))
.range(0.0..=100.0)
.align(align)
.id_salt((at, "mixed"))
.show(ui)
.response
.rect,
);
boxes.push(
NumberField::new("Off", Bind::given(Some(100.0)))
.range(0.0..=100.0)
.align(align)
.enabled(false)
.id_salt((at, "off"))
.show(ui)
.response
.rect,
);
}
});
fixture.direction(direction);
fixture.settle();
let field = boxes.borrow()[0];
let boxes = boxes.borrow();
let runs = painted_runs(&mut fixture);
let value = shown(&runs, field, "100").rect;
assert!(fixture.focused().is_none(), "the resting paint is the unfocused field's: {field:?}");
fixture.key(Key::Tab, Modifiers::NONE);
fixture.settle();
fixture.key(Key::End, Modifiers::NONE);
let end = fixture.ime().expect("a field in edit reports its caret").caret;
fixture.key(Key::Home, Modifiers::NONE);
let home = fixture.ime().expect("a field in edit reports its caret").caret;
let which = format!("centred {centred}, mono {mono}");
assert!((end.min.x - value.right()).abs() <= 1.5, "the caret at End sits after the value {value:?}: {end:?} ({which})");
assert!((home.min.x - value.left()).abs() <= 1.5, "the caret at Home sits before the value {value:?}: {home:?} ({which})");
for (at, align) in ALIGNS.into_iter().enumerate() {
let mixed = runs.iter().find(|run| boxes[at * 2].expand(1.0).contains_rect(run.rect)).expect("a mixed dash");
let disabled = shown(&runs, boxes[at * 2 + 1], "100");
assert!(
(edge(mixed.rect, boxes[at * 2], align, rtl) - edge(disabled.rect, boxes[at * 2 + 1], align, rtl)).abs() <= 0.5,
"mixed and disabled values keep {align:?} under {direction:?}"
);
}
let offsets: Vec<_> = [0, 2, 4]
.into_iter()
.map(|at| runs.iter().find(|run| boxes[at].expand(1.0).contains_rect(run.rect)).unwrap().rect.left() - boxes[at].left())
.collect();
assert!(
field.expand(1.5).contains_rect(end) && field.expand(1.5).contains_rect(home),
"the caret stays in the field {field:?}: {end:?} {home:?} ({which})"
(offsets[0] - offsets[1]).abs() > 2.0 && (offsets[1] - offsets[2]).abs() > 2.0,
"mixed values really follow all three alignments: {offsets:?}"
);
}
}
/// A value wider than the room the field gives it: the text is drawn from the field's inner edge and overflows the box,
/// and in edit the field scrolls so that Home and End both keep the caret inside it. egui's single-line editor clamps
/// either alignment to the inner edge once the text is wider than the room (`builder.rs`, `align_size_within_rect(...)
/// .intersect(inner_rect)`), so this trial covers the overflow and the scrolling and cannot tell the two settings apart;
/// both are exercised for that shared path only.
#[trial]
fn a_long_value_overflows_its_room_and_keeps_its_caret_in_view() {
const LONG: f64 = 123_456_789_012_345.0;
const LONG_TEXT: &str = "123456789012345";
for centred in [false, true] {
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(centred).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 found by text alone: 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 (centred {centred}): {value:?} in {field:?}");
// The clamped text begins at the field's inner edge: not left of it, and not at a placed offset inside it.
let start = inset(value, field, true, false);
assert!(
start >= -0.5 && start < field.width() / 4.0,
"an overflowing value is drawn from the field's inner edge (centred {centred}): {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 (centred {centred}): {home:?} in {field:?}");
assert!(field.expand(1.5).contains_rect(end), "the field scrolls so the caret at End stays in it (centred {centred}): {end:?} in {field:?}");
assert!(end.min.x > home.min.x, "End is past Home (centred {centred}): {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() {
fn a_number_field_measures_and_paints_its_chosen_type_role() {
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();
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_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);
let mut boxes = b.borrow_mut();
boxes.clear();
boxes.push(NumberField::new("Default", Bind::given(Some(111.0))).sizing(Sizing::Content).id_salt("default").show(ui).response.rect);
for (at, role) in ROLES.into_iter().enumerate() {
boxes.push(NumberField::new("N", Bind::given(Some(111.0))).role(role).sizing(Sizing::Content).id_salt(at).show(ui).response.rect);
Text::new(format!("{role:?} reference")).role(role).show(ui);
}
});
fixture.settle();
let fields = boxes.borrow().clone();
let boxes = boxes.borrow();
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.
let default = shown(&runs, boxes[0], "111");
let mut rooms = Vec::new();
for (at, role) in ROLES.into_iter().enumerate() {
let field = boxes[at + 1];
let value = shown(&runs, field, "111");
let reference = runs.iter().find(|run| run.text == format!("{role:?} reference")).expect("a reference run");
assert_eq!(value.font, reference.font, "{role:?} sets both face and size");
rooms.push(field.width() - value.rect.width());
}
assert_eq!(default.font, shown(&runs, boxes[1], "111").font, "the default stays in the mono role");
assert_ne!(shown(&runs, boxes[1], "111").font, shown(&runs, boxes[2], "111").font, "the role choice changes the actual painted font");
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]
rooms.iter().all(|room| (room - rooms[0]).abs() <= 1.0),
"measurement uses the painted role, leaving the same padding and caret room: {rooms:?}"
);
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}");
}
#[trial]
fn editing_keeps_the_caret_beside_the_aligned_value_in_either_reading_direction() {
for direction in [Direction::Ltr, Direction::Rtl] {
for align in ALIGNS {
for role in ROLES {
let boxes: Rc<RefCell<Vec<Rect>>> = Rc::default();
let b = boxes.clone();
let mut fixture = Fixture::new(move |ui| {
let field = NumberField::new("N", Bind::given(Some(100.0))).align(align).role(role).id_salt("n").show(ui);
*b.borrow_mut() = vec![field.response.rect];
});
fixture.direction(direction);
fixture.settle();
let field = boxes.borrow()[0];
let runs = painted_runs(&mut fixture);
let value = shown(&runs, field, "100").rect;
assert!(fixture.focused().is_none(), "compare editing with resting paint");
fixture.key(Key::Tab, Modifiers::NONE);
fixture.settle();
fixture.key(Key::End, Modifiers::NONE);
let end = fixture.ime().expect("a caret in edit").caret;
fixture.key(Key::Home, Modifiers::NONE);
let home = fixture.ime().expect("a caret in edit").caret;
assert!(
(end.min.x - value.right()).abs() <= 1.5 && (home.min.x - value.left()).abs() <= 1.5,
"editing and paint agree for {align:?}, {role:?}, {direction:?}: {value:?}, {home:?}, {end:?}"
);
assert!(field.expand(1.5).contains_rect(end) && field.expand(1.5).contains_rect(home), "the caret stays in its field");
}
}
}
}