From b93efe6da430fa35775b9f28555ecda513ec64ed Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Sun, 29 Nov 2026 19:17:37 +0000 Subject: [PATCH] Icon picker: columns asks a content-sized picker for that many cells across (the grid's card_columns) Co-Authored-By: Claude Opus 5.5 --- crates/ui-widgets/src/pickers/icon_picker.rs | 22 +++++++++++++--- .../tests/it/pickers/icon_picker.rs | 26 ++++++++++++++++++- 2 files changed, 43 insertions(+), 5 deletions(-) diff --git a/crates/ui-widgets/src/pickers/icon_picker.rs b/crates/ui-widgets/src/pickers/icon_picker.rs index 2f2a57394d..b04bb87c72 100644 --- a/crates/ui-widgets/src/pickers/icon_picker.rs +++ b/crates/ui-widgets/src/pickers/icon_picker.rs @@ -133,6 +133,10 @@ pub struct IconPicker<'a, K: Clone + Eq + Hash + Send + Sync + 'static> { /// picker in a menu or popover, whose search field stays in view above the rows. #[prop] max_rows: Option, + /// The columns of cells the picker asks for where it takes its width from its content (a dialog sized from its + /// rows): its width holds this many cells. 0 asks for one cell, and the room offered decides the rest. + #[prop] + columns: usize, } /// Whether the icon picker draws its search field. @@ -204,14 +208,16 @@ impl<'a, K: Clone + Eq + Hash + Send + Sync + 'static> IconPicker<'a, K> { search: IconPickerSearch::default(), natural_height: false, max_rows: None, + columns: 0, } } /// Shows the picker and returns what the user did this pass. pub fn show(self, ui: &mut Ui) -> IconPickerOutput { - let IconPicker { label, mut value, groups, group_display, empty, search, natural_height, max_rows } = self; - let shown = Stack::new() - .show(ui, |ui, cx| Self::body(ui, cx, &label, &mut value, groups, (group_display, empty.as_ref(), search, natural_height, max_rows))); + let IconPicker { label, mut value, groups, group_display, empty, search, natural_height, max_rows, columns } = self; + let shown = Stack::new().show(ui, |ui, cx| { + Self::body(ui, cx, &label, &mut value, groups, (group_display, empty.as_ref(), search, natural_height, max_rows, columns)) + }); shown.inner.unwrap_or_else(|| IconPickerOutput { response: shown.output.clone(), picked: None, search_changed: false }) } @@ -223,7 +229,14 @@ impl<'a, K: Clone + Eq + Hash + Send + Sync + 'static> IconPicker<'a, K> { label: &Label, value: &mut Bind<'a, Option>, groups: &[IconPickerGroup], - (group_display, empty, search, natural_height, max_rows): (IconPickerGroups, Option<&Label>, IconPickerSearch, bool, Option), + (group_display, empty, search, natural_height, max_rows, columns): ( + IconPickerGroups, + Option<&Label>, + IconPickerSearch, + bool, + Option, + usize, + ), ) -> IconPickerOutput { let env = Env::of(ui); let words = env.words::(); @@ -312,6 +325,7 @@ impl<'a, K: Clone + Eq + Hash + Send + Sync + 'static> IconPicker<'a, K> { .activate(ActivateOn::Click) .caption(CaptionPlace::None) .content_cells() + .card_columns(columns) .groups(&shown_groups) .collapsible() .selection(Bind::given(selection)) diff --git a/crates/ui-widgets/tests/it/pickers/icon_picker.rs b/crates/ui-widgets/tests/it/pickers/icon_picker.rs index bc6a7e4de1..5e3aa3fcb6 100644 --- a/crates/ui-widgets/tests/it/pickers/icon_picker.rs +++ b/crates/ui-widgets/tests/it/pickers/icon_picker.rs @@ -2,7 +2,8 @@ //! inside the category, and the category outlives a pass. use craft_ui::contract::{Bind, ContainerProps, Props as _, Scroll, ShowContainer as _}; -use craft_ui::pickers::{IconPicker, IconPickerGroup, IconPickerGroups, IconPickerItem}; +use craft_ui::layout_components::Row; +use craft_ui::pickers::{IconPicker, IconPickerGroup, IconPickerGroups, IconPickerItem, IconPickerSearch}; use craft_ui::testing::{Fixture, Room, trial}; use craft_ui_test_kit::body::Slab; @@ -97,6 +98,29 @@ fn a_natural_height_picker_shows_whole_in_a_scrolling_body() { assert!(shows(&fixture, "Move") && shows(&fixture, "Eraser"), "every group's cells show in the scrolling body"); } +/// DeckCraft's Bullets: a picker that takes its width from its content asks for the columns it names, so its cells +/// share a row where it would otherwise stand them one per row. +#[trial] +fn a_content_sized_picker_asks_for_its_columns() { + let rows = |columns: usize| { + let mut fixture = Fixture::new(move |ui| { + let groups = [IconPickerGroup::new("", (1..=4u8).map(|key| IconPickerItem::new(key, format!("Cell {key}"))).collect())]; + let mut value = None; + Row::new().show(ui, |ui, _| { + IconPicker::new("Bullets", Bind::bound(&mut value), &groups).search(IconPickerSearch::None).columns(columns).show(ui); + }); + }); + fixture.in_room(Room::Dialog).settle(); + let top = |name: &str| fixture.by_label(name).unwrap_or_else(|| panic!("{name}")).rect.center().y; + (top("Cell 1"), top("Cell 3"), top("Cell 4")) + }; + let (first, third, fourth) = rows(3); + assert!((third - first).abs() < 1.0, "three columns hold the third cell on the first row"); + assert!(fourth > first + 1.0, "the fourth cell starts the next row"); + let (first, third, _) = rows(0); + assert!(third > first + 1.0, "without columns the content-sized picker is one cell wide"); +} + /// Escape with a query typed in the search field of a picker in a popover: the default field clears the query and the /// popover stays; a field that keeps its query hands the same Escape on, and the popover closes. #[trial]