mirror of
https://github.com/storytold/craft-libs.git
synced 2026-11-17 01:44:25 +00:00
A bar gives way from its end
A row in a bar too long for its room narrows its last unit to its least before the one before it gives anything (ShrinkOrder::FromEnd, read from InBar), as the old bars clipped from their end; rows elsewhere keep the even share. A status bar's message now elides before its readout.
This commit is contained in:
@@ -561,8 +561,8 @@ pub enum Gap {
|
||||
CardRow,
|
||||
}
|
||||
|
||||
/// Set by a bar for its body: its fields put `space.gap.item` between a caption and the control, and a vertical
|
||||
/// divider takes a box wider than its line.
|
||||
/// Set by a bar for its body: its fields put `space.gap.item` between a caption and the control, a vertical divider
|
||||
/// takes a box wider than its line, and a row too long for its room narrows its last units first.
|
||||
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
|
||||
#[doc(hidden)]
|
||||
pub struct InBar(pub bool);
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
//! `BodyLayout` and the layout of a body: the physical egui layout, the measured placement of `Row` End and
|
||||
//! Center and of each row child on the row's line, and the cursors of `Grid` and of a `Row` that spreads its children.
|
||||
|
||||
use crate::contract::InBar;
|
||||
use crate::runtime::egui::{alloc, child};
|
||||
use crate::runtime::env::{Env, Shrink};
|
||||
use crate::runtime::layout_memory;
|
||||
@@ -182,23 +183,56 @@ fn shrink_key(row: Id, leaf: Id) -> Id {
|
||||
sub(row, ("shrink", leaf))
|
||||
}
|
||||
|
||||
/// Which units of a row too long for its room give way first.
|
||||
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
|
||||
#[non_exhaustive]
|
||||
pub enum ShrinkOrder {
|
||||
/// Every unit gives the same share of what it can give, so all reach their least together.
|
||||
#[default]
|
||||
Even,
|
||||
/// The last unit in reading order goes to its least before the one before it gives anything: a bar gives way from
|
||||
/// its end, where the members that matter least sit.
|
||||
FromEnd,
|
||||
}
|
||||
|
||||
/// 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, and so does a row
|
||||
/// that does not fit even with every unit at its least: it overflows (or its body scrolls) at its natural widths,
|
||||
/// instead of cutting its units to their least and overflowing all the same.
|
||||
pub fn shrink_widths(units: &[ShrinkUnit], fixed: f32, room: f32) -> Vec<f32> {
|
||||
/// the shortfall comes off the units in `order`, never past the least each reports (its ideal less what it can give).
|
||||
/// A row that fits gives each unit its ideal, and so does a row that does not fit even with every unit at its least:
|
||||
/// it overflows (or its body scrolls) at its natural widths, instead of cutting its units to their least and
|
||||
/// overflowing all the same.
|
||||
pub fn shrink_widths(units: &[ShrinkUnit], fixed: f32, room: f32, order: ShrinkOrder) -> Vec<f32> {
|
||||
let short = fixed + units.iter().map(|unit| unit.ideal).sum::<f32>() - room;
|
||||
let slack: f32 = units.iter().map(|unit| unit.ideal - unit.least).sum();
|
||||
// Half a point: less than a pixel shows at any scale the box runs.
|
||||
let fits_at_least = short - slack < 0.5;
|
||||
let share = if short > 0.0 && slack > 0.0 && fits_at_least { (short / slack).min(1.0) } else { 0.0 };
|
||||
units.iter().map(|unit| unit.ideal - (unit.ideal - unit.least) * share).collect()
|
||||
if short <= 0.0 || slack <= 0.0 || !fits_at_least {
|
||||
return units.iter().map(|unit| unit.ideal).collect();
|
||||
}
|
||||
match order {
|
||||
ShrinkOrder::Even => {
|
||||
let share = (short / slack).min(1.0);
|
||||
units.iter().map(|unit| unit.ideal - (unit.ideal - unit.least) * share).collect()
|
||||
}
|
||||
ShrinkOrder::FromEnd => {
|
||||
let mut left = short;
|
||||
let mut widths: Vec<f32> = units
|
||||
.iter()
|
||||
.rev()
|
||||
.map(|unit| {
|
||||
let given = (unit.ideal - unit.least).min(left);
|
||||
left -= given;
|
||||
unit.ideal - given
|
||||
})
|
||||
.collect();
|
||||
widths.reverse();
|
||||
widths
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Runs `body`, the children of the unwrapped `Row` body `id`, in `ui`, the block they are placed in, with the row's
|
||||
/// shrink step: a row fits its room by narrowing its units, never past the least each reports, and a row that cannot
|
||||
/// fit even at its units' leasts keeps their natural widths. A unit is a leaf that
|
||||
/// shrink step: a row fits its room by narrowing its units, never past the least each reports, evenly or, in a bar,
|
||||
/// from its end, and a row that cannot fit even at its units' leasts keeps their natural widths. A unit is a leaf that
|
||||
/// can shrink (elidable text, fields, selects), whose least is its `Proposal::Min`, or a body that does not fill the
|
||||
/// row (a captioned field, a nested row), whose least is what its content takes unbounded with every leaf at its least.
|
||||
///
|
||||
@@ -231,10 +265,11 @@ fn run_shrinking<R>(ui: &mut Ui, id: Id, mut body: impl FnMut(&mut Ui) -> R) ->
|
||||
return child::block(ui, None, layout, |ui| sweep(ui, Shrink::Apply(id), &mut body)).0;
|
||||
}
|
||||
let room = shrink_room(ui);
|
||||
let order = if Env::of(ui).values.get::<InBar>().0 { ShrinkOrder::FromEnd } else { ShrinkOrder::Even };
|
||||
// Records the width each unit gets, and returns whether one took another width in the first sweep.
|
||||
let settle = |leaves: &[Reported], fixed: f32| {
|
||||
let units: Vec<ShrinkUnit> = leaves.iter().map(|leaf| leaf.widths).collect();
|
||||
let widths = if room.is_finite() { shrink_widths(&units, fixed, room) } else { units.iter().map(|unit| unit.ideal).collect() };
|
||||
let widths = if room.is_finite() { shrink_widths(&units, fixed, room, order) } else { units.iter().map(|unit| unit.ideal).collect() };
|
||||
let mut again = false;
|
||||
for (leaf, width) in leaves.iter().zip(widths) {
|
||||
layout_memory::emit_record(&ctx, shrink_key(id, leaf.leaf), layout_memory::Measured::new(Vec2::new(room, 0.0), Vec2::new(width, 0.0)));
|
||||
|
||||
@@ -7,7 +7,7 @@ mod label;
|
||||
mod slot;
|
||||
|
||||
pub use arrange::{Placed, arrange};
|
||||
pub use body::{BodyLayout, RowAlign, ShrinkUnit, shrink_widths};
|
||||
pub use body::{BodyLayout, RowAlign, ShrinkOrder, ShrinkUnit, shrink_widths};
|
||||
pub use column_group::ColumnGroup;
|
||||
#[doc(hidden)]
|
||||
pub use column_group::ambient_width;
|
||||
|
||||
@@ -6,7 +6,7 @@ use super::style::ListRowStyle;
|
||||
use super::thumb::{Thumb, ThumbSize};
|
||||
use super::trailing::{TrailingItem, trailing_run};
|
||||
use crate::contract::{ColumnAlign, PartsCx, WordSetCx as _};
|
||||
use crate::layout::{ShrinkUnit, shrink_widths};
|
||||
use crate::layout::{ShrinkOrder, ShrinkUnit, shrink_widths};
|
||||
use crate::list::toggle::ToggleState;
|
||||
use crate::list::words::ListWords;
|
||||
use crate::primitives::{HAlign, TextRun, Wrap};
|
||||
@@ -373,7 +373,7 @@ impl ListRow<'_> {
|
||||
.chain(std::iter::once(ShrinkUnit { ideal: indent_full, least: 0.0 }))
|
||||
.collect();
|
||||
let gaps = kept.iter().filter(|kept| **kept).count() as f32 * sizes.gap;
|
||||
let mut given = shrink_widths(&units, base + column + leading_width + sizes.min_label + gaps, end);
|
||||
let mut given = shrink_widths(&units, base + column + leading_width + sizes.min_label + gaps, end, ShrinkOrder::Even);
|
||||
let indent_given = given.pop().unwrap_or(0.0);
|
||||
let used = gaps + given.iter().sum::<f32>();
|
||||
let label_end = end - used;
|
||||
|
||||
@@ -1,9 +1,11 @@
|
||||
//! `Bar`: a body too wide for the bar shrinks its members before it scrolls, a captioned field among them too; a
|
||||
//! status bar's message gives way while its trailing group keeps its width.
|
||||
//! bar gives way from its end, and a status bar's trailing group keeps its width.
|
||||
|
||||
use craft_behavior::CanvasView;
|
||||
use craft_behavior::IconId;
|
||||
use craft_behavior::kurbo::{Point, Size};
|
||||
use craft_theme::variant::Tone;
|
||||
use craft_ui::bars::{Bar, BarKind, BreadcrumbBar, StatusMessage, StatusProgress, path_crumbs};
|
||||
use craft_ui::bars::{Bar, BarKind, BreadcrumbBar, StatusItem, StatusMessage, StatusProgress, StatusReadout, StatusValues, path_crumbs};
|
||||
use craft_ui::buttons::{Button, IconButton, Link};
|
||||
use craft_ui::contract::{Bind, Entry, Props as _, Role, Show as _, ShowContainer as _};
|
||||
use craft_ui::display::Text;
|
||||
@@ -223,3 +225,34 @@ fn a_status_progress_is_named_by_its_task_and_reports_cancel() {
|
||||
fixture.click(&cancel).settle();
|
||||
assert!(cancelled.get(), "a click on Cancel reports it");
|
||||
}
|
||||
|
||||
/// The texts a status bar `width` wide paints, each with whether it was cut: the readout of the document sizes, a long
|
||||
/// message and the render-path link in its trailing group.
|
||||
fn readout_texts(width: f32) -> Vec<(String, bool)> {
|
||||
let mut fixture = Fixture::new(|ui| {
|
||||
let mut values = StatusValues::default();
|
||||
values.document_sizes = Some((26_100_000, 31_400_000));
|
||||
let canvas = egui::Rect::from_min_max(egui::Pos2::ZERO, egui::Pos2::new(800.0, 600.0));
|
||||
let view = CanvasView::new(canvas, Point::new(1000.0, 750.0), 0.6667);
|
||||
Bar::new("Status", BarKind::Status)
|
||||
.trailing(|ui| {
|
||||
Link::new("CPU Rendering").tone(Tone::Warn).show(ui);
|
||||
})
|
||||
.show(ui, |ui, _| {
|
||||
StatusReadout::new(Bind::given(view), Size::new(2000.0, 1500.0), StatusItem::DocumentSizes, &values).show(ui);
|
||||
StatusMessage::new("Exported 14 layers to the folder Documents/Exports/2026").show(ui);
|
||||
});
|
||||
});
|
||||
fixture.room(Vec2::new(width, 80.0)).settle();
|
||||
fixture.texts().iter().map(|text| (text.text.clone(), text.elided)).collect()
|
||||
}
|
||||
|
||||
#[trial]
|
||||
fn a_narrow_bar_gives_way_from_its_end_so_the_message_elides_before_the_readout() {
|
||||
let cut = |texts: &[(String, bool)], start: &str| texts.iter().find(|(text, _)| text.starts_with(start)).expect(start).1;
|
||||
let wide = readout_texts(1200.0);
|
||||
assert!(!cut(&wide, "Doc") && !cut(&wide, "Exported"), "a wide bar cuts nothing");
|
||||
let narrow = readout_texts(480.0);
|
||||
assert!(cut(&narrow, "Exported"), "the message kept its whole text in a 480-point bar");
|
||||
assert!(!cut(&narrow, "Doc"), "the readout's text was cut while the message after it could still give way");
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user