mirror of
https://github.com/storytold/craft-libs.git
synced 2026-11-17 00:34:25 +00:00
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
This commit is contained in:
@@ -138,6 +138,9 @@ pub enum Validity<T, M> {
|
||||
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.
|
||||
|
||||
@@ -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<f64>| 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<f64>| 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<f32>| {
|
||||
@@ -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() {
|
||||
|
||||
@@ -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 });
|
||||
}
|
||||
|
||||
|
||||
@@ -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<V>(validity: &Validity<V>) -> 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,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -49,7 +49,7 @@ pub(crate) fn value_flags<C: Component>(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,
|
||||
_ => {}
|
||||
}
|
||||
|
||||
@@ -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<T> + 'a) -> Self {
|
||||
self.check = Some(Box::new(check));
|
||||
self
|
||||
@@ -486,6 +487,7 @@ impl<T: Clone> EditResponse<T> {
|
||||
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 {
|
||||
|
||||
@@ -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, _| {
|
||||
|
||||
@@ -22,7 +22,7 @@ impl<T: Value + 'static> GroupKind for RadioKind<T> {
|
||||
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<T: Value + 'static> 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<T> {
|
||||
|
||||
@@ -210,6 +210,7 @@ fn lifted(verdict: Validity<f64>) -> Validity<Option<f64>> {
|
||||
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,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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<f64>| 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");
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user