mirror of
https://github.com/storytold/craft-libs.git
synced 2026-11-17 01:44:25 +00:00
Every region of a container spaces its children itself, in the measuring and the real run alike: header, footer and end region by the container's body gap, the header's controls box by the item gap
A region inherited the item spacing of the ui around it, which differs between the two runs (egui's default while measuring, the parent body's gap when placed), so a region of several children took more than it measured. One opener (RegionFrame::open) makes every region's ui and sets its spacing; the footer's own spacing fix goes.
This commit is contained in:
@@ -648,8 +648,9 @@ impl<C: Container> ShapeHooks<C> for BodyHooks<'_> {
|
||||
}
|
||||
let region_env = env.derive(|scoped| self.scope_regions(scoped, &props, cx, window, values, real));
|
||||
let body_id = sub(id, "body");
|
||||
let owner = BodyOwner::new(cx.style, gap_of(&env.tokens, props.body_gap));
|
||||
let frame = RegionFrame { id, rtl, node, env: ®ion_env };
|
||||
let gap = Vec2::splat(gap_of(&env.tokens, props.body_gap));
|
||||
let owner = BodyOwner::new(cx.style, gap.y);
|
||||
let frame = RegionFrame { id, rtl, node, env: ®ion_env, gap };
|
||||
let (resolved, env_view) = (cx.core.resolved, region_env.view());
|
||||
let mut open = self.open;
|
||||
let mut gate = self.gate;
|
||||
@@ -689,7 +690,7 @@ impl<C: Container> ShapeHooks<C> for BodyHooks<'_> {
|
||||
}
|
||||
})
|
||||
};
|
||||
let end_frame = RegionFrame { id, rtl, node, env: &line_env };
|
||||
let end_frame = RegionFrame { id, rtl, node, env: &line_env, gap };
|
||||
// A footer in the end region is part of a column sized from its content: a fill-width child takes its
|
||||
// measured width there and shows across the column, so it never sets the column's width.
|
||||
let footer_env = if footer_in_end(&props) { region_env.derive_local(|scoped| scoped.content_column = true) } else { region_env.clone() };
|
||||
@@ -699,7 +700,6 @@ impl<C: Container> ShapeHooks<C> for BodyHooks<'_> {
|
||||
// A held fill-width container is part of the content column it sits in: its own stack is one too.
|
||||
let (sizing, held) = (self.sizing(&props), self.held(&props, env));
|
||||
let content_column = (sizing == Sizing::Content || held || !self.given.0) && matches!(props.layout, layout::BodyLayout::Stack);
|
||||
let gap = Vec2::splat(gap_of(&env.tokens, props.body_gap));
|
||||
let layout_id = sub(id, "layout");
|
||||
// The body is measured in the line the real run gives it: what the header and the end region leave. The
|
||||
// measuring run places it once both have measured, so its end region runs first (its content never depends
|
||||
@@ -955,10 +955,7 @@ impl<C: Container> ShapeHooks<C> for BodyHooks<'_> {
|
||||
let mut bcx = BodyCx::new(body_id, env_view, resolved, footer_rect, &mut *cx.core.memory, &mut open, gate.as_mut())
|
||||
.with_field_message(field_message)
|
||||
.with_owner(owner);
|
||||
let mut footer_ui = alloc::child(ui, frame.region("footer", footer_rect));
|
||||
// The footer's children are spaced as the body's, in the measuring run and the real run alike: the item
|
||||
// spacing of the ui around differs between the two, and the footer's measured height must be the height it takes.
|
||||
style::set_item_spacing(&mut footer_ui, gap);
|
||||
let mut footer_ui = frame.region(ui, "footer", footer_rect);
|
||||
let installed = Env::install_for(&footer_ui, footer_env);
|
||||
comp.footer(&mut footer_ui, &mut bcx);
|
||||
Env::restore(&ctx, installed);
|
||||
|
||||
@@ -105,19 +105,31 @@ pub(super) struct RegionSizes {
|
||||
pub(super) end: Option<Vec2>,
|
||||
}
|
||||
|
||||
/// What every region of a container shares: its id, direction, whether it is an AccessKit node and its env.
|
||||
/// What every region of a container shares: its id, direction, whether it is an AccessKit node, its env and the
|
||||
/// container's body gap.
|
||||
pub(super) struct RegionFrame<'e> {
|
||||
pub(super) id: Id,
|
||||
pub(super) rtl: bool,
|
||||
pub(super) node: bool,
|
||||
pub(super) env: &'e Env,
|
||||
pub(super) gap: Vec2,
|
||||
}
|
||||
|
||||
impl RegionFrame<'_> {
|
||||
/// The ui builder of the region `name` in `rect`.
|
||||
pub(super) fn region(&self, name: &'static str, rect: Rect) -> UiBuilder {
|
||||
/// The ui of a region, an AccessKit child of the container when it is a node, its children spaced by `spacing`.
|
||||
/// The ui around a region spaces its own children differently in the measuring run and the real run, so a region
|
||||
/// sets its spacing itself: its measured size is then the size it takes.
|
||||
pub(super) fn open(&self, ui: &mut Ui, builder: UiBuilder, spacing: Vec2) -> Ui {
|
||||
let builder = if self.node { access::with_parent(builder, self.id) } else { builder };
|
||||
let mut region = alloc::child(ui, builder);
|
||||
style::set_item_spacing(&mut region, spacing);
|
||||
region
|
||||
}
|
||||
|
||||
/// The column ui of the region `name` in `rect`, its children spaced by the container's body gap.
|
||||
pub(super) fn region(&self, ui: &mut Ui, name: &'static str, rect: Rect) -> Ui {
|
||||
let builder = UiBuilder::new().max_rect(rect).layout(child::column_layout(self.rtl)).scope_id(sub(self.id, name));
|
||||
if self.node { access::with_parent(builder, self.id) } else { builder }
|
||||
self.open(ui, builder, self.gap)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -217,10 +229,7 @@ impl<'b> BodyHooks<'b> {
|
||||
None => header,
|
||||
};
|
||||
let builder = UiBuilder::new().max_rect(room).layout(layout).scope_id(sub(id, "controls"));
|
||||
let builder = if frame.node { access::with_parent(builder, id) } else { builder };
|
||||
let mut controls_ui = alloc::child(ui, builder);
|
||||
// The same spacing in the measuring run and the placing run, whatever the parent's item spacing is.
|
||||
style::set_item_spacing(&mut controls_ui, Vec2::splat(gap_of(&frame.env.tokens, Gap::Item)));
|
||||
let mut controls_ui = frame.open(ui, builder, Vec2::splat(gap_of(&frame.env.tokens, Gap::Item)));
|
||||
let installed = Env::install_for(&controls_ui, frame.env.clone());
|
||||
comp.controls(&mut controls_ui, bcx);
|
||||
Env::restore(&ctx, installed);
|
||||
@@ -235,7 +244,7 @@ impl<'b> BodyHooks<'b> {
|
||||
}
|
||||
}
|
||||
}
|
||||
let mut header_ui = alloc::child(ui, frame.region("header", header_rect));
|
||||
let mut header_ui = frame.region(ui, "header", header_rect);
|
||||
let installed = Env::install_for(&header_ui, frame.env.clone());
|
||||
comp.header(&mut header_ui, bcx);
|
||||
Env::restore(&ctx, installed);
|
||||
@@ -291,8 +300,7 @@ impl<'b> BodyHooks<'b> {
|
||||
let cross = if end_full_height(props) { Align::Min } else { Align::Center };
|
||||
let layout = if frame.rtl { Layout::right_to_left(cross) } else { Layout::left_to_right(cross) };
|
||||
let builder = UiBuilder::new().max_rect(end).layout(layout).scope_id(sub(frame.id, "end"));
|
||||
let builder = if frame.node { access::with_parent(builder, frame.id) } else { builder };
|
||||
let mut end_ui = alloc::child(ui, builder);
|
||||
let mut end_ui = frame.open(ui, builder, frame.gap);
|
||||
let installed = Env::install_for(&end_ui, frame.env.clone());
|
||||
comp.end(&mut end_ui, bcx);
|
||||
Env::restore(ui.ctx(), installed);
|
||||
|
||||
@@ -34,8 +34,8 @@ struct Regioned<'a> {
|
||||
header: Option<Vec2>,
|
||||
end: Vec2,
|
||||
footer: Vec2,
|
||||
/// How many blocks of `footer` the footer stacks.
|
||||
footer_rows: usize,
|
||||
/// How many blocks of its size the header and the footer each stack.
|
||||
rows: usize,
|
||||
took: &'a RefCell<Took>,
|
||||
}
|
||||
|
||||
@@ -59,7 +59,7 @@ impl Component for Regioned<'_> {
|
||||
|
||||
impl Container for Regioned<'_> {
|
||||
fn header(&mut self, ui: &mut Ui, _: &mut BodyCx<'_>) {
|
||||
self.took.borrow_mut().header = self.header.map(|size| allocate(ui, size));
|
||||
self.took.borrow_mut().header = self.header.and_then(|size| stacked(ui, size, self.rows));
|
||||
}
|
||||
|
||||
fn end(&mut self, ui: &mut Ui, _: &mut BodyCx<'_>) {
|
||||
@@ -67,13 +67,17 @@ impl Container for Regioned<'_> {
|
||||
}
|
||||
|
||||
fn footer(&mut self, ui: &mut Ui, _: &mut BodyCx<'_>) {
|
||||
let rows = (0..self.footer_rows).map(|_| allocate(ui, self.footer)).reduce(|a, b| a.union(b));
|
||||
self.took.borrow_mut().footer = rows;
|
||||
self.took.borrow_mut().footer = stacked(ui, self.footer, self.rows);
|
||||
}
|
||||
}
|
||||
|
||||
/// Shows a filling `Regioned` at `AREA` with `props` for one frame and returns what its regions took in the frame's
|
||||
/// first pass, before any discard pass: no region has a size from an earlier pass then.
|
||||
/// `rows` blocks of `size`, one under the other: the rect they cover.
|
||||
fn stacked(ui: &mut Ui, size: Vec2, rows: usize) -> Option<Rect> {
|
||||
(0..rows).map(|_| allocate(ui, size)).reduce(|a, b| a.union(b))
|
||||
}
|
||||
|
||||
fn first_pass(props: ContainerProps, header: Option<Vec2>) -> Took {
|
||||
let took = RefCell::new(Took::default());
|
||||
let passes = RefCell::new(Vec::new());
|
||||
@@ -85,7 +89,7 @@ fn first_pass(props: ContainerProps, header: Option<Vec2>) -> Took {
|
||||
header,
|
||||
end: Vec2::new(80.0, 40.0),
|
||||
footer: Vec2::new(70.0, 16.0),
|
||||
footer_rows: 1,
|
||||
rows: 1,
|
||||
took: &took,
|
||||
}
|
||||
.show_at(ui, AREA, |_, cx| cx.rect);
|
||||
@@ -128,7 +132,7 @@ fn a_row_body_between_an_inline_header_and_an_end_region_measures_in_the_line_th
|
||||
header: Some(Vec2::new(40.0, 20.0)),
|
||||
end: Vec2::new(80.0, 20.0),
|
||||
footer: Vec2::ZERO,
|
||||
footer_rows: 1,
|
||||
rows: 1,
|
||||
took: &took,
|
||||
};
|
||||
bar.show(ui, |ui, _| *text.borrow_mut() = Text::new(WORDS).show(ui).rect);
|
||||
@@ -204,7 +208,7 @@ fn a_footer_of_several_rows_lies_inside_its_container_whatever_the_parent_spacin
|
||||
header: None,
|
||||
end: Vec2::ZERO,
|
||||
footer: Vec2::new(70.0, 16.0),
|
||||
footer_rows: 3,
|
||||
rows: 3,
|
||||
took: &took,
|
||||
};
|
||||
*shown.borrow_mut() = regioned.show(ui, |ui, _| allocate(ui, Vec2::new(70.0, 16.0))).output.rect;
|
||||
@@ -215,3 +219,29 @@ fn a_footer_of_several_rows_lies_inside_its_container_whatever_the_parent_spacin
|
||||
let (footer, rect) = (took.borrow().footer.expect("the footer ran"), *shown.borrow());
|
||||
assert!(footer.max.y <= rect.max.y + EDGE_SLACK, "the footer {footer:?} lies inside its container {rect:?}");
|
||||
}
|
||||
|
||||
/// A stacked header of several rows, in a container whose parent spaces its own children differently: the body
|
||||
/// starts under the header's last row, as the header is placed by the height it measured.
|
||||
#[trial]
|
||||
fn a_header_of_several_rows_lies_above_the_body_whatever_the_parent_spacing() {
|
||||
let took = RefCell::new(Took::default());
|
||||
let body = RefCell::new(None);
|
||||
let show = |ui: &mut Ui| {
|
||||
Slab::new(ContainerProps::new().body_gap(Gap::Section)).show_at(ui, AREA, |ui, _| {
|
||||
let regioned = Regioned {
|
||||
common: Common::new(),
|
||||
props: ContainerProps::new().header(HeaderRegion::new()),
|
||||
header: Some(Vec2::new(70.0, 16.0)),
|
||||
end: Vec2::ZERO,
|
||||
footer: Vec2::ZERO,
|
||||
rows: 3,
|
||||
took: &took,
|
||||
};
|
||||
*body.borrow_mut() = regioned.show(ui, |ui, _| allocate(ui, Vec2::new(70.0, 16.0))).inner;
|
||||
});
|
||||
};
|
||||
let mut frames = Frames::new();
|
||||
frames.idle(3, show);
|
||||
let (header, body) = (took.borrow().header.expect("the header ran"), body.borrow().expect("the body ran"));
|
||||
assert!(body.min.y >= header.max.y - EDGE_SLACK, "the body {body:?} starts under the header {header:?}");
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user