mirror of
https://github.com/storytold/craft-libs.git
synced 2026-11-25 20:22:39 +00:00
Preset picker: a caption range keeps the field at its widest caption
A chip whose caption follows a value (the Brush Preset chip's diameter) took the width of its current caption, so the chip widened as the caption gained a digit. During a size drag that moved what follows it on the bar, and the bar's kept measure no longer matched, so the frame ran a second pass. PresetPicker::caption_range names the captions at the two ends of what the caption shows; the field measures with the wider, as a number field holds the widest value of its range. BrushPresetPicker passes the captions of 1 and its largest diameter. Part of storytold/photocraft#15476, for storytold/photocraft#15859.
This commit is contained in:
@@ -467,6 +467,7 @@ impl<'a, K: Value + Eq + Hash> BrushPresetPicker<'a, K> {
|
||||
let mut picker = PresetPicker::new(words.picker.clone(), current.reborrowed(), rows, Bind::bound(&mut browser))
|
||||
.field(PickerField::Chip)
|
||||
.caption(env.units().number(values.diameter.round(), 0..=0))
|
||||
.caption_range([1.0, max_diameter].map(|diameter| Label::from(env.units().number(diameter.round(), 0..=0))))
|
||||
.cell_shape(CellShape::Square)
|
||||
.view(if parts.tip_only() { PresetView::Grid } else { PresetView::List })
|
||||
.browser(!compact)
|
||||
|
||||
@@ -193,6 +193,11 @@ pub struct PresetPicker<'a, K: Value + Eq + Hash + 'static> {
|
||||
/// A short line under the field's thumbnail (a brush's current size).
|
||||
#[prop(into)]
|
||||
caption: Option<Label>,
|
||||
/// The captions at the two ends of what `caption` shows (a brush's least and largest size). The field holds the
|
||||
/// wider, as a number field holds the widest value of its range, so a caption that follows a dragged value moves
|
||||
/// nothing beside the field.
|
||||
#[prop]
|
||||
caption_range: Option<[Label; 2]>,
|
||||
/// The field's tooltip.
|
||||
#[prop(into)]
|
||||
tooltip: Option<Tooltip>,
|
||||
@@ -270,6 +275,7 @@ impl<'a, K: Value + Eq + Hash + 'static> PresetPicker<'a, K> {
|
||||
placeholder: None,
|
||||
display_name: None,
|
||||
caption: None,
|
||||
caption_range: None,
|
||||
tooltip: None,
|
||||
head: None,
|
||||
keep_open: false,
|
||||
@@ -329,6 +335,7 @@ impl<'a, K: Value + Eq + Hash + 'static> PresetPicker<'a, K> {
|
||||
placeholder,
|
||||
display_name,
|
||||
caption,
|
||||
caption_range,
|
||||
tooltip,
|
||||
mut head,
|
||||
keep_open,
|
||||
@@ -361,6 +368,7 @@ impl<'a, K: Value + Eq + Hash + 'static> PresetPicker<'a, K> {
|
||||
placeholder: placeholder.clone(),
|
||||
display_name: display_name.clone(),
|
||||
caption: caption.clone(),
|
||||
caption_range: caption_range.clone(),
|
||||
face_thumb: face_thumb.clone(),
|
||||
edit_face,
|
||||
open,
|
||||
@@ -534,6 +542,7 @@ struct PickerFace<'a, K: Value + Eq + Hash + 'static> {
|
||||
placeholder: Option<Label>,
|
||||
display_name: Option<Label>,
|
||||
caption: Option<Label>,
|
||||
caption_range: Option<[Label; 2]>,
|
||||
face_thumb: Option<PreviewPaint>,
|
||||
edit_face: bool,
|
||||
open: bool,
|
||||
@@ -578,24 +587,33 @@ impl<K: Value + Eq + Hash + 'static> PickerFace<'_, K> {
|
||||
}
|
||||
}
|
||||
|
||||
/// `face` with the caption under its thumbnail, when the picker has one, at the chip's size for a chip.
|
||||
fn captioned<'f>(&self, face: SelectFace<'f>) -> SelectFace<'f> {
|
||||
/// `face` with `caption` under its thumbnail, when there is one, at the chip's size for a chip.
|
||||
fn captioned<'f>(&self, face: SelectFace<'f>, caption: Option<&Label>) -> SelectFace<'f> {
|
||||
let face = if self.field == PickerField::Chip { face.kind(FaceKind::Chip) } else { face };
|
||||
match &self.caption {
|
||||
match caption {
|
||||
Some(caption) => face.thumb_caption(caption.clone()),
|
||||
None => face,
|
||||
}
|
||||
}
|
||||
|
||||
/// `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.thumb_only()))
|
||||
/// `face` with an empty thumbnail of the cell shape over `caption`, and the name unless the field shows the
|
||||
/// thumbnail alone.
|
||||
fn empty_thumb<'f>(&self, face: SelectFace<'f>, caption: Option<&Label>) -> SelectFace<'f> {
|
||||
self.captioned(face.thumb(self.cell_shape.aspect(), |_, _| {}).name(!self.thumb_only()), caption)
|
||||
}
|
||||
|
||||
/// 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, 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> {
|
||||
/// The caption the field measures with: the wider end of `caption_range`, else the caption. The caption shown is
|
||||
/// not measured beside the ends: its measure would change with it, and the field's with it.
|
||||
fn widest_caption(&self, cx: &MeasureCx<'_, Self>) -> Option<Label> {
|
||||
let Some(range) = &self.caption_range else { return self.caption.clone() };
|
||||
let width = |caption: &Label| cx.face(self.empty_thumb(SelectFace::new(Label::Empty), Some(caption))).x;
|
||||
range.iter().map(|caption| (width(caption), caption)).max_by(|a, b| a.0.total_cmp(&b.0)).map(|(_, caption)| caption.clone())
|
||||
}
|
||||
|
||||
/// The face of a field showing `text` over `caption`: with a `row`, its thumbnail and name as `field` says;
|
||||
/// without, the muted 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, caption: Option<&Label>) -> SelectFace<'static> {
|
||||
let face = SelectFace::new(text);
|
||||
if let Some(thumb) = self.face_thumb.as_ref().filter(|_| self.field != PickerField::Name) {
|
||||
let face = if row.is_some() { face } else { face.placeholder() };
|
||||
@@ -607,10 +625,10 @@ impl<K: Value + Eq + Hash + 'static> PickerFace<'_, K> {
|
||||
}
|
||||
})
|
||||
.name(!self.thumb_only());
|
||||
return self.captioned(face);
|
||||
return self.captioned(face, caption);
|
||||
}
|
||||
match (row, self.field) {
|
||||
(None, PickerField::Thumbnail | PickerField::Chip) => self.empty_thumb(face.placeholder()),
|
||||
(None, PickerField::Thumbnail | PickerField::Chip) => self.empty_thumb(face.placeholder(), caption),
|
||||
(None, _) => face.placeholder(),
|
||||
(Some(row), field) => match (&row.preview, field) {
|
||||
(_, PickerField::Name) | (None, _) => face,
|
||||
@@ -623,7 +641,7 @@ impl<K: Value + Eq + Hash + 'static> PickerFace<'_, K> {
|
||||
}
|
||||
})
|
||||
.name(field == PickerField::ThumbnailAndName);
|
||||
self.captioned(face)
|
||||
self.captioned(face, caption)
|
||||
}
|
||||
},
|
||||
}
|
||||
@@ -654,19 +672,22 @@ impl<K: Value + Eq + Hash + 'static> Component for PickerFace<'_, K> {
|
||||
Sense::CLICK | Sense::FOCUS | Sense::TAKE_FOCUS
|
||||
}
|
||||
|
||||
/// The widest face that can show, so the field keeps its width whichever preset is picked: the face of each
|
||||
/// preset, the mixed word and the placeholder, and an empty thumbnail face for the height.
|
||||
/// The widest face that can show, so the field keeps its width whichever preset is picked and whatever its
|
||||
/// caption shows of `caption_range`: the face of each preset, the mixed word and the placeholder, and an empty
|
||||
/// thumbnail face for the height, each over the widest caption.
|
||||
fn measure(&mut self, cx: &MeasureCx<'_, Self>) -> Vec2 {
|
||||
let words = cx.words::<PresetWords>();
|
||||
let caption = self.widest_caption(cx);
|
||||
let caption = caption.as_ref();
|
||||
let empty = SelectFace::new(Label::Empty);
|
||||
let empty = if self.field == PickerField::Name { empty } else { self.empty_thumb(empty) };
|
||||
let empty = if self.field == PickerField::Name { empty } else { self.empty_thumb(empty, caption) };
|
||||
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);
|
||||
self.face_of(text, Some(row), false)
|
||||
self.face_of(text, Some(row), false, caption)
|
||||
});
|
||||
let words_shown = [self.shown(None, &words, &cx.env), self.shown(Some(&None), &words, &cx.env)];
|
||||
let notes = words_shown.into_iter().map(|(text, row)| self.face_of(text, row, false));
|
||||
let notes = words_shown.into_iter().map(|(text, row)| self.face_of(text, row, false, caption));
|
||||
std::iter::once(empty).chain(faces).chain(notes).map(|face| cx.face(face)).fold(Vec2::ZERO, Vec2::max)
|
||||
}
|
||||
|
||||
@@ -709,7 +730,7 @@ impl<K: Value + Eq + Hash + 'static> Component for PickerFace<'_, K> {
|
||||
fn paint(&mut self, ui: &mut PaintUi<'_>, cx: &Cx<'_, Self>) {
|
||||
let words = cx.words::<PresetWords>();
|
||||
let (text, row) = self.shown(cx.value(), &words, &cx.env);
|
||||
ui.face(FACE, self.face_of(text, row, true));
|
||||
ui.face(FACE, self.face_of(text, row, true, self.caption.as_ref()));
|
||||
}
|
||||
|
||||
fn finish(self, cx: Cx<'_, Self>) -> FaceOutput<K> {
|
||||
|
||||
@@ -52,6 +52,25 @@ fn preset_picker_chip(ui: &mut Ui, data: &mut StoryData) -> StoryOutput {
|
||||
output.map(StoryOutput::from).unwrap_or_default()
|
||||
}
|
||||
|
||||
/// The chip with a caption range: it holds the width of its widest caption ("5000"), so a caption that follows a
|
||||
/// dragged size ("7") moves nothing beside it on the bar.
|
||||
#[story(PresetPicker<'static, u32>, covers = [], drive = Drive::Never)]
|
||||
fn preset_picker_chip_caption_range(ui: &mut Ui, data: &mut StoryData) -> StoryOutput {
|
||||
let rows = rows();
|
||||
let (swatch, state) = data.value("chip range", (Some(TEAL), PresetState::<u32>::new()));
|
||||
let mut output = None;
|
||||
Bar::new("Brush options", BarKind::Options).show(ui, |ui, _| {
|
||||
output = Some(
|
||||
PresetPicker::new("Swatch", Bind::bound(&mut *swatch), &rows, Bind::bound(&mut *state))
|
||||
.field(PickerField::Chip)
|
||||
.caption("7")
|
||||
.caption_range(["1".into(), "5000".into()])
|
||||
.show(ui),
|
||||
);
|
||||
});
|
||||
output.map(StoryOutput::from).unwrap_or_default()
|
||||
}
|
||||
|
||||
#[story(PresetPicker<'static, u32>, covers = [Mixed], drive = Drive::Never)]
|
||||
fn preset_picker_mixed(ui: &mut Ui, data: &mut StoryData) -> StoryOutput {
|
||||
let rows = rows();
|
||||
|
||||
@@ -2,9 +2,10 @@
|
||||
//! closes; Escape closes and keeps the value; inside a popover, the picker's own popover nests in the parent, which
|
||||
//! stays open through the pick.
|
||||
|
||||
use craft_ui::bars::{Bar, BarKind};
|
||||
use craft_ui::buttons::Button;
|
||||
use craft_ui::contract::{Bind, Role, Show as _, ShowContainer as _};
|
||||
use craft_ui::patterns::presets::{PresetPicker, PresetRow, PresetState};
|
||||
use craft_ui::patterns::presets::{PickerField, PresetPicker, PresetRow, PresetState};
|
||||
use craft_ui::popover::Popover;
|
||||
use craft_ui::testing::{Fixture, trial};
|
||||
use egui::{Key, Modifiers, Vec2};
|
||||
@@ -91,3 +92,38 @@ fn a_picker_in_a_popover_opens_and_picks_with_the_popover_kept_open() {
|
||||
assert!(fixture.by_label("Alpha").is_none(), "the pick closes the picker: only the field shows, with Beta");
|
||||
assert!(parent.get(), "the pick keeps the popover open");
|
||||
}
|
||||
|
||||
/// A chip whose caption follows a dragged value (a brush size) holds the width of its widest caption: as the caption
|
||||
/// gains digits the chip and what follows it on the bar stay in place, and each frame runs one pass (a caption that
|
||||
/// changed the chip's width made the bar's kept measure stale, and the frame ran again).
|
||||
#[trial]
|
||||
fn a_chip_with_a_caption_range_keeps_its_width_as_the_caption_grows() {
|
||||
let size = Rc::new(Cell::new(9u32));
|
||||
let shown = Rc::clone(&size);
|
||||
let rows = vec![PresetRow::item(CORAL, "Coral", 0), PresetRow::item(TEAL, "Teal", 0)];
|
||||
let mut state = PresetState::<u32>::new();
|
||||
let mut value = Some(CORAL);
|
||||
let mut fixture = Fixture::new(move |ui| {
|
||||
Bar::new("Brush options", BarKind::Options).show(ui, |ui, _| {
|
||||
let _ = PresetPicker::new("Brush", Bind::bound(&mut value), &rows, Bind::bound(&mut state))
|
||||
.field(PickerField::Chip)
|
||||
.caption(shown.get().to_string())
|
||||
.caption_range(["1".into(), "5000".into()])
|
||||
.show(ui);
|
||||
let _ = Button::new("Mode").show(ui);
|
||||
});
|
||||
});
|
||||
fixture.settle();
|
||||
let place = |fixture: &Fixture| {
|
||||
let chip = fixture.by_label("Brush").expect("the chip shows").rect;
|
||||
let mode = fixture.by_label("Mode").expect("the button after it shows").rect;
|
||||
(chip.width(), mode.left())
|
||||
};
|
||||
let first = place(&fixture);
|
||||
for next in [10, 100, 1000, 7] {
|
||||
size.set(next);
|
||||
fixture.frame(Vec::new());
|
||||
assert_eq!(fixture.passes(), 1, "caption {next}: the frame runs one pass");
|
||||
assert_eq!(place(&fixture), first, "caption {next}: the chip and the button after it stay in place");
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user