mirror of
https://github.com/storytold/craft-libs.git
synced 2026-12-01 10:48:15 +01:00
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 <noreply@anthropic.com>
This commit is contained in:
1 parent
a15005e4f9
commit
b93efe6da4
2 files changed
+43
-5
No files matched your search
@@ -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<u16>,
|
||||
/// 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<K> {
|
||||
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<K>>,
|
||||
groups: &[IconPickerGroup<K>],
|
||||
(group_display, empty, search, natural_height, max_rows): (IconPickerGroups, Option<&Label>, IconPickerSearch, bool, Option<u16>),
|
||||
(group_display, empty, search, natural_height, max_rows, columns): (
|
||||
IconPickerGroups,
|
||||
Option<&Label>,
|
||||
IconPickerSearch,
|
||||
bool,
|
||||
Option<u16>,
|
||||
usize,
|
||||
),
|
||||
) -> IconPickerOutput<K> {
|
||||
let env = Env::of(ui);
|
||||
let words = env.words::<PickerWords>();
|
||||
@@ -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))
|
||||
|
||||
@@ -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]
|
||||
|
||||
Reference in new issue
Block a user