diff --git a/crates/ui-contract/src/contract/body/hooks.rs b/crates/ui-contract/src/contract/body/hooks.rs index bf9e23c47..9cda37695 100644 --- a/crates/ui-contract/src/contract/body/hooks.rs +++ b/crates/ui-contract/src/contract/body/hooks.rs @@ -291,13 +291,13 @@ impl ShapeHooks for BodyHooks<'_> { let props = comp.props(Internal::new()); self.room = self.mount_room(props, ui, id, at, scx.tokens); let env = Env::of(ui); - self.given = self.given_axes(props, &env, at); + self.given = self.given_axes(props, (&env, ui), at); self.fixed_height = self.fixed_height(props, at); self.passes = self.passes_width(props, &env, at); self.held = self.held(props, &env); self.across = self.held && matches!(env.column, Some(ColumnSweep::Column(..))); self.in_column = label::in_column(ui); - self.given.1 |= self.given_room(props, &env, at, ui.ctx(), id); + self.given.1 |= self.given_room(props, (&env, ui), at, id); let measured = self.measured_body.unwrap_or(Vec2::ZERO); let body = if self.presence.visible { Vec2::new(measured.x, measured.y * self.presence.t) } else { Vec2::ZERO }; let room = self.room.x - 2.0 * self.pads(props, scx.tokens).x; @@ -1828,8 +1828,8 @@ impl BodyHooks<'_> { Some(width) => Vec2::new(width, props.room_height.unwrap_or(f32::INFINITY)), None => self.mount_room(&props, at.ui, id, at.at, at.scx.tokens), }; - self.given = self.given_axes(&props, at.env, at.at); - self.given.1 |= self.given_room(&props, at.env, at.at, at.ui.ctx(), id); + self.given = self.given_axes(&props, (at.env, at.ui), at.at); + self.given.1 |= self.given_room(&props, (at.env, at.ui), at.at, id); self.fixed_height = self.fixed_height(&props, at.at); self.passes = self.passes_width(&props, at.env, at.at); self.held = self.held(&props, at.env); diff --git a/crates/ui-contract/src/contract/body/size.rs b/crates/ui-contract/src/contract/body/size.rs index 1cb5f02c7..cae3774d6 100644 --- a/crates/ui-contract/src/contract/body/size.rs +++ b/crates/ui-contract/src/contract/body/size.rs @@ -125,9 +125,9 @@ impl<'b> BodyHooks<'b> { /// too: the line is a fixed cross extent, and the container fills it (a bar in a shell slot). A container that /// fills its height where its parent sizes from its content is capped (`capped`), and `given_room` decides whether /// it is given its room. - pub(super) fn given_axes(&self, props: &ContainerProps, env: &Env, at: ShowAt) -> (bool, bool) { + pub(super) fn given_axes(&self, props: &ContainerProps, (env, ui): (&Env, &Ui), at: ShowAt) -> (bool, bool) { let sizing = self.sizing(props); - let fills_line = sizing == Sizing::FillWidth && unwrapped_row(props) && env.row_line.is_some(); + let fills_line = sizing == Sizing::FillWidth && unwrapped_row(props) && crate::layout::row_line_of(ui, env).is_some(); let fits_bar = props.content_in_bar && env.values.get::().0; let (x, y) = (!sizing.content_width() && !fits_bar, sizing.fills_height() || fills_line); match (at, &props.mount) { @@ -143,12 +143,15 @@ impl<'b> BodyHooks<'b> { /// body runs in it, so the children in it that fill share it (a ribbon group as tall as the tallest group, its /// title at its foot). In the row's measuring run there is no settled line, not even where the row shares a line /// with an inline header or an end region, and the container measures its content. - pub(super) fn line_height(&self, props: &ContainerProps, env: &Env, at: ShowAt) -> Option { + pub(super) fn line_height(&self, props: &ContainerProps, (env, ui): (&Env, &Ui), at: ShowAt) -> Option { let inline = matches!((at, &props.mount), (ShowAt::Flow, Mount::Inline)); if !(inline && self.sizing(props).fills_height() && env.content.1) { return None; } - env.row_line.filter(|line| line.settled).and_then(|line| line.height).filter(|height| height.is_finite() && *height > 0.0) + crate::layout::row_line_of(ui, env) + .filter(|line| line.settled) + .and_then(|line| line.height) + .filter(|height| height.is_finite() && *height > 0.0) } /// Whether a content-sized inline container passes the width its column gives it on to its own body, where `env` @@ -217,10 +220,11 @@ impl<'b> BodyHooks<'b> { /// children that fill share it: the room is the settled line of the row it stands on (`line_height`), or its /// content is taller than the room, as this pass's measure found it (the record `content` its measuring run leaves /// then). Otherwise it takes its content height. - pub(super) fn given_room(&self, props: &ContainerProps, env: &Env, at: ShowAt, ctx: &Context, id: Id) -> bool { + pub(super) fn given_room(&self, props: &ContainerProps, (env, ui): (&Env, &Ui), at: ShowAt, id: Id) -> bool { + let ctx = ui.ctx(); let tolerance = measure_tolerance(crate::runtime::egui::scale::pixels_per_point(ctx)); self.capped(props, env, at) - && (self.line_height(props, env, at).is_some() + && (self.line_height(props, (env, ui), at).is_some() || layout_memory::measured(ctx, sub(id, "content")).is_some_and(|content| content.took.y > self.room.y + tolerance)) } @@ -345,7 +349,7 @@ impl<'b> BodyHooks<'b> { let shares_view = !room.y.is_finite() && self.sizing(props).fills_height() && crate::layout::shares_y(ui); let y = shares_view.then(|| crate::layout::view_room(ui, craft_behavior::Axis::Y)).flatten().unwrap_or(room.y); // A body that fills the height of the row line it stands on runs in that line. - let y = self.line_height(props, &Env::of(ui), at).unwrap_or(y); + let y = self.line_height(props, (&Env::of(ui), ui), at).unwrap_or(y); Vec2::new(cap.map_or(room.x, |cap| room.x.min(cap)), props.room_height.unwrap_or(y)) } }, diff --git a/crates/ui-contract/src/contract/lazy.rs b/crates/ui-contract/src/contract/lazy.rs index bb622ea9f..6594e2b68 100644 --- a/crates/ui-contract/src/contract/lazy.rs +++ b/crates/ui-contract/src/contract/lazy.rs @@ -317,7 +317,7 @@ fn place_view(ui: &mut Ui, rect: Rect) -> Rect { /// view taller than that reaches, and a view no run showed yet, stays at the row's top. The room keeps its size, so a /// kept run replays moved with it. fn on_row_line(ui: &Ui, id: egui::Id, room: Rect) -> Rect { - let Some(centre) = Env::of(ui).row_line.and_then(|line| line.centre) else { + let Some(centre) = crate::layout::row_line_of(ui, &Env::of(ui)).and_then(|line| line.centre) else { return room; }; let Some(size) = lazy::last_extent(ui.ctx(), id) else { diff --git a/crates/ui-contract/src/layout/body.rs b/crates/ui-contract/src/layout/body.rs index f65e44ecc..57accbccc 100644 --- a/crates/ui-contract/src/layout/body.rs +++ b/crates/ui-contract/src/layout/body.rs @@ -328,7 +328,7 @@ pub(crate) fn fill_size(ui: &Ui, id: Id, axis: Axis, measured: f32, offered: f32 // its measuring run, where the child measured at its own size. if axis == Axis::Y && env.content.1 - && let Some(line) = env.row_line.and_then(|line| line.height) + && let Some(line) = row_line_of(ui, &env).and_then(|line| line.height) { return measured.max(line); } @@ -502,7 +502,7 @@ pub fn levelled_unit(ui: &mut Ui, key: Id, levels: usize, mut show: impl FnMut(& let shrink = Env::of(ui).shrink; // The unit's content is no unit of the row: it runs in a room of its own, on the row's line only to fill it. Its // children take no slots on the line and report nothing to the row: the unit's block is what stands in the row. - let line = Env::of(ui).row_line.map(|line| RowLine { places: false, ..line }); + let line = row_line_of(ui, &Env::of(ui)).map(|line| RowLine { places: false, ..line }); let own_room = Env::of(ui).derive_local(|scoped| { scoped.start_room(); scoped.row_line = line; @@ -1161,7 +1161,10 @@ fn run_spread(ui: &mut Ui, id: Id, gap: f32, rtl: bool, body: &mut dyn FnMut( let sweep = |ui: &mut Ui, last: Option, leasts: Vec, probe: bool, body: &mut dyn FnMut(&mut Ui) -> R| { let flow = Flow::new(ui.available_rect_before_wrap(), rtl); pass_state::with::(&ctx, |cursors| cursors.0.insert(id, Cursor::Spread(Spread::new(flow, gap, last, leasts, probe)))); + // The children run in the sweep's own `Ui`: the row's env is installed for it, so they are its direct children. + let installed = Env::install_for(ui, Env::of(ui)); let inner = body(ui); + Env::restore(&ctx, installed); let spread = match pass_state::with::(&ctx, |cursors| cursors.0.remove(&id)) { Some(Cursor::Spread(spread)) => Some(spread), _ => None, @@ -1262,7 +1265,7 @@ pub fn place_in_flow(ui: &mut Ui, size: Vec2) -> Rect { /// and its first line to the container whose header or body it starts. pub(crate) fn place_in_cell(ui: &mut Ui, size: Vec2, child: InFlow) -> Rect { let env = Env::of(ui); - let (line, target) = (env.row_line.filter(|line| line.places), env.first_line); + let (line, target) = (row_line_of(ui, &env).filter(|line| line.places), env.first_line); let child = InFlow { first: if child.line { child.first } else { one_line(child.first, size.y) }, ..child }; // A one-line child of a bracketed row sits with its centre on the bracket's middle, between the controls its // rows reported before it. @@ -1304,6 +1307,18 @@ pub(crate) fn place_in_cell(ui: &mut Ui, size: Vec2, child: InFlow) -> Rect { placed } +/// The line of the unwrapped `Row` body a child placed in flow in `ui` stands on, `env` being `ui`'s env: only a direct +/// child of the row has it (`Env::installed_for`). +pub(crate) fn row_line_of(ui: &Ui, env: &Env) -> Option { + env.row_line.filter(|_| Env::installed_for(ui)) +} + +/// The `Grid` or spread `Row` body whose next cell a child placed in flow in `ui` takes, `env` being `ui`'s env: only a +/// direct child of the body takes one (`Env::installed_for`). +fn grid_of(ui: &Ui, env: &Env) -> Option { + env.grid.filter(|_| Env::installed_for(ui)) +} + /// Takes `size` in flow in `ui`: the next cell of the `Grid` or spread `Row` body that encloses it directly, else the /// next allocation of `ui`'s layout. Returns the rect taken and whether it is a cell. pub(crate) fn take_in_flow(ui: &mut Ui, size: Vec2) -> (Rect, bool) { @@ -1315,7 +1330,8 @@ pub(crate) fn take_in_flow(ui: &mut Ui, size: Vec2) -> (Rect, bool) { /// cells after it stay in their columns. Outside a grid it does nothing. pub fn empty_cell(ui: &mut Ui) { let env = Env::of(ui); - let grid = env.grid.filter(|body| pass_state::with::(ui.ctx(), |cursors| matches!(cursors.0.get(body), Some(Cursor::Grid(_))))); + let grid = + grid_of(ui, &env).filter(|body| pass_state::with::(ui.ctx(), |cursors| matches!(cursors.0.get(body), Some(Cursor::Grid(_))))); if grid.is_some() { take_in(ui, &env, Vec2::ZERO); } @@ -1325,7 +1341,8 @@ pub fn empty_cell(ui: &mut Ui) { /// closes. fn take_in(ui: &mut Ui, env: &Env, size: Vec2) -> (Rect, bool) { close_flow_start(ui.ctx(), env); - match cell_in(ui, env.grid, size) { + let grid = grid_of(ui, env); + match cell_in(ui, grid, size) { Some(cell) => (cell, true), None => (alloc::space(ui, size), false), } @@ -1335,7 +1352,8 @@ fn take_in(ui: &mut Ui, env: &Env, size: Vec2) -> (Rect, bool) { /// it; `None`, taking nothing, outside such a body. pub(crate) fn take_cell(ui: &mut Ui, size: Vec2) -> Option { let env = Env::of(ui); - let cell = cell_in(ui, env.grid, size); + let grid = grid_of(ui, &env); + let cell = cell_in(ui, grid, size); if cell.is_some() { close_flow_start(ui.ctx(), &env); } @@ -1351,7 +1369,8 @@ fn cell_in(ui: &mut Ui, grid: Option, size: Vec2) -> Option { /// The region the next child shown in flow in `ui` measures and is placed in: that of the next cell of the `Grid` or /// spread `Row` body that encloses `ui` directly, else the rest of `ui`'s layout. pub(crate) fn flow_region(ui: &Ui) -> Rect { - let cell = Env::of(ui).grid.and_then(|body| pass_state::with::(ui.ctx(), |cursors| cursors.0.get(&body).map(Cursor::region))); + let cell = + grid_of(ui, &Env::of(ui)).and_then(|body| pass_state::with::(ui.ctx(), |cursors| cursors.0.get(&body).map(Cursor::region))); cell.unwrap_or_else(|| ui.available_rect_before_wrap()) } @@ -1365,7 +1384,7 @@ pub(crate) fn row_alone_room(ui: &Ui) -> Option { /// The room a child shown in flow in `ui` measures and sizes in: the room of the next cell of the `Grid` or spread /// `Row` body that encloses `ui` directly, else the rest of `ui`'s layout. pub(crate) fn flow_room(ui: &Ui) -> Vec2 { - let cell = Env::of(ui).grid.and_then(|body| pass_state::with::(ui.ctx(), |cursors| cursors.0.get(&body).map(Cursor::room))); + let cell = grid_of(ui, &Env::of(ui)).and_then(|body| pass_state::with::(ui.ctx(), |cursors| cursors.0.get(&body).map(Cursor::room))); cell.unwrap_or_else(|| open_room(ui)) } @@ -1392,7 +1411,8 @@ fn open_room(ui: &Ui) -> Vec2 { /// The room a child that fills its width fills in `ui`: `flow_room`, except that in a `Content` column of a `Grid` it /// is the column's width, so the child never measures as the room and pushes the next column out of it. pub(crate) fn flow_fill_room(ui: &Ui) -> Vec2 { - let cell = Env::of(ui).grid.and_then(|body| pass_state::with::(ui.ctx(), |cursors| cursors.0.get(&body).map(Cursor::fill_room))); + let cell = + grid_of(ui, &Env::of(ui)).and_then(|body| pass_state::with::(ui.ctx(), |cursors| cursors.0.get(&body).map(Cursor::fill_room))); cell.unwrap_or_else(|| open_room(ui)) } @@ -1414,15 +1434,14 @@ pub(crate) fn shares_y(ui: &Ui) -> bool { /// Whether the next child shown in flow in `ui` goes in a `Content` column of the `Grid` body that encloses `ui` /// directly: like a content-sized column, a child that fills its width is placed at its measured width there. pub(crate) fn in_grid_content_column(ui: &Ui) -> bool { - Env::of(ui) - .grid + grid_of(ui, &Env::of(ui)) .is_some_and(|body| pass_state::with::(ui.ctx(), |cursors| cursors.0.get(&body).is_some_and(Cursor::in_content_column))) } /// The cell the next child shown in flow in `ui` takes, when the body that encloses `ui` directly is a `Grid`: the /// grid's body, the cell's column and whether the column is a `Content` one. pub(crate) fn grid_cell(ui: &Ui) -> Option<(Id, usize, bool)> { - let grid = Env::of(ui).grid?; + let grid = grid_of(ui, &Env::of(ui))?; pass_state::with::(ui.ctx(), |cursors| { let cursor = cursors.0.get(&grid)?; Some((grid, cursor.grid_column()?, cursor.in_content_column())) diff --git a/crates/ui-contract/src/layout/mod.rs b/crates/ui-contract/src/layout/mod.rs index c9afb1e1d..77090807d 100644 --- a/crates/ui-contract/src/layout/mod.rs +++ b/crates/ui-contract/src/layout/mod.rs @@ -26,6 +26,6 @@ pub(crate) use body::run_body; pub(crate) use body::{ BodyLine, BracketTook, HeldKind, InFlow, at_flow_start, close_first_line, close_flow_start_in, counting_fills, fill_share, fill_size, flow_fill_room, flow_region, flow_room, flow_start, grid_cell, held_kind, in_grid_content_column, leaf_fill_room, members_line, open_first_lines, - place_in_cell, report_bracket_control, report_held, report_least, report_shrink, row_alone_room, run_body_measured, shares_y, shrink_cap, - take_bracket, take_cell, take_in_flow, view_room, + place_in_cell, report_bracket_control, report_held, report_least, report_shrink, row_alone_room, row_line_of, run_body_measured, shares_y, + shrink_cap, take_bracket, take_cell, take_in_flow, view_room, }; diff --git a/crates/ui-contract/src/runtime/env.rs b/crates/ui-contract/src/runtime/env.rs index 9708a392e..a76a73908 100644 --- a/crates/ui-contract/src/runtime/env.rs +++ b/crates/ui-contract/src/runtime/env.rs @@ -307,7 +307,8 @@ pub(crate) enum ColumnSweep { } /// The line of an unwrapped `Row` body, as its direct children placed in flow see it: a child of one line sits with -/// its centre on the line's centre, and a child of more than one line, or with no line of its own, at its top. +/// its centre on the line's centre, and a child of more than one line, or with no line of its own, at its top. Read +/// through `layout::row_line_of`, which gives it only to the direct children (`Env::installed_for`). #[derive(Clone, Copy, Debug, PartialEq)] pub(crate) struct RowLine { /// The row body, which the children of a measuring run report their lines to. @@ -772,6 +773,16 @@ impl Env { Installed { viewport, node, previous } } + /// Whether `ui` is a `Ui` an env entry was installed for (`install_for`), not one that reads an entry installed + /// further up its stack. What a body sets up for the children it places in flow (its `Grid` or spread row's cells, + /// its unwrapped row's line) is for its direct children, which it shows in the `Ui` it installed its env for. A + /// `Ui` egui code makes inside that one (an `egui::Grid`, `ui.horizontal` in a caller's closure) lays out its own + /// children: a component placed there takes no cell or line of the body around it. + pub(crate) fn installed_for(ui: &Ui) -> bool { + let node = stack::node_id(ui); + PerViewport::::with(ui.ctx(), pass::current_viewport(ui.ctx()), |envs| envs.entries.contains_key(&node)) + } + /// Puts back the entry `install_for` replaced. pub(crate) fn restore(ctx: &Context, installed: Installed) { PerViewport::::with(ctx, installed.viewport, |envs| match installed.previous { diff --git a/crates/ui-widgets/tests/it/layout/row_line.rs b/crates/ui-widgets/tests/it/layout/row_line.rs index 92a3cd6b8..b77106fb8 100644 --- a/crates/ui-widgets/tests/it/layout/row_line.rs +++ b/crates/ui-widgets/tests/it/layout/row_line.rs @@ -1,7 +1,8 @@ //! The row line: a child of an unwrapped row that is one line sits with its centre on the row's line; a child of more //! than one line (a column, a field with a help line below) sits at the row's top. A control with its label after it //! or above it, and a field labelled above, sit on the line by their control, as a bare control does, in a row and in -//! a grid's cell. +//! a grid's cell. Components an app shows in an egui container inside a row or grid are laid out by that container: +//! they take neither the row's line nor the grid's cells. use craft_behavior::ColumnWidth; use craft_theme::ControlSize; @@ -165,3 +166,69 @@ fn a_labelled_check_box_sits_with_its_box_on_a_buttons_line_in_a_row_and_a_grid_ assert!((button.center().y - check.center().y).abs() <= ROUNDING, "{at}: the box {check:?} sits on the button's line {button:?}"); } } + +/// Check boxes an app lays out in an `egui::Grid` inside a row (a dialog's custom row of two columns of check boxes) +/// are no children of the row: the grid's rows are as high as the same grid's outside a row, not as high as the row's +/// line, and what follows the row starts under the grid. +#[trial] +fn check_boxes_in_an_egui_grid_in_a_row_keep_their_own_rows() { + const ROWS: usize = 3; + let shown = |in_row: bool| { + let seen = Rc::new(Cell::new(([Rect::NOTHING; ROWS], Rect::NOTHING))); + let out = Rc::clone(&seen); + let _fixture = settled(move |ui| { + let room = Rect::from_min_size(ui.available_rect_before_wrap().min, egui::vec2(300.0, 600.0)); + let out = Rc::clone(&out); + Stack::new().fill().show_at(ui, room, move |ui, _| { + let mut boxes = [Rect::NOTHING; ROWS]; + let mut grid = |ui: &mut egui::Ui| { + egui::Grid::new("boxes").num_columns(2).spacing([24.0, 6.0]).show(ui, |ui| { + for (row, rect) in boxes.iter_mut().enumerate() { + *rect = Checkbox::new(Bind::given(true)).label(format!("Left {row}")).show(ui).response.rect; + Checkbox::new(Bind::given(false)).label(format!("Right {row}")).show(ui); + ui.end_row(); + } + }); + }; + if in_row { + Row::new().fill().show(ui, |ui, _| grid(ui)); + } else { + Stack::new().fill().show(ui, |ui, _| grid(ui)); + } + let after = Button::new("After").show(ui).rect; + out.set((boxes, after)); + }); + }); + seen.get() + }; + let (alone, _) = shown(false); + let (boxes, after) = shown(true); + let pitch = |boxes: &[Rect; ROWS]| boxes[1].top() - boxes[0].top(); + assert!((pitch(&boxes) - pitch(&alone)).abs() <= ROUNDING, "the grid rows in a row {boxes:?} are as high as outside one {alone:?}"); + assert!(after.top() >= boxes[ROWS - 1].bottom() - ROUNDING, "the button {after:?} is under the grid {boxes:?}"); +} + +/// Check boxes in a `ui.horizontal` an app shows in a `Grid` body are laid out by it: side by side on one line, taking +/// none of the grid's cells. +#[trial] +fn check_boxes_in_an_egui_container_in_a_grid_take_no_cells() { + let seen = Rc::new(Cell::new((Rect::NOTHING, Rect::NOTHING))); + let out = Rc::clone(&seen); + let _fixture = settled(move |ui| { + let room = Rect::from_min_size(ui.available_rect_before_wrap().min, egui::vec2(400.0, 400.0)); + let out = Rc::clone(&out); + Stack::new().fill().show_at(ui, room, move |ui, _| { + Grid::new(&[ColumnWidth::Content, ColumnWidth::Content]).show(ui, |ui, _| { + Text::new("Name").show(ui); + let pair = ui.horizontal(|ui| { + let one = Checkbox::new(Bind::given(true)).label("One").show(ui).response.rect; + let two = Checkbox::new(Bind::given(true)).label("Two").show(ui).response.rect; + (one, two) + }); + out.set(pair.inner); + }); + }); + }); + let (one, two) = seen.get(); + assert!((one.center().y - two.center().y).abs() <= ROUNDING && two.left() >= one.right() - ROUNDING, "One {one:?}, then Two {two:?} on its line"); +}