mirror of
https://github.com/storytold/craft-libs.git
synced 2026-11-26 09:08:11 +00:00
ui-widgets: Select::picture_aspect sets the width of an entry's picture in its row and on the face; square stays the default
This commit is contained in:
@@ -2,7 +2,7 @@
|
||||
|
||||
use super::dress::{RowDress, SelectPicture};
|
||||
use crate::contract::{Entry, Props as _, TooltipSpec};
|
||||
use crate::list::{ListRow, ThumbSize};
|
||||
use crate::list::{ListRow, ThumbAspect, ThumbSize};
|
||||
use crate::runtime::env::Label;
|
||||
use craft_behavior::{DisplayOrder, ItemSource};
|
||||
use std::borrow::Cow;
|
||||
@@ -20,13 +20,14 @@ impl<'e, 'l, T> EntryRows<'e, 'l, T> {
|
||||
}
|
||||
|
||||
/// The row of the entry at `at`: its label and icon as an option, checked when it is the `chosen` entry, a caption
|
||||
/// for a heading, a separator row for a separator, the host's picture as the thumbnail and its dress over the rest.
|
||||
/// for a heading, a separator row for a separator, the host's picture as the thumbnail at its aspect and its dress
|
||||
/// over the rest.
|
||||
pub(super) fn row<'r>(
|
||||
&self,
|
||||
at: usize,
|
||||
chosen: Option<usize>,
|
||||
dress: Option<&RowDress<'r, T>>,
|
||||
picture: Option<&SelectPicture<'r, T>>,
|
||||
picture: Option<(&SelectPicture<'r, T>, ThumbAspect)>,
|
||||
) -> ListRow<'r>
|
||||
where
|
||||
'e: 'r,
|
||||
@@ -42,9 +43,9 @@ impl<'e, 'l, T> EntryRows<'e, 'l, T> {
|
||||
None => row,
|
||||
};
|
||||
let row = match picture {
|
||||
Some(picture) => {
|
||||
Some((picture, aspect)) => {
|
||||
let picture = picture.clone();
|
||||
row.thumb(ThumbSize::Small, move |cx, rect| picture.paint(value, cx, rect))
|
||||
row.thumb_aspect(ThumbSize::Small, aspect, move |cx, rect| picture.paint(value, cx, rect))
|
||||
}
|
||||
None => row,
|
||||
};
|
||||
|
||||
@@ -111,7 +111,8 @@ impl<'a> HeaderMenu<'a> {
|
||||
///
|
||||
/// The face shows the picked entry's [closed label](Entry::closed_label) where it has one; the list, type-ahead and
|
||||
/// the accessible value keep the full label. A [`picture`](Select::picture) draws each entry's thumbnail in its row
|
||||
/// and before the face's text, or alone on the face with [`picture_only`](Select::picture_only).
|
||||
/// and before the face's text, or alone on the face with [`picture_only`](Select::picture_only); the picture is
|
||||
/// square unless [`picture_aspect`](Select::picture_aspect) sets its width.
|
||||
#[derive(crate::contract::Component, crate::contract::Props)]
|
||||
#[component(overlay, probe(Select<'static, u8>))]
|
||||
pub struct Select<'a, T: Value + 'static> {
|
||||
@@ -146,6 +147,9 @@ pub struct Select<'a, T: Value + 'static> {
|
||||
/// The face shows the picked entry's picture alone; its label stays the accessible value.
|
||||
#[prop(flag)]
|
||||
picture_only: bool,
|
||||
/// The picture's width as a multiple of its height, in the rows and on the face; square without it.
|
||||
#[prop]
|
||||
picture_aspect: ThumbAspect,
|
||||
/// The row under the pointer is proposed as the value while the list is open, as the rows the keys reach are;
|
||||
/// closing the list without a pick restores the value.
|
||||
#[prop(flag)]
|
||||
@@ -175,6 +179,7 @@ impl<'a, T: Value + 'static> Select<'a, T> {
|
||||
decorate: None,
|
||||
picture: None,
|
||||
picture_only: false,
|
||||
picture_aspect: ThumbAspect::SQUARE,
|
||||
preview: false,
|
||||
header_menu: None,
|
||||
look: SelectLook::Field,
|
||||
@@ -204,7 +209,7 @@ impl<'a, T: Value + 'static> Select<'a, T> {
|
||||
/// The face of `text`, with the picture's room before it, or the picture alone, when the select has a picture.
|
||||
fn sized_face(&self, text: Label, picture: bool, fit: bool) -> SelectFace<'static> {
|
||||
let face = SelectFace::new(text).fit(fit);
|
||||
if self.picture.is_some() && picture { face.thumb(ThumbAspect::SQUARE, |_, _| {}).name(!self.picture_only) } else { face }
|
||||
if self.picture.is_some() && picture { face.thumb(self.picture_aspect, |_, _| {}).name(!self.picture_only) } else { face }
|
||||
}
|
||||
|
||||
/// Sets the value to the entry at `at` unless it is the value already.
|
||||
@@ -405,7 +410,8 @@ impl<T: Value + 'static> Component for Select<'_, T> {
|
||||
let name = cx.label();
|
||||
let words = cx.words::<PickerWords>();
|
||||
let held = self.position(cx.value());
|
||||
let (searchable, dress, picture) = (self.searchable, self.decorate.as_ref(), self.picture.as_ref());
|
||||
let (searchable, dress, picture) =
|
||||
(self.searchable, self.decorate.as_ref(), self.picture.as_ref().map(|picture| (picture, self.picture_aspect)));
|
||||
let kept =
|
||||
|at: usize| self.search_kept.as_ref().is_some_and(|kept| entries.get(at).and_then(Entry::value).is_some_and(|value| (kept.0)(value)));
|
||||
let (search_label, no_matches, wide) = (self.search_label.clone(), self.no_matches.clone(), self.list_width.is_some());
|
||||
@@ -500,7 +506,7 @@ impl<T: Value + 'static> Component for Select<'_, T> {
|
||||
face = face.leading(icon.clone());
|
||||
}
|
||||
if let (Some(picture), Some(value)) = (self.picture.as_ref(), held.and_then(|at| self.entries[at].value())) {
|
||||
face = face.thumb(ThumbAspect::SQUARE, move |cx, rect| picture.paint(value, cx, rect)).name(!self.picture_only);
|
||||
face = face.thumb(self.picture_aspect, move |cx, rect| picture.paint(value, cx, rect)).name(!self.picture_only);
|
||||
}
|
||||
ui.face(FACE, if muted { face.placeholder() } else { face });
|
||||
}
|
||||
|
||||
@@ -2,10 +2,13 @@
|
||||
//! offers both steps, and a step forward lands on the first stop and a step back on the last.
|
||||
|
||||
use craft_ui::contract::{Bind, Entry, Role, Show as _};
|
||||
use craft_ui::list::ThumbAspect;
|
||||
use craft_ui::pickers::Select;
|
||||
use craft_ui::testing::access::Declared;
|
||||
use craft_ui::testing::{Fixture, Story, StoryData, StoryOutput, trial};
|
||||
use egui::{Key, Modifiers, Ui};
|
||||
use egui::{Key, Modifiers, Rect, Ui};
|
||||
use std::cell::RefCell;
|
||||
use std::rc::Rc;
|
||||
|
||||
/// Entries 1 to 4; 2 and 4 are reached from the open list only.
|
||||
fn select(ui: &mut Ui, data: &mut StoryData) -> StoryOutput {
|
||||
@@ -50,6 +53,44 @@ fn the_closed_face_shows_an_entrys_closed_label_and_the_accessible_value_keeps_i
|
||||
assert_eq!(fixture.tree().value_of(node), Some("1 Dots"), "the accessible value is the full label");
|
||||
}
|
||||
|
||||
/// The rects a select's picture is painted into: on the closed face, then while the list is open (its rows and the
|
||||
/// face), with the picture `aspect` times as wide as high, or with no aspect set.
|
||||
fn picture_rects(aspect: Option<ThumbAspect>) -> (Vec<Rect>, Vec<Rect>) {
|
||||
let painted = Rc::new(RefCell::new(Vec::new()));
|
||||
let into = Rc::clone(&painted);
|
||||
let mut fixture = Fixture::new(move |ui| {
|
||||
let entries = [Entry::new(1u8, "Uniform"), Entry::new(2u8, "Taper")];
|
||||
let into = Rc::clone(&into);
|
||||
let select = Select::new("Profile", Bind::given(1u8), &entries).picture(move |_, _, rect| into.borrow_mut().push(rect)).picture_only();
|
||||
let select = match aspect {
|
||||
Some(aspect) => select.picture_aspect(aspect),
|
||||
None => select,
|
||||
};
|
||||
select.show(ui);
|
||||
});
|
||||
fixture.settle();
|
||||
let face = painted.take();
|
||||
fixture.key(Key::Tab, Modifiers::NONE);
|
||||
fixture.settle();
|
||||
fixture.key(Key::ArrowDown, Modifiers::ALT);
|
||||
fixture.settle();
|
||||
assert!(!fixture.by_role(Role::LIST_BOX).is_empty(), "Alt+Down opens the list");
|
||||
(face, painted.take())
|
||||
}
|
||||
|
||||
/// A picture aspect widens the picture on the face and in every row of the list; without one the picture is square.
|
||||
#[trial]
|
||||
fn a_picture_aspect_sets_the_pictures_width_on_the_face_and_in_the_rows() {
|
||||
let wide = |rect: &Rect| rect.height() > 0.0 && rect.width() > 3.5 * rect.height();
|
||||
let square = |rect: &Rect| rect.height() > 0.0 && (rect.width() - rect.height()).abs() < 0.5;
|
||||
let (face, rows) = picture_rects(Some(ThumbAspect::new(4, 1)));
|
||||
assert!(!face.is_empty() && face.iter().all(wide), "the face's picture is four times as wide as high: {face:?}");
|
||||
assert!(rows.len() >= 3 && rows.iter().all(wide), "each row's picture is four times as wide as high: {rows:?}");
|
||||
let (face, rows) = picture_rects(None);
|
||||
assert!(!face.is_empty() && face.iter().all(square), "without an aspect the face's picture is square: {face:?}");
|
||||
assert!(rows.len() >= 3 && rows.iter().all(square), "without an aspect each row's picture is square: {rows:?}");
|
||||
}
|
||||
|
||||
/// A value that names no entry shows the placeholder on the face, and the field reads it as its value, as the face
|
||||
/// shows it; a value that names an entry reads that entry. Guards against the shown placeholder leaving the field
|
||||
/// with no value to read.
|
||||
|
||||
Reference in New Issue
Block a user