mirror of
https://github.com/storytold/craft-libs.git
synced 2026-11-30 18:27:00 +01:00
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 <noreply@anthropic.com>
This commit is contained in:
1 parent
4ca579a638
commit
a787c30e93
2 files changed
+26
-4
No files matched your search
@@ -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<K> {
|
||||
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<K>>,
|
||||
groups: &[IconPickerGroup<K>],
|
||||
(group_display, empty, search): (IconPickerGroups, Option<&Label>, IconPickerSearch),
|
||||
(group_display, empty, search, natural_height): (IconPickerGroups, Option<&Label>, IconPickerSearch, bool),
|
||||
) -> IconPickerOutput<K> {
|
||||
let env = Env::of(ui);
|
||||
let words = env.words::<PickerWords>();
|
||||
@@ -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];
|
||||
|
||||
@@ -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<IconPickerGroup<u8>> {
|
||||
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");
|
||||
}
|
||||
Reference in new issue
Block a user