diff --git a/crates/ui-contract/src/contract/body/hooks.rs b/crates/ui-contract/src/contract/body/hooks.rs index 15b43cb4e..a76d07d8c 100644 --- a/crates/ui-contract/src/contract/body/hooks.rs +++ b/crates/ui-contract/src/contract/body/hooks.rs @@ -747,6 +747,7 @@ impl ShapeHooks 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); diff --git a/crates/ui-contract/src/contract/container.rs b/crates/ui-contract/src/contract/container.rs index 7b0e792ab..91d102ca8 100644 --- a/crates/ui-contract/src/contract/container.rs +++ b/crates/ui-contract/src/contract/container.rs @@ -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, + /// 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. diff --git a/crates/ui-contract/src/contract/cx/body.rs b/crates/ui-contract/src/contract/cx/body.rs index bbd953688..2d72e865b 100644 --- a/crates/ui-contract/src/contract/cx/body.rs +++ b/crates/ui-contract/src/contract/cx/body.rs @@ -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() } diff --git a/crates/ui-contract/src/contract/cx/context.rs b/crates/ui-contract/src/contract/cx/context.rs index 3cb104a92..d9480c2d4 100644 --- a/crates/ui-contract/src/contract/cx/context.rs +++ b/crates/ui-contract/src/contract/cx/context.rs @@ -168,6 +168,9 @@ pub struct CxCore<'a> { pub(crate) editor_inputs: Vec, /// 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, /// 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, diff --git a/crates/ui-contract/src/contract/label/step.rs b/crates/ui-contract/src/contract/label/step.rs index 1df584fff..181787d9b 100644 --- a/crates/ui-contract/src/contract/label/step.rs +++ b/crates/ui-contract/src/contract/label/step.rs @@ -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 = [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(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); diff --git a/crates/ui-contract/src/contract/run.rs b/crates/ui-contract/src/contract/run.rs index 0e46376e7..663124bd1 100644 --- a/crates/ui-contract/src/contract/run.rs +++ b/crates/ui-contract/src/contract/run.rs @@ -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, 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, 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; diff --git a/crates/ui-contract/src/contract/run/typed.rs b/crates/ui-contract/src/contract/run/typed.rs index aa51e0a71..0c5535055 100644 --- a/crates/ui-contract/src/contract/run/typed.rs +++ b/crates/ui-contract/src/contract/run/typed.rs @@ -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 { diff --git a/crates/ui-contract/src/contract/steps/report.rs b/crates/ui-contract/src/contract/steps/report.rs index 8a65d85ae..4db32a73d 100644 --- a/crates/ui-contract/src/contract/steps/report.rs +++ b/crates/ui-contract/src/contract/steps/report.rs @@ -47,13 +47,27 @@ pub(crate) fn validity_of(validity: &Validity) -> 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 { + 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) { 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() { diff --git a/crates/ui-contract/src/contract/text.rs b/crates/ui-contract/src/contract/text.rs index 0aa72e557..346958b96 100644 --- a/crates/ui-contract/src/contract/text.rs +++ b/crates/ui-contract/src/contract/text.rs @@ -441,6 +441,7 @@ pub(crate) fn run( } 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); diff --git a/crates/ui-contract/src/contract/tooltip.rs b/crates/ui-contract/src/contract/tooltip.rs index 1ffe43c16..8762a341b 100644 --- a/crates/ui-contract/src/contract/tooltip.rs +++ b/crates/ui-contract/src/contract/tooltip.rs @@ -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, } /// 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 { +/// 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) -> Option { + 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, diff --git a/crates/ui-contract/src/primitives/text_input.rs b/crates/ui-contract/src/primitives/text_input.rs index 345757e58..8d9fd3edc 100644 --- a/crates/ui-contract/src/primitives/text_input.rs +++ b/crates/ui-contract/src/primitives/text_input.rs @@ -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, } impl Default for TextOptions { @@ -108,6 +111,7 @@ impl Default for TextOptions { mixed: false, centred: false, arrows_from_end: false, + unparsed: None, } } } diff --git a/crates/ui-contract/src/runtime/access/scope.rs b/crates/ui-contract/src/runtime/access/scope.rs index f100e5e92..85786fa71 100644 --- a/crates/ui-contract/src/runtime/access/scope.rs +++ b/crates/ui-contract/src/runtime/access/scope.rs @@ -45,6 +45,8 @@ pub struct FieldLabel { pub detail: Option, /// 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::(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. diff --git a/crates/ui-widgets/src/form/field.rs b/crates/ui-widgets/src/form/field.rs index b93d44511..cb6a0fa6d 100644 --- a/crates/ui-widgets/src/form/field.rs +++ b/crates/ui-widgets/src/form/field.rs @@ -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