From 643cd9ed3d824304964663afabf8a8d59a6757f5 Mon Sep 17 00:00:00 2001 From: aiongg Date: Mon, 16 Nov 2026 12:53:27 +0000 Subject: [PATCH 1/5] merge-c: 1.6: Arrowhead Concavity -50..50 whole; the arrowhead popover and the Shape panel report a value typed past its range (ArrowheadsOutput.out_of_range, ShapePanelOutput.out_of_range) --- crates/draw/src/ui/shape/arrowheads.rs | 31 +++--- crates/draw/src/ui/shape/bar.rs | 6 +- crates/draw/src/ui/shape/mod.rs | 7 +- crates/draw/src/ui/shape/panel.rs | 129 ++++++++++++++++++------- crates/draw/tests/it/shape_cells.rs | 76 ++++++++++++++- 5 files changed, 195 insertions(+), 54 deletions(-) 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"); +} From 184aebf64c71dd87d6eee5cde123e2c2093e297d Mon Sep 17 00:00:00 2001 From: aiongg Date: Mon, 16 Nov 2026 12:53:36 +0000 Subject: [PATCH 2/5] merge-c: 1.7: LiveShape::triangle; the Shape panel shows a triangle's W, H and one Corner radius (#13360) --- crates/draw/src/ui/shape/mod.rs | 15 +++++++++++++++ crates/draw/src/ui/shape/panel.rs | 14 ++++++++++---- crates/draw/src/ui/shape/stories.rs | 14 ++++++++++++-- crates/draw/tests/it/shape_cells.rs | 27 ++++++++++++++++++++++++++- 4 files changed, 63 insertions(+), 7 deletions(-) diff --git a/crates/draw/src/ui/shape/mod.rs b/crates/draw/src/ui/shape/mod.rs index d58b75940..481e7a238 100644 --- a/crates/draw/src/ui/shape/mod.rs +++ b/crates/draw/src/ui/shape/mod.rs @@ -119,6 +119,16 @@ pub enum LiveShape { /// Inner radius over outer radius; 1 for a plain polygon. star_ratio: f64, }, + /// A triangle: one corner radius for every corner. + #[non_exhaustive] + Triangle { + /// Width in document pixels. + width: f64, + /// Height in document pixels. + height: f64, + /// The corner radius in document pixels. + corner_radius: f64, + }, /// A line. #[non_exhaustive] Line { @@ -146,6 +156,11 @@ impl LiveShape { LiveShape::Polygon { width, height, corner_radius, sides, star_ratio } } + /// A triangle of `width` by `height` with `corner_radius` at every corner. + pub fn triangle(width: f64, height: f64, corner_radius: f64) -> Self { + LiveShape::Triangle { width, height, corner_radius } + } + /// A line of `weight`. pub fn line(weight: f64) -> Self { LiveShape::Line { weight, arrows: None } diff --git a/crates/draw/src/ui/shape/panel.rs b/crates/draw/src/ui/shape/panel.rs index 1fd398396..c321de3ed 100644 --- a/crates/draw/src/ui/shape/panel.rs +++ b/crates/draw/src/ui/shape/panel.rs @@ -65,10 +65,11 @@ pub struct ShapePanelOutput { /// [`stroke_open`](Self::stroke_open)). Stroke width 0 is no stroke; an edit that turns a stroke on at 0 carries /// [`ShapeChange::ENABLED_STROKE_WIDTH`] in the same change; an arrow step that clamps at 0 reports nothing. /// - Shape (a collapsible section), by the [`LiveShape`]: W and H (1 to 300,000 px, whole pixels) for a rect, -/// 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 (-50 to 50 %, whole), then the "Set as default" check box ([`arrow_default`](Self::arrow_default)). +/// ellipse, polygon or triangle; 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 triangle's Corner radius; a +/// line's Weight (1 to 10,000), and with 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 @@ -497,6 +498,11 @@ impl<'p, 'a> Cells<'p, 'a> { let ratio = NumberCell::percent(units, "star-ratio", &words.star_ratio, star_ratio * 100.0, RATIO_PERCENT); self.number_row(ui, ratio, |percent| ShapeChange::StarRatio(percent / 100.0)); } + LiveShape::Triangle { width, height, corner_radius } => { + self.size_row(ui, (*width, *height)); + let radius = NumberCell::pixels(units, ppi, "corner-radius", &words.corner_radius, *corner_radius, RADIUS_RANGE); + self.number_row(ui, radius, ShapeChange::CornerRadius); + } LiveShape::Line { weight, arrows } => { let cell = NumberCell::pixels(units, ppi, "weight", &words.weight, *weight, LINE_WEIGHT_RANGE); match arrows { diff --git a/crates/draw/src/ui/shape/stories.rs b/crates/draw/src/ui/shape/stories.rs index ae6404360..c1f6d2cc5 100644 --- a/crates/draw/src/ui/shape/stories.rs +++ b/crates/draw/src/ui/shape/stories.rs @@ -41,12 +41,17 @@ fn apply(change: ShapeChange, shape: &mut LiveShape, look: &mut ShapeAppearance) } ( ShapeChange::Size { width: w, height: h }, - LiveShape::Rect { width, height, .. } | LiveShape::Ellipse { width, height } | LiveShape::Polygon { width, height, .. }, + LiveShape::Rect { width, height, .. } + | LiveShape::Ellipse { width, height } + | LiveShape::Polygon { width, height, .. } + | LiveShape::Triangle { width, height, .. }, ) => { (*width, *height) = (w, h); } (ShapeChange::Radii(next), LiveShape::Rect { radii, .. }) => *radii = next, - (ShapeChange::CornerRadius(next), LiveShape::Polygon { corner_radius, .. }) => *corner_radius = next, + (ShapeChange::CornerRadius(next), LiveShape::Polygon { corner_radius, .. } | LiveShape::Triangle { corner_radius, .. }) => { + *corner_radius = next + } (ShapeChange::Sides(next), LiveShape::Polygon { sides, .. }) => *sides = next, (ShapeChange::StarRatio(next), LiveShape::Polygon { star_ratio, .. }) => *star_ratio = next, (ShapeChange::Weight(next), LiveShape::Line { weight, .. }) => *weight = next, @@ -95,6 +100,11 @@ fn shape_panel_polygon(ui: &mut Ui, data: &mut StoryData) -> StoryOutput { panel(ui, data, polygon()) } +#[story(ShapePanel<'static>, covers = [Default], drive = Drive::Never)] +fn shape_panel_triangle(ui: &mut Ui, data: &mut StoryData) -> StoryOutput { + panel(ui, data, LiveShape::triangle(240.0, 200.0, 0.0)) +} + #[story(ShapePanel<'static>, covers = [Default], drive = Drive::Never)] fn shape_panel_line(ui: &mut Ui, data: &mut StoryData) -> StoryOutput { panel(ui, data, LiveShape::line(3.0)) diff --git a/crates/draw/tests/it/shape_cells.rs b/crates/draw/tests/it/shape_cells.rs index a2a017aea..546114e2d 100644 --- a/crates/draw/tests/it/shape_cells.rs +++ b/crates/draw/tests/it/shape_cells.rs @@ -1,4 +1,4 @@ -//! The shape cells: the arrowhead rows behind the popover and the Shape panel. +//! The shape cells: the arrowhead rows behind the popover and the Shape panel, and the panel's rows by shape. use craft_behavior::EditEvent; use craft_color::ui::Paint; @@ -170,3 +170,28 @@ fn a_concavity_typed_past_its_range_keeps_the_end_and_reports_it_once_from_the_p 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"); } + +#[trial] +fn a_triangle_shows_its_size_and_one_corner_radius() { + let host = Rc::new(RefCell::new(PanelHost { + shape: LiveShape::triangle(240.0, 200.0, 0.0), + look: ShapeAppearance::new(Paint::None, Paint::None, 2.0), + events: Vec::new(), + })); + let shown = Rc::clone(&host); + let mut fixture = Fixture::new(move |ui| { + let mut held = shown.borrow_mut(); + let PanelHost { shape, look, events } = &mut *held; + events.extend(ShapePanel::new("Shape properties", shape, look, 72.0).show(ui).events); + }); + fixture.settle(); + for shown in ["Width", "Height", "Corner radius"] { + assert!(fixture.by_label(shown).is_some(), "a triangle shows {shown}"); + } + for absent in ["Sides", "Star ratio", "Top Left"] { + assert!(fixture.by_label(absent).is_none(), "a triangle has no {absent} field"); + } + type_into(&mut fixture, "Corner radius", "12"); + let radius = host.borrow().events.iter().rev().find_map(|event| event.value().cloned()); + assert_eq!(radius, Some(ShapeChange::CornerRadius(12.0)), "the radius is one radius for every corner"); +} From 14c337949f0107ac00493328d3e02d2c51f3a9a9 Mon Sep 17 00:00:00 2001 From: aiongg Date: Mon, 16 Nov 2026 12:53:44 +0000 Subject: [PATCH 3/5] merge-c: 1.4b: Rectangle gear popover: Unconstrained, Square, Fixed Size, Proportional and From Center over BoxConstraints, its sizes reporting range alerts (BoxConstraintsPopover; ShapeToolSettings.rect_box) (#13343) --- crates/draw/src/ui/mod.rs | 5 +- crates/draw/src/ui/shape/gear.rs | 154 ++++++++++++++++++++++++++++ crates/draw/src/ui/shape/mod.rs | 20 +++- crates/draw/src/ui/shape/stories.rs | 18 +++- crates/draw/src/ui/shape/words.rs | 20 ++++ crates/draw/tests/it/shape_cells.rs | 67 +++++++++++- 6 files changed, 273 insertions(+), 11 deletions(-) create mode 100644 crates/draw/src/ui/shape/gear.rs diff --git a/crates/draw/src/ui/mod.rs b/crates/draw/src/ui/mod.rs index ad15fcda0..de12947b8 100644 --- a/crates/draw/src/ui/mod.rs +++ b/crates/draw/src/ui/mod.rs @@ -98,11 +98,14 @@ pub use pen::{ }; pub use picture_fill::{PictureFill, PictureFillEditor, PictureFillOutput, PictureMode}; pub use shape::{Arrowheads, ArrowheadsOutput, ArrowheadsPopover, LINE_WEIGHT_RANGE, RADIUS_RANGE, StrokeCells, VectorMode, VectorModeSelect}; +pub use shape::{BoxConstraintsOutput, BoxConstraintsPopover}; pub use shape::{ ChipSetup, LiveShape, ShapeAppearance, ShapeBar, ShapeBarOutput, ShapeChange, ShapePaint, ShapePanel, ShapePanelOutput, ShapeToolSettings, ShapeWords, }; -pub use shape_drag::{CreateShapeDialog, CreateShapeOptions, CreatedShape, ShapeDragEvent, ShapeDragOptions, ShapeDragTool, ShapeKind}; +pub use shape_drag::{ + BoxConstraint, BoxConstraints, CreateShapeDialog, CreateShapeOptions, CreatedShape, ShapeDragEvent, ShapeDragOptions, ShapeDragTool, ShapeKind, +}; pub use shape_presets::{ShapeMenu, ShapePresetWords}; pub use slice::{ DivideBy, DivideSlices, DivideSlicesDialog, SliceBar, SliceBarOutput, SliceCommand, SliceEvent, SliceItem, SliceKind, SliceMode, SliceOptions, diff --git a/crates/draw/src/ui/shape/gear.rs b/crates/draw/src/ui/shape/gear.rs new file mode 100644 index 000000000..182377e0c --- /dev/null +++ b/crates/draw/src/ui/shape/gear.rs @@ -0,0 +1,154 @@ +//! [`BoxConstraintsPopover`]: the Rectangle tool's gear. + +use std::sync::Arc; + +use super::ShapeWords; +use super::panel::{NumberCell, glyph_side}; +use crate::ui::shape_drag::{BoxConstraint, BoxConstraints}; +use craft_behavior::{EditEvent, EditEvents, IconId, Mixed}; +use craft_ui::buttons::IconButton; +use craft_ui::choice::{Checkbox, Radio, RadioGroup}; +use craft_ui::contract::{Bind, LabelScheme, PopupKind, Props as _, Show as _, ShowContainer as _, clicked}; +use craft_ui::form::{Field, FieldGroup}; +use craft_ui::layout_components::{Row, Stack}; +use craft_ui::popover::Popover; +use craft_ui::runtime::env::{Env, Label, Units}; +use craft_ui::text::RangeAlert; +use egui::{Response, Ui}; + +/// The Rectangle tool's gear: a button that opens the box constraints of a drag. The popover holds the radios +/// Unconstrained, Square, Fixed Size and Proportional, each size's W and H beside its radio and enabled only while it +/// is chosen, and the From Center check box. Fixed Size takes whole pixels (0 to 300,000), Proportional up to three +/// decimals (0 to 99,999); an emptied field is `None`, which draws as Unconstrained, and a value typed past its range +/// is clamped and reported once in [`BoxConstraintsOutput::out_of_range`]. Escape closes it and keeps the values. Its +/// `open` flag is read and written through the binding, so give it `Bind::bound`. +#[must_use = "show the popover with `show`"] +#[derive(craft_ui::contract::Pattern)] +pub struct BoxConstraintsPopover<'a> { + value: Bind<'a, BoxConstraints>, + open: Bind<'a, bool>, + pixels_per_inch: f64, +} + +/// What [`BoxConstraintsPopover::show`] reports. +#[derive(Debug)] +#[non_exhaustive] +pub struct BoxConstraintsOutput { + /// The gear button's response. + pub response: Response, + /// The edits of this pass in order, each the whole value with one field replaced. + pub edits: Vec>, + /// A size typed past its field's range this pass, which the field clamped: the app answers with its range alert. + pub out_of_range: Option, +} + +/// The most decimals a Proportional field shows. +const RATIO_DECIMALS: usize = 3; + +impl<'a> BoxConstraintsPopover<'a> { + /// The gear of `value`, open while `open` is set, in a document of `pixels_per_inch`, which sizes a Fixed Size typed + /// in inches or points. + pub fn new(value: Bind<'a, BoxConstraints>, open: Bind<'a, bool>, pixels_per_inch: f64) -> Self { + BoxConstraintsPopover { value, open, pixels_per_inch } + } + + /// Shows the gear button and, while open, the popover. + pub fn show(self, ui: &mut Ui) -> BoxConstraintsOutput { + let BoxConstraintsPopover { mut value, mut open, pixels_per_inch } = self; + let env = Env::of(ui); + let (words, units) = (env.words::(), env.units()); + let current = match value.reborrow().get() { + Some(Mixed::Same(shown)) => shown, + _ => BoxConstraints::default(), + }; + let was = matches!(open.reborrow().get(), Some(Mixed::Same(true))); + let gear = IconButton::new(IconId::new("settings")) + .label(words.shape_options.clone()) + .tooltip(words.shape_options.clone()) + .popup(PopupKind::Dialog) + .expanded(was) + .show(ui) + .response; + if clicked(&gear) { + // The popover's opener is manual: the gear's press is the pattern's to apply. + let event = EditEvent::set(Mixed::Same(was), !was, Arc::from(words.shape_options.as_str()), None); + let _ = open.reborrow().apply(&event); + } + let mut body = Body { words: &words, units: &units, pixels_per_inch, value: current, edits: Vec::new(), out_of_range: None }; + let _ = Popover::new(words.shape_options.clone(), open.reborrowed()).opened_by(&gear).show(ui, |ui, _| { + FieldGroup::new(words.shape_options.clone()).labels(LabelScheme::Inspector).show(ui, |ui, _| body.show(ui)); + }); + BoxConstraintsOutput { response: gear, edits: body.edits, out_of_range: body.out_of_range } + } +} + +/// The popover's rows over one value, and what they report in one run. +struct Body<'p> { + words: &'p ShapeWords, + units: &'p Units, + pixels_per_inch: f64, + value: BoxConstraints, + edits: Vec>, + out_of_range: Option, +} + +impl Body<'_> { + fn show(&mut self, ui: &mut Ui) { + // The body may run more than once in a pass; each run starts clean. + self.edits.clear(); + self.out_of_range = None; + let (words, units, ppi, value) = (self.words, self.units, self.pixels_per_inch, self.value); + let chosen = value.constraint; + let fixed = [ + NumberCell::whole_pixels(units, ppi, "fixed-width", &words.fixed_width, value.fixed.0, BoxConstraints::FIXED), + NumberCell::whole_pixels(units, ppi, "fixed-height", &words.fixed_height, value.fixed.1, BoxConstraints::FIXED), + ]; + let ratio = [ + NumberCell::plain(units, None, RATIO_DECIMALS, "ratio-width", &words.ratio_width, value.ratio.0, BoxConstraints::RATIO), + NumberCell::plain(units, None, RATIO_DECIMALS, "ratio-height", &words.ratio_height, value.ratio.1, BoxConstraints::RATIO), + ]; + let (mut fixed_edits, mut ratio_edits) = (Pair::default(), Pair::default()); + let group = RadioGroup::new(words.shape_options.clone(), Bind::given(chosen)).show(ui, |ui, _| { + Stack::new().show(ui, |ui, _| { + Radio::new(BoxConstraint::Unconstrained).label(words.unconstrained.clone()).show(ui); + Radio::new(BoxConstraint::Square).label(words.square.clone()).show(ui); + Row::new().show(ui, |ui, _| { + Radio::new(BoxConstraint::FixedSize).label(words.fixed_size.clone()).show(ui); + fixed_edits = pair(ui, words, &fixed, chosen == BoxConstraint::FixedSize); + }); + Row::new().show(ui, |ui, _| { + Radio::new(BoxConstraint::Proportional).label(words.proportional.clone()).show(ui); + ratio_edits = pair(ui, words, &ratio, chosen == BoxConstraint::Proportional); + }); + }); + }); + self.edits.extend(group.output.events.map(|constraint| BoxConstraints { constraint, ..value })); + let [width, height] = fixed_edits.edits; + self.edits.extend(width.map(|width| BoxConstraints { fixed: (width, value.fixed.1), ..value })); + self.edits.extend(height.map(|height| BoxConstraints { fixed: (value.fixed.0, height), ..value })); + let [width, height] = ratio_edits.edits; + self.edits.extend(width.map(|width| BoxConstraints { ratio: (width, value.ratio.1), ..value })); + self.edits.extend(height.map(|height| BoxConstraints { ratio: (value.ratio.0, height), ..value })); + let centre = Checkbox::new(Bind::given(value.from_center)).label(words.from_center.clone()).show(ui).events; + self.edits.extend(centre.map(|from_center| BoxConstraints { from_center, ..value })); + self.out_of_range = fixed_edits.alerts.into_iter().chain(ratio_edits.alerts).flatten().last(); + } +} + +/// The edits and range alerts of a W and H pair. +#[derive(Default)] +struct Pair { + edits: [EditEvents>; 2], + alerts: [Option; 2], +} + +/// The W and H fields of `cells`, each under its short glyph and enabled while `on`. +fn pair(ui: &mut Ui, words: &ShapeWords, cells: &[NumberCell; 2], on: bool) -> Pair { + let mut out = Pair::default(); + let glyphs: [&Label; 2] = [&words.width, &words.height]; + for (index, (cell, glyph)) in cells.iter().zip(glyphs).enumerate() { + let shown = Field::new(cell.caption.clone()).id_salt(cell.key).side(glyph_side(glyph)).enabled(on).show(ui, |ui, _| cell.show_optional(ui)); + (out.edits[index], out.alerts[index]) = shown.inner.unwrap_or_default(); + } + out +} diff --git a/crates/draw/src/ui/shape/mod.rs b/crates/draw/src/ui/shape/mod.rs index 481e7a238..950c26dc6 100644 --- a/crates/draw/src/ui/shape/mod.rs +++ b/crates/draw/src/ui/shape/mod.rs @@ -8,10 +8,11 @@ use serde::{Deserialize, Serialize}; use crate::stroke::StrokeStyle; -use super::shape_drag::{ShapeDragOptions, ShapeKind}; +use super::shape_drag::{BoxConstraints, ShapeDragOptions, ShapeKind}; mod arrowheads; mod bar; +mod gear; mod mode_select; mod panel; #[cfg(feature = "screen-testing")] @@ -21,6 +22,7 @@ mod words; pub use arrowheads::{ArrowheadsOutput, ArrowheadsPopover}; pub use bar::{ShapeBar, ShapeBarOutput}; +pub use gear::{BoxConstraintsOutput, BoxConstraintsPopover}; pub use mode_select::VectorModeSelect; pub use panel::{ChipSetup, ShapePaint, ShapePanel, ShapePanelOutput}; pub use stroke_cells::StrokeCells; @@ -269,7 +271,7 @@ impl ShapeChange { } /// The Shape tools' persisted options-bar values (Fill on, radius, sides, star ratio, weight, -/// stroke width); the paints are the app's foreground and background. +/// stroke width, the Rectangle's gear); the paints are the app's foreground and background. #[derive(Clone, Copy, Debug, PartialEq, Serialize, Deserialize)] #[serde(default, rename_all = "camelCase")] #[non_exhaustive] @@ -288,11 +290,13 @@ pub struct ShapeToolSettings { pub stroke_width: f64, /// The Line's arrowheads. pub arrowheads: Arrowheads, + /// The Rectangle's gear ([`BoxConstraintsPopover`]). + pub rect_box: BoxConstraints, } impl Default for ShapeToolSettings { /// Fill on, square corners, 5 sides, a star ratio of 0.5 (production's Star), weight 3, no stroke, no - /// arrowheads. + /// arrowheads, an unconstrained Rectangle. fn default() -> Self { ShapeToolSettings { fill_on: true, @@ -302,13 +306,19 @@ impl Default for ShapeToolSettings { line_weight: 3.0, stroke_width: 0.0, arrowheads: Arrowheads::default(), + rect_box: BoxConstraints::default(), } } } impl ShapeToolSettings { - /// The drag options of `kind` with these values. + /// The drag options of `kind` with these values; the gear's constraints apply to the Rectangle only. pub fn drag_options(&self, kind: ShapeKind) -> ShapeDragOptions { - ShapeDragOptions::new(kind).corner_radius(self.corner_radius).sides(self.sides).star_ratio(self.star_ratio).line_weight(self.line_weight) + let options = + ShapeDragOptions::new(kind).corner_radius(self.corner_radius).sides(self.sides).star_ratio(self.star_ratio).line_weight(self.line_weight); + match kind { + ShapeKind::Rectangle => options.constraints(self.rect_box), + _ => options, + } } } diff --git a/crates/draw/src/ui/shape/stories.rs b/crates/draw/src/ui/shape/stories.rs index c1f6d2cc5..161017b57 100644 --- a/crates/draw/src/ui/shape/stories.rs +++ b/crates/draw/src/ui/shape/stories.rs @@ -2,10 +2,10 @@ //! for the document, and the panel and the bar edit it as an app does: each field's change is applied to the fake. use super::{ - Arrowheads, ArrowheadsPopover, LiveShape, ShapeAppearance, ShapeBar, ShapeChange, ShapePanel, ShapeToolSettings, StrokeCells, VectorMode, - VectorModeSelect, + Arrowheads, ArrowheadsPopover, BoxConstraintsPopover, LiveShape, ShapeAppearance, ShapeBar, ShapeChange, ShapePanel, ShapeToolSettings, + StrokeCells, VectorMode, VectorModeSelect, }; -use crate::ui::shape_drag::ShapeKind; +use crate::ui::shape_drag::{BoxConstraints, ShapeKind}; use craft_behavior::EditEvent; use craft_color::ui::Paint; use craft_theme::Srgba; @@ -195,6 +195,18 @@ fn arrowheads_popover_open(ui: &mut Ui, data: &mut StoryData) -> StoryOutput { StoryOutput::default() } +#[story(BoxConstraintsPopover<'static>, covers = [Default], drive = Drive::Never)] +fn box_constraints_popover_open(ui: &mut Ui, data: &mut StoryData) -> StoryOutput { + let value = *data.value("box", BoxConstraints::default()); + let mut open = *data.value("open", true); + let out = BoxConstraintsPopover::new(Bind::given(value), Bind::bound(&mut open), DOCUMENT_PIXELS_PER_INCH).show(ui); + *data.value("open", true) = open; + if let Some(next) = out.edits.iter().rev().find_map(|event| event.value()) { + *data.value("box", BoxConstraints::default()) = *next; + } + StoryOutput::default() +} + #[story(VectorModeSelect<'static>, covers = [Default], drive = Drive::Never)] fn vector_mode_select_greyed_pixels(ui: &mut Ui, data: &mut StoryData) -> StoryOutput { let mode = *data.value("mode", VectorMode::Shape); diff --git a/crates/draw/src/ui/shape/words.rs b/crates/draw/src/ui/shape/words.rs index b62199d35..1700dd0ab 100644 --- a/crates/draw/src/ui/shape/words.rs +++ b/crates/draw/src/ui/shape/words.rs @@ -62,5 +62,25 @@ craft_ui::words! { set_as_default: "shape.setAsDefault" = "Set as default"; /// The tooltip of that check box. set_as_default_tip: "shape.setAsDefaultTip" = "Use these settings for new arrows"; + /// The Rectangle gear's button: its accessible name and tooltip. + shape_options: "shape.shapeOptions" = "Set additional shape and path options"; + /// The gear's radio that draws the dragged box. + unconstrained: "shape.unconstrained" = "Unconstrained"; + /// The gear's radio that draws a square. + square: "shape.square" = "Square"; + /// The gear's radio that draws a box of a fixed width and height. + fixed_size: "shape.fixedSize" = "Fixed Size"; + /// The gear's radio that draws a box of a fixed width to height ratio. + proportional: "shape.proportional" = "Proportional"; + /// The gear's check box that draws from the press as the centre. + from_center: "shape.fromCenter" = "From Center"; + /// The Fixed Size width field's accessible name. + fixed_width: "shape.fixedWidth" = "Fixed Size W"; + /// The Fixed Size height field's accessible name. + fixed_height: "shape.fixedHeight" = "Fixed Size H"; + /// The Proportional width field's accessible name. + ratio_width: "shape.ratioWidth" = "Proportional W"; + /// The Proportional height field's accessible name. + ratio_height: "shape.ratioHeight" = "Proportional H"; } } diff --git a/crates/draw/tests/it/shape_cells.rs b/crates/draw/tests/it/shape_cells.rs index 546114e2d..ef51c208c 100644 --- a/crates/draw/tests/it/shape_cells.rs +++ b/crates/draw/tests/it/shape_cells.rs @@ -1,8 +1,11 @@ -//! The shape cells: the arrowhead rows behind the popover and the Shape panel, and the panel's rows by shape. +//! The shape cells: the arrowhead rows behind the popover and the Shape panel, the panel's rows by shape, and the +//! Rectangle's gear. use craft_behavior::EditEvent; use craft_color::ui::Paint; -use craft_draw::ui::{Arrowheads, ArrowheadsPopover, LiveShape, ShapeAppearance, ShapeChange, ShapePanel}; +use craft_draw::ui::{ + Arrowheads, ArrowheadsPopover, BoxConstraint, BoxConstraints, BoxConstraintsPopover, LiveShape, ShapeAppearance, ShapeChange, ShapePanel, +}; use craft_ui::contract::Bind; use craft_ui::testing::{Fixture, trial}; use craft_ui::text::RangeAlert; @@ -195,3 +198,63 @@ fn a_triangle_shows_its_size_and_one_corner_radius() { let radius = host.borrow().events.iter().rev().find_map(|event| event.value().cloned()); assert_eq!(radius, Some(ShapeChange::CornerRadius(12.0)), "the radius is one radius for every corner"); } + +struct GearHost { + value: BoxConstraints, + open: bool, + alerts: Vec, +} + +/// A fixture with the gear open over `value`. +fn gear(value: BoxConstraints) -> (Fixture, Rc>) { + let host = Rc::new(RefCell::new(GearHost { 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 GearHost { value, open, alerts } = &mut *held; + let out = BoxConstraintsPopover::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); + (fixture, host) +} + +fn disabled(fixture: &Fixture, label: &str) -> bool { + let field = fixture.by_label(label).unwrap_or_else(|| panic!("the {label} field")); + fixture.tree().is_disabled_key(field.node) +} + +#[trial] +fn the_gear_enables_only_the_chosen_sizes_and_escape_keeps_the_pick() { + let (mut fixture, host) = gear(BoxConstraints::default()); + for field in ["Fixed Size W", "Fixed Size H", "Proportional W", "Proportional H"] { + assert!(disabled(&fixture, field), "Unconstrained: {field} is off"); + } + let fixed = fixture.by_label("Fixed Size").expect("the Fixed Size radio"); + fixture.click(&fixed).settle(); + assert_eq!(host.borrow().value.constraint, BoxConstraint::FixedSize, "the radio picks Fixed Size"); + assert!(!disabled(&fixture, "Fixed Size W") && !disabled(&fixture, "Fixed Size H"), "Fixed Size: its fields are on"); + assert!(disabled(&fixture, "Proportional W") && disabled(&fixture, "Proportional H"), "Fixed Size: Proportional's stay off"); + fixture.key(Key::Escape, Modifiers::NONE).settle(); + assert!(!host.borrow().open, "Escape closes the gear"); + assert_eq!(host.borrow().value.constraint, BoxConstraint::FixedSize, "Escape keeps the pick"); +} + +#[trial] +fn a_fixed_size_typed_past_its_range_keeps_the_end_and_reports_it_once_and_an_emptied_field_is_empty() { + let mut start = BoxConstraints::default(); + start.constraint = BoxConstraint::FixedSize; + start.fixed = (Some(100.0), Some(50.0)); + let (mut fixture, host) = gear(start); + type_into(&mut fixture, "Fixed Size W", "400000"); + assert_eq!(host.borrow().value.fixed, (Some(300_000.0), Some(50.0)), "a width past 300,000 keeps 300,000"); + assert_eq!(host.borrow().alerts.len(), 1, "the clamp is reported once"); + type_into(&mut fixture, "Fixed Size W", ""); + assert_eq!(host.borrow().value.fixed, (None, Some(50.0)), "an emptied field is empty"); + assert_eq!(host.borrow().alerts.len(), 1, "an emptied field reports nothing"); +} From 25d5a945912431e83ec53e7282dca4bc13c1604b Mon Sep 17 00:00:00 2001 From: aiongg Date: Mon, 16 Nov 2026 12:55:09 +0000 Subject: [PATCH 4/5] merge-c2: 1.4b: the gear popover is a Chrome surface in the level table --- crates/ui-contract/src/contract/level_table.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/crates/ui-contract/src/contract/level_table.rs b/crates/ui-contract/src/contract/level_table.rs index d961ff363..67e3be6a9 100644 --- a/crates/ui-contract/src/contract/level_table.rs +++ b/crates/ui-contract/src/contract/level_table.rs @@ -15,6 +15,7 @@ pub(super) const SURFACES: &[(&str, &str, Surface)] = &[ ("craft_color", "SampleSizeSelect", Chrome), ("craft_draw", "AddBesideHandles", Canvas), ("craft_draw", "ArrowheadsPopover", Chrome), + ("craft_draw", "BoxConstraintsPopover", Chrome), ("craft_draw", "ArtboardOverlay", Canvas), ("craft_draw", "BezierOverlay", Canvas), ("craft_draw", "ConnectorHandles", Canvas), From 45273b3a7460a1d60e4c606d898d7734c07d7480 Mon Sep 17 00:00:00 2001 From: aiongg Date: Mon, 16 Nov 2026 12:58:36 +0000 Subject: [PATCH 5/5] merge-c2: stroke width range alert: StrokeCells::show reports StrokeCellsOutput { events, out_of_range }; the Shape bar passes it through Cells::alert; trial case in shape_cells --- crates/draw/src/ui/mod.rs | 4 ++- crates/draw/src/ui/shape/bar.rs | 8 ++++-- crates/draw/src/ui/shape/mod.rs | 2 +- crates/draw/src/ui/shape/stories.rs | 4 ++- crates/draw/src/ui/shape/stroke_cells.rs | 27 +++++++++++++++--- crates/draw/tests/it/shape_cells.rs | 35 ++++++++++++++++++++++-- 6 files changed, 67 insertions(+), 13 deletions(-) diff --git a/crates/draw/src/ui/mod.rs b/crates/draw/src/ui/mod.rs index de12947b8..388a018c4 100644 --- a/crates/draw/src/ui/mod.rs +++ b/crates/draw/src/ui/mod.rs @@ -97,7 +97,9 @@ pub use pen::{ PathSelection, PenStyle, }; pub use picture_fill::{PictureFill, PictureFillEditor, PictureFillOutput, PictureMode}; -pub use shape::{Arrowheads, ArrowheadsOutput, ArrowheadsPopover, LINE_WEIGHT_RANGE, RADIUS_RANGE, StrokeCells, VectorMode, VectorModeSelect}; +pub use shape::{ + Arrowheads, ArrowheadsOutput, ArrowheadsPopover, LINE_WEIGHT_RANGE, RADIUS_RANGE, StrokeCells, StrokeCellsOutput, VectorMode, VectorModeSelect, +}; pub use shape::{BoxConstraintsOutput, BoxConstraintsPopover}; pub use shape::{ ChipSetup, LiveShape, ShapeAppearance, ShapeBar, ShapeBarOutput, ShapeChange, ShapePaint, ShapePanel, ShapePanelOutput, ShapeToolSettings, diff --git a/crates/draw/src/ui/shape/bar.rs b/crates/draw/src/ui/shape/bar.rs index 641234871..28d3a6673 100644 --- a/crates/draw/src/ui/shape/bar.rs +++ b/crates/draw/src/ui/shape/bar.rs @@ -28,7 +28,8 @@ pub struct ShapeBarOutput { 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)>, - /// The kind cell's (Radius, Sides, Weight) value was typed out of range. + /// A value typed past its cell's range this pass (the stroke width, or the kind's Radius, Sides or Weight), which + /// the field clamped: the app answers with its range alert. pub out_of_range: Option, } @@ -138,8 +139,9 @@ impl<'a> ShapeBar<'a> { if let Some(open) = stroke_open.as_mut() { cells = cells.stroke_open(open.reborrowed()); } - let events = cells.show(ui); - pass.events.extend(events); + let out = cells.show(ui); + pass.events.extend(out.events); + pass.alert(out.out_of_range); } let cell = match kind { ShapeKind::Rectangle | ShapeKind::RoundedRectangle => Some(( diff --git a/crates/draw/src/ui/shape/mod.rs b/crates/draw/src/ui/shape/mod.rs index 950c26dc6..6d5512c56 100644 --- a/crates/draw/src/ui/shape/mod.rs +++ b/crates/draw/src/ui/shape/mod.rs @@ -25,7 +25,7 @@ pub use bar::{ShapeBar, ShapeBarOutput}; pub use gear::{BoxConstraintsOutput, BoxConstraintsPopover}; pub use mode_select::VectorModeSelect; pub use panel::{ChipSetup, ShapePaint, ShapePanel, ShapePanelOutput}; -pub use stroke_cells::StrokeCells; +pub use stroke_cells::{StrokeCells, StrokeCellsOutput}; pub use words::ShapeWords; /// The drawing mode of the vector tools. diff --git a/crates/draw/src/ui/shape/stories.rs b/crates/draw/src/ui/shape/stories.rs index 161017b57..6684ec9ea 100644 --- a/crates/draw/src/ui/shape/stories.rs +++ b/crates/draw/src/ui/shape/stories.rs @@ -173,7 +173,9 @@ fn stroke_cells_with(ui: &mut Ui, data: &mut StoryData, corners: bool) -> StoryO let appearance = data.value("look", look()).clone(); let mut open = *data.value("open", false); let events = Bar::new("Stroke options", BarKind::Options) - .show(ui, |ui, _| StrokeCells::new(&appearance, DOCUMENT_PIXELS_PER_INCH).corners(corners).stroke_open(Bind::bound(&mut open)).show(ui)) + .show(ui, |ui, _| { + StrokeCells::new(&appearance, DOCUMENT_PIXELS_PER_INCH).corners(corners).stroke_open(Bind::bound(&mut open)).show(ui).events + }) .inner .unwrap_or_default(); *data.value("open", false) = open; diff --git a/crates/draw/src/ui/shape/stroke_cells.rs b/crates/draw/src/ui/shape/stroke_cells.rs index 02e46a629..93c35ab9c 100644 --- a/crates/draw/src/ui/shape/stroke_cells.rs +++ b/crates/draw/src/ui/shape/stroke_cells.rs @@ -8,6 +8,7 @@ use craft_behavior::{EditEvent, EditEvents}; use craft_ui::contract::{Bind, LabelKind, Props as _, Show as _, ShowContainer as _, SideLabel, Sizing}; use craft_ui::form::Field; use craft_ui::runtime::env::{Env, Units}; +use craft_ui::text::RangeAlert; use egui::Ui; /// The largest stroke width in pixels. @@ -53,6 +54,7 @@ pub(super) fn style_button( /// - The width is 0 to 288 px; 0 is no stroke, and an edit that turns the stroke on carries /// [`ShapeChange::ENABLED_STROKE_WIDTH`]. /// - The style button shows only with [`stroke_open`](Self::stroke_open) and is off while the width is 0. +/// - A width typed past its range is clamped and reported once in [`StrokeCellsOutput::out_of_range`]. #[must_use = "show the cells with `show`"] #[derive(craft_ui::contract::Pattern)] pub struct StrokeCells<'a> { @@ -81,18 +83,35 @@ impl<'a> StrokeCells<'a> { } /// Adds the cells to the bar's row `ui` and reports each change. - pub fn show(self, ui: &mut Ui) -> Vec> { + pub fn show(self, ui: &mut Ui) -> StrokeCellsOutput { let StrokeCells { appearance, pixels_per_inch, corners, mut stroke_open } = self; let env = Env::of(ui); let (words, units) = (env.words::(), env.units()); let mut events = Vec::new(); let cell = width_cell(&words, &units, pixels_per_inch, appearance); let hidden = SideLabel::new().kind(LabelKind::Hidden); - let width = Field::new(cell.caption.clone()).id_salt(cell.key).side(hidden).sizing(Sizing::Content).show(ui, |ui, _| cell.show(ui).0).inner; - take_width(appearance, width.unwrap_or_default(), &mut events); + let (width, out_of_range) = Field::new(cell.caption.clone()) + .id_salt(cell.key) + .side(hidden) + .sizing(Sizing::Content) + .show(ui, |ui, _| cell.show(ui)) + .inner + .unwrap_or_default(); + take_width(appearance, width, &mut events); if let Some(open) = stroke_open.as_mut() { style_button(ui, &words, appearance, open, corners, &mut events); } - events + StrokeCellsOutput { events, out_of_range } } } + +/// What [`StrokeCells::show`] reports. +#[derive(Debug, Default)] +#[non_exhaustive] +#[must_use = "the app routes each change to the selected layer or the tool settings"] +pub struct StrokeCellsOutput { + /// Each changed cell this pass, in order. + pub events: Vec>, + /// A width typed past its range this pass, which the field clamped: the app answers with its range alert. + pub out_of_range: Option, +} diff --git a/crates/draw/tests/it/shape_cells.rs b/crates/draw/tests/it/shape_cells.rs index ef51c208c..e7c78e604 100644 --- a/crates/draw/tests/it/shape_cells.rs +++ b/crates/draw/tests/it/shape_cells.rs @@ -4,9 +4,11 @@ use craft_behavior::EditEvent; use craft_color::ui::Paint; use craft_draw::ui::{ - Arrowheads, ArrowheadsPopover, BoxConstraint, BoxConstraints, BoxConstraintsPopover, LiveShape, ShapeAppearance, ShapeChange, ShapePanel, + Arrowheads, ArrowheadsPopover, BoxConstraint, BoxConstraints, BoxConstraintsPopover, LiveShape, ShapeAppearance, ShapeBar, ShapeChange, + ShapeKind, ShapePanel, ShapeToolSettings, }; -use craft_ui::contract::Bind; +use craft_ui::bars::{Bar, BarKind}; +use craft_ui::contract::{Bind, ShowContainer as _}; use craft_ui::testing::{Fixture, trial}; use craft_ui::text::RangeAlert; use egui::{Key, Modifiers, vec2}; @@ -129,7 +131,7 @@ struct AlertingPanel { } #[trial] -fn a_concavity_typed_past_its_range_keeps_the_end_and_reports_it_once_from_the_popover_and_the_panel() { +fn a_concavity_or_stroke_width_typed_past_its_range_keeps_the_end_and_reports_it_once() { 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| { @@ -172,6 +174,33 @@ fn a_concavity_typed_past_its_range_keeps_the_end_and_reports_it_once_from_the_p 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"); + + // The Shape bar's stroke width (StrokeCells, which the Pen tools' bar shares) reports its clamp the same way. + 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 { look, alerts, .. } = &mut *held; + let settings = ShapeToolSettings::default(); + let out = + Bar::new("Shape options", BarKind::Options).show(ui, |ui, _| ShapeBar::new(ShapeKind::Rectangle, &settings, look, 72.0).show(ui)).inner; + let Some(out) = out else { return }; + if let Some(ShapeChange::StrokeWidth(width)) = out.events.iter().rev().find_map(|event| event.value()) { + look.stroke_width = *width; + } + alerts.extend(out.out_of_range); + }); + fixture.settle(); + type_into(&mut fixture, "Stroke width", "400"); + assert_eq!(host.borrow().look.stroke_width, 288.0, "bar: a stroke width past 288 px keeps 288"); + assert_eq!(host.borrow().alerts.len(), 1, "bar: the clamp is reported once"); + type_into(&mut fixture, "Stroke width", "12"); + assert_eq!(host.borrow().look.stroke_width, 12.0, "bar: a stroke width in range"); + assert_eq!(host.borrow().alerts.len(), 1, "bar: a value in range reports nothing"); } #[trial]