mirror of
https://github.com/storytold/craft-libs.git
synced 2026-11-25 20:22:39 +00:00
Merge pull request #2490 from aiongg/picker-caption-widest
Preset picker: a caption range keeps the field at its widest caption
This commit is contained in:
@@ -4,7 +4,7 @@
|
||||
use std::cell::{Cell, RefCell};
|
||||
use std::hash::Hash;
|
||||
|
||||
use craft_behavior::{EditEvent, EditEvents, IconId, Mixed, Value};
|
||||
use craft_behavior::{Codec as _, EditEvent, EditEvents, IconId, Mixed, NumberCodec, Value};
|
||||
use craft_ui::buttons::IconButton;
|
||||
use craft_ui::contract::{
|
||||
Bind, BodyCx, EditResponse, Outcome, Props as _, Remember as _, SaveKey, Show as _, ShowContainer as _, TooltipSpec, Trigger, clicked,
|
||||
@@ -466,7 +466,8 @@ 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(NumberCodec::new(env.units().reader(None), 0).format(&Some(values.diameter.round())))
|
||||
.caption_range((1.0..=max_diameter.round(), 0))
|
||||
.cell_shape(CellShape::Square)
|
||||
.view(if parts.tip_only() { PresetView::Grid } else { PresetView::List })
|
||||
.browser(!compact)
|
||||
|
||||
@@ -30,9 +30,10 @@ use crate::runtime::memory::StageAccess;
|
||||
use crate::runtime::overlay::CloseReason;
|
||||
use crate::style::{ComponentStyle, FocusLook, StyleCx, SurfaceLook, UiTokens};
|
||||
use crate::tile_grid::TileZoom;
|
||||
use craft_behavior::{EditEvent, EditEvents, HitLevel, HitPriority, Intent, Metrics, Mixed, PartKey, PressPhase, Sense, Value};
|
||||
use craft_behavior::{EditEvent, EditEvents, HitLevel, HitPriority, Intent, Metrics, Mixed, NumberCodec, PartKey, PressPhase, Sense, Value};
|
||||
use egui::{Pos2, Rect, Response, Ui, Vec2};
|
||||
use std::hash::Hash;
|
||||
use std::ops::RangeInclusive;
|
||||
|
||||
/// The face under the whole field.
|
||||
const FACE: PartKey = PartKey::new("face");
|
||||
@@ -193,6 +194,12 @@ 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 range and decimal places of the number `caption` shows (a brush's least and largest size, whole), written
|
||||
/// as [`NumberCodec`] writes plain numbers. The field holds the widest number of the range as a number field does
|
||||
/// ([`NumberCodec::widest`], tabular figures), so a caption that follows a dragged value moves nothing beside the
|
||||
/// field.
|
||||
#[prop]
|
||||
caption_range: Option<(RangeInclusive<f64>, usize)>,
|
||||
/// The field's tooltip.
|
||||
#[prop(into)]
|
||||
tooltip: Option<Tooltip>,
|
||||
@@ -270,6 +277,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 +337,7 @@ impl<'a, K: Value + Eq + Hash + 'static> PresetPicker<'a, K> {
|
||||
placeholder,
|
||||
display_name,
|
||||
caption,
|
||||
caption_range,
|
||||
tooltip,
|
||||
mut head,
|
||||
keep_open,
|
||||
@@ -361,6 +370,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 +544,7 @@ struct PickerFace<'a, K: Value + Eq + Hash + 'static> {
|
||||
placeholder: Option<Label>,
|
||||
display_name: Option<Label>,
|
||||
caption: Option<Label>,
|
||||
caption_range: Option<(RangeInclusive<f64>, usize)>,
|
||||
face_thumb: Option<PreviewPaint>,
|
||||
edit_face: bool,
|
||||
open: bool,
|
||||
@@ -578,24 +589,31 @@ 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 widest number of `caption_range`, else the caption.
|
||||
fn measured_caption(&self, env: &EnvView<'_>) -> Option<Label> {
|
||||
let Some((range, decimals)) = &self.caption_range else { return self.caption.clone() };
|
||||
Some(Label::from(NumberCodec::new(env.units().reader(None), *decimals).widest(range)))
|
||||
}
|
||||
|
||||
/// 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.measured_caption(&cx.env);
|
||||
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 the widest number of the range ("5000", in tabular figures),
|
||||
/// 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.0..=5000.0, 0))
|
||||
.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,39 @@ 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 the widest number of its range: as
|
||||
/// the caption gains digits, and whatever its digits are, 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.0..=5000.0, 0))
|
||||
.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, 4888, 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");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
|
||||
use crate::contract::{Common, Component, Cx, MeasureCx, PaintUi, Pointer as _, PopupHandle, Props as _, Role, Styled as _};
|
||||
use crate::list::{RowThumb, ThumbAspect, ThumbPaint};
|
||||
use crate::primitives::{ContentPainterCx, IconSpec, TextRun};
|
||||
use crate::primitives::{ContentPainterCx, IconSpec, Span, TextRun};
|
||||
use crate::runtime::env::Label;
|
||||
use crate::style::{ComponentStyle, IconLook, Ink, StyleCx, TextLook, TypeRole};
|
||||
use crate::text::{Adornment, FieldStyle, place, reserve};
|
||||
@@ -68,7 +68,8 @@ pub struct SelectFace<'a> {
|
||||
/// The picked entry's icon, drawn before the text in the chevron's ink.
|
||||
#[prop]
|
||||
leading: Option<IconSpec<'static>>,
|
||||
/// A short line centred under the thumbnail (a brush's size); it shows only with a thumbnail.
|
||||
/// A short line centred under the thumbnail (a brush's size), in tabular figures, so a number's width follows its
|
||||
/// digit count only; it shows only with a thumbnail.
|
||||
#[prop(into)]
|
||||
thumb_caption: Option<Label>,
|
||||
/// Whether the text shows beside the thumbnail; `false` shows the thumbnail alone, as wide as the thumbnail and
|
||||
@@ -123,7 +124,10 @@ impl<'a> SelectFace<'a> {
|
||||
|
||||
/// The caption's run, when there is a thumbnail to show it under.
|
||||
fn caption_run<'s>(&'s self, look: &TextLook) -> Option<TextRun<'s>> {
|
||||
self.thumb.as_ref().and(self.thumb_caption.as_ref()).map(|caption| TextRun::new(caption.as_str()).role(look.role))
|
||||
self.thumb
|
||||
.as_ref()
|
||||
.and(self.thumb_caption.as_ref())
|
||||
.map(|caption| TextRun::spans(vec![Span::new(caption.as_str()).tabular()]).role(look.role))
|
||||
}
|
||||
|
||||
fn leading_glyphs(&self) -> Vec<Adornment> {
|
||||
|
||||
Reference in New Issue
Block a user