mirror of
https://github.com/storytold/craft-libs.git
synced 2026-11-17 04:04:25 +00:00
Thumbnail-only picker fields as wide as the thumbnail and chevron: a select face showing its thumbnail alone keeps no text-field widths, and a thumbnail field with no preset shows an empty thumbnail; the export template select sized to its entries
This commit is contained in:
@@ -1,7 +1,7 @@
|
||||
//! `OutputPathRow`: where a render or an export goes, as a template list and the resulting file name as a link.
|
||||
|
||||
use crate::buttons::Link;
|
||||
use crate::contract::{Bind, EditResponse, Entry, NoValue, Outcome, Props as _, Remember as _, Show as _, ShowContainer as _, clicked};
|
||||
use crate::contract::{Bind, EditResponse, Entry, NoValue, Outcome, Props as _, Remember as _, Show as _, ShowContainer as _, Sizing, clicked};
|
||||
use crate::form::Field;
|
||||
use crate::layout_components::Row;
|
||||
use crate::pickers::Select;
|
||||
@@ -105,7 +105,7 @@ impl<'a> OutputPathRow<'a> {
|
||||
Row::new().show(ui, |ui, _| {
|
||||
if !entries.is_empty() {
|
||||
let picked: EditResponse<String> =
|
||||
Select::new(words.templates.clone(), value.reborrowed(), &entries).id_salt("templates").show(ui);
|
||||
Select::new(words.templates.clone(), value.reborrowed(), &entries).id_salt("templates").sizing(Sizing::Content).show(ui);
|
||||
edit.extend(picked.events);
|
||||
}
|
||||
editing = match std::mem::take(&mut editing) {
|
||||
|
||||
@@ -51,7 +51,8 @@ impl<'a> PickerHead<'a> {
|
||||
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
|
||||
#[non_exhaustive]
|
||||
pub enum PickerField {
|
||||
/// The thumbnail alone; the name when the preset has none.
|
||||
/// The thumbnail alone; the name when the preset has no thumbnail, and an empty thumbnail
|
||||
/// with no preset or a mixed value.
|
||||
#[default]
|
||||
Thumbnail,
|
||||
/// The name alone.
|
||||
@@ -281,25 +282,35 @@ impl<'a, K: Value + Eq + Hash + 'static> PresetPicker<'a, K> {
|
||||
}
|
||||
}
|
||||
|
||||
/// `face` with an empty thumbnail of the cell shape, and the name unless the field shows the thumbnail alone.
|
||||
fn empty_thumb<'f>(&self, face: SelectFace<'f>) -> SelectFace<'f> {
|
||||
self.captioned(face.thumb(self.cell_shape.aspect(), |_, _| {}).name(self.field != PickerField::Thumbnail))
|
||||
}
|
||||
|
||||
/// The face of a field showing `text`: with a `row`, its thumbnail and name as `field` says; without, the muted
|
||||
/// look of a mixed value or a placeholder. The measured candidates and the painted face are both built here.
|
||||
/// look of a mixed value or a placeholder, which a thumbnail-only field shows as an empty thumbnail. The measured
|
||||
/// candidates and the painted face are both built here.
|
||||
fn face_of(&self, text: Label, row: Option<&PresetRow<K>>, paint: bool) -> SelectFace<'static> {
|
||||
let Some(row) = row else { return SelectFace::new(text).placeholder() };
|
||||
let face = SelectFace::new(text);
|
||||
match (&row.preview, self.field) {
|
||||
(_, PickerField::Name) | (None, _) => face,
|
||||
(Some(preview), field) => {
|
||||
let draw = paint.then(|| preview.paint.clone());
|
||||
let face = face
|
||||
.thumb(preview.aspect, move |cx, rect| {
|
||||
if let Some(draw) = &draw {
|
||||
draw(cx, rect);
|
||||
}
|
||||
})
|
||||
.name(field == PickerField::ThumbnailAndName);
|
||||
self.captioned(face)
|
||||
}
|
||||
}
|
||||
let face = match (row, self.field) {
|
||||
(None, PickerField::Thumbnail) => self.empty_thumb(face.placeholder()),
|
||||
(None, _) => face.placeholder(),
|
||||
(Some(row), field) => match (&row.preview, field) {
|
||||
(_, PickerField::Name) | (None, _) => face,
|
||||
(Some(preview), field) => {
|
||||
let draw = paint.then(|| preview.paint.clone());
|
||||
let face = face
|
||||
.thumb(preview.aspect, move |cx, rect| {
|
||||
if let Some(draw) = &draw {
|
||||
draw(cx, rect);
|
||||
}
|
||||
})
|
||||
.name(field == PickerField::ThumbnailAndName);
|
||||
self.captioned(face)
|
||||
}
|
||||
},
|
||||
};
|
||||
face
|
||||
}
|
||||
}
|
||||
|
||||
@@ -320,13 +331,8 @@ impl<K: Value + Eq + Hash + 'static> Component for PresetPicker<'_, K> {
|
||||
/// preset, the mixed word and the placeholder, and an empty thumbnail face for the height.
|
||||
fn measure(&mut self, cx: &MeasureCx<'_, Self>) -> Vec2 {
|
||||
let words = cx.words::<PresetWords>();
|
||||
let aspect = self.cell_shape.aspect();
|
||||
let empty = SelectFace::new(Label::Empty);
|
||||
let empty = if self.field == PickerField::Name {
|
||||
empty
|
||||
} else {
|
||||
self.captioned(empty.thumb(aspect, |_, _| {}).name(self.field != PickerField::Thumbnail))
|
||||
};
|
||||
let empty = if self.field == PickerField::Name { empty } else { self.empty_thumb(empty) };
|
||||
let rows = self.rows.iter().filter(|row| !row.is_group());
|
||||
let faces = rows.map(|row| {
|
||||
let (text, _) = self.shown(Some(&Some(row.key.clone())), &words, &cx.env);
|
||||
|
||||
@@ -39,7 +39,7 @@ pub struct SelectFaceStyle {
|
||||
}
|
||||
|
||||
/// The closed box of a picker: `text` elided to the room left of a trailing chevron. The box is as wide as the text
|
||||
/// and the chevron need, at least a field wide. A host shows it as the look of one of its parts with
|
||||
/// and the chevron need, at least a field wide; a thumbnail alone takes only its own width. A host shows it as the look of one of its parts with
|
||||
/// `ui.face(part, SelectFace::new(text))` over the field well it paints itself, and gives the box the width of its
|
||||
/// widest text so the width does not change with the pick. A thumbnail goes before the text, or alone.
|
||||
#[derive(crate::contract::Component, crate::contract::Props)]
|
||||
@@ -57,8 +57,9 @@ pub struct SelectFace<'a> {
|
||||
/// A short line centred under the thumbnail (a brush's size); it shows only with a thumbnail.
|
||||
#[prop(into)]
|
||||
thumb_caption: Option<Label>,
|
||||
/// Whether the text shows beside the thumbnail; `false` shows the thumbnail alone. The text is the face's
|
||||
/// accessible name either way.
|
||||
/// Whether the text shows beside the thumbnail; `false` shows the thumbnail alone, as wide as the thumbnail and
|
||||
/// the chevron need. The text is the face's accessible name either way.
|
||||
#[style_key]
|
||||
#[prop]
|
||||
name: bool,
|
||||
}
|
||||
@@ -111,14 +112,17 @@ impl Component for SelectFace<'_> {
|
||||
type Style = SelectFaceStyle;
|
||||
type Output = Response;
|
||||
|
||||
fn style(cx: &StyleCx<'_>, _: SelectFaceStyleKey) -> SelectFaceStyle {
|
||||
fn style(cx: &StyleCx<'_>, key: SelectFaceStyleKey) -> SelectFaceStyle {
|
||||
let field = FieldStyle::at(cx);
|
||||
// A thumbnail alone keeps the field's padding and height, not the widths a field keeps for its text.
|
||||
let metrics = field.metrics();
|
||||
let metrics = if key.name { metrics } else { metrics.ideal_width(0.0).min_size(Vec2::new(0.0, metrics.min_size.y)) };
|
||||
let off = cx.color(cx.tokens.color.text.disabled);
|
||||
let (ink, muted) = (cx.ink(Ink::OnPanel), cx.color(cx.tokens.color.text.faint));
|
||||
let caption = cx.color(cx.tokens.color.text.muted);
|
||||
let ink = Stateful::new(ink).off(States::new(ink, ink, ink, off));
|
||||
SelectFaceStyle {
|
||||
metrics: field.metrics(),
|
||||
metrics,
|
||||
text: TextLook::new(TypeRole::Body, cx.host_ink(ink.clone())),
|
||||
placeholder: TextLook::new(TypeRole::Body, cx.host_muted_ink(Stateful::new(muted).off(States::new(muted, muted, muted, off)))),
|
||||
chevron: IconLook::new(field.glyph().side, cx.host_ink(ink)),
|
||||
|
||||
Reference in New Issue
Block a user