From a787c30e93dfd2a7df887a2db7dbde3c9070757f Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Sat, 28 Nov 2026 06:21:31 +0000 Subject: [PATCH] icon picker: an opt-in natural height for a picker shown in a scrolling body IconPicker owned its grid's scroll with no way to hand it to the body around it, so an app that showed one in a dialog or menu body that scrolls hit the collection rule (16.3) and crashed in debug builds. natural_height(true) passes the grid's natural height through; the default is unchanged. Trial: a_natural_height_picker_shows_whole_in_a_scrolling_body (fails without the option on the 16.3 assertion). Co-Authored-By: Claude Opus 5.5 --- crates/ui-widgets/src/pickers/icon_picker.rs | 12 +++++++++--- .../ui-widgets/tests/it/pickers/icon_picker.rs | 18 +++++++++++++++++- 2 files changed, 26 insertions(+), 4 deletions(-) diff --git a/crates/ui-widgets/src/pickers/icon_picker.rs b/crates/ui-widgets/src/pickers/icon_picker.rs index a4a4a73c84..707544730e 100644 --- a/crates/ui-widgets/src/pickers/icon_picker.rs +++ b/crates/ui-widgets/src/pickers/icon_picker.rs @@ -126,6 +126,9 @@ pub struct IconPicker<'a, K: Clone + Eq + Hash + Send + Sync + 'static> { /// Whether the picker draws a search field. #[prop] search: IconPickerSearch, + /// The grid takes its full content height and owns no scroll: for a picker shown in a body that scrolls. + #[prop] + natural_height: bool, } /// Whether the icon picker draws its search field. @@ -189,13 +192,15 @@ impl<'a, K: Clone + Eq + Hash + Send + Sync + 'static> IconPicker<'a, K> { group_display: IconPickerGroups::default(), empty: None, search: IconPickerSearch::default(), + natural_height: false, } } /// 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 } = self; - let shown = Stack::new().show(ui, |ui, cx| Self::body(ui, cx, &label, &mut value, groups, (group_display, empty.as_ref(), search))); + let IconPicker { label, mut value, groups, group_display, empty, search, natural_height } = 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))); shown.inner.unwrap_or_else(|| IconPickerOutput { response: shown.output.clone(), picked: None, search_changed: false }) } @@ -207,7 +212,7 @@ 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): (IconPickerGroups, Option<&Label>, IconPickerSearch), + (group_display, empty, search, natural_height): (IconPickerGroups, Option<&Label>, IconPickerSearch, bool), ) -> IconPickerOutput { let env = Env::of(ui); let words = env.words::(); @@ -289,6 +294,7 @@ impl<'a, K: Clone + Eq + Hash + Send + Sync + 'static> IconPicker<'a, K> { .collapsible() .selection(Bind::given(selection)) .rows_stamp(query_stamp(&query, category)) + .natural_height(natural_height) .empty(empty.cloned().unwrap_or_else(|| words.icon_no_match.clone())) .show_items(ui, &source, &mut |at, _| { let item = source.items[*at]; diff --git a/crates/ui-widgets/tests/it/pickers/icon_picker.rs b/crates/ui-widgets/tests/it/pickers/icon_picker.rs index 69692e573b..0b537a92c5 100644 --- a/crates/ui-widgets/tests/it/pickers/icon_picker.rs +++ b/crates/ui-widgets/tests/it/pickers/icon_picker.rs @@ -1,9 +1,10 @@ //! The icon picker's filter row: a category shows only its group's tiles, All shows every group, the search narrows //! inside the category, and the category outlives a pass. -use craft_ui::contract::Bind; +use craft_ui::contract::{Bind, ContainerProps, Props as _, Scroll, ShowContainer as _}; use craft_ui::pickers::{IconPicker, IconPickerGroup, IconPickerGroups, IconPickerItem}; use craft_ui::testing::{Fixture, Room, trial}; +use craft_ui_test_kit::body::Slab; fn groups() -> Vec> { vec![ @@ -80,3 +81,18 @@ fn wide_glyph_cells_fit_the_text_without_growing_into_squares() { let next = wide.by_label("Alternative").expect("the adjacent tile is accessible").rect; assert!(cell.intersect(next).area() <= 0.0, "neighbouring cells do not overlap"); } + +/// A picker shown in a body that scrolls along its rows takes its natural height, so the body scrolls it whole: a +/// picker that kept its own scroll there would share the body's (the collection rule 16.3, asserted in debug builds). +#[trial] +fn a_natural_height_picker_shows_whole_in_a_scrolling_body() { + let mut fixture = Fixture::new(|ui| { + let groups = groups(); + let mut value = None; + Slab::new(ContainerProps::new().scroll(Scroll::vertical())).id_salt("dialog body").show(ui, |ui, _| { + IconPicker::new("Tool", Bind::bound(&mut value), &groups).natural_height(true).show(ui); + }); + }); + fixture.in_room(Room::Dialog).settle(); + assert!(shows(&fixture, "Move") && shows(&fixture, "Eraser"), "every group's cells show in the scrolling body"); +}