diff --git a/crates/draw/src/ui/shape/arrowheads.rs b/crates/draw/src/ui/shape/arrowheads.rs index 4a020ea57..8f5faea48 100644 --- a/crates/draw/src/ui/shape/arrowheads.rs +++ b/crates/draw/src/ui/shape/arrowheads.rs @@ -12,6 +12,7 @@ use craft_ui::form::FieldGroup; use craft_ui::layout_components::Row; use craft_ui::popover::Popover; use craft_ui::runtime::env::{Env, Units}; +use craft_ui::text::RangeAlert; use egui::{Response, Ui}; /// The arrowhead popover: which ends carry a head and the head's width, length and concavity. Its `open` flag is read @@ -33,6 +34,8 @@ pub struct ArrowheadsOutput { /// The edits of this pass in order, each the whole value with one field replaced. A gesture's first pass carries /// its `Begin` and first `Update` together. pub edits: Vec>, + /// A value typed past its field's range this pass, which the field clamped: the app answers with its range alert. + pub out_of_range: Option, } impl<'a> ArrowheadsPopover<'a> { @@ -58,19 +61,19 @@ impl<'a> ArrowheadsPopover<'a> { let event = EditEvent::set(Mixed::Same(was), !was, Arc::from(words.arrowheads.as_str()), None); let _ = open.reborrow().apply(&event); } - let mut edits = Vec::new(); + let (mut edits, mut out_of_range) = (Vec::new(), None); let _ = Popover::new(words.arrowheads.clone(), open.reborrowed()).opened_by(&gear).show(ui, |ui, _| { FieldGroup::new(words.arrowheads.clone()).labels(LabelScheme::Inspector).show(ui, |ui, _| { - edits = arrow_rows(ui, &words, &units, pixels_per_inch, current, |_| {}); + (edits, out_of_range) = arrow_rows(ui, &words, &units, pixels_per_inch, current, |_| {}); }); }); - ArrowheadsOutput { response: gear, edits } + ArrowheadsOutput { response: gear, edits, out_of_range } } } /// The arrowhead rows of `value`, shared by the popover and the Shape panel: the Start and End check boxes, then Width -/// and Length, then whatever `after_length` adds (the panel's Weight), then Concavity. Each edit is the whole value with -/// its one field replaced. +/// and Length, then whatever `after_length` adds (the panel's Weight), then Concavity (-50 to 50 %, whole). Each edit is +/// the whole value with its one field replaced; the alert is that of a value typed past its field's range. pub(super) fn arrow_rows( ui: &mut Ui, words: &ShapeWords, @@ -78,8 +81,8 @@ pub(super) fn arrow_rows( pixels_per_inch: f64, value: Arrowheads, after_length: impl FnOnce(&mut Ui), -) -> Vec> { - let mut edits = Vec::new(); +) -> (Vec>, Option) { + let (mut edits, mut alerts) = (Vec::new(), Vec::new()); let (mut start, mut end) = (EditEvents::new(), EditEvents::new()); Row::new().show(ui, |ui, _| { start = Checkbox::new(Bind::given(value.start)).label(words.arrow_start.clone()).show(ui).events; @@ -87,12 +90,18 @@ pub(super) fn arrow_rows( }); edits.extend(start.map(|on| Arrowheads { start: on, ..value })); edits.extend(end.map(|on| Arrowheads { end: on, ..value })); - let width = NumberCell::pixels(units, pixels_per_inch, "arrow-width", &words.arrow_width, value.width, Arrowheads::ARROW_SIZE).labelled(ui); + let (width, alert) = + NumberCell::pixels(units, pixels_per_inch, "arrow-width", &words.arrow_width, value.width, Arrowheads::ARROW_SIZE).labelled(ui); edits.extend(width.map(|width| Arrowheads { width, ..value })); - let length = NumberCell::pixels(units, pixels_per_inch, "arrow-length", &words.arrow_length, value.length, Arrowheads::ARROW_SIZE).labelled(ui); + alerts.push(alert); + let (length, alert) = + NumberCell::pixels(units, pixels_per_inch, "arrow-length", &words.arrow_length, value.length, Arrowheads::ARROW_SIZE).labelled(ui); edits.extend(length.map(|length| Arrowheads { length, ..value })); + alerts.push(alert); after_length(ui); - let concavity = NumberCell::percent(units, "arrow-concavity", &words.arrow_concavity, value.concavity, Arrowheads::ARROW_CONCAVITY).labelled(ui); + let (concavity, alert) = + NumberCell::whole_percent(units, "arrow-concavity", &words.arrow_concavity, value.concavity, Arrowheads::ARROW_CONCAVITY).labelled(ui); edits.extend(concavity.map(|concavity| Arrowheads { concavity, ..value })); - edits + alerts.push(alert); + (edits, alerts.into_iter().flatten().last()) } diff --git a/crates/draw/src/ui/shape/bar.rs b/crates/draw/src/ui/shape/bar.rs index 443498b90..641234871 100644 --- a/crates/draw/src/ui/shape/bar.rs +++ b/crates/draw/src/ui/shape/bar.rs @@ -113,11 +113,9 @@ impl<'a> ShapeBar<'a> { let (words, units) = (env.words::(), env.units()); let mut pass = Cells::new(&words, &units, pixels_per_inch, appearance, fallbacks, sampled, chips); let mut stroke_open = stroke_open; - let mut out_of_range = None; Row::new().enabled(!appearance.locked).show(ui, |ui, _| { // The body may run more than once in a pass; each run starts clean. pass.begin(); - out_of_range = None; if paint { let fill_on = !matches!(appearance.fill, craft_color::ui::Paint::None); let check = Checkbox::new(Bind::given(fill_on)).label(words.fill.clone()).show(ui).events; @@ -169,9 +167,9 @@ impl<'a> ShapeBar<'a> { .inner .unwrap_or_default(); pass.events.extend(events.map(change)); - out_of_range = alert; + pass.alert(alert); } }); - ShapeBarOutput { events: pass.events, screen_pick: pass.screen_pick, pick_color: pass.pick_color, out_of_range } + ShapeBarOutput { events: pass.events, screen_pick: pass.screen_pick, pick_color: pass.pick_color, out_of_range: pass.out_of_range } } } diff --git a/crates/draw/src/ui/shape/mod.rs b/crates/draw/src/ui/shape/mod.rs index fa12d2631..d58b75940 100644 --- a/crates/draw/src/ui/shape/mod.rs +++ b/crates/draw/src/ui/shape/mod.rs @@ -53,15 +53,16 @@ pub struct Arrowheads { pub width: f64, /// The head's length in document pixels. pub length: f64, - /// The head's concavity in percent. + /// The head's concavity in percent of its length, in [`ARROW_CONCAVITY`](Self::ARROW_CONCAVITY); below 0 the + /// back bulges out. pub concavity: f64, } impl Arrowheads { /// The range of the width and length fields, in document pixels (production and engine units). pub const ARROW_SIZE: RangeInclusive = 1.0..=4096.0; - /// The range of the concavity field, in percent. - pub const ARROW_CONCAVITY: RangeInclusive = 0.0..=100.0; + /// The range of the concavity field, in whole percent. + pub const ARROW_CONCAVITY: RangeInclusive = -50.0..=50.0; /// No arrowheads at the default size. pub const fn new() -> Self { diff --git a/crates/draw/src/ui/shape/panel.rs b/crates/draw/src/ui/shape/panel.rs index 3cd5cce50..1fd398396 100644 --- a/crates/draw/src/ui/shape/panel.rs +++ b/crates/draw/src/ui/shape/panel.rs @@ -54,6 +54,8 @@ pub struct ShapePanelOutput { pub screen_pick: Option<(ShapePaint, ScreenPickGesture)>, /// A chip asked for the app's full colour picker, with the chip it is for. pub pick_color: Option<(ShapePaint, ColorRequest)>, + /// A value typed past its field's range this pass, which the field clamped: the app answers with its range alert. + pub out_of_range: Option, } /// A shape layer's Properties sections, as PhotoCraft's `shape_properties`. @@ -66,8 +68,8 @@ pub struct ShapePanelOutput { /// ellipse or polygon; a rect's "Corner radius" (0 to 150,000, sets all four) then Top Left, Top Right, Bottom /// Left, Bottom Right rows (PhotoCraft's row order, mapped to the radii order TL, TR, BR, BL); a polygon's Corner /// radius, Sides (3 to 100) and Star ratio (1 to 100 %, stored 0.01 to 1); a line's Weight (1 to 10,000), and with -/// arrowheads its Start and End check boxes, Width, Length and Concavity, then the "Set as default" check box -/// ([`arrow_default`](Self::arrow_default)). +/// arrowheads its Start and End check boxes, Width, Length and Concavity (-50 to 50 %, whole), then the "Set as default" check box ([`arrow_default`](Self::arrow_default)). +/// - A value typed past its field's range is clamped and reported once in [`ShapePanelOutput::out_of_range`]. /// - A locked layer ([`ShapeAppearance::locked`]) disables the whole panel. /// - Keyboard: every field and chip is a Tab stop in reading order; sections fold with their title rows. Right to /// left the rows mirror; the corner names stay. @@ -171,15 +173,16 @@ impl<'a> ShapePanel<'a> { } else { body(ui); } - ShapePanelOutput { events: pass.events, screen_pick: pass.screen_pick, pick_color: pass.pick_color } + ShapePanelOutput { events: pass.events, screen_pick: pass.screen_pick, pick_color: pass.pick_color, out_of_range: pass.out_of_range } } } /// One number cell: the field's key, caption, value, range, the lengths it converts between and what reads its text. +/// An empty value (`None`) shows an empty field. pub(super) struct NumberCell { pub key: &'static str, pub caption: Label, - pub value: f64, + pub value: Option, pub range: RangeInclusive, lengths: Option, codec: NumberCodec, @@ -187,13 +190,27 @@ pub(super) struct NumberCell { impl NumberCell { /// A length in pixels with up to two decimals, in a document of `pixels_per_inch`. - pub fn pixels(units: &Units, pixels_per_inch: f64, key: &'static str, caption: &Label, value: f64, range: RangeInclusive) -> Self { - Self::length(units, pixels_per_inch, PIXEL_DECIMALS, key, caption, value, range) + pub fn pixels( + units: &Units, + pixels_per_inch: f64, + key: &'static str, + caption: &Label, + value: impl Into>, + range: RangeInclusive, + ) -> Self { + Self::length(units, pixels_per_inch, PIXEL_DECIMALS, key, caption, value.into(), range) } /// A length in whole pixels, in a document of `pixels_per_inch`. - pub fn whole_pixels(units: &Units, pixels_per_inch: f64, key: &'static str, caption: &Label, value: f64, range: RangeInclusive) -> Self { - Self::length(units, pixels_per_inch, 0, key, caption, value, range) + pub fn whole_pixels( + units: &Units, + pixels_per_inch: f64, + key: &'static str, + caption: &Label, + value: impl Into>, + range: RangeInclusive, + ) -> Self { + Self::length(units, pixels_per_inch, 0, key, caption, value.into(), range) } fn length( @@ -202,7 +219,7 @@ impl NumberCell { decimals: usize, key: &'static str, caption: &Label, - value: f64, + value: Option, range: RangeInclusive, ) -> Self { let lengths = LengthUnits::new(UnitKind::Pixels, UnitKind::Pixels, pixels_per_inch); @@ -216,26 +233,37 @@ impl NumberCell { } } + /// A number with up to `decimals` decimals, read in `unit` (none for a plain number). + pub fn plain( + units: &Units, + unit: Option, + decimals: usize, + key: &'static str, + caption: &Label, + value: impl Into>, + range: RangeInclusive, + ) -> Self { + NumberCell { key, caption: caption.clone(), value: value.into(), range, lengths: None, codec: NumberCodec::new(units.reader(unit), decimals) } + } + /// A plain whole number. pub fn count(units: &Units, key: &'static str, caption: &Label, value: f64, range: RangeInclusive) -> Self { - NumberCell { key, caption: caption.clone(), value, range, lengths: None, codec: NumberCodec::new(units.reader(None), 0) } + Self::plain(units, None, 0, key, caption, value, range) } /// A percentage with up to one decimal. pub fn percent(units: &Units, key: &'static str, caption: &Label, value: f64, range: RangeInclusive) -> Self { - NumberCell { - key, - caption: caption.clone(), - value, - range, - lengths: None, - codec: NumberCodec::new(units.reader(Some(UnitKind::Percent)), PERCENT_DECIMALS), - } + Self::plain(units, Some(UnitKind::Percent), PERCENT_DECIMALS, key, caption, value, range) + } + + /// A whole percentage. + pub fn whole_percent(units: &Units, key: &'static str, caption: &Label, value: f64, range: RangeInclusive) -> Self { + Self::plain(units, Some(UnitKind::Percent), 0, key, caption, value, range) } /// The field over the cell's value. pub fn field(&self) -> NumberField<'static> { - let value = Bind::given(Some(self.value)); + let value = Bind::given(self.value); let field = match self.lengths { Some(lengths) => NumberField::length(self.caption.clone(), value, lengths), None => NumberField::new(self.caption.clone(), value), @@ -243,19 +271,25 @@ impl NumberCell { field.range(self.range.clone()).codec(self.codec.clone()) } - /// The edit of the field shown in `ui` and the alert of a typed value that was clamped; an emptied field keeps the - /// value. - pub fn show(&self, ui: &mut Ui) -> (EditEvents, Option) { - let kept = self.value; + /// The edit of the field shown in `ui`, an emptied field as `None`, and the alert of a typed value that was + /// clamped. + pub fn show_optional(&self, ui: &mut Ui) -> (EditEvents>, Option) { let out = self.field().scrub_by_label().show(ui); - let alert = out.out_of_range; - (out.edit.events.map(|value| value.unwrap_or(kept)), alert) + (out.edit.events, out.out_of_range) } - /// The field in a row under its caption, which scrubs it: the edit of the field. - pub fn labelled(&self, ui: &mut Ui) -> EditEvents { + /// The edit of the field shown in `ui` and the alert of a typed value that was clamped; an emptied field keeps the + /// value. For a cell that always has a value. + pub fn show(&self, ui: &mut Ui) -> (EditEvents, Option) { + let kept = self.value.unwrap_or(*self.range.start()); + let (events, alert) = self.show_optional(ui); + (events.map(|value| value.unwrap_or(kept)), alert) + } + + /// The field in a row under its caption, which scrubs it: the edit of the field and its range alert. + pub fn labelled(&self, ui: &mut Ui) -> (EditEvents, Option) { let side = SideLabel::new().scrub(true); - Field::new(self.caption.clone()).id_salt(self.key).side(side).show(ui, |ui, _| self.show(ui).0).inner.unwrap_or_default() + Field::new(self.caption.clone()).id_salt(self.key).side(side).show(ui, |ui, _| self.show(ui)).inner.unwrap_or_default() } } @@ -278,6 +312,8 @@ pub(super) struct Cells<'p, 'a> { pub events: Vec>, pub screen_pick: Option<(ShapePaint, ScreenPickGesture)>, pub pick_color: Option<(ShapePaint, ColorRequest)>, + /// The range alert of a value typed past its cell's range this run. + pub out_of_range: Option, } impl<'p, 'a> Cells<'p, 'a> { @@ -302,6 +338,7 @@ impl<'p, 'a> Cells<'p, 'a> { events: Vec::new(), screen_pick: None, pick_color: None, + out_of_range: None, } } @@ -310,6 +347,14 @@ impl<'p, 'a> Cells<'p, 'a> { self.events.clear(); self.screen_pick = None; self.pick_color = None; + self.out_of_range = None; + } + + /// Keeps a cell's range alert; a cell without one leaves the alert of an earlier cell. + pub fn alert(&mut self, alert: Option) { + if alert.is_some() { + self.out_of_range = alert; + } } /// The paint chip of `which`, decorated by the app's setup. @@ -371,8 +416,9 @@ impl<'p, 'a> Cells<'p, 'a> { fn width_row(&mut self, ui: &mut Ui) { let cell = width_cell(self.words, self.units, self.pixels_per_inch, self.appearance); - let events = cell.labelled(ui); + let (events, alert) = cell.labelled(ui); take_width(self.appearance, events, &mut self.events); + self.alert(alert); } fn style_row(&mut self, ui: &mut Ui, corners: bool) { @@ -383,8 +429,9 @@ impl<'p, 'a> Cells<'p, 'a> { /// A labelled row with `cell`; its edit goes out through `change`. fn number_row(&mut self, ui: &mut Ui, cell: NumberCell, change: impl Fn(f64) -> ShapeChange) { - let events = cell.labelled(ui); + let (events, alert) = cell.labelled(ui); self.events.extend(events.map(&change)); + self.alert(alert); } /// The W and H row of a rect, ellipse or polygon: whole pixels, 1 to 300,000. @@ -396,12 +443,20 @@ impl<'p, 'a> Cells<'p, 'a> { ]; let glyphs = [&words.width, &words.height]; let mut edits = [EditEvents::new(), EditEvents::new()]; + let mut alerts = [None, None]; Row::new().fill_width().show(ui, |ui, _| { - for ((cell, glyph), edit) in cells.iter().zip(glyphs).zip(edits.iter_mut()) { - let side = SideLabel::new().kind(LabelKind::Glyph(GlyphLabel::text(glyph.clone()))).scrub(true); - *edit = Field::new(cell.caption.clone()).id_salt(cell.key).side(side).show(ui, |ui, _| cell.show(ui).0).inner.unwrap_or_default(); + for (((cell, glyph), edit), alert) in cells.iter().zip(glyphs).zip(edits.iter_mut()).zip(alerts.iter_mut()) { + (*edit, *alert) = Field::new(cell.caption.clone()) + .id_salt(cell.key) + .side(glyph_side(glyph)) + .show(ui, |ui, _| cell.show(ui)) + .inner + .unwrap_or_default(); } }); + for alert in alerts { + self.alert(alert); + } let [w, h] = edits; self.events.extend(w.map(|w| ShapeChange::Size { width: w, height })); self.events.extend(h.map(|h| ShapeChange::Size { width, height: h })); @@ -448,8 +503,9 @@ impl<'p, 'a> Cells<'p, 'a> { None => self.number_row(ui, cell, ShapeChange::Weight), Some(arrows) => { // PhotoCraft's order puts the weight between the head's length and its concavity. - let edits = arrow_rows(ui, words, units, ppi, *arrows, |ui| self.number_row(ui, cell, ShapeChange::Weight)); + let (edits, alert) = arrow_rows(ui, words, units, ppi, *arrows, |ui| self.number_row(ui, cell, ShapeChange::Weight)); self.events.extend(edits.into_iter().map(|edit| edit.map(ShapeChange::Arrowheads))); + self.alert(alert); if let Some(link) = arrow_default { let _ = Checkbox::new(link.reborrowed()) .label(words.set_as_default.clone()) @@ -463,6 +519,11 @@ impl<'p, 'a> Cells<'p, 'a> { } } +/// The side label of a cell shown by a short glyph (W, H) instead of its caption, which scrubs it. +pub(super) fn glyph_side(glyph: &Label) -> SideLabel { + SideLabel::new().kind(LabelKind::Glyph(GlyphLabel::text(glyph.clone()))).scrub(true) +} + /// The change a Sides value makes: a whole number of sides in range. pub(super) fn sides_change(sides: f64) -> ShapeChange { ShapeChange::Sides(sides.round().clamp(*SIDES_RANGE.start(), *SIDES_RANGE.end()) as u32) diff --git a/crates/draw/tests/it/shape_cells.rs b/crates/draw/tests/it/shape_cells.rs index a2d690c31..a2a017aea 100644 --- a/crates/draw/tests/it/shape_cells.rs +++ b/crates/draw/tests/it/shape_cells.rs @@ -1,11 +1,12 @@ -//! The arrowhead rows: one builder behind the popover and the Shape panel. +//! The shape cells: the arrowhead rows behind the popover and the Shape panel. use craft_behavior::EditEvent; use craft_color::ui::Paint; use craft_draw::ui::{Arrowheads, ArrowheadsPopover, LiveShape, ShapeAppearance, ShapeChange, ShapePanel}; use craft_ui::contract::Bind; use craft_ui::testing::{Fixture, trial}; -use egui::vec2; +use craft_ui::text::RangeAlert; +use egui::{Key, Modifiers, vec2}; use std::cell::RefCell; use std::rc::Rc; @@ -98,3 +99,74 @@ fn an_arrowhead_row_scrub_reports_the_whole_value_from_the_popover_and_the_panel }; assert_width_scrub(&host.borrow().events, whole, start_value(), "panel"); } + +/// Types `text` over the field named `label` and commits it with Enter. +fn type_into(fixture: &mut Fixture, label: &str, text: &str) { + let field = fixture.by_label(label).unwrap_or_else(|| panic!("the {label} field")); + fixture.click(&field).settle(); + fixture.key(Key::A, Modifiers::COMMAND); + if text.is_empty() { + fixture.key(Key::Backspace, Modifiers::NONE); + } else { + fixture.type_text(text); + } + fixture.key(Key::Enter, Modifiers::NONE).settle(); +} + +struct AlertingPopover { + value: Arrowheads, + open: bool, + alerts: Vec, +} + +struct AlertingPanel { + shape: LiveShape, + look: ShapeAppearance, + alerts: Vec, +} + +#[trial] +fn a_concavity_typed_past_its_range_keeps_the_end_and_reports_it_once_from_the_popover_and_the_panel() { + let host = Rc::new(RefCell::new(AlertingPopover { value: start_value(), open: false, alerts: Vec::new() })); + let shown = Rc::clone(&host); + let mut fixture = Fixture::new(move |ui| { + let mut held = shown.borrow_mut(); + let AlertingPopover { value, open, alerts } = &mut *held; + let out = ArrowheadsPopover::new(Bind::given(*value), Bind::bound(open), 72.0).show(ui); + if let Some(next) = out.edits.iter().rev().find_map(|event| event.value()) { + *value = *next; + } + alerts.extend(out.out_of_range); + }); + fixture.settle(); + host.borrow_mut().open = true; + fixture.idle(3); + type_into(&mut fixture, "Concavity", "80"); + assert_eq!(host.borrow().value.concavity, 50.0, "popover: a concavity past 50 keeps 50"); + assert_eq!(host.borrow().alerts.len(), 1, "popover: the clamp is reported once"); + type_into(&mut fixture, "Concavity", "-20"); + assert_eq!(host.borrow().value.concavity, -20.0, "popover: a concavity below 0 is in range"); + assert_eq!(host.borrow().alerts.len(), 1, "popover: a value in range reports nothing"); + + let host = Rc::new(RefCell::new(AlertingPanel { + shape: LiveShape::arrow_line(3.0, start_value()), + look: ShapeAppearance::new(Paint::None, Paint::None, 2.0), + alerts: Vec::new(), + })); + let shown = Rc::clone(&host); + let mut fixture = Fixture::new(move |ui| { + let mut held = shown.borrow_mut(); + let AlertingPanel { shape, look, alerts } = &mut *held; + let out = ShapePanel::new("Shape properties", shape, look, 72.0).show(ui); + if let Some(ShapeChange::Arrowheads(next)) = out.events.iter().rev().find_map(|event| event.value()) { + *shape = LiveShape::arrow_line(3.0, *next); + } + alerts.extend(out.out_of_range); + }); + fixture.settle(); + type_into(&mut fixture, "Concavity", "80"); + let mut kept = start_value(); + kept.concavity = 50.0; + assert_eq!(host.borrow().shape, LiveShape::arrow_line(3.0, kept), "panel: 80 keeps 50"); + assert_eq!(host.borrow().alerts.len(), 1, "panel: the clamp is reported once"); +}