diff --git a/crates/ui-canvas/tests/it/canvas/navigator.rs b/crates/ui-canvas/tests/it/canvas/navigator.rs index b6b82f14b..060c7b05c 100644 --- a/crates/ui-canvas/tests/it/canvas/navigator.rs +++ b/crates/ui-canvas/tests/it/canvas/navigator.rs @@ -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}"); } } } diff --git a/crates/ui-contract/src/contract/steps/report.rs b/crates/ui-contract/src/contract/steps/report.rs index d9e04def0..42bec8b45 100644 --- a/crates/ui-contract/src/contract/steps/report.rs +++ b/crates/ui-contract/src/contract/steps/report.rs @@ -229,7 +229,7 @@ fn report_access(p: &dyn ErasedPass<'_>, facts: &TypeFacts, input: &ReportIn<'_> pass_state::with::(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); } diff --git a/crates/ui-contract/src/runtime/access.rs b/crates/ui-contract/src/runtime/access.rs index 5864bd09e..ed8f48fd9 100644 --- a/crates/ui-contract/src/runtime/access.rs +++ b/crates/ui-contract/src/runtime/access.rs @@ -289,6 +289,7 @@ pub struct Access { pub(crate) description: Option, pub(crate) placeholder: Option, pub(crate) value: Option, + pub(crate) unit: Option, pub(crate) color: Option, pub(crate) numeric: Option, pub(crate) expanded: Option, @@ -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) -> &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); diff --git a/crates/ui-contract/src/runtime/access/text.rs b/crates/ui-contract/src/runtime/access/text.rs index 20e24f0ec..3e4e52fc3 100644 --- a/crates/ui-contract/src/runtime/access/text.rs +++ b/crates/ui-contract/src/runtime/access/text.rs @@ -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::::run(ctx, |tables| tables.0.insert(owner, entries)); } diff --git a/crates/ui-contract/src/runtime/egui/access/tree.rs b/crates/ui-contract/src/runtime/egui/access/tree.rs index 7f17f6bcf..71e994ad6 100644 --- a/crates/ui-contract/src/runtime/egui/access/tree.rs +++ b/crates/ui-contract/src/runtime/egui/access/tree.rs @@ -299,7 +299,13 @@ impl AccessTree { /// The text of each `TextRun` node below `id`, in order. pub fn run_texts(&self, id: Id) -> Vec { - 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 { + let Some(node) = self.lookup(NodeId(key.0)) else { return Vec::new() }; node.children() .iter() .filter_map(|child| self.lookup(*child)) diff --git a/crates/ui-widgets/src/text/number.rs b/crates/ui-widgets/src/text/number.rs index 042fd065e..733d2f046 100644 --- a/crates/ui-widgets/src/text/number.rs +++ b/crates/ui-widgets/src/text/number.rs @@ -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