mirror of
https://github.com/storytold/craft-libs.git
synced 2026-11-25 20:14:31 +01:00
ui-widgets: the inline number trials share one field rig
This commit is contained in:
1 parent
ddd2e3c4e6
commit
17d8a34242
1 file changed
+46
-34
@@ -10,66 +10,78 @@ use egui::{Id, Key, Modifiers, Rect};
|
||||
use std::cell::Cell;
|
||||
use std::rc::Rc;
|
||||
|
||||
/// A length field in points under the inline look, its value, id and rect.
|
||||
fn inline_field() -> (Fixture, Rc<Cell<Option<f64>>>, Rc<Cell<Id>>, Rc<Cell<Rect>>) {
|
||||
let (value, id, rect) = (Rc::new(Cell::new(Some(720.0))), Rc::new(Cell::new(Id::NULL)), Rc::new(Cell::new(Rect::NOTHING)));
|
||||
let (v, i, r) = (value.clone(), id.clone(), rect.clone());
|
||||
let mut fixture = Fixture::new(move |ui| {
|
||||
let mut shown = v.get();
|
||||
let lengths = LengthUnits::new(UnitKind::Points, UnitKind::Points, 72.0);
|
||||
let out = NumberField::length("Width", Bind::bound(&mut shown), lengths).id_salt("width").show(ui);
|
||||
v.set(shown);
|
||||
i.set(out.response.id);
|
||||
r.set(out.response.rect);
|
||||
});
|
||||
fixture.root_value(NumberLook::Inline);
|
||||
fixture.settle();
|
||||
(fixture, value, id, rect)
|
||||
/// A length field in points under the inline look: the fixture, the bound value, and the field's id and rect.
|
||||
struct Inline {
|
||||
fixture: Fixture,
|
||||
value: Rc<Cell<Option<f64>>>,
|
||||
field: Rc<Cell<(Id, Rect)>>,
|
||||
}
|
||||
|
||||
/// The field's text as the tree reads it, and the runs painted inside the field.
|
||||
fn shown(fixture: &Fixture, id: Id, rect: Rect) -> (String, Vec<String>) {
|
||||
let text = fixture.tree().value(id).unwrap_or_default().to_owned();
|
||||
let runs = fixture.texts().iter().filter(|run| rect.contains_rect(run.rect)).map(|run| run.text.clone()).collect();
|
||||
(text, runs)
|
||||
impl Inline {
|
||||
fn new() -> Inline {
|
||||
let (value, field) = (Rc::new(Cell::new(Some(720.0))), Rc::new(Cell::new((Id::NULL, Rect::NOTHING))));
|
||||
let (v, f) = (value.clone(), field.clone());
|
||||
let mut fixture = Fixture::new(move |ui| {
|
||||
let mut shown = v.get();
|
||||
let lengths = LengthUnits::new(UnitKind::Points, UnitKind::Points, 72.0);
|
||||
let out = NumberField::length("Width", Bind::bound(&mut shown), lengths).id_salt("width").show(ui);
|
||||
v.set(shown);
|
||||
f.set((out.response.id, out.response.rect));
|
||||
});
|
||||
fixture.root_value(NumberLook::Inline);
|
||||
fixture.settle();
|
||||
Inline { fixture, value, field }
|
||||
}
|
||||
|
||||
/// The field's text as the tree reads it, and the runs painted inside the field.
|
||||
fn shown(&self) -> (String, Vec<String>) {
|
||||
let (id, rect) = self.field.get();
|
||||
let text = self.fixture.tree().value(id).unwrap_or_default().to_owned();
|
||||
let runs = self.fixture.texts().iter().filter(|run| rect.contains_rect(run.rect)).map(|run| run.text.clone()).collect();
|
||||
(text, runs)
|
||||
}
|
||||
}
|
||||
|
||||
/// At rest the field paints one run, the number with its unit, from the field's start; the unit is no run of its own.
|
||||
#[trial]
|
||||
fn an_inline_field_paints_its_number_and_unit_as_one_run_at_its_start() {
|
||||
let (fixture, _, id, rect) = inline_field();
|
||||
let (text, runs) = shown(&fixture, id.get(), rect.get());
|
||||
let inline = Inline::new();
|
||||
let (text, runs) = inline.shown();
|
||||
let (_, rect) = inline.field.get();
|
||||
assert!(text.starts_with("720") && text.len() > "720".len(), "the field's text holds the number and its unit: {text:?}");
|
||||
assert_eq!(runs, [text.clone()], "the field paints its text as one run, no unit apart");
|
||||
let run = fixture.texts().iter().find(|run| run.text == text).map(|run| run.rect).unwrap_or(Rect::NOTHING);
|
||||
assert!(run.center().x < rect.get().center().x, "the run starts at the field's start: {run:?} in {:?}", rect.get());
|
||||
assert_eq!(runs, std::slice::from_ref(&text), "the field paints its text as one run, no unit apart");
|
||||
let run = inline.fixture.texts().iter().find(|run| run.text == text).map(|run| run.rect).unwrap_or(Rect::NOTHING);
|
||||
assert!(run.center().x < rect.center().x, "the run starts at the field's start: {run:?} in {rect:?}");
|
||||
}
|
||||
|
||||
/// Focus selects the whole run, unit included; typed text replaces it and commits, and the unit comes back with the
|
||||
/// new number. A caret edit inside the run keeps the unit's text and commits the edited number.
|
||||
#[trial]
|
||||
fn an_inline_field_edits_its_whole_run_and_commits_the_number() {
|
||||
let (mut fixture, value, id, rect) = inline_field();
|
||||
let (before, _) = shown(&fixture, id.get(), rect.get());
|
||||
fixture.click_at(rect.get().center());
|
||||
let mut inline = Inline::new();
|
||||
let (before, _) = inline.shown();
|
||||
let (id, rect) = inline.field.get();
|
||||
let fixture = &mut inline.fixture;
|
||||
fixture.click_at(rect.center());
|
||||
fixture.key(Key::A, Modifiers::COMMAND);
|
||||
let selection = fixture.tree().text_selection(id.get()).map(|(anchor, focus)| anchor.1.abs_diff(focus.1));
|
||||
let selection = fixture.tree().text_selection(id).map(|(anchor, focus)| anchor.1.abs_diff(focus.1));
|
||||
assert_eq!(selection, Some(before.chars().count()), "select all covers the number and its unit");
|
||||
fixture.type_text("36");
|
||||
fixture.key(Key::Enter, Modifiers::NONE);
|
||||
fixture.settle();
|
||||
assert_eq!(value.get(), Some(36.0), "the typed number commits");
|
||||
let (after, runs) = shown(&fixture, id.get(), rect.get());
|
||||
assert_eq!(inline.value.get(), Some(36.0), "the typed number commits");
|
||||
let (after, runs) = inline.shown();
|
||||
assert_eq!(after.strip_prefix("36"), before.strip_prefix("720"), "the unit follows the new number: {after:?}");
|
||||
assert_eq!(runs, [after.clone()], "the field shows the same one run");
|
||||
assert_eq!(runs, std::slice::from_ref(&after), "the field shows the same one run");
|
||||
|
||||
let fixture = &mut inline.fixture;
|
||||
fixture.key(Key::Home, Modifiers::NONE);
|
||||
fixture.key(Key::Delete, Modifiers::NONE);
|
||||
fixture.type_text("4");
|
||||
assert_eq!(fixture.tree().value(id.get()).map(|text| text.strip_prefix("46") == after.strip_prefix("36")), Some(true));
|
||||
assert_eq!(fixture.tree().value(id).map(|text| text.strip_prefix("46") == after.strip_prefix("36")), Some(true));
|
||||
fixture.key(Key::Enter, Modifiers::NONE);
|
||||
fixture.settle();
|
||||
assert_eq!(value.get(), Some(46.0), "the number edited inside the run commits without its unit");
|
||||
assert_eq!(inline.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
|
||||
|
||||
Reference in new issue
Block a user