diff --git a/crates/color-ui/src/ui/swatches/library.rs b/crates/color-ui/src/ui/swatches/library.rs index caa848c68..f8209be3e 100644 --- a/crates/color-ui/src/ui/swatches/library.rs +++ b/crates/color-ui/src/ui/swatches/library.rs @@ -136,11 +136,19 @@ pub(super) fn group_rename(groups: &[SwatchGroup], index: usize, text: &str) -> (!name.is_empty() && !groups.iter().enumerate().any(|(i, g)| i != index && g.name == name)).then(|| name.to_owned()) } -/// The flat rows of the browser: each group's header, then its swatches. +/// Whether the browser shows a header row for `group`: every group but the one with an empty name, which holds the +/// swatches outside every group. +fn has_header(group: &SwatchGroup) -> bool { + !group.name.is_empty() +} + +/// The flat rows of the browser: each group's header (none for the swatches outside every group), then its swatches. fn flat<'a>(groups: impl IntoIterator) -> Vec { let mut rows = Vec::new(); for group in groups { - rows.push(SwatchKey::Group(group.name.clone())); + if has_header(group) { + rows.push(SwatchKey::Group(group.name.clone())); + } rows.extend(group.entries.iter().map(|e| SwatchKey::Swatch(e.name.clone()))); } rows @@ -167,10 +175,21 @@ fn resolve_move(groups: &[SwatchGroup], names: &[&str], to: usize, parent: Optio (group < groups.len()).then_some((group, slot)) } -/// The slot groups `names` land in when dropped at flat row `to`, counted in the groups that stay. +/// The slot groups `names` land in when dropped at flat row `to`, counted in the groups that stay: each staying group +/// whose first row (its header, or its first swatch when it has none) is above `to`. Groups never land above the +/// swatches outside every group when those come first. fn group_slot(groups: &[SwatchGroup], names: &[&str], to: usize) -> usize { - let remaining = flat(groups.iter().filter(|g| !names.contains(&g.name.as_str()))); - remaining[..to.min(remaining.len())].iter().filter(|k| matches!(k, SwatchKey::Group(_))).count() + let mut row = 0; + let mut slot = 0; + let mut staying = groups.iter().filter(|g| !names.contains(&g.name.as_str())).peekable(); + let floor = usize::from(staying.peek().is_some_and(|g| !has_header(g))); + for group in staying { + if row < to { + slot += 1; + } + row += usize::from(has_header(group)) + group.entries.len(); + } + slot.max(floor) } /// The `at` of each edit that moves `moved` (in library order) one by one within `order`, so that they end up together @@ -352,8 +371,8 @@ impl SwatchLibrary { } /// Applies a reorder of the browser: swatches go into a group at a place, or groups to a place among the - /// groups. `to` counts in the flat rows (a header, then its swatches) that remain once the moved ones are - /// taken out. Anything else leaves the library as it is. + /// groups. `to` counts in the flat rows (a header, then its swatches; no header for the group with an empty + /// name) that remain once the moved ones are taken out. Anything else leaves the library as it is. pub fn apply_reorder(&mut self, mv: &ReorderMove) -> bool { if mv.copy { return false; @@ -641,17 +660,24 @@ fn source_rows(inputs: &SwatchInputs<'_>, env: &Env) -> Vec } /// The rows the Swatches panel lists for `groups`: each group's header, then its swatches, each named by its name and -/// colour value (the name alone for a swatch without a colour). +/// colour value (the name alone for a swatch without a colour). A group with an empty name holds the swatches outside +/// every group: its swatches show at the top level, with no header. pub fn swatch_rows(groups: &[SwatchGroup], words: &SwatchesWords) -> Vec> { let mut rows = Vec::with_capacity(groups.len() + groups.iter().map(|g| g.entries.len()).sum::()); for group in groups { - rows.push(PresetRow::group(SwatchKey::Group(group.name.clone()), group.name.as_str(), 0).count(group.entries.len())); + let header = has_header(group); + let depth = usize::from(header); + if header { + rows.push(PresetRow::group(SwatchKey::Group(group.name.clone()), group.name.as_str(), 0).count(group.entries.len())); + } for entry in &group.entries { let named = match entry.color() { Some(color) => format!("{}, {}", entry.name, value_text(color, words.book_color.as_str())), None => entry.name.clone(), }; - rows.push(PresetRow::item(SwatchKey::Swatch(entry.name.clone()), entry.name.as_str(), 1).tooltip(named.clone()).accessible_name(named)); + rows.push( + PresetRow::item(SwatchKey::Swatch(entry.name.clone()), entry.name.as_str(), depth).tooltip(named.clone()).accessible_name(named), + ); } } rows @@ -676,6 +702,27 @@ pub(crate) mod tests { SwatchGroup { name: name.to_owned(), entries: entries.iter().map(|n| SwatchEntry::new((*n).to_owned(), SwatchColor::Gray(0.5))).collect() } } + /// The swatches outside every group (the group with an empty name) have no header row, so a place below them + /// counts them as rows: a group dropped there lands after them, and a swatch dropped there joins them. A group + /// dropped above them lands after them too. + #[trial] + fn drops_below_the_ungrouped_swatches_land_after_them() { + let library = SwatchLibrary::new(vec![group("", &["A", "B"]), group("G", &["C"]), group("H", &["D"])]); + let rows: Vec<_> = swatch_rows(&library.groups, &SwatchesWords::default()).into_iter().map(|row| row.key).collect(); + assert_eq!(rows, flat(&library.groups), "the reorder counts the rows the browser shows"); + + for to in [0, 2] { + let mut moved = library.clone(); + assert!(moved.apply_reorder(&ReorderMove::new(SwatchKey::Group("H".to_owned()), vec![SwatchKey::Group("H".to_owned())], to, false))); + let order: Vec<_> = moved.groups.iter().map(|g| g.name.as_str()).collect(); + assert_eq!(order, ["", "H", "G"], "H dropped at row {to} lands below the ungrouped swatches, above G"); + } + + let mut moved = library.clone(); + assert!(moved.apply_reorder(&ReorderMove::new(SwatchKey::Swatch("C".to_owned()), vec![SwatchKey::Swatch("C".to_owned())], 2, false))); + assert_eq!(moved.find("C"), Some(SwatchAt::new(0, 2)), "C joins the ungrouped swatches, after B"); + } + /// Edits that move several items down or up give the order the browser's own reorder gives. #[trial] fn multi_item_moves_apply_as_the_reorder_does() { diff --git a/crates/color-ui/tests/it/panels.rs b/crates/color-ui/tests/it/panels.rs index d69987bb2..bc8681a74 100644 --- a/crates/color-ui/tests/it/panels.rs +++ b/crates/color-ui/tests/it/panels.rs @@ -595,6 +595,30 @@ fn swatches_panel_reports_each_edit_and_leaves_the_library() { assert_eq!(state.rename, Some(SwatchKey::Swatch("Color Swatch 2".to_owned())), "the new swatch opens for renaming"); } +/// A group with an empty name holds the swatches outside every group: they show at the top with no header, so no +/// heading or disclosure button of the panel is left without a name, and a named group keeps its header. +#[trial] +fn swatches_outside_every_group_show_without_a_header() { + let entry = |name: &str, rgb: [f32; 3]| craft_color_ui::palette::SwatchEntry::new(name.to_owned(), SwatchColor::Rgb(rgb)); + let groups = vec![ + SwatchGroup { name: String::new(), entries: vec![entry("Red", [1.0, 0.0, 0.0]), entry("Blue", [0.0, 0.0, 1.0])] }, + SwatchGroup { name: "Other".to_owned(), entries: vec![entry("Green", [0.0, 1.0, 0.0])] }, + ]; + let (fixture, _) = swatches_rig(groups, SwatchesState::default()); + let tree = fixture.tree(); + let unnamed: Vec<_> = tree + .keys() + .into_iter() + .filter(|&key| matches!(tree.role_of(key), Some(Role::HEADING | Role::BUTTON))) + .filter(|&key| tree.name_of(key).is_none_or(|name| name.is_empty() || name.starts_with(','))) + .map(|key| (tree.role_of(key), tree.name_of(key), tree.bounds_of(key))) + .collect(); + assert!(unnamed.is_empty(), "every heading and button is named, starting with a label: {unnamed:?}"); + assert!(fixture.by_label("Red, #ff0000").is_some(), "the ungrouped swatches show"); + let headings: Vec<_> = tree.with_role(Role::HEADING).into_iter().filter_map(|key| tree.name_of(key)).collect(); + assert!(headings.iter().any(|name| name.starts_with("Other")), "a named group keeps its header: {headings:?}"); +} + /// Tb-8 (5): a click uses any swatch, as production applied every one: a colour swatch reports its colour, and a /// gradient, pattern or None swatch reports where it is with no colour, for the host to apply its paint. The panel /// reported only colour swatches.