mirror of
https://github.com/storytold/craft-libs.git
synced 2026-11-30 02:04:32 +01:00
ui: a wrapping spread row wraps content that takes a second line and reports its spread line
A wrapping SpaceBetween row counted only its craft children when deciding whether its content fits one line. A direct egui allocation that wrapped onto a second line left the count unchanged, so the real run spread the row and the allocation ran past the row's end edge. The row now also compares how far its content reaches below its top with its tallest child. When the row spreads its children on one line, its measuring run now reports the line they sit on as the row's first line, as an unwrapped row does, instead of the first child's own first line. A parent row then places the spread children on its own line.
This commit is contained in:
1 parent
0a123905ee
commit
ef5313acec
2 files changed
+95
-7
No files matched your search
@@ -260,8 +260,10 @@ fn open_wraps(ctx: &egui::Context, sizing: bool, id: Id) {
|
||||
|
||||
/// Ends the children of the wrapping `Row` body `id`, `room` wide; a measuring run records each child's line and the
|
||||
/// height of its tallest child. A measuring run returns how many children the row placed and what they took about a
|
||||
/// line when every one of them sits on its first line.
|
||||
fn close_wraps(ctx: &egui::Context, sizing: bool, id: Id, room: Vec2) -> Option<(usize, LineTook)> {
|
||||
/// line when every one of them sits on its first line and the row's `content`, how far below its top everything in it
|
||||
/// reaches (direct egui allocations included), is no taller than its tallest child: egui sets anything it wraps below
|
||||
/// the first line.
|
||||
fn close_wraps(ctx: &egui::Context, sizing: bool, id: Id, room: Vec2, content: f32) -> Option<(usize, LineTook)> {
|
||||
let took = pass_state::with::<WrapRows, _>(ctx, |rows| rows.0.remove(&id)).unwrap_or_default();
|
||||
if !sizing {
|
||||
return None;
|
||||
@@ -277,7 +279,8 @@ fn close_wraps(ctx: &egui::Context, sizing: bool, id: Id, room: Vec2) -> Option<
|
||||
for (i, &(line, _)) in took.children.iter().enumerate() {
|
||||
found(ctx, true, sub(id, ("wrap", i)), room, Vec2::new(line as f32, heights.get(line).copied().unwrap_or_default()));
|
||||
}
|
||||
(heights.len() <= 1).then_some((took.children.len(), took.line))
|
||||
let first = heights.first().copied().unwrap_or_default();
|
||||
(heights.len() <= 1 && content <= first + ONE_LINE_SLACK).then_some((took.children.len(), took.line))
|
||||
}
|
||||
|
||||
/// Whether egui puts the next child `width` wide in `ui`, a wrapping row's block, on a new line (`Layout::next_frame`):
|
||||
@@ -2158,6 +2161,10 @@ fn run_row<R>(ui: &mut Ui, env: &Env, at: &RowIn, (wrap, align): (bool, RowAlign
|
||||
// line of its own.
|
||||
let block_layout = if wrap && env.least { child::column_layout(rtl) } else { child::row_layout(rtl, wrap) };
|
||||
let mut one_line = None;
|
||||
// The children of a wrapping `SpaceBetween` row report their first line to the row, which reports the line they
|
||||
// sit on when the real run spreads them on one line, and the first child's otherwise.
|
||||
let spreads = lines && align == RowAlign::SpaceBetween;
|
||||
let first_target = env.first_line.filter(|_| spreads && sizing).map(|target| (target, sub(id, "first_line")));
|
||||
let (inner, took) = child::block(ui, rect, block_layout, |ui| {
|
||||
let row = env.derive_local(|scoped| {
|
||||
scoped.row_room = Some(room.width());
|
||||
@@ -2166,6 +2173,9 @@ fn run_row<R>(ui: &mut Ui, env: &Env, at: &RowIn, (wrap, align): (bool, RowAlign
|
||||
if line.is_some() {
|
||||
scoped.first_line = None;
|
||||
}
|
||||
if let Some((_, own)) = first_target {
|
||||
scoped.first_line = Some(own);
|
||||
}
|
||||
// A leaf wider than a whole line takes the line, never below its least, as in a stack.
|
||||
if wrap {
|
||||
scoped.shrink = Some(Shrink::Fit);
|
||||
@@ -2175,26 +2185,49 @@ fn run_row<R>(ui: &mut Ui, env: &Env, at: &RowIn, (wrap, align): (bool, RowAlign
|
||||
if lines {
|
||||
open_wraps(&ctx, sizing, id);
|
||||
}
|
||||
if let Some((_, own)) = first_target {
|
||||
open_first_lines(&ctx, &[own]);
|
||||
}
|
||||
// How far below the row's top its content reaches, direct egui allocations included, which the children's
|
||||
// lines (`close_wraps`) do not count.
|
||||
let mut content = 0.0;
|
||||
let inner = if wrap && align == RowAlign::SpaceBetween {
|
||||
// In a child, as the spread row runs its children (`run_spread`): they take the same ids whether the real
|
||||
// run spreads them on one line or wraps them, and keep their state when the row starts or stops wrapping.
|
||||
let layout = *ui.layout();
|
||||
child::block(ui, None, layout, |ui| Env::run_in(ui, row.clone(), &mut body)).0
|
||||
child::block(ui, None, layout, |ui| {
|
||||
let top = ui.max_rect().top();
|
||||
let inner = Env::run_in(ui, row.clone(), &mut body);
|
||||
content = (ui.min_rect().bottom() - top).max(0.0);
|
||||
inner
|
||||
})
|
||||
.0
|
||||
} else if wrap {
|
||||
body(ui)
|
||||
} else {
|
||||
run_shrinking(ui, &row, id, &mut body)
|
||||
};
|
||||
if lines {
|
||||
one_line = close_wraps(&ctx, sizing, id, room.size());
|
||||
one_line = close_wraps(&ctx, sizing, id, room.size(), content);
|
||||
// Children that the real run may spread on one line sit on the line they take there, as an unwrapped
|
||||
// row's do: the block takes that line's height, which may be more than its tallest child's (a check box
|
||||
// with a detail line under its label sits its first line on the line).
|
||||
let spread = one_line.filter(|(count, _)| align == RowAlign::SpaceBetween && *count >= 2);
|
||||
if let Some(line) = spread.and_then(|(_, took)| spread_line(id, took, shared)) {
|
||||
let top = ui.max_rect().top();
|
||||
let top = ui.max_rect().top();
|
||||
let line = spread.and_then(|(_, took)| spread_line(id, took, shared));
|
||||
if let Some(line) = line {
|
||||
alloc::reach_down_to(ui, top + line.y);
|
||||
}
|
||||
// The row's first line is the line the real run spreads its children on, as `record_line` reports an
|
||||
// unwrapped row's, else the first child's.
|
||||
if let Some((target, own)) = first_target {
|
||||
let child = close_first_line(&ctx, own);
|
||||
if let Some(line) = line {
|
||||
report_first_line(&ctx, target, Some(top + line.x));
|
||||
} else if let Some(at) = child {
|
||||
report_first_line(&ctx, target, at);
|
||||
}
|
||||
}
|
||||
}
|
||||
Env::restore(&ctx, installed);
|
||||
if !wrap {
|
||||
|
||||
@@ -637,6 +637,61 @@ fn a_wrapping_space_between_row_spreads_children_that_fit_one_line() {
|
||||
assert_eq!(ids[0], ids[1], "the children keep their ids when the row starts wrapping");
|
||||
}
|
||||
|
||||
/// A wrapping `SpaceBetween` row whose children fit one line but whose direct egui allocation after them wraps onto a
|
||||
/// second line wraps: the allocation stays inside the row instead of running past its end.
|
||||
#[trial]
|
||||
fn a_spread_wrapping_row_wraps_a_direct_allocation_that_takes_a_second_line() {
|
||||
let seen: Rc<[RefCell<Seen>; 2]> = Rc::new(Default::default());
|
||||
let rects = Rc::new(Cell::new((Rect::NOTHING, Rect::NOTHING)));
|
||||
let (shown, shown_rects) = (Rc::clone(&seen), Rc::clone(&rects));
|
||||
let mut fixture = Fixture::new(move |ui| {
|
||||
let layout = BodyLayout::row().wrap(true).align(RowAlign::SpaceBetween);
|
||||
let direct = Cell::new(Rect::NOTHING);
|
||||
let panel = Panel::new().layout(layout).sizing(Sizing::FillWidth).id_salt("spread").show(ui, |ui, _| {
|
||||
for (i, seen) in shown.iter().enumerate() {
|
||||
Probe::new(seen).size(Vec2::new(20.0, 10.0)).id_salt(i).show(ui);
|
||||
}
|
||||
direct.set(ui.allocate_exact_size(Vec2::new(60.0, 10.0), egui::Sense::hover()).0);
|
||||
});
|
||||
shown_rects.set((panel.output.rect, direct.get()));
|
||||
});
|
||||
fixture.room(Vec2::new(100.0, 400.0)).settle();
|
||||
let (row, direct) = rects.get();
|
||||
let first = seen[0].borrow().rect;
|
||||
let dump = format!("row {row:?}, first {first:?}, direct {direct:?}");
|
||||
assert!(direct.max.x <= row.max.x + SLACK, "the direct allocation ends inside the row: {dump}");
|
||||
assert!(direct.top() >= first.bottom() - SLACK, "the direct allocation wraps onto a second line: {dump}");
|
||||
}
|
||||
|
||||
/// A wrapping `SpaceBetween` row that spreads children of different heights on one line, in an unwrapped row beside a
|
||||
/// taller block, reports the line its children sit on as its first line: its children sit on the outer row's line.
|
||||
#[trial]
|
||||
fn a_spread_wrapping_row_reports_its_spread_line_as_its_first_line() {
|
||||
let seen = Rc::new(Cell::new((Rect::NOTHING, Rect::NOTHING, Rect::NOTHING)));
|
||||
let shown = Rc::clone(&seen);
|
||||
let mut fixture = Fixture::new(move |ui| {
|
||||
Panel::new().layout(BodyLayout::row()).id_salt("outer").show(ui, |ui, _| {
|
||||
let tall = Block::new(Vec2::new(20.0, 50.0)).show(ui).rect;
|
||||
let layout = BodyLayout::row().wrap(true).align(RowAlign::SpaceBetween);
|
||||
let inner = Panel::new().layout(layout).id_salt("inner").show(ui, |ui, _| {
|
||||
let short = Block::new(ITEM).show(ui).rect;
|
||||
let big = Block::new(Vec2::new(40.0, 30.0)).show(ui).rect;
|
||||
(short, big)
|
||||
});
|
||||
if !is_measuring(ui.ctx()) {
|
||||
let (short, big) = inner.inner.unwrap_or((Rect::NOTHING, Rect::NOTHING));
|
||||
shown.set((tall, short, big));
|
||||
}
|
||||
});
|
||||
});
|
||||
fixture.room(Vec2::splat(400.0)).settle();
|
||||
let (tall, short, big) = seen.get();
|
||||
let dump = format!("tall {tall:?}, short {short:?}, big {big:?}");
|
||||
assert!(short.height() > 0.0 && big.height() > 0.0, "the inner row showed its blocks: {dump}");
|
||||
assert!((short.center().y - big.center().y).abs() < SLACK, "the inner row's blocks share one line: {dump}");
|
||||
assert!((short.center().y - tall.center().y).abs() < SLACK, "the inner row's line is the outer row's line: {dump}");
|
||||
}
|
||||
|
||||
/// An unwrapped row in a row sits on the outer row's line by its own line, whatever its alignment: a row that places
|
||||
/// its block from its start, at its end, centred or spread reports its line as its first line alike.
|
||||
#[trial]
|
||||
|
||||
Reference in new issue
Block a user