A text draft that does not parse says why: in its row's status line, else in its own tooltip

TextOptions::unparsed (TextField::unparsed) is the refusal the pass reports while the draft does not parse, unless
the binding gives its own. A container declares that its footer shows the message (FooterRegion::status; Field
does), the label scope carries that fact, and register_message reports whether the innermost label scope shows the
message. A control whose message no status line shows shows it as its own tooltip. HexField says "Not a hex color".
This commit is contained in:
aiongg
2026-11-12 14:32:43 +00:00
parent 0125617548
commit ef2b7c84f1
16 changed files with 143 additions and 35 deletions
@@ -747,6 +747,7 @@ impl<C: Container> ShapeHooks<C> for BodyHooks<'_> {
control_min: scope::measured_control_min(&ctx, id),
leaf: false,
claims: false,
status: props.footer.is_some_and(|footer| footer.status),
trailing: Vec2::ZERO,
};
let placed = label::run(ui, env, &input, None);
+10 -1
View File
@@ -319,12 +319,15 @@ pub struct FooterRegion {
pub place: FooterPlace,
/// The space above the footer's content, inside the region; none by default.
pub gap: Option<Gap>,
/// The footer shows the validity message of the control the container labels as its status line
/// (`BodyCx::field_message`); a control under a container without one shows its message itself.
pub status: bool,
}
impl FooterRegion {
/// A footer across the container under the line, with no rule.
pub const fn new() -> Self {
FooterRegion { rule: false, place: FooterPlace::Across, gap: None }
FooterRegion { rule: false, place: FooterPlace::Across, gap: None, status: false }
}
/// Whether a rule separates the footer from the content beside it.
@@ -344,6 +347,12 @@ impl FooterRegion {
self.gap = Some(gap);
self
}
/// The footer shows the validity message of the control the container labels as its status line.
pub const fn status(mut self) -> Self {
self.status = true;
self
}
}
/// Where a footer region sits.
+2 -2
View File
@@ -72,8 +72,8 @@ impl<'a> BodyCx<'a> {
}
/// The validity message of the first control in the container's label scope, recorded last frame, with the
/// tone it shows in: the container shows it as its status text. `None` when the container has no label or no
/// control reported one.
/// tone it shows in: a container whose footer declares `FooterRegion::status` shows it as its status text. `None`
/// when the container has no label or no control reported one.
pub fn field_message(&self) -> Option<(Message, craft_theme::variant::Tone)> {
self.field_message.clone()
}
@@ -168,6 +168,9 @@ pub struct CxCore<'a> {
pub(crate) editor_inputs: Vec<craft_behavior::EditorInput>,
/// The text part's draft is not a value of its codec.
pub(crate) text_invalid: bool,
/// The text part's `TextOptions::unparsed` while its draft does not parse: the refusal the pass reports when the
/// binding gives none.
pub(crate) text_unparsed: Option<crate::runtime::env::Message>,
/// The text part takes the pointer this pass: the field is in edit, or the press under way began in edit. A body
/// scrub (`Scrub::on_field`) leaves such a press to the text.
pub(crate) text_takes_pointer: bool,
@@ -43,6 +43,8 @@ pub(crate) struct LabelIn<'a> {
pub leaf: bool,
/// The leaf child draws only its mark (`Component::LABELLED_AFTER`): the label is part of its hit target.
pub claims: bool,
/// The container shows its control's validity message as its status line (`FooterRegion::status`).
pub status: bool,
/// The size of the content shown after the label's text, inside the label; zero without one.
pub trailing: Vec2,
}
@@ -50,7 +52,7 @@ pub(crate) struct LabelIn<'a> {
/// The label fact the scope records: the name's text, and the detail and the help line (resolved) as the control's
/// description.
fn field_label(env: &Env, input: &LabelIn<'_>, id: Id, rect: Rect) -> FieldLabel {
let label = FieldLabel::new(id, rect, input.text.as_str()).role(input.side.role);
let label = FieldLabel::new(id, rect, input.text.as_str()).role(input.side.role).status(input.status);
let described: Vec<String> =
[input.side.detail.as_ref(), input.help].into_iter().flatten().map(|text| env.view().resolve(text).as_str().to_owned()).collect();
if described.is_empty() { label } else { label.detail(described.join(". ")) }
@@ -118,6 +120,7 @@ pub(crate) fn leaf<C: Show>(ui: &mut Ui, env: &Env, at: &LeafLabel<'_>, child: &
control_min,
leaf: true,
claims: C::LABELLED_AFTER,
status: false,
trailing: trailing.as_ref().map_or(Vec2::ZERO, |trailing| trailing.size),
};
let placed = run(ui, env, &input, trailing);
+3 -2
View File
@@ -375,6 +375,7 @@ fn blank_core<'a>(
editor: None,
editor_inputs: Vec::new(),
text_invalid: false,
text_unparsed: None,
text_takes_pointer: false,
grid: crate::runtime::access::GridPlace::default(),
escape_to_list: false,
@@ -1060,7 +1061,7 @@ fn run_in_host(p: &mut Pass<'_, '_, '_>, ui: &mut Ui, host_at: Option<ShowAt>, h
steps::rerun_after_edit(e, style, edit, &MeasureIn { ui: &*ui, env: p.env, proposal: Proposal::Ideal, room: p.room }, resolved, leaf);
// 16 report, then the tooltip.
p.trace.mark(Step::Report);
steps::report(
let message = steps::report(
e,
facts,
&steps::ReportIn {
@@ -1075,7 +1076,7 @@ fn run_in_host(p: &mut Pass<'_, '_, '_>, ui: &mut Ui, host_at: Option<ShowAt>, h
},
);
let shows_label = e.shows_label();
tooltip::run(e.core_ref(), tooltip::TipIn { ui: &*ui, env, rich: p.tip.take(), parts: &parts.decls, shows_label });
tooltip::run(e.core_ref(), tooltip::TipIn { ui: &*ui, env, rich: p.tip.take(), parts: &parts.decls, shows_label, message });
// Activations accumulate across the passes of a viewport mount; step 17 fires the bound command once, with the
// injected source when there is one.
let natural = e.activated() && resolved.enabled;
+1 -1
View File
@@ -324,7 +324,7 @@ impl<'a, C: Component> ErasedPass<'a> for TypedPass<'_, 'a, C> {
}
fn validity(&self) -> Option<(&Message, bool)> {
steps::validity_of(&self.cx.validity)
steps::validity_of(&self.cx.validity).or_else(|| self.cx.core.text_unparsed.as_ref().map(|message| (message, false)))
}
fn shows_label(&self) -> bool {
+18 -10
View File
@@ -47,13 +47,27 @@ pub(crate) fn validity_of<V>(validity: &Validity<V>) -> Option<(&Message, bool)>
}
/// Step 16, report: the AccessKit node filled with its sub-nodes, the label-scope patch and the merge, then the
/// cursor request.
pub(crate) fn report(p: &dyn ErasedPass<'_>, facts: &TypeFacts, input: &ReportIn<'_>) {
report_access(p, facts, input);
/// cursor request. Returns the validity message no enclosing container shows as its status line: the component's
/// own tooltip shows it.
pub(crate) fn report(p: &dyn ErasedPass<'_>, facts: &TypeFacts, input: &ReportIn<'_>) -> Option<String> {
let validity = register_validity(p);
report_access(p, facts, input, validity.as_ref().map(|(text, _)| text.clone()));
crate::contract::cx::write_cursor(p.core_ref());
validity.filter(|(_, shown)| !shown).map(|(text, _)| text)
}
fn report_access(p: &dyn ErasedPass<'_>, facts: &TypeFacts, input: &ReportIn<'_>) {
/// The message of a validity that warns or refuses as text, registered with the innermost label scope, and whether
/// that scope's container shows it as its status line.
fn register_validity(p: &dyn ErasedPass<'_>) -> Option<(String, bool)> {
let core = p.core_ref();
let (message, warns) = p.validity()?;
let text = core.env.resolve(&message.text).as_str().to_owned();
let shown = scope::register_message(&core.ctx, message, message.effective_tone(warns), text.clone());
Some((text, shown))
}
/// `validity` is the validity message as text: the node's description when the component gives none.
fn report_access(p: &dyn ErasedPass<'_>, facts: &TypeFacts, input: &ReportIn<'_>, validity: Option<String>) {
let core = p.core_ref();
let (ctx, id) = (&core.ctx, core.id);
let (mut access, value_text) = p.access();
@@ -83,12 +97,6 @@ fn report_access(p: &dyn ErasedPass<'_>, facts: &TypeFacts, input: &ReportIn<'_>
for (_, label) in &mut access.custom_actions {
*label = core.env.resolve(label);
}
// The message of a validity that warns or refuses as text; the node's description when the component gives none.
let message = p.validity();
let validity = message.map(|(message, _)| core.env.resolve(&message.text).as_str().to_owned());
if let (Some((message, warns)), Some(text)) = (message, &validity) {
scope::register_message(ctx, message, message.effective_tone(warns), text.clone());
}
let tooltip_text = resolved.tooltip.map(|tooltip| core.env.resolve(&tooltip.spec().text));
let tooltip = tooltip_text.as_ref().map(Label::as_str);
if access.live.is_none() {
+1
View File
@@ -441,6 +441,7 @@ pub(crate) fn run<C: Component>(
}
facts(cx, &mut state, widget, &out, decl.rect, decl.options.mask, read_only);
cx.core.text_invalid = state.invalid;
cx.core.text_unparsed = decl.options.unparsed.clone().filter(|_| state.invalid);
cx.core.escape_to_list = state.completions.open;
if state.draft.is_some() || (decl.options.clear_on_escape && !out.layout.full.is_empty()) {
keys::hold_escape(&ctx, id);
+11 -5
View File
@@ -57,6 +57,8 @@ pub(crate) struct TipIn<'a> {
pub parts: &'a Decls,
/// The instance's `shows_label` fact.
pub shows_label: bool,
/// The validity message no enclosing container shows as its status line (step 16's report).
pub message: Option<String>,
}
/// The text a tooltip shows for `spec`: the disabled text while disabled.
@@ -68,10 +70,14 @@ fn spec_text(env: &crate::runtime::env::EnvView<'_>, spec: &TooltipSpec, enabled
env.resolve(label).as_str().to_owned()
}
/// The text the component's own tooltip shows, or `None` when it has none: the tooltip's text, or its disabled text
/// while disabled, with the bound command's chord; else the full label when the paint cut it, or its own label when
/// the component declares it paints no label (`shows_label`).
fn text_of(core: &super::CxCore<'_>, shows_label: bool) -> Option<String> {
/// The text the component's own tooltip shows, or `None` when it has none: its validity message when no container
/// shows it (`message`); else the tooltip's text, or its disabled text while disabled, with the bound command's
/// chord; else the full label when the paint cut it, or its own label when the component declares it paints no
/// label (`shows_label`).
fn text_of(core: &super::CxCore<'_>, shows_label: bool, message: Option<String>) -> Option<String> {
if message.is_some() {
return message;
}
let base = match core.resolved.tooltip.map(|tooltip| tooltip.spec()) {
Some(spec) => spec_text(&core.env, spec, core.resolved.enabled),
None => {
@@ -150,7 +156,7 @@ pub(crate) fn run(core: &super::CxCore<'_>, input: TipIn<'_>) {
part.hovered || keyboard,
Some(part.part),
),
None => (text_of(core, input.shows_label), own_spec, core.rect, core.response.hovered() || keyboard, None),
None => (text_of(core, input.shows_label, input.message), own_spec, core.rect, core.response.hovered() || keyboard, None),
};
let call = TipCall {
ctx: &core.ctx,
@@ -1,7 +1,7 @@
//! 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::runtime::env::Label;
use crate::runtime::env::{Label, Message};
use crate::style::Ink;
use craft_behavior::{PartKey, TextPos};
use craft_theme::TypeRole;
@@ -83,6 +83,9 @@ pub struct TextOptions {
/// 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,
/// Why a draft the codec cannot parse is no value ("Not a hex color"): while such a draft shows, it is the
/// component's refusing validity message, unless its binding refuses or warns with one of its own.
pub unparsed: Option<Message>,
}
impl Default for TextOptions {
@@ -108,6 +111,7 @@ impl Default for TextOptions {
mixed: false,
centred: false,
arrows_from_end: false,
unparsed: None,
}
}
}
+18 -6
View File
@@ -45,6 +45,8 @@ pub struct FieldLabel {
pub detail: Option<String>,
/// The label was clicked or activated this pass: focus goes to its control.
pub activated: bool,
/// The container shows its control's validity message as its status line (`FooterRegion::status`).
pub status: bool,
}
impl FieldLabel {
@@ -59,6 +61,7 @@ impl FieldLabel {
role: FieldLabelRole::Names,
detail: None,
activated: false,
status: false,
}
}
@@ -76,6 +79,13 @@ impl FieldLabel {
self
}
/// Whether the container shows its control's validity message as its status line.
#[must_use]
pub fn status(mut self, status: bool) -> Self {
self.status = status;
self
}
/// Whether the label was clicked or activated this pass.
#[must_use]
pub fn activated(mut self, activated: bool) -> Self {
@@ -333,13 +343,15 @@ pub(crate) fn register_reset(ctx: &Context, control: Id, differs: bool) {
}
/// Registers the validity message of a control with the innermost label scope; `text` is the message resolved.
/// `tone` is the effective tone of the message. The first message wins.
pub(crate) fn register_message(ctx: &Context, message: &Message, tone: Tone, text: String) {
/// `tone` is the effective tone of the message. The first message wins. Returns whether that scope's container shows
/// the message as its status line; without one, the control shows it itself.
pub(crate) fn register_message(ctx: &Context, message: &Message, tone: Tone, text: String) -> bool {
pass_state::with::<Stack, _>(ctx, |stack| {
if let Some(scope) = stack.0.iter_mut().rev().find(|scope| matches!(scope.kind, Kind::Label(_))) {
scope.message.get_or_insert_with(|| (message.clone(), tone, text));
}
});
let Some(scope) = stack.0.iter_mut().rev().find(|scope| matches!(scope.kind, Kind::Label(_))) else { return false };
let status = matches!(&scope.kind, Kind::Label(label) if label.status);
scope.message.get_or_insert_with(|| (message.clone(), tone, text));
status
})
}
/// The message the label scope of `container` recorded last frame.
+1 -1
View File
@@ -54,7 +54,7 @@ pub struct Field {
impl Field {
/// A field labelled `label`, beside its control, filling the width offered.
pub fn new(label: impl Into<Label>) -> Self {
let props = ContainerProps::new().sizing(Sizing::FillWidth).footer(FooterRegion::new().gap(Gap::Tight)).label(SideLabel::new());
let props = ContainerProps::new().sizing(Sizing::FillWidth).footer(FooterRegion::new().gap(Gap::Tight).status()).label(SideLabel::new());
Field { common: Common::new(), props, help: None, layout: None, value: None }.label(label)
}
+7 -2
View File
@@ -7,6 +7,7 @@ use super::TextField;
use crate::contract::Bind;
use crate::runtime::env::Label;
use craft_behavior::{Caret, Codec, NotAValue};
use craft_i18n::Word;
use craft_theme::Rgba;
use std::borrow::Cow;
@@ -105,10 +106,14 @@ impl Codec for HexCodec {
/// A text field over a colour's hexadecimal digits.
pub type HexField<'a> = TextField<'a, HexCodec>;
/// Why a hex field's draft is no colour.
const NOT_HEX: Word = Word::keyed("hexField.unparsed", "Not a hex color");
impl<'a> TextField<'a, HexCodec> {
/// A hex field named `label` that shows and edits `value`: monospace digits behind a fixed `#`.
/// A hex field named `label` that shows and edits `value`: monospace digits behind a fixed `#`. A draft that is no
/// colour says so ("Not a hex color", `TextField::unparsed`).
pub fn hex(label: impl Into<Label>, value: Bind<'a, Rgba>) -> Self {
let mut field = TextField::with_codec(label, value, HexCodec::default()).mono().prefix("#");
let mut field = TextField::with_codec(label, value, HexCodec::default()).mono().prefix("#").unparsed(NOT_HEX);
field.empty = Some(Rgba::rgb(0, 0, 0));
field
}
+10 -1
View File
@@ -10,7 +10,7 @@ use crate::contract::{
Styled as _, WordSetCx as _,
};
use crate::primitives::{Completer, IconSpec, TextOptions, TextRun};
use crate::runtime::env::Label;
use crate::runtime::env::{Label, Message};
use craft_behavior::{Codec, IconId, PartKey, PlainText, Proposal, Sense};
use egui::{Id, Rect, Vec2};
use smallvec::SmallVec;
@@ -174,6 +174,15 @@ impl<'a, C: Codec + Clone + 'static> TextField<'a, C> {
self
}
/// Why a draft that does not parse is no value ("Not a hex color"). While such a draft shows, an enclosing field
/// row shows it as its status line, or the field shows it as its tooltip where no row does, and assistive
/// technology reads it as the field's description. A refusal or warning of the binding comes first.
#[must_use]
pub fn unparsed(mut self, reason: impl Into<Label>) -> Self {
self.options.unparsed = Some(Message::new(reason));
self
}
/// `text` replaces the selection in this pass: at the caret it inserts. Set it for one pass only.
#[must_use]
pub fn replace_selection(mut self, text: impl Into<String>) -> Self {
+48 -2
View File
@@ -1,8 +1,11 @@
//! The hex field: uppercase digits behind a fixed `#` that is not part of the text, a typed colour applies before
//! Enter, and one digit per channel reads and writes web colours.
//! Enter, one digit per channel reads and writes web colours, and a draft that is no colour says why.
use craft_theme::Rgba;
use craft_ui::contract::{Bind, Show as _};
use craft_ui::contract::tooltip::DELAY;
use craft_ui::contract::{Bind, Show as _, ShowContainer as _};
use craft_ui::form::Field;
use craft_ui::testing::pointer::moved;
use craft_ui::testing::{Fixture, trial};
use craft_ui::text::HexField;
use egui::{Key, Modifiers};
@@ -39,3 +42,46 @@ fn a_typed_colour_applies_before_enter_behind_a_fixed_hash() {
web.type_text("F80");
assert_eq!(*value.borrow(), Rgba::rgb(0xff, 0x88, 0x00), "one digit per channel doubles each");
}
const NOT_HEX: &str = "Not a hex color";
fn reasons(fixture: &Fixture) -> usize {
fixture.texts().iter().filter(|run| run.text == NOT_HEX).count()
}
/// A draft that is no colour says why: a field row shows the reason as its status line, a field no row holds shows
/// it as its own tooltip, and either way assistive technology reads it as the field's description. A colour clears
/// it.
#[trial]
fn a_draft_that_is_no_colour_says_why_in_its_row_or_its_own_tooltip() {
for in_row in [true, false] {
let value = Rc::new(RefCell::new(Rgba::rgb(0x33, 0x66, 0x99)));
let shown = value.clone();
let mut fixture = Fixture::new(move |ui| {
let mut value = shown.borrow_mut();
if in_row {
Field::new("Hex color").show(ui, |ui, _| HexField::hex("Hex color", Bind::bound(&mut *value)).show(ui));
} else {
HexField::hex("Hex color", Bind::bound(&mut *value)).show(ui);
}
});
fixture.settle();
fixture.key(Key::Tab, Modifiers::NONE);
fixture.type_text("12");
fixture.settle();
let field = fixture.by_label("Hex color").expect("the hex field");
assert_eq!(fixture.tree().description_of(field.node), Some(NOT_HEX), "in a row: {in_row}");
assert_eq!(reasons(&fixture), usize::from(in_row), "only a row shows the reason before any rest; in a row: {in_row}");
fixture.frame(vec![moved(field.center())]);
fixture.advance(DELAY);
fixture.settle();
assert_eq!(reasons(&fixture), 1, "a rest on the field shows the reason once; in a row: {in_row}");
fixture.key(Key::A, Modifiers::COMMAND);
fixture.type_text("336699");
fixture.settle();
let field = fixture.by_label("Hex color").expect("the hex field");
assert_eq!(fixture.tree().description_of(field.node), None, "a colour clears the reason; in a row: {in_row}");
assert_eq!(reasons(&fixture), 0, "in a row: {in_row}");
}
}