From b537a04cc01376557448160e2dfd704317283b83 Mon Sep 17 00:00:00 2001 From: aiongg Date: Thu, 12 Nov 2026 02:03:18 +0000 Subject: [PATCH] Validity::Error: a checked value that stands, read invalid with its message in the bad tone; TextColumns marks an out-of-range Number or Spacing field so, in place of the notices after it; the New Document preset name refusal uses it --- crates/behavior/src/value.rs | 3 ++ crates/text/src/ui/columns.rs | 39 ++++++++++++------- crates/ui-canvas/src/canvas/crop/mod.rs | 2 +- .../ui-contract/src/contract/steps/report.rs | 4 +- .../ui-contract/src/contract/steps/state.rs | 2 +- crates/ui-contract/src/contract/values.rs | 4 +- .../form_dialogs/new_document/details.rs | 3 +- crates/ui-widgets/src/choice/radio.rs | 4 +- crates/ui-widgets/src/form/slider_field.rs | 1 + crates/ui-widgets/tests/it/text/number.rs | 36 +++++++++++++++++ 10 files changed, 75 insertions(+), 23 deletions(-) diff --git a/crates/behavior/src/value.rs b/crates/behavior/src/value.rs index 839f41461..10acc1542 100644 --- a/crates/behavior/src/value.rs +++ b/crates/behavior/src/value.rs @@ -138,6 +138,9 @@ pub enum Validity { Warn(M), /// Not accepted; a commit is refused. Refuse(M), + /// Accepted, and shown as an error: the value stands while its owner holds it (a dialog's OK waits for a valid + /// one), the control reads invalid and the message shows in the bad tone. + Error(M), } /// Where a gesture stands in one pass. diff --git a/crates/text/src/ui/columns.rs b/crates/text/src/ui/columns.rs index ae7036619..ee47e38fd 100644 --- a/crates/text/src/ui/columns.rs +++ b/crates/text/src/ui/columns.rs @@ -4,19 +4,18 @@ use super::length::{DocUnit, length_field, shown}; use craft_behavior::{EditEvent, EditEvents, Metrics, Mixed, NumberCodec}; -use craft_theme::variant::Tone; use craft_ui::buttons::Button; use craft_ui::choice::Checkbox; use craft_ui::contract::{ - Bind, Common, Component, Cx, Entry, FieldLayout, MeasureCx, PaintUi, Props as _, Role, Show as _, ShowContainer as _, SideLabel, Sizing, clicked, + Bind, Common, Component, Cx, Entry, FieldLayout, MeasureCx, PaintUi, Props as _, Role, Show as _, ShowContainer as _, SideLabel, Sizing, + Validity, clicked, }; use craft_ui::dialog::{Dialog, DialogId, DialogResult, DialogStack, StackDialog, dialog_id}; use craft_ui::form::{Field, FieldGroup}; use craft_ui::layout_components::Row; -use craft_ui::notices::{Notice, NoticeLayout}; use craft_ui::pickers::Select; use craft_ui::primitives::{PaintShape, StrokeWidth}; -use craft_ui::runtime::env::{Env, Label, Units}; +use craft_ui::runtime::env::{Env, Label, Message, Units}; use craft_ui::runtime::words::CommonWords; use craft_ui::sections::{DialogRole, FooterButton, GroupBox}; use craft_ui::style::{ComponentStyle, StyleColor, StyleCx, SurfaceKind, TypeRole}; @@ -131,7 +130,7 @@ pub enum ColumnsPreset { /// The Columns dialog: only what changed comes back. /// /// - [`word`](Self::word) off (PowerPoint): Number (1 to [`MAX_COLUMNS`]) and Spacing (0 to [`MAX_SPACING`] points); -/// values out of range keep the dialog open with the range messages under the fields. +/// values out of range mark their fields invalid, with the range messages under them, and keep the dialog open. /// - On (Word): the presets row, Number, the width and spacing grid (one row per column, Col 1 to n; width fields /// disabled after the first with Equal column width), Equal column width, Line between, and the page's text width for /// the grid's sums. @@ -433,19 +432,34 @@ impl TextColumnsDialog { GroupBox::new(words.presets.clone(), None).show(ui, |ui, _| self.presets(ui, words)); } FieldGroup::new(words.title.clone()).show(ui, |ui, _| { - let bad = !self.count_in_range(); let shown = self.edited.count.map_or(Mixed::Mixed, |n| Mixed::Same(Some(f64::from(n)))); let codec = NumberCodec::new(units.reader(None), 0); + let most = self.most_columns(); + let message = Self::number_message(words, units, most); + let count_check = move |count: &Option| match count { + Some(n) if !(1.0..=f64::from(most)).contains(n) => Validity::Error(Message::new(message.clone())), + _ => Validity::Ok, + }; let edit = Field::new(words.number.clone()) - .show(ui, |ui, _| NumberField::new(words.number.clone(), Bind::given(shown)).codec(codec.clone()).show(ui)) + .show(ui, |ui, _| NumberField::new(words.number.clone(), Bind::given(shown).check(&count_check)).codec(codec.clone()).show(ui)) .inner; self.edit_count(edit.map(|out| out.edit.events).unwrap_or_default()); - if bad { - Notice::new(Tone::Bad, Self::number_message(words, units, self.most_columns())).layout(NoticeLayout::Inline).show(ui); - } if self.word.is_none() { + let points = self.edited.spacing.map_or(Mixed::Mixed, |p| Mixed::Same(Some(f64::from(p)))); + let range = 0.0..=f64::from(MAX_SPACING); + let message = self.spacing_message(words, units); + let in_range = range.clone(); + let spacing_check = move |spacing: &Option| match spacing { + Some(points) if !in_range.contains(points) => Validity::Error(Message::new(message.clone())), + _ => Validity::Ok, + }; + let lengths = self.unit.lengths(); let edit = Field::new(words.spacing.clone()) - .show(ui, |ui, _| self.length_row(ui, words.spacing.as_str(), self.edited.spacing, true, 0.0..=MAX_SPACING)) + .show(ui, |ui, _| { + let bind = Bind::given(points).check(&spacing_check); + let field = NumberField::length(words.spacing.clone(), bind, lengths).range(range.clone()); + field.show(ui).edit.events.map(|v| v.map(|p| p as f32)) + }) .inner .unwrap_or_default(); self.edit(edit, |me, v: Option| { @@ -453,9 +467,6 @@ impl TextColumnsDialog { me.edited.spacing = Some(v); } }); - if !self.spacing_in_range() { - Notice::new(Tone::Bad, self.spacing_message(words, units)).layout(NoticeLayout::Inline).show(ui); - } } }); if self.word.is_some() { diff --git a/crates/ui-canvas/src/canvas/crop/mod.rs b/crates/ui-canvas/src/canvas/crop/mod.rs index 39a697e1d..d5456d341 100644 --- a/crates/ui-canvas/src/canvas/crop/mod.rs +++ b/crates/ui-canvas/src/canvas/crop/mod.rs @@ -597,7 +597,7 @@ impl Component for CropOverlay<'_> { let frame = self.shown(cx.value(), &space.view); let picked = cx.focus_visible().then(|| cx.active_part()).flatten().filter(|part| part.key == FRAME).and_then(|part| part.index).and_then(frame_part); - let refused = matches!(cx.validity(), crate::contract::Validity::Refuse(_)); + let refused = matches!(cx.validity(), crate::contract::Validity::Refuse(_) | crate::contract::Validity::Error(_)); paint::paint(ui, cx, &paint::Scene { space: &space, frame, drag: live.drag.as_ref(), picked, refused, overlay: self }); } diff --git a/crates/ui-contract/src/contract/steps/report.rs b/crates/ui-contract/src/contract/steps/report.rs index 4db32a73d..faac16be7 100644 --- a/crates/ui-contract/src/contract/steps/report.rs +++ b/crates/ui-contract/src/contract/steps/report.rs @@ -37,11 +37,11 @@ fn command_facts(command: &ResolvedCommand, platform: craft_keymap::Platform) -> (command.hint.as_deref(), command.chord.as_ref().map(|chord| chord.display(platform))) } -/// The message of a validity that warns or refuses, and whether it warns. +/// The message of a validity that warns, refuses or errs, and whether it warns. pub(crate) fn validity_of(validity: &Validity) -> Option<(&Message, bool)> { match validity { Validity::Warn(message) => Some((message, true)), - Validity::Refuse(message) => Some((message, false)), + Validity::Refuse(message) | Validity::Error(message) => Some((message, false)), _ => None, } } diff --git a/crates/ui-contract/src/contract/steps/state.rs b/crates/ui-contract/src/contract/steps/state.rs index 3c84625eb..0e54ee505 100644 --- a/crates/ui-contract/src/contract/steps/state.rs +++ b/crates/ui-contract/src/contract/steps/state.rs @@ -49,7 +49,7 @@ pub(crate) fn value_flags(comp: &C, cx: &Cx<'_, C>) -> Flags { flags |= Flags::MIXED; } match &cx.validity { - Validity::Refuse(_) => flags |= Flags::INVALID, + Validity::Refuse(_) | Validity::Error(_) => flags |= Flags::INVALID, Validity::Warn(_) => flags |= Flags::WARN, _ => {} } diff --git a/crates/ui-contract/src/contract/values.rs b/crates/ui-contract/src/contract/values.rs index f8d53be9b..401e71876 100644 --- a/crates/ui-contract/src/contract/values.rs +++ b/crates/ui-contract/src/contract/values.rs @@ -166,7 +166,8 @@ impl<'a, T: Value> Bind<'a, T> { self } - /// The check on a value: `Fix` replaces it, `Warn` and `Refuse` set the component's state. + /// The check on a value: `Fix` replaces it; `Warn`, `Refuse` and `Error` set the component's state, and `Refuse` + /// also refuses a commit. pub fn check(mut self, check: impl Fn(&T) -> Validity + 'a) -> Self { self.check = Some(Box::new(check)); self @@ -486,6 +487,7 @@ impl EditResponse { Validity::Fix(value) => Validity::Fix(f(value)), Validity::Warn(message) => Validity::Warn(message), Validity::Refuse(message) => Validity::Refuse(message), + Validity::Error(message) => Validity::Error(message), _ => Validity::Ok, }; let shown = self.shown.map(|shown| match shown { diff --git a/crates/ui-patterns/src/patterns/form_dialogs/new_document/details.rs b/crates/ui-patterns/src/patterns/form_dialogs/new_document/details.rs index 33c0a51cb..b648ead27 100644 --- a/crates/ui-patterns/src/patterns/form_dialogs/new_document/details.rs +++ b/crates/ui-patterns/src/patterns/form_dialogs/new_document/details.rs @@ -18,7 +18,6 @@ use crate::style::TypeRole; use crate::text::TextField; use craft_behavior::{EditEvent, EditEvents, PixelSize}; use craft_theme::Srgba; -use craft_theme::variant::Tone; use egui::Ui; /// What the details column asks the dialog to do after the pass. @@ -299,7 +298,7 @@ fn save_row(ui: &mut Ui, session: &mut Session, read: &Read<'_>, right: &mut Rig let tried = session.name_tried; let library = read.library; let check = |typed: &String| match refusal(library, typed) { - Some(refused) if tried => Validity::Warn(Message::new(refusal_message(read, &refused)).tone(Tone::Bad)), + Some(refused) if tried => Validity::Error(Message::new(refusal_message(read, &refused))), _ => Validity::Ok, }; Field::new(words.preset_name.clone()).id_salt("preset-name").show(ui, |ui, _| { diff --git a/crates/ui-widgets/src/choice/radio.rs b/crates/ui-widgets/src/choice/radio.rs index 1c442b0fc..518309780 100644 --- a/crates/ui-widgets/src/choice/radio.rs +++ b/crates/ui-widgets/src/choice/radio.rs @@ -22,7 +22,7 @@ impl GroupKind for RadioKind { type Member = T; } -/// Whether the group's value is refused, read by its members so each radio shows the invalid look. +/// Whether the group's value is refused or an error, read by its members so each radio shows the invalid look. #[derive(Clone, Copy, Debug, Default, PartialEq)] struct GroupInvalid(bool); @@ -126,7 +126,7 @@ impl Component for RadioGroup<'_, T> { handle.begin_registration(); } values.set(handle); - values.set(GroupInvalid(matches!(cx.validity(), Validity::Refuse(_)))); + values.set(GroupInvalid(matches!(cx.validity(), Validity::Refuse(_) | Validity::Error(_)))); } fn finish(self, cx: Cx<'_, Self>) -> EditResponse { diff --git a/crates/ui-widgets/src/form/slider_field.rs b/crates/ui-widgets/src/form/slider_field.rs index 014c84efd..9be95711a 100644 --- a/crates/ui-widgets/src/form/slider_field.rs +++ b/crates/ui-widgets/src/form/slider_field.rs @@ -210,6 +210,7 @@ fn lifted(verdict: Validity) -> Validity> { Validity::Fix(number) => Validity::Fix(Some(number)), Validity::Warn(message) => Validity::Warn(message), Validity::Refuse(message) => Validity::Refuse(message), + Validity::Error(message) => Validity::Error(message), _ => Validity::Ok, } } diff --git a/crates/ui-widgets/tests/it/text/number.rs b/crates/ui-widgets/tests/it/text/number.rs index 888f5d198..838c8e0a7 100644 --- a/crates/ui-widgets/tests/it/text/number.rs +++ b/crates/ui-widgets/tests/it/text/number.rs @@ -280,3 +280,39 @@ fn a_named_value_shows_its_row_label_and_the_typed_name_commits_it() { fixture.key(Key::A, Modifiers::COMMAND).type_text("metrics").key(Key::Enter, Modifiers::NONE).settle(); assert_eq!(*value.borrow(), NamedOr::Named(Metrics), "the typed name commits the name"); } + +/// A value the binding's check calls an error stays: the commit goes through, the field reads invalid and its row shows +/// the message. +#[trial] +fn a_value_the_check_calls_an_error_commits_and_shows_invalid_with_its_message() { + use craft_ui::contract::{ShowContainer as _, Validity}; + use craft_ui::form::Field; + use craft_ui::runtime::env::Message; + const TOO_MANY: &str = "Enter a number from 1 to 10"; + let value = std::rc::Rc::new(RefCell::new(Some(START))); + let (held, id) = (value.clone(), std::rc::Rc::new(Cell::new(egui::Id::NULL))); + let shown_id = id.clone(); + let mut fixture = craft_ui::testing::Fixture::new(move |ui| { + let check = |count: &Option| match count { + Some(n) if *n > 10.0 => Validity::Error(Message::new(TOO_MANY)), + _ => Validity::Ok, + }; + let shown = *held.borrow(); + let out = + Field::new("Number").show(ui, |ui, _| NumberField::new("Number", Bind::given(shown).check(&check)).id_salt("number").show(ui)).inner; + if let Some(out) = out { + shown_id.set(out.response.id); + if let Some(typed) = out.value().copied() { + *held.borrow_mut() = typed; + } + } + }); + fixture.settle(); + assert!(!fixture.tree().invalid(id.get()), "a value in range reads valid"); + let field = fixture.by_label("Number").expect("the field"); + fixture.click(&field).settle(); + fixture.key(Key::A, Modifiers::COMMAND).type_text("12").key(Key::Enter, Modifiers::NONE).settle(); + assert_eq!(*value.borrow(), Some(12.0), "the commit went through"); + assert!(fixture.tree().invalid(id.get()), "the field reads invalid"); + assert!(fixture.texts().iter().any(|text| text.text == TOO_MANY), "the row shows the message"); +}