mirror of
https://github.com/storytold/craft-libs.git
synced 2026-11-26 12:47:25 +00:00
ui-contract, ui-widgets: a number field's value reads the unit it draws after its number ("160 pt") through Access::unit; trials that read a field's text read its text runs (AccessTree::run_texts_of)
This commit is contained in:
@@ -100,7 +100,7 @@ fn a_typed_zoom_lands_exactly_after_its_ease() {
|
||||
assert!(!space.easing(), "the ease has ended; animated: {animated}");
|
||||
assert_eq!(percent(&space.view), typed, "the shown view; animated: {animated}");
|
||||
let field = fixture.by_label("Zoom").expect("the Navigator's zoom field");
|
||||
assert_eq!(fixture.tree().value_of(field.node), Some(text.as_str()), "the field's text; animated: {animated}");
|
||||
assert_eq!(fixture.tree().run_texts_of(field.node).concat(), text, "the field's text; animated: {animated}");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -229,7 +229,7 @@ fn report_access(p: &dyn ErasedPass<'_>, facts: &TypeFacts, input: &ReportIn<'_>
|
||||
pass_state::with::<EditorRecords, _>(ctx, |records| records.0.remove(&widget))
|
||||
};
|
||||
if let Some(facts) = core.text_access.take().or_else(editor_record) {
|
||||
crate::runtime::access::text::report_text(ctx, id, &facts);
|
||||
crate::runtime::access::text::report_text(ctx, id, &facts, access.unit.as_deref());
|
||||
} else if let Some(editor) = &core.editor {
|
||||
crate::runtime::access::text::fold_editor(ctx, editor.widget, id);
|
||||
}
|
||||
|
||||
@@ -289,6 +289,7 @@ pub struct Access {
|
||||
pub(crate) description: Option<String>,
|
||||
pub(crate) placeholder: Option<String>,
|
||||
pub(crate) value: Option<String>,
|
||||
pub(crate) unit: Option<String>,
|
||||
pub(crate) color: Option<Srgba>,
|
||||
pub(crate) numeric: Option<Numeric>,
|
||||
pub(crate) expanded: Option<bool>,
|
||||
@@ -342,6 +343,13 @@ impl Access {
|
||||
self
|
||||
}
|
||||
|
||||
/// The unit a text component draws after its text: the node's value reads it after the text ("160 pt"), while
|
||||
/// the text runs stay the editable text.
|
||||
pub fn unit(&mut self, unit: impl Into<String>) -> &mut Self {
|
||||
self.unit = Some(unit.into());
|
||||
self
|
||||
}
|
||||
|
||||
/// The colour value of a colour well.
|
||||
pub fn color(&mut self, color: Srgba) -> &mut Self {
|
||||
self.color = Some(color);
|
||||
|
||||
@@ -97,9 +97,9 @@ pub(crate) fn fold_editor(ctx: &Context, widget: Id, owner: Id) {
|
||||
|
||||
/// Adds the text facts to the node of `owner` and fills its run nodes. The owner's node is the one node of the text:
|
||||
/// the widget's own node folds into it, so a component whose root edits its value through the text (a text field, a
|
||||
/// spin button, an editable combo box) is one node that takes focus and the text actions. Call it after the node's
|
||||
/// common facts.
|
||||
pub(crate) fn report_text(ctx: &Context, owner: Id, facts: &TextFacts) {
|
||||
/// spin button, an editable combo box) is one node that takes focus and the text actions. The value reads `unit`, the
|
||||
/// unit the component draws after the text, after a shown text. Call it after the node's common facts.
|
||||
pub(crate) fn report_text(ctx: &Context, owner: Id, facts: &TextFacts, unit: Option<&str>) {
|
||||
adapter::fold_widget(ctx, facts.widget, owner);
|
||||
let mut entries = Vec::new();
|
||||
if !facts.masked {
|
||||
@@ -122,8 +122,11 @@ pub(crate) fn report_text(ctx: &Context, owner: Id, facts: &TextFacts) {
|
||||
let (focus_node, focus_at) = end_of(&entries, owner, focus)?;
|
||||
Some((anchor_node, anchor_at, focus_node, focus_at))
|
||||
});
|
||||
let value = (!facts.masked).then_some(facts.text.as_str());
|
||||
adapter::write_text(ctx, owner, &TextNode { value, editable: facts.editable, disabled: facts.disabled, selection });
|
||||
let value = (!facts.masked).then(|| match unit {
|
||||
Some(unit) if !facts.text.is_empty() => format!("{} {unit}", facts.text),
|
||||
_ => facts.text.clone(),
|
||||
});
|
||||
adapter::write_text(ctx, owner, &TextNode { value: value.as_deref(), editable: facts.editable, disabled: facts.disabled, selection });
|
||||
Service::<Tables>::run(ctx, |tables| tables.0.insert(owner, entries));
|
||||
}
|
||||
|
||||
|
||||
@@ -299,7 +299,13 @@ impl AccessTree {
|
||||
|
||||
/// The text of each `TextRun` node below `id`, in order.
|
||||
pub fn run_texts(&self, id: Id) -> Vec<String> {
|
||||
let Some(node) = self.node(id) else { return Vec::new() };
|
||||
self.run_texts_of(NodeKey::of(id))
|
||||
}
|
||||
|
||||
/// The text of each `TextRun` node below the node `key`, in order: a text node's editable text, which its value
|
||||
/// may read with more (a number field's unit).
|
||||
pub fn run_texts_of(&self, key: NodeKey) -> Vec<String> {
|
||||
let Some(node) = self.lookup(NodeId(key.0)) else { return Vec::new() };
|
||||
node.children()
|
||||
.iter()
|
||||
.filter_map(|child| self.lookup(*child))
|
||||
|
||||
@@ -327,6 +327,12 @@ impl<'a, V: NumberValue + 'static> NumberField<'a, V> {
|
||||
self.unit.clone().or_else(|| self.unit_kind.map(|kind| units.symbol(kind).into()))
|
||||
}
|
||||
|
||||
/// The unit drawn this pass. A mixed value shows the dash alone: a unit beside it would claim a measure the
|
||||
/// selection does not have.
|
||||
fn painted_unit(&self, cx: &Cx<'_, Self>) -> Option<Label> {
|
||||
self.shown_unit(&cx.env.units()).filter(|_| cx.value().is_some_and(|value| !non_finite(Some(value))))
|
||||
}
|
||||
|
||||
/// `stored` in the shown unit.
|
||||
fn to_shown(&self, stored: f64) -> f64 {
|
||||
stored * self.scale
|
||||
@@ -733,8 +739,8 @@ impl<V: NumberValue + 'static> Component for NumberField<'_, V> {
|
||||
}
|
||||
}
|
||||
|
||||
/// The numeric value, and a button node per chevron named by its word (the painted face). The text part adds the
|
||||
/// text value, the selection and the text actions to the same node.
|
||||
/// The numeric value, the painted unit, and a button node per chevron named by its word (the painted face). The
|
||||
/// text part adds the text value, read with the unit, the selection and the text actions to the same node.
|
||||
fn access(&self, access: &mut Access, cx: &Cx<'_, Self>) {
|
||||
let rule = self.rule();
|
||||
// A value that is not finite shows the dash, as a mixed one does, and has no number to read.
|
||||
@@ -743,6 +749,9 @@ impl<V: NumberValue + 'static> Component for NumberField<'_, V> {
|
||||
None => Numeric::empty(rule.bounds()),
|
||||
};
|
||||
access.numeric(numeric.step(rule.step));
|
||||
if let Some(unit) = self.painted_unit(cx) {
|
||||
access.unit(cx.env.resolve(&unit).as_str());
|
||||
}
|
||||
for (key, shown) in [(PRESETS, self.presets.is_some()), (SLIDER, self.popup_slider.is_some())] {
|
||||
if shown {
|
||||
access.node(key, Role::BUTTON);
|
||||
@@ -754,9 +763,7 @@ impl<V: NumberValue + 'static> Component for NumberField<'_, V> {
|
||||
let rtl = cx.flow.is_rtl();
|
||||
let chevrons = self.chevrons(&cx.words::<TextWords>());
|
||||
let placed = self.laid_out(cx.style, cx.content_rect, rtl);
|
||||
// A mixed value shows the dash alone: a unit beside it would claim a measure the selection does not have.
|
||||
if self.shown_unit(&cx.env.units()).is_some()
|
||||
&& cx.value().is_some_and(|value| !non_finite(Some(value)))
|
||||
if self.painted_unit(cx).is_some()
|
||||
&& let (Some(unit), Some(at)) = (self.trailing.first(), placed.trailing.first())
|
||||
{
|
||||
unit.paint(ui, *at, cx.resolve(&cx.style.unit.color), self.options.role);
|
||||
|
||||
@@ -70,3 +70,35 @@ fn a_number_field_with_no_number_has_no_numeric_value() {
|
||||
assert_eq!(numeric.value, read, "the numeric value of a field holding {value}");
|
||||
}
|
||||
}
|
||||
|
||||
/// A number field's value reads its number, then the unit it draws after it ("160 pt"), as the field shows them; a
|
||||
/// value that is not a number shows the dash alone, draws no unit and reads its text alone. Guards against the text
|
||||
/// part's value, which is the editable text, leaving out the unit.
|
||||
#[trial]
|
||||
fn a_number_field_reads_its_unit_after_its_number() {
|
||||
use craft_ui::runtime::env::UnitKind;
|
||||
use craft_ui::text::NumberField;
|
||||
|
||||
for value in [160.0, f64::NAN] {
|
||||
let field = Rc::new(Cell::new((egui::Id::NULL, egui::Rect::NOTHING)));
|
||||
let seen = field.clone();
|
||||
let mut fixture = Fixture::new(move |ui| {
|
||||
let mut shown = Some(value);
|
||||
let out = NumberField::new("Radius", Bind::bound(&mut shown)).unit_kind(UnitKind::Points).id_salt("radius").show(ui);
|
||||
seen.set((out.response.id, out.response.rect));
|
||||
});
|
||||
fixture.settle();
|
||||
let (id, rect) = field.get();
|
||||
let tree = fixture.tree();
|
||||
let read = tree.value(id).unwrap_or_default();
|
||||
if value.is_finite() {
|
||||
let mut painted: Vec<_> = fixture.texts().iter().filter(|text| !text.text.is_empty() && rect.contains_rect(text.rect)).collect();
|
||||
painted.sort_by(|a, b| a.rect.left().total_cmp(&b.rect.left()));
|
||||
let shown: Vec<&str> = painted.iter().map(|text| text.text.as_str()).collect();
|
||||
assert_eq!(shown.len(), 2, "the number and its unit: {shown:?}");
|
||||
assert_eq!(read, shown.join(" "), "the value reads the number, then the unit");
|
||||
} else {
|
||||
assert_eq!(read, tree.run_texts(id).concat(), "a field that draws no unit reads its text alone");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -148,10 +148,10 @@ fn focused(comma: bool, beside: bool) -> (Fixture, Kept) {
|
||||
(fixture, kept)
|
||||
}
|
||||
|
||||
/// The field's text.
|
||||
/// The field's text: its text runs, which the node's value reads with the unit after it.
|
||||
fn text(fixture: &Fixture) -> String {
|
||||
let field = fixture.by_label(ZOOM).expect("the zoom field");
|
||||
fixture.tree().value_of(field.node).unwrap_or_default().to_owned()
|
||||
fixture.tree().run_texts_of(field.node).concat()
|
||||
}
|
||||
|
||||
/// How an entry reaches the field.
|
||||
|
||||
Reference in New Issue
Block a user