diff --git a/crates/ui-egui/src/panels/appearance.rs b/crates/ui-egui/src/panels/appearance.rs index 758442c657..3d95d2c28d 100644 --- a/crates/ui-egui/src/panels/appearance.rs +++ b/crates/ui-egui/src/panels/appearance.rs @@ -30,7 +30,7 @@ use craft::ui::contract::{ Bind, CollectionEvent, CollectionResponse, ColumnSpec, FieldLabelRole, ItemCx, ListDrag, MemoProps, PopupKind, Props as _, ReorderMove, ReorderRule, Show as _, ShowCollection as _, ShowContainer as _, SideLabel, ToggleColumn, Trigger, clicked, }; -use craft::ui::display::{Picture, Shade, Text}; +use craft::ui::display::{Elide, Picture, Shade, Text}; use craft::ui::form::Field; use craft::ui::layout_components::Stack; use craft::ui::list::{List, ListRow, RowDragSpot, RowEvent, RowInk, ThumbAspect, ThumbSize, ThumbTarget, ToggleCell, TreeLeaves}; @@ -403,6 +403,9 @@ const SWATCH_COLUMNS: u16 = 12; const SWATCH_ROWS: u16 = 12; /// The width and height of a stroke row's profile picture, as its aspect. const PROFILE_ASPECT: (u16, u16) = (14, 5); +/// The most characters of a stroke's notes that set the notes' column width on every fill and stroke row; longer notes +/// are cut at the end and show whole in a tooltip. +const NOTES_SAMPLE_CHARS: usize = 16; /// The eye column. const EYE: ColumnKey = ColumnKey::new("eye"); @@ -830,23 +833,35 @@ fn item_row<'r>(cx: &'r RowCx<'_>, key: RowKey, i: usize) -> ListRow<'r> { link_output(cx.pass, key), ); // The first trailing part added is the outermost: the profile closes the row, the swatch sits - // nearest the label. - if let AppearanceItem::Stroke(st) = it { - if let Some(points) = st.profile.as_ref().filter(|p| p.preset_id() != Some("uniform")).map(|p| p.points.clone()) { - let aspect = ThumbAspect::new(PROFILE_ASPECT.0, PROFILE_ASPECT.1); - let profile = Picture::painter(Label::Empty, aspect, move |painter, rect| stroke::paint_profile(painter, rect, Some(points.as_slice()))); + // nearest the label. Every fill and stroke row of the stack takes the same room for the profile, the notes and the + // weight, kept empty where the row has none, so all their swatches stand in one column. + let (any_profile, widest_notes) = trailing_room(stack.map_or(&[], |ap| ap.items.as_slice())); + let stroke_layer = match it { + AppearanceItem::Stroke(st) => Some(st), + _ => None, + }; + match stroke_layer.and_then(shown_profile) { + Some(points) => { + let profile = + Picture::painter(Label::Empty, profile_aspect(), move |painter, rect| stroke::paint_profile(painter, rect, Some(points.as_slice()))); row = row.trailing_field(profile, |_| {}); } - if let Some(notes) = stroke_notes(st) { - row = row.trailing_field(Text::new(notes).shade(Shade::Muted), |_| {}); - } + None if any_profile => row = row.trailing_space(Picture::painter(Label::Empty, profile_aspect(), |_, _| {})), + None => {} + } + if let Some(widest) = widest_notes { + row = match stroke_layer.and_then(stroke_notes) { + Some(notes) => row.trailing_field(Text::new(notes).shade(Shade::Muted).sample(widest).elide(Elide::End), |_| {}), + None => row.trailing_space(Text::new(Label::Empty).shade(Shade::Muted).sample(widest)), + }; + } + if let Some(st) = stroke_layer { let target = if new_art { WeightTarget::Shown } else { WeightTarget::Item(i) }; let pass = cx.pass; row = row.trailing_field(stroke::weight_number(cx.app, Some(st.width)), move |out| { pass.borrow_mut().acts.push(Act::Weight(target, Box::new(out))) }); } else { - // A fill row keeps the weight's room empty, so its swatch stands in the stroke rows' swatch column. row = row.trailing_space(stroke::weight_number(cx.app, None)); } let painted = if new_art { Painted::NewArt { fill } } else { Painted::Item(i) }; @@ -880,6 +895,31 @@ fn stroke_notes(st: &vectorcraft_doc::StrokeLayer) -> Option { (!notes.is_empty()).then(|| notes.join(", ")) } +/// The points of a stroke's width profile, when it has one other than Uniform, which its row draws. +fn shown_profile(st: &vectorcraft_doc::StrokeLayer) -> Option> { + st.profile.as_ref().filter(|p| p.preset_id() != Some("uniform")).map(|p| p.points.clone()) +} + +/// The shape of a stroke row's profile picture. +fn profile_aspect() -> ThumbAspect { + ThumbAspect::new(PROFILE_ASPECT.0, PROFILE_ASPECT.1) +} + +/// The trailing room every fill and stroke row of `items` keeps before its weight: whether a stroke there shows a +/// profile, and the longest of the strokes' notes, up to [`NOTES_SAMPLE_CHARS`] characters, which sets the notes' width +/// on every row. +fn trailing_room(items: &[AppearanceItem]) -> (bool, Option) { + let strokes = || { + items.iter().filter_map(|it| match it { + AppearanceItem::Stroke(st) => Some(st), + _ => None, + }) + }; + let any_profile = strokes().any(|st| shown_profile(st).is_some()); + let widest = strokes().filter_map(stroke_notes).max_by_key(|notes| notes.chars().count()); + (any_profile, widest.map(|notes| notes.chars().take(NOTES_SAMPLE_CHARS).collect())) +} + /// An Opacity row: of the object or, under it, of a fill/stroke. "Opacity:" opens the /// Transparency panel on it. A mixed selection whose objects' opacity or blend mode differ /// reads "Mixed". diff --git a/crates/ui-egui/src/panels/graphic_styles.rs b/crates/ui-egui/src/panels/graphic_styles.rs index e99898605f..76b17c33be 100644 --- a/crates/ui-egui/src/panels/graphic_styles.rs +++ b/crates/ui-egui/src/panels/graphic_styles.rs @@ -1574,9 +1574,10 @@ mod tests { let mut p = PanelHarness::new(app, appearance_and_styles); let hold = |p: &mut PanelHarness| { - // The thumbnail is the first part of the object row, at its start. + // The thumbnail is the first part of the object row, after the empty disclosure column that lines the row's + // label up with the Stroke and Fill labels. let row = p.rect("Rectangle: Drop Target"); - let (from, to) = (row.left_center() + egui::vec2(14.0, 0.0), p.rect("Drop Target").center()); + let (from, to) = (row.left_center() + egui::vec2(34.0, 0.0), p.rect("Drop Target").center()); p.drag_hold_between(from, to); let carried = drag::active::(p.ctx()); assert!( diff --git a/crates/ui-egui/src/panels/tests_panel_rows.rs b/crates/ui-egui/src/panels/tests_panel_rows.rs index 2bbe9ad856..ae639d59be 100644 --- a/crates/ui-egui/src/panels/tests_panel_rows.rs +++ b/crates/ui-egui/src/panels/tests_panel_rows.rs @@ -1,6 +1,8 @@ //! Headless frames of panel rows that the framework lays out: controls side by side stay inside the panel's width. -use super::tests_appearance::app_with_rect; +use serde_json::json; + +use super::tests_appearance::{app_with_rect, run}; use super::*; use crate::panel_docking::PANEL_MIN_WIDTH; use crate::tests::panel_harness::{Panel, Place, ROOM, floated}; @@ -115,6 +117,31 @@ fn the_appearance_swatches_and_labels_line_up() { } } +#[test] +fn the_fill_swatch_lines_up_with_a_dashed_stroke_that_has_a_profile() { + let mut panel = Panel::new(app_with_rect(), appearance::show); + run(&mut panel.app(), "stroke.set", json!({"dash": [4, 2], "profile": "lens"})); + panel.frame(); + assert!(panel.shows("Dashed"), "the stroke row shows no notes"); + let (stroke, fill) = (panel.rect("Stroke swatch"), panel.rect("Fill swatch")); + assert!((stroke.left() - fill.left()).abs() < 1.0, "the stroke swatch at {stroke:?} and the fill swatch at {fill:?} are in different columns"); +} + +#[test] +fn long_stroke_notes_stay_inside_a_narrow_appearance_panel() { + let mut panel = Panel::new(app_with_rect(), appearance::show); + let name = "A calligraphic brush with a very long name for the notes"; + run(&mut panel.app(), "brush.new", json!({"type": "calligraphic", "name": name})); + run(&mut panel.app(), "brush.apply", json!({"name": name})); + run(&mut panel.app(), "stroke.set", json!({"dash": [4, 2]})); + panel.room(egui::vec2(PANEL_MIN_WIDTH, ROOM.y)).frame(); + let (stroke, fill) = (panel.rect("Stroke swatch"), panel.rect("Fill swatch")); + assert!((stroke.left() - fill.left()).abs() < 1.0, "the stroke swatch at {stroke:?} and the fill swatch at {fill:?} are in different columns"); + for at in [stroke, fill] { + assert!(at.right() <= PANEL_MIN_WIDTH, "the swatch at {at:?} runs past the {PANEL_MIN_WIDTH} pt panel"); + } +} + #[test] fn align_to_shares_the_distribute_spacing_line_inside_the_align_panel() { let panel = Panel::new(app_with_rect(), align::show);