mirror of
https://github.com/storytold/craft-libs.git
synced 2026-11-16 23:24:24 +00:00
Fields narrow past their own least where a row shrinks them; the cut is shared
`Metrics::least_width` is the floor a least measure takes in place of `min_size`'s width. A field keeps its 96-point floor where it takes its own width and, in a row that shrinks it, narrows to its padding and one glyph plus what its content measure adds there (a select face's ellipsis and chevron, a text field's adornments). A short row takes its shortfall from every unit in proportion to what each can give, so all reach their least together.
This commit is contained in:
@@ -37,12 +37,24 @@ pub struct Metrics {
|
||||
/// The width the component takes where its room allows, though its content needs less: its ideal measure is at
|
||||
/// least this wide, up to the room it is offered, and its least measure is its content's.
|
||||
pub ideal_width: f32,
|
||||
/// The least width of the component, padding included, where its least measure (`Proposal::Min`) floors at it in
|
||||
/// place of `min_size`'s width: a field that is at least `min_size` wide where it takes its own width, and narrows
|
||||
/// past that only where a row shrinks it.
|
||||
pub least_width: Option<f32>,
|
||||
}
|
||||
|
||||
impl Metrics {
|
||||
/// No padding, no minimum, no wrap width.
|
||||
pub const fn new() -> Self {
|
||||
Metrics { padding: Insets::new(0.0, 0.0, 0.0, 0.0), min_size: Vec2::ZERO, max_width: None, gap: 0.0, min_text: 0.0, ideal_width: 0.0 }
|
||||
Metrics {
|
||||
padding: Insets::new(0.0, 0.0, 0.0, 0.0),
|
||||
min_size: Vec2::ZERO,
|
||||
max_width: None,
|
||||
gap: 0.0,
|
||||
min_text: 0.0,
|
||||
ideal_width: 0.0,
|
||||
least_width: None,
|
||||
}
|
||||
}
|
||||
|
||||
/// Sets the padding.
|
||||
@@ -80,6 +92,12 @@ impl Metrics {
|
||||
self.ideal_width = width;
|
||||
self
|
||||
}
|
||||
|
||||
/// Sets the least width, padding included, that the least measure floors at in place of `min_size`'s width.
|
||||
pub const fn least_width(mut self, width: f32) -> Self {
|
||||
self.least_width = Some(width);
|
||||
self
|
||||
}
|
||||
}
|
||||
|
||||
/// The sizes a control takes from its `ControlSize`: height, icon side, horizontal padding and gap.
|
||||
|
||||
@@ -31,13 +31,17 @@ pub(crate) fn measure_content<C: Component>(
|
||||
}
|
||||
|
||||
/// Step 5, measure: the `content` size plus padding, then `max(min_size)`, and for any proposal but `Min` at least the
|
||||
/// ideal width, up to the room. `extent` is what the shape adds to the content: a body's remembered size or a
|
||||
/// collection's content size.
|
||||
/// ideal width, up to the room. At `Min` a least width the style sets takes the place of `min_size`'s width. `extent`
|
||||
/// is what the shape adds to the content: a body's remembered size or a collection's content size.
|
||||
pub(crate) fn measure(style: &dyn StyleView, input: &MeasureIn<'_>, content: Vec2, extent: Vec2) -> Vec2 {
|
||||
let metrics = style.metrics();
|
||||
let content = content + extent;
|
||||
let padding = Vec2::new(metrics.padding.start + metrics.padding.end, metrics.padding.top + metrics.padding.bottom);
|
||||
let mut size = (content + padding).max(metrics.min_size);
|
||||
let floor = match (input.proposal, metrics.least_width) {
|
||||
(Proposal::Min, Some(least)) => Vec2::new(least, metrics.min_size.y),
|
||||
_ => metrics.min_size,
|
||||
};
|
||||
let mut size = (content + padding).max(floor);
|
||||
if input.proposal != Proposal::Min {
|
||||
size.x = size.x.max(input.room.map_or(metrics.ideal_width, |room| metrics.ideal_width.min(room)));
|
||||
}
|
||||
|
||||
@@ -174,18 +174,14 @@ fn shrink_key(row: Id, leaf: Id) -> Id {
|
||||
sub(row, ("shrink", leaf))
|
||||
}
|
||||
|
||||
/// The widths of the leaves of a row whose leaves, with `fixed` (its other children and gaps), ask for more than
|
||||
/// `room`: the shortfall comes off the last leaf first, down to its least, then off the one before it, and so on, as
|
||||
/// a flow gives each child what the ones before it left. A row that fits gives each leaf what it asks for.
|
||||
/// The widths of the units of a row whose units, with `fixed` (its other children and gaps), ask for more than `room`:
|
||||
/// the shortfall comes off every unit in proportion to what it can give (its ideal less its least), the same share of
|
||||
/// that from each, so all reach their least together. A row that fits gives each unit its ideal.
|
||||
fn shrink_widths(leaves: &[Reported], fixed: f32, room: f32) -> Vec<f32> {
|
||||
let mut short = fixed + leaves.iter().map(|leaf| leaf.ideal).sum::<f32>() - room;
|
||||
let mut widths: Vec<f32> = leaves.iter().map(|leaf| leaf.ideal).collect();
|
||||
for (leaf, width) in leaves.iter().zip(widths.iter_mut()).rev() {
|
||||
let cut = short.clamp(0.0, leaf.ideal - leaf.least);
|
||||
*width -= cut;
|
||||
short -= cut;
|
||||
}
|
||||
widths
|
||||
let short = fixed + leaves.iter().map(|leaf| leaf.ideal).sum::<f32>() - room;
|
||||
let slack: f32 = leaves.iter().map(|leaf| leaf.ideal - leaf.least).sum();
|
||||
let share = if short > 0.0 && slack > 0.0 { (short / slack).min(1.0) } else { 0.0 };
|
||||
leaves.iter().map(|leaf| leaf.ideal - (leaf.ideal - leaf.least) * share).collect()
|
||||
}
|
||||
|
||||
/// Runs `body`, the children of the unwrapped `Row` body `id`, in `ui`, the block they are placed in, with the row's
|
||||
|
||||
@@ -117,9 +117,12 @@ impl Component for SelectFace<'_> {
|
||||
}
|
||||
|
||||
fn measure(&mut self, cx: &MeasureCx<'_, Self>) -> Vec2 {
|
||||
// At its least the face elides its name to nothing.
|
||||
// At its least the face elides its name to an ellipsis.
|
||||
let text = match (self.name, cx.proposal) {
|
||||
(true, Proposal::Min) => Vec2::new(0.0, cx.layout(&TextRun::new(cx.resolved.label).role(cx.style.text.role)).size().y),
|
||||
(true, Proposal::Min) => {
|
||||
let ellipsis = cx.layout(&TextRun::new("…").role(cx.style.text.role)).size().x;
|
||||
Vec2::new(ellipsis, cx.layout(&TextRun::new(cx.resolved.label).role(cx.style.text.role)).size().y)
|
||||
}
|
||||
(true, _) => cx.layout(&TextRun::new(cx.resolved.label).role(cx.style.text.role)).size(),
|
||||
(false, _) => Vec2::ZERO,
|
||||
};
|
||||
|
||||
@@ -53,7 +53,10 @@ impl FieldStyle {
|
||||
.padding(Insets::horizontal(control.pad_x))
|
||||
.gap(control.gap)
|
||||
.min_size(Vec2::new(cx.tokens.size.length.short, control.height))
|
||||
.ideal_width(cx.tokens.size.length.field);
|
||||
.ideal_width(cx.tokens.size.length.field)
|
||||
// Where a row shrinks it, a field narrows to its padding and one glyph; what it shows there (an
|
||||
// ellipsis, its adornments) its content measure adds.
|
||||
.least_width(2.0 * control.pad_x + control.icon);
|
||||
let ink = cx.ink(Ink::OnPanel);
|
||||
let off = cx.color(cx.tokens.color.text.disabled);
|
||||
let glyph = IconLook::new(control.icon, Stateful::new(ink).off(States::new(ink, ink, ink, off)));
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
//! `Bar`: a body too wide for the bar shrinks its members before it scrolls, a captioned field among them too.
|
||||
|
||||
use craft_behavior::IconId;
|
||||
use craft_ui::bars::{Bar, BarKind, BreadcrumbBar, path_crumbs};
|
||||
use craft_ui::buttons::{Button, IconButton};
|
||||
use craft_ui::contract::{Bind, Entry, Props as _, Show as _, ShowContainer as _};
|
||||
@@ -8,7 +9,6 @@ use craft_ui::form::Field;
|
||||
use craft_ui::pickers::Select;
|
||||
use craft_ui::testing::{Fixture, shrink_least_runs, trial};
|
||||
use craft_ui::text::TextField;
|
||||
use craft_behavior::IconId;
|
||||
use egui::{Rect, Vec2};
|
||||
use std::cell::Cell;
|
||||
use std::rc::Rc;
|
||||
@@ -120,3 +120,30 @@ fn the_members_of_a_tool_bar_never_overlap_as_it_narrows() {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// What a select in an options bar `width` wide shows, with three buttons after it: its rect and whether its text
|
||||
/// was elided.
|
||||
fn select_in(width: f32) -> (Rect, bool) {
|
||||
let seen = Rc::new(Cell::new(Rect::NOTHING));
|
||||
let out = Rc::clone(&seen);
|
||||
let mut fixture = Fixture::new(move |ui| {
|
||||
let entries = [Entry::new(0u8, "Point Sample"), Entry::new(1, "5 by 5 Average")];
|
||||
Bar::new("Options", BarKind::Options).show(ui, |ui, _| {
|
||||
out.set(Select::new("Sample Size", Bind::given(0u8), &entries).show(ui).response.rect);
|
||||
for at in 0..3 {
|
||||
Button::new(format!("Button {at}")).show(ui);
|
||||
}
|
||||
});
|
||||
});
|
||||
fixture.room(Vec2::new(width, 80.0)).settle();
|
||||
let elided = fixture.texts().iter().any(|text| text.text == "Point Sample" && text.elided);
|
||||
(seen.get(), elided)
|
||||
}
|
||||
|
||||
#[trial]
|
||||
fn a_select_in_a_short_bar_narrows_past_a_fields_own_least_and_elides() {
|
||||
let (wide, _) = select_in(900.0);
|
||||
let (narrow, elided) = select_in(200.0);
|
||||
assert!(narrow.width() < 96.0, "the select stayed {} wide", narrow.width());
|
||||
assert!(narrow.width() < wide.width() - SLACK && elided, "the select {narrow:?} did not elide its pick");
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user