From f4bd0ae95f28f3ec91c9dc97374e0db237a3b269 Mon Sep 17 00:00:00 2001 From: aiongg Date: Sat, 21 Nov 2026 21:50:53 +0000 Subject: [PATCH] Layout: what a body sets up for its children in flow stops at egui containers; labels unbounded only in an egui::Grid cell Env::of clears the start_room facts (grid cursor, row line and room, shrink step, first line, flow start, fill share, content column, offered width) when the entry it finds was installed for an ancestor of the Ui, not for the Ui itself. Copies made from it (with_env, MemoView's counting install, with_item_facts) and the measure keys start from the cleared facts. Env::installed_for, row_line_of and grid_of go. Craft code that runs children in a Ui it makes installs its env there: run_sharing and measure_sharing's no-share block, run_spread's sweeps, Lazy and MemoView, Pages, measure_slot, the levelled unit's probe, a content-height pane at a caller rect (a room of its own), PaintUi's children. flow_scope is the craft form of ui.scope, push_id and with_layout around craft content; the dock group header and the Liquify footer use it, and the select search row and the date popup's Today row are craft Rows. A craft Grid documents that an egui container in it is no cell. The label step's signal is egui's Ui::is_grid (egui_cell): in an egui::Grid cell a labelled control or container decides its label at its own width and widens at the cell's right edge in either direction. ui.horizontal keeps wrapping and stacking at the room's end. --- crates/imaging/src/ui/liquify/mod.rs | 4 +- crates/ui-contract/src/contract/body.rs | 4 + crates/ui-contract/src/contract/body/hooks.rs | 12 +-- crates/ui-contract/src/contract/body/size.rs | 26 +++---- .../ui-contract/src/contract/label/place.rs | 40 ++++++---- crates/ui-contract/src/contract/label/step.rs | 27 ++++--- crates/ui-contract/src/contract/lazy.rs | 8 +- crates/ui-contract/src/contract/mount.rs | 4 +- crates/ui-contract/src/contract/pages.rs | 7 +- crates/ui-contract/src/contract/paint_ui.rs | 6 +- crates/ui-contract/src/layout/body.rs | 73 +++++++------------ crates/ui-contract/src/layout/mod.rs | 4 +- crates/ui-contract/src/layout/slot.rs | 5 +- crates/ui-contract/src/runtime/env.rs | 57 +++++++++++---- .../ui-contract/src/runtime/env/overrides.rs | 9 +++ crates/ui-panels/src/docking/group.rs | 39 +++++----- .../ui-widgets/src/layout_components/mod.rs | 4 + crates/ui-widgets/src/pickers/date.rs | 5 +- crates/ui-widgets/src/pickers/select.rs | 3 +- 19 files changed, 192 insertions(+), 145 deletions(-) diff --git a/crates/imaging/src/ui/liquify/mod.rs b/crates/imaging/src/ui/liquify/mod.rs index 61e2304eb..cd382c75e 100644 --- a/crates/imaging/src/ui/liquify/mod.rs +++ b/crates/imaging/src/ui/liquify/mod.rs @@ -81,7 +81,7 @@ use craft_ui::layout_components::Row; use craft_ui::primitives::Checker; use craft_ui::rails::{LastUsed, ToolRail, ToolSlot}; use craft_ui::runtime::commands::{CommandSet as _, Commands as _, FirePhase}; -use craft_ui::runtime::env::Env; +use craft_ui::runtime::env::{Env, flow_scope}; use craft_ui::runtime::memory::{SaveKey, saved_value, set_saved_value}; use craft_ui::runtime::words::CommonWords; use craft_ui::sections::{DialogRole, FooterButton}; @@ -462,7 +462,7 @@ impl LiquifyWorkspace { .opened(open) .buttons([FooterButton::new(common.ok.clone(), DialogRole::Accept), FooterButton::new(common.cancel.clone(), DialogRole::Cancel)]) .footer(|ui, _| { - ui.vertical_centered(|ui| { + let _ = flow_scope(ui, egui::UiBuilder::new().layout(egui::Layout::top_down(egui::Align::Center)), |ui| { let mut guard = this.borrow_mut(); let work: &mut LiquifyWorkspace = &mut guard; let events = Checkbox::new(Bind::given(work.preview)).label(preview_label.clone()).show(ui).events; diff --git a/crates/ui-contract/src/contract/body.rs b/crates/ui-contract/src/contract/body.rs index e03be55cb..82d220de4 100644 --- a/crates/ui-contract/src/contract/body.rs +++ b/crates/ui-contract/src/contract/body.rs @@ -166,6 +166,9 @@ pub(super) struct BodyHooks<'b> { /// The container is a row of a vertical flow, not a member of a row's line (`label::in_column`), as its parent /// flow showed it this run: its own measuring run shows its content in a flow of its own. in_column: bool, + /// The container is shown in a cell of an `egui::Grid` (`LabelIn::egui_cell`), as its parent flow showed it this + /// run: its label is decided at its own width. + egui_cell: bool, /// The line just before the container in its flow is a rule (`layout::flow_start`), as its parent flow showed it /// this run: a rule above its header is not drawn, so two rules never follow each other. after_rule: bool, @@ -245,6 +248,7 @@ impl<'b> BodyHooks<'b> { held: false, across: false, in_column: true, + egui_cell: false, after_rule: false, follows_end: false, } diff --git a/crates/ui-contract/src/contract/body/hooks.rs b/crates/ui-contract/src/contract/body/hooks.rs index 2e46f888b..701d3f3e0 100644 --- a/crates/ui-contract/src/contract/body/hooks.rs +++ b/crates/ui-contract/src/contract/body/hooks.rs @@ -291,13 +291,14 @@ 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, ui), at); + self.given = self.given_axes(props, &env, 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, ui), at, id); + self.egui_cell = ui.is_grid(); + self.given.1 |= self.given_room(props, &env, at, ui.ctx(), 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; @@ -1288,7 +1289,7 @@ impl BodyHooks<'_> { plan.regions.body.width() }, content_row: false, - unbounded: false, + egui_cell: self.egui_cell, text: resolved.label_value, side, help: props.help.as_ref(), @@ -1829,8 +1830,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.ui), at.at); - self.given.1 |= self.given_room(&props, (at.env, at.ui), at.at, id); + 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.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); @@ -1840,6 +1841,7 @@ impl BodyHooks<'_> { self.room.x = width; } self.in_column = label::in_column(at.ui); + self.egui_cell = at.ui.is_grid(); // A body that fills the main axis of the body it sits in is measured and shown at its share of it. The first // sweep of that body's measuring run only counts it: its contents run in the second sweep, at the share. let (x, y) = (craft_behavior::Axis::X, craft_behavior::Axis::Y); diff --git a/crates/ui-contract/src/contract/body/size.rs b/crates/ui-contract/src/contract/body/size.rs index cae3774d6..932b2fa41 100644 --- a/crates/ui-contract/src/contract/body/size.rs +++ b/crates/ui-contract/src/contract/body/size.rs @@ -29,9 +29,9 @@ pub(super) struct MeasureKey { /// Whether the body shows, and how much of it an animated collapse shows. shows: (bool, f32), env: EnvFacts, - /// Where its parent flow showed the container: as a row of a vertical flow (`label::in_column`), and with a rule - /// just before it (a rule above its header is not drawn). - flow: (bool, bool), + /// Where its parent flow showed the container: as a row of a vertical flow (`label::in_column`), with a rule just + /// before it (a rule above its header is not drawn), and in a cell of an `egui::Grid`. + flow: (bool, bool, bool), } /// The props a body was measured under, compared on the fields its measure reads. @@ -95,7 +95,7 @@ impl<'b> BodyHooks<'b> { width, shows: (self.open, self.presence.t), env: EnvFacts::of(env), - flow: (self.in_column, self.after_rule), + flow: (self.in_column, self.after_rule, self.egui_cell), } } @@ -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, ui): (&Env, &Ui), at: ShowAt) -> (bool, bool) { + pub(super) fn given_axes(&self, props: &ContainerProps, env: &Env, at: ShowAt) -> (bool, bool) { let sizing = self.sizing(props); - let fills_line = sizing == Sizing::FillWidth && unwrapped_row(props) && crate::layout::row_line_of(ui, env).is_some(); + let fills_line = sizing == Sizing::FillWidth && unwrapped_row(props) && env.row_line.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,15 +143,12 @@ 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, ui): (&Env, &Ui), at: ShowAt) -> Option { + pub(super) fn line_height(&self, props: &ContainerProps, env: &Env, 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; } - crate::layout::row_line_of(ui, env) - .filter(|line| line.settled) - .and_then(|line| line.height) - .filter(|height| height.is_finite() && *height > 0.0) + env.row_line.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` @@ -220,11 +217,10 @@ 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, ui): (&Env, &Ui), at: ShowAt, id: Id) -> bool { - let ctx = ui.ctx(); + pub(super) fn given_room(&self, props: &ContainerProps, env: &Env, at: ShowAt, ctx: &Context, id: Id) -> bool { let tolerance = measure_tolerance(crate::runtime::egui::scale::pixels_per_point(ctx)); self.capped(props, env, at) - && (self.line_height(props, (env, ui), at).is_some() + && (self.line_height(props, env, at).is_some() || layout_memory::measured(ctx, sub(id, "content")).is_some_and(|content| content.took.y > self.room.y + tolerance)) } @@ -349,7 +345,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), ui), at).unwrap_or(y); + let y = self.line_height(props, &Env::of(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/label/place.rs b/crates/ui-contract/src/contract/label/place.rs index ca9c88259..11403fc5d 100644 --- a/crates/ui-contract/src/contract/label/place.rs +++ b/crates/ui-contract/src/contract/label/place.rs @@ -87,7 +87,7 @@ pub(super) fn decide(ctx: &egui::Context, env: &Env, input: &LabelIn<'_>, kids: let gap = label_gap(env); let indent = after_indent(ctx, env, input, gap); let mut natural = leaf.measure_in(kids, craft_behavior::Proposal::Ideal); - if input.side.layout == FieldLayout::After && !input.unbounded { + if input.side.layout == FieldLayout::After && !input.egui_cell { // The label takes the room the control and the gap leave, and its detail wraps there. let room = (input.row.width() - indent - input.control_min - gap).max(0.0); if natural.x > room { @@ -95,9 +95,11 @@ pub(super) fn decide(ctx: &egui::Context, env: &Env, input: &LabelIn<'_>, kids: } } let baseline = leaf.baseline.get(); + // An egui grid's cell grows at its right edge in any direction (`LabelIn::egui_cell`); a row grows at its end edge. + let grows = if input.egui_cell { Grows::Right } else { Grows::End }; if input.side.layout == FieldLayout::Line { let number = env.control(input.size).number; - let flow = if input.unbounded { widened(flow, natural.x + gap + number) } else { flow }; + let flow = if input.egui_cell { widened(flow, natural.x + gap + number, grows) } else { flow }; let (cell, rest) = flow.take_end(number); let width = natural.x.min((rest.rect().width() - gap).max(0.0)); let drop = (cell.height() - natural.y).max(0.0) / 2.0; @@ -127,18 +129,18 @@ pub(super) fn decide(ctx: &egui::Context, env: &Env, input: &LabelIn<'_>, kids: // The room decides; a beside label gets the width it needs even where last frame's content was narrower. A label // that never sits beside stacks in an unbounded room as well. let stacks = (input.room < stack_below || stack_below == f32::INFINITY) - && !((input.content_row || input.unbounded) && input.side.layout == FieldLayout::Beside); - // A stacked label has the row's width at most, where its name elides; in a row without an end it keeps its own. - let column = if stacks && !input.unbounded { column.min(input.row.width()) } else { column }; + && !((input.content_row || input.egui_cell) && input.side.layout == FieldLayout::Beside); + // A stacked label has the row's width at most, where its name elides; in an egui grid's cell it keeps its own. + let column = if stacks && !input.egui_cell { column.min(input.row.width()) } else { column }; // A beside label that does not stack takes its column, the gap and the control's least, past the row's end where - // the row is narrower. A row without an end is as wide as its label and control need, whatever the layout. + // the row is narrower. An egui grid's cell is as wide as its label and control need, whatever the layout. let need = match input.side.layout { - FieldLayout::After if input.unbounded => indent + input.control_min + gap + natural.x, - _ if input.unbounded && stacks => column.max(input.control_min), + FieldLayout::After if input.egui_cell => indent + input.control_min + gap + natural.x, + _ if input.egui_cell && stacks => column.max(input.control_min), _ if stacks => 0.0, _ => stack_below, }; - let flow = widened(flow, need); + let flow = widened(flow, need, grows); if input.side.layout == FieldLayout::After { let flow = if indent > 0.0 { flow.take_start(indent).1 } else { flow }; @@ -163,16 +165,24 @@ pub(super) fn decide(ctx: &egui::Context, env: &Env, input: &LabelIn<'_>, kids: } } -/// `flow` widened from its start edge to `need` when it is narrower: its end edge moves, to the left under RTL. -fn widened(flow: Flow, need: f32) -> Flow { +/// The edge a flow that is too narrow grows at (`widened`). +#[derive(Clone, Copy)] +enum Grows { + /// Its end edge: the left one under RTL. + End, + /// Its right edge, in either direction. + Right, +} + +/// `flow` widened to `need` at the edge `grows` names, when it is narrower. +fn widened(flow: Flow, need: f32, grows: Grows) -> Flow { let mut wide = flow.rect(); if wide.width() >= need { return flow; } - if flow.is_rtl() { - wide.min.x = wide.max.x - need; - } else { - wide.max.x = wide.min.x + need; + match grows { + Grows::End if flow.is_rtl() => wide.min.x = wide.max.x - need, + Grows::End | Grows::Right => wide.max.x = wide.min.x + need, } Flow::new(wide, flow.is_rtl()) } diff --git a/crates/ui-contract/src/contract/label/step.rs b/crates/ui-contract/src/contract/label/step.rs index c186c28c6..f81ab751f 100644 --- a/crates/ui-contract/src/contract/label/step.rs +++ b/crates/ui-contract/src/contract/label/step.rs @@ -32,9 +32,9 @@ pub(crate) struct LabelIn<'a> { /// The row sits in a column that takes its width from its content: a beside label never stacks for want of room, /// since the column is as wide as the label, the gap and the control need. pub content_row: bool, - /// The row's flow sets no end to it (`LeafLabel::unbounded`): the row is as wide as its label and control need, so - /// the label neither wraps nor stacks for want of room. - pub unbounded: bool, + /// The row is a cell of an `egui::Grid` (`LeafLabel::egui_cell`): the row is as wide as its label and control need, + /// so the label neither wraps nor stacks for want of room, and it grows at its right edge in any direction. + pub egui_cell: bool, pub text: &'a Label, pub side: &'a SideLabel, /// The help line the container's footer shows; it joins the control's description with the label's detail. @@ -85,9 +85,9 @@ pub(crate) struct LeafLabel<'a> { pub parent: Option, /// The row the label and the child share. pub row: Rect, - /// The row's flow sets no end to it: egui lays it out and extends text there (an `egui::Grid` row, `ui.horizontal`). - /// `row` then gives only the row's start edge, top and height. - pub unbounded: bool, + /// The row is a cell of an `egui::Grid`: egui places it from the cell's left edge in any direction, and gives the + /// column the width it takes from the next frame. `row` then gives only the cell's left edge, top and height. + pub egui_cell: bool, pub text: &'a Label, pub side: &'a SideLabel, /// What the child inherits (`leaf_under`). @@ -154,7 +154,7 @@ fn leaf_input<'a>(env: &Env, at: &LeafLabel<'a>, row: Rect, facts: LeafFacts, tr row, room: row.width(), content_row: env.content.0, - unbounded: at.unbounded, + egui_cell: at.egui_cell, text: at.text, side: at.side, help: None, @@ -399,12 +399,10 @@ fn flow_leaf_row( // grid's cell or a horizontal layout. The row spans the room's own edges: a room that scrolls along x is unbounded // at its start edge, the left one under RTL. let row = Rect::from_min_max(avail.min, egui::pos2(avail.max.x, avail.min.y + ideal.y)); - // A flow egui lays out (no body installed its env for `ui`) in which egui extends text rather than wrapping it - // (`ui.horizontal`, an `egui::Grid` without a max column width) offers the row no end: the label is decided at - // its own width. An `egui::Grid` offers a column with no width from the last frame its least cell width, where - // the label would wrap and keep the column that narrow. - let unbounded = !Env::installed_for(ui) && ui.wrap_mode() == egui::TextWrapMode::Extend; - let at = LeafLabel { id, owner, parent: None, row, unbounded, text: &label, side: &side, under: &under }; + // An `egui::Grid` offers a column with no width from the last frame its least cell width, where the label would + // wrap and keep the column that narrow: in its cell the label is decided at its own width. + let egui_cell = ui.is_grid(); + let at = LeafLabel { id, owner, parent: None, row, egui_cell, text: &label, side: &side, under: &under }; let decided = decide_leaf(ui, &env, &at, &mut child, trailing.as_ref().map_or(Vec2::ZERO, |trailing| trailing.size)); // In a column that sizes from its content the row is the label plus the control, never the room around them. // A row of a horizontal flow is as wide as its label and control: the room it has there is unbounded. @@ -415,9 +413,10 @@ fn flow_leaf_row( let rtl = env.direction.is_rtl(); let span = |cell: Rect| Flow::new(Rect::from_min_size(cell.min, Vec2::new(cell.width(), ideal.y)), rtl).span(0.0, control_width); let (decided_control, decided_label) = (span(decided.control()), decided.label()); + // An egui grid's cell takes its extent from its left edge in any direction. let extent = if !content { avail.width() - } else if rtl { + } else if rtl && !egui_cell { avail.max.x - decided_control.min.x.min(decided_label.min.x) } else { decided_control.max.x.max(decided_label.max.x) - avail.min.x diff --git a/crates/ui-contract/src/contract/lazy.rs b/crates/ui-contract/src/contract/lazy.rs index 6594e2b68..f5d4ab15d 100644 --- a/crates/ui-contract/src/contract/lazy.rs +++ b/crates/ui-contract/src/contract/lazy.rs @@ -172,8 +172,10 @@ impl Lazy { open_record(&ctx, audited.is_some()); } let mut child = alloc::child(ui, UiBuilder::new().max_rect(available).layout(layout).scope_id(id)); - // Its extent in the body around it comes from its content's measure, so a change inside reaches that body. - let out = layout_memory::boundary(&ctx, false, || add(&mut child, &mut run)); + // Its extent in the body around it comes from its content's measure, so a change inside reaches that body. The + // content runs in the env of `ui`, as the direct children of the body around it: a plain `Lazy` keeps their + // line, cells and shrink step, and a memoized view starts its own room on top of it. + let out = layout_memory::boundary(&ctx, false, || Env::run_in(&mut child, env, |child| add(child, &mut run))); let rect = child.min_rect(); // The child `Ui` writes its widget rect when it drops: the record holds it as written. drop(child); @@ -317,7 +319,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) = crate::layout::row_line_of(ui, &Env::of(ui)).and_then(|line| line.centre) else { + let Some(centre) = Env::of(ui).row_line.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/contract/mount.rs b/crates/ui-contract/src/contract/mount.rs index 34137bf9a..06b25e3d6 100644 --- a/crates/ui-contract/src/contract/mount.rs +++ b/crates/ui-contract/src/contract/mount.rs @@ -149,7 +149,9 @@ impl Host for PaneHost { if self.0.content_height { let builder = egui::UiBuilder::new().max_rect(rect); let mut ui = crate::runtime::egui::alloc::child(parent, builder); - pass(&mut ui, Some(ShowAt::Flow), None); + // A layout room of its own, as a docked pane's. + let room = env.new_room(); + Env::run_in(&mut ui, room.clone(), |ui| pass(ui, Some(ShowAt::Flow), Some(&room))); crate::runtime::egui::alloc::advance_past(parent, ui.min_rect()); } else { pass(parent, Some(ShowAt::Rect(rect)), None); diff --git a/crates/ui-contract/src/contract/pages.rs b/crates/ui-contract/src/contract/pages.rs index 28c7b0916..02d0fb37f 100644 --- a/crates/ui-contract/src/contract/pages.rs +++ b/crates/ui-contract/src/contract/pages.rs @@ -3,6 +3,7 @@ use crate::layout::flow_region; use crate::runtime::egui::alloc; +use crate::runtime::env::Env; use crate::runtime::layout_memory; use crate::runtime::lazy; use crate::runtime::memory::sub; @@ -42,6 +43,8 @@ impl<'a, K: Hash + PartialEq> Pages<'a, K> { let base = sub(ui.scope_id(), ("pages", self.salt)); let region = flow_region(ui); let layout = *ui.layout(); + // Each page runs in the env of `ui`, as the direct children of the body around it. + let env = Env::of(ui); let hidden = self.pages.iter().filter(|key| *key != self.current).fold(Vec2::ZERO, |largest, key| { let id = sub(base, hash(key)); let kept = lazy::measured(&ctx, id).map(|(size, reads)| { @@ -59,7 +62,7 @@ impl<'a, K: Hash + PartialEq> Pages<'a, K> { layout_memory::boundary(&ctx, true, || { let room = Rect::from_min_size(region.min, Vec2::new(region.width(), f32::INFINITY)); let mut child = alloc::child(ui, UiBuilder::new().max_rect(room).layout(layout).scope_id(id).sizing_pass().invisible()); - page(&mut child, key); + Env::run_in(&mut child, env.clone(), |child| page(child, key)); child.min_rect().size() }) }); @@ -72,7 +75,7 @@ impl<'a, K: Hash + PartialEq> Pages<'a, K> { let id = sub(base, hash(self.current)); let mut child = alloc::child(ui, UiBuilder::new().max_rect(region).layout(layout).scope_id(id)); child.set_min_size(hidden.min(region.size())); - let out = page(&mut child, self.current); + let out = Env::run_in(&mut child, env, |child| page(child, self.current)); let rect = child.min_rect(); drop(child); // What the place took comes from the hidden pages' measures as well as from the shown page's components. diff --git a/crates/ui-contract/src/contract/paint_ui.rs b/crates/ui-contract/src/contract/paint_ui.rs index 1e61f4d8f..5ad18430c 100644 --- a/crates/ui-contract/src/contract/paint_ui.rs +++ b/crates/ui-contract/src/contract/paint_ui.rs @@ -163,7 +163,7 @@ impl PaintUi<'_> { let env = Env::of(self.ui); let mut child = child; let under = label::leaf_under(&env, &side, &child, (self.enabled, self.size)); - let at = LeafLabel { id, owner: self.id, parent: Some(self.id), row: rect, unbounded: false, text: &label, side: &side, under: &under }; + let at = LeafLabel { id, owner: self.id, parent: Some(self.id), row: rect, egui_cell: false, text: &label, side: &side, under: &under }; let ctx = self.ui.ctx().clone(); layout_memory::boundary(&ctx, true, || { let placed = crate::layout::ColumnGroup::field(&mut *self.ui, |ui| label::leaf(ui, &env, &at, &mut child, None)); @@ -183,12 +183,14 @@ impl PaintUi<'_> { fn child_with(&mut self, id: Id, rect: Rect, child: C, tip: Option>, inside: Option>) -> C::Output { let scope = sub(self.id, "children"); let parent = &mut *self.ui; + // The kids' `Ui` holds no entry of its own: they inherit the env of the composite's `Ui`. + let outer = Env::of(parent); let kids = self.kids.get_or_insert_with(|| { let builder = egui::UiBuilder::new().layout(child::pinned_layout()).scope_id(scope).max_rect(self.rect); alloc::child(parent, access::with_parent(builder, self.id)) }); let ctx = kids.ctx().clone(); - let env = super::cx::child_env(&Env::of(kids), &super::cx::Under { enabled: self.enabled, size: self.size, inside }); + let env = super::cx::child_env(&outer, &super::cx::Under { enabled: self.enabled, size: self.size, inside }); // A child shown while the owner's fade runs lies under the owner's veil, so a faded child adds none of its own. let env = match self.fade { Some(_) => { diff --git a/crates/ui-contract/src/layout/body.rs b/crates/ui-contract/src/layout/body.rs index 57accbccc..909d0f76e 100644 --- a/crates/ui-contract/src/layout/body.rs +++ b/crates/ui-contract/src/layout/body.rs @@ -92,7 +92,9 @@ impl BodyLayout { BodyLayout::Row { wrap: false, align: RowAlign::Start } } - /// Children row-major over `columns`: a `'static` slice, or columns built in the pass. + /// Children row-major over `columns`: a `'static` slice, or columns built in the pass. The cells are the body's + /// craft children: an egui container shown in the body is no cell, and egui places it below the cells placed so + /// far. Content that should take a cell goes in a craft body (`Row`). pub fn grid(columns: impl Into>) -> Self { BodyLayout::Grid { columns: columns.into(), fit: GridFit::Stack, cells: CellAlign::Center } } @@ -328,7 +330,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) = row_line_of(ui, &env).and_then(|line| line.height) + && let Some(line) = env.row_line.and_then(|line| line.height) { return measured.max(line); } @@ -502,7 +504,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 = row_line_of(ui, &Env::of(ui)).map(|line| RowLine { places: false, ..line }); + let line = Env::of(ui).row_line.map(|line| RowLine { places: false, ..line }); let own_room = Env::of(ui).derive_local(|scoped| { scoped.start_room(); scoped.row_line = line; @@ -521,13 +523,13 @@ pub fn levelled_unit(ui: &mut Ui, key: Id, levels: usize, mut show: impl FnMut(& }); let width = ui.max_rect().width(); for level in 0..levels.max(1) { - let installed = Env::install_for(ui, no_room.clone()); // Every level shows the same bodies under the same ids: each measures as a probe, so no level reuses a // record another level left, and none serves the sweep at the levels given, which records its own. let (_, took) = layout_memory::probing(&ctx, || { - crate::runtime::egui::sizing::run_sized(ui, width, child::column_layout(rtl), |ui| show(ui, level)) + crate::runtime::egui::sizing::run_sized(ui, width, child::column_layout(rtl), |ui| { + Env::run_in(ui, no_room.clone(), |ui| show(ui, level)) + }) }); - Env::restore(&ctx, installed); if level == 0 { widest = took; } @@ -889,7 +891,7 @@ fn run_sharing(ui: &mut Ui, env: &Env, id: Id, axis: Axis, layout: egui::Layo return inner; } if !shares_along(env, axis) { - return child::block(ui, None, layout, body).0; + return child::block(ui, None, layout, |ui| Env::run_in(ui, env.clone(), body)).0; } let key = sub(id, "share"); child::block(ui, None, layout, |ui| { @@ -943,7 +945,7 @@ fn measure_sharing(ui: &mut Ui, env: &Env, id: Id, axis: Axis, layout: egui:: let ctx = ui.ctx().clone(); let in_child = |ui: &mut Ui, body: &mut dyn FnMut(&mut Ui) -> R| { let mut child = laid_out_child(ui, layout); - let inner = body(&mut child); + let inner = Env::run_in(&mut child, env.clone(), body); (inner, child.min_rect()) }; if !shares_along(env, axis) { @@ -1158,13 +1160,12 @@ impl Spread { fn run_spread(ui: &mut Ui, id: Id, gap: f32, rtl: bool, body: &mut dyn FnMut(&mut Ui) -> R) -> (R, Option) { let ctx = ui.ctx().clone(); let layout = child::row_layout(rtl, false); - let sweep = |ui: &mut Ui, last: Option, leasts: Vec, probe: bool, body: &mut dyn FnMut(&mut Ui) -> R| { + let env = Env::of(ui); + let sweep = |ui: &mut Ui, env: &Env, 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 inner = Env::run_in(ui, env.clone(), body); let spread = match pass_state::with::(&ctx, |cursors| cursors.0.remove(&id)) { Some(Cursor::Spread(spread)) => Some(spread), _ => None, @@ -1177,13 +1178,13 @@ fn run_spread(ui: &mut Ui, id: Id, gap: f32, rtl: bool, body: &mut dyn FnMut( let count = this_pass(&ctx, false, leasts_id).map_or(0, |count| count.x as usize); let leasts = (0..count).map(|i| this_pass(&ctx, false, sub(id, ("least", i))).map_or(0.0, |least| least.x)).collect(); let last = this_pass(&ctx, false, id); - let (inner, spread) = child::block(ui, None, layout, |ui| sweep(ui, last, leasts, false, body)).0; + let (inner, spread) = child::block(ui, None, layout, |ui| sweep(ui, &env, last, leasts, false, body)).0; return (inner, natural(&spread)); } let room = ui.available_rect_before_wrap().size(); let ((inner, spread), rect, short) = crate::contract::keeping_ids(&ctx, || { let mut first = ui.new_child(egui::UiBuilder::new().layout(layout)); - let (inner, spread) = sweep(&mut first, None, Vec::new(), false, body); + let (inner, spread) = sweep(&mut first, &env, None, Vec::new(), false, body); // Half a point: less than a pixel shows at any scale the box runs. let short = spread.as_ref().is_some_and(|spread| spread.count >= 2 && room.x.is_finite() && spread.natural > room.x + 0.5); (((inner, spread), first.min_rect(), short), short) @@ -1191,21 +1192,19 @@ fn run_spread(ui: &mut Ui, id: Id, gap: f32, rtl: bool, body: &mut dyn FnMut( let (inner, spread, leasts) = if short { #[cfg(feature = "testing")] measure::count_least(&ctx, id); - let probe = Env::of(ui).derive_local(|scoped| { + let probe = env.derive_local(|scoped| { scoped.unbounded.0 = true; scoped.least = true; }); let leasts = crate::contract::keeping_ids(&ctx, || { let leasts = layout_memory::probing(&ctx, || { let mut least = ui.new_child(egui::UiBuilder::new().layout(layout)); - let installed = Env::install_for(&least, probe.clone()); - let (_, spread) = sweep(&mut least, None, Vec::new(), true, body); - Env::restore(&ctx, installed); + let (_, spread) = sweep(&mut least, &probe, None, Vec::new(), true, body); spread.map_or_else(Vec::new, |spread| spread.widths) }); (leasts, true) }); - let (inner, spread) = child::block(ui, None, layout, |ui| sweep(ui, None, leasts.clone(), false, body)).0; + let (inner, spread) = child::block(ui, None, layout, |ui| sweep(ui, &env, None, leasts.clone(), false, body)).0; (inner, spread, leasts) } else { ui.advance_cursor_after_rect(rect); @@ -1265,7 +1264,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) = (row_line_of(ui, &env).filter(|line| line.places), env.first_line); + let (line, target) = (env.row_line.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. @@ -1307,18 +1306,6 @@ 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) { @@ -1330,8 +1317,7 @@ 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 = - grid_of(ui, &env).filter(|body| pass_state::with::(ui.ctx(), |cursors| matches!(cursors.0.get(body), Some(Cursor::Grid(_))))); + let grid = env.grid.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); } @@ -1341,8 +1327,7 @@ 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); - let grid = grid_of(ui, env); - match cell_in(ui, grid, size) { + match cell_in(ui, env.grid, size) { Some(cell) => (cell, true), None => (alloc::space(ui, size), false), } @@ -1352,8 +1337,7 @@ 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 grid = grid_of(ui, &env); - let cell = cell_in(ui, grid, size); + let cell = cell_in(ui, env.grid, size); if cell.is_some() { close_flow_start(ui.ctx(), &env); } @@ -1369,8 +1353,7 @@ 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 = - grid_of(ui, &Env::of(ui)).and_then(|body| pass_state::with::(ui.ctx(), |cursors| cursors.0.get(&body).map(Cursor::region))); + let cell = Env::of(ui).grid.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()) } @@ -1384,7 +1367,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 = grid_of(ui, &Env::of(ui)).and_then(|body| pass_state::with::(ui.ctx(), |cursors| cursors.0.get(&body).map(Cursor::room))); + let cell = Env::of(ui).grid.and_then(|body| pass_state::with::(ui.ctx(), |cursors| cursors.0.get(&body).map(Cursor::room))); cell.unwrap_or_else(|| open_room(ui)) } @@ -1411,8 +1394,7 @@ 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 = - grid_of(ui, &Env::of(ui)).and_then(|body| pass_state::with::(ui.ctx(), |cursors| cursors.0.get(&body).map(Cursor::fill_room))); + let cell = Env::of(ui).grid.and_then(|body| pass_state::with::(ui.ctx(), |cursors| cursors.0.get(&body).map(Cursor::fill_room))); cell.unwrap_or_else(|| open_room(ui)) } @@ -1434,14 +1416,15 @@ 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 { - grid_of(ui, &Env::of(ui)) + Env::of(ui) + .grid .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 = grid_of(ui, &Env::of(ui))?; + let grid = Env::of(ui).grid?; 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 77090807d..c9afb1e1d 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, row_line_of, 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, run_body_measured, shares_y, shrink_cap, + take_bracket, take_cell, take_in_flow, view_room, }; diff --git a/crates/ui-contract/src/layout/slot.rs b/crates/ui-contract/src/layout/slot.rs index 79e573ffb..e4f26734b 100644 --- a/crates/ui-contract/src/layout/slot.rs +++ b/crates/ui-contract/src/layout/slot.rs @@ -7,6 +7,7 @@ use egui::{Ui, Vec2}; /// Runs `body` in an invisible sizing pass `width` wide and returns the size it took. Nothing is allocated in /// `ui` and nothing is painted; the closure sees the env of `ui`. pub fn measure_slot(ui: &mut Ui, width: f32, body: impl FnOnce(&mut Ui)) -> Vec2 { - let rtl = Env::of(ui).direction.is_rtl(); - sizing::run_sized(ui, width, child::column_layout(rtl), body).1 + let env = Env::of(ui); + let rtl = env.direction.is_rtl(); + sizing::run_sized(ui, width, child::column_layout(rtl), |ui| Env::run_in(ui, env, body)).1 } diff --git a/crates/ui-contract/src/runtime/env.rs b/crates/ui-contract/src/runtime/env.rs index a76a73908..fd345be5b 100644 --- a/crates/ui-contract/src/runtime/env.rs +++ b/crates/ui-contract/src/runtime/env.rs @@ -44,7 +44,7 @@ mod view; pub use app_values::{AppValues, EnvValue, StyleValue}; pub use craft_platform::appearance::SystemReader; use host_words::{host_dates, host_format, host_words}; -pub use overrides::{Overrides, ThemeScope, WheelScope, WordsOverride, with_env}; +pub use overrides::{Overrides, ThemeScope, WheelScope, WordsOverride, flow_scope, with_env}; pub use root::{ResolvedRoot, RootEnv}; pub use system::{SystemState, system_appearance}; pub(crate) use tokens::TokenInputs; @@ -307,8 +307,7 @@ 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. Read -/// through `layout::row_line_of`, which gives it only to the direct children (`Env::installed_for`). +/// 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. #[derive(Clone, Copy, Debug, PartialEq)] pub(crate) struct RowLine { /// The row body, which the children of a measuring run report their lines to. @@ -417,7 +416,8 @@ impl Scoped { /// Ends the layout room of the enclosing body: its flow grid and content column. The axes the body sizes from its /// content stay (`content`): like `cell_fills` and `least`, they say what a child that fills takes there, its - /// measured size or the room, and the place of a memoized view holds them (`flow`). + /// measured size or the room, and the place of a memoized view holds them (`flow`). `has_room` checks the same set, + /// and `Env::of` ends it for a `Ui` below the one an entry was installed for. pub(crate) fn start_room(&mut self) { self.grid = None; self.grid_top = false; @@ -435,6 +435,24 @@ impl Scoped { self.offered = None; } + /// Whether any of the facts `start_room` ends is set. + fn has_room(&self) -> bool { + self.grid.is_some() + || self.grid_top + || self.content_column + || self.given_column + || self.row_room.is_some() + || self.row_line.is_some() + || self.first_line.is_some() + || self.flow_body.is_some() + || self.column_row + || self.fill_body.is_some() + || self.fill_share.is_some() + || self.shrink.is_some() + || self.column.is_some() + || self.offered.is_some() + } + /// Starts the layout room of a memoized view's content (`start_room`), keeping whether the enclosing body is a /// content-sized or a given column, as `start_room` keeps the content axes. The view's content then measures and /// fills as it would shown without the view: a child that fills takes its measured size where the body sizes from @@ -664,14 +682,27 @@ impl Env { /// The env of `ui`: the nearest entry up the `Ui` stack, else the running viewport's root env, so the `Ui` /// of an egui `Area` or `Window` reads the app's root values. For app panels (layer 8) and caller closures only. /// + /// What a body sets up for the children it places in flow (`Scoped::start_room`: its `Grid` or spread row's cells, + /// its unwrapped row's line and shrink step, its flow start, its content column) is for its direct children, which + /// it shows in the `Ui` it installed its env for. A `Ui` egui code makes below that one (an `egui::Grid`, + /// `ui.horizontal`, `ui.push_id` in a caller's closure) lays out its own children: an entry found for an ancestor + /// of `ui` comes without those facts, so a component there takes no cell, line or shrink step of the body around it. + /// /// While apps still use `add_enabled_ui`, it also ANDs `ui.is_enabled()`, outside sizing passes. pub fn of(ui: &Ui) -> Env { let ctx = ui.ctx(); let viewport = pass::current_viewport(ctx); + let own = stack::node_id(ui); let found = PerViewport::::with(ctx, viewport, |envs| { - stack::nearest(ui, |node| envs.entries.get(&node).cloned()).or_else(|| envs.root.clone()) + stack::nearest(ui, |node| envs.entries.get(&node).map(|env| (env.clone(), node == own))) + .or_else(|| envs.root.clone().map(|env| (env, true))) }); - let env = found.unwrap_or_else(|| Env::from_root(ctx, &RootEnv::fallback(ctx), None, scale::pixels_per_point(ctx))); + let env = match found { + Some((env, true)) => env, + Some((env, false)) if env.scoped.has_room() => env.derive_local(Scoped::start_room), + Some((env, false)) => env, + None => Env::from_root(ctx, &RootEnv::fallback(ctx), None, scale::pixels_per_point(ctx)), + }; // egui's `invisible()` also disables the `Ui`, and a sizing `Ui` is invisible, not disabled: in a sizing pass // the env keeps the enabled state the real run has, so a memoized view keys alike in both. if style::is_enabled(ui) || ui.is_sizing_pass() { env } else { env.derive_local(|s| s.enabled = false) } @@ -773,14 +804,12 @@ 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)) + /// Runs `body` in `ui` with `env` installed for it, and puts back what the install replaced, also when `body` + /// panics. A body runs its direct children this way in a `Ui` it makes for them. + pub(crate) fn run_in(ui: &mut Ui, env: Env, body: impl FnOnce(&mut Ui) -> R) -> R { + let ctx = ui.ctx().clone(); + let _restore = Restore { ctx: &ctx, installed: Some(Env::install_for(ui, env)) }; + body(ui) } /// Puts back the entry `install_for` replaced. diff --git a/crates/ui-contract/src/runtime/env/overrides.rs b/crates/ui-contract/src/runtime/env/overrides.rs index cb9e7d2c4..8be00d377 100644 --- a/crates/ui-contract/src/runtime/env/overrides.rs +++ b/crates/ui-contract/src/runtime/env/overrides.rs @@ -176,6 +176,15 @@ impl Overrides { } } +/// Runs `body` in a child `Ui` built from `builder`, as `ui.scope_builder` does, under the env of `ui`: the components +/// it shows are children of the body around `ui` as those shown in `ui` itself are, on its line and in its cells. The +/// craft form of `ui.scope`, `ui.push_id` (`UiBuilder::id_salt`) and `ui.with_layout` around craft content: what a +/// body sets up for its children in flow does not reach a `Ui` egui code makes below the body's own (`Env::of`). +pub fn flow_scope(ui: &mut Ui, builder: egui::UiBuilder, body: impl FnOnce(&mut Ui) -> R) -> egui::InnerResponse { + let env = Env::of(ui); + ui.scope_builder(builder, |child| Env::run_in(child, env, body)) +} + /// Runs `body` in a child `Ui` with `set`'s changes applied to the inherited values. A scope that disables /// disables its child `Ui`; one that changes direction builds the child with an explicit layout and never /// inherits the parent's. diff --git a/crates/ui-panels/src/docking/group.rs b/crates/ui-panels/src/docking/group.rs index 237c1014a..6f0f181aa 100644 --- a/crates/ui-panels/src/docking/group.rs +++ b/crates/ui-panels/src/docking/group.rs @@ -510,26 +510,25 @@ impl Container for let surface_pressed = cx.surface_pressed(); let layout = if rtl { egui::Layout::right_to_left(egui::Align::Min) } else { egui::Layout::left_to_right(egui::Align::Min) }; let mut sent = Vec::new(); - let strip = ui - .with_layout(layout, |ui| { - ui.spacing_mut().item_spacing.x = gap; // design: allow[style-mutation](the header lays its title cells with an egui layout and sets that layout's gap) - if let Some(hook) = extra.as_mut() { - hook.run(ui, cx); - } - let row = TabRow { - anchor: self.anchor.clone(), - panels: self.panels.clone(), - active: self.active, - closable: self.closable, - wheel_selects: self.wheel_selects_tabs, - dock: self.dock, - sent: Outbox::new(&mut sent), - }; - // The env around the tab row holds only values its key follows (the shell, the dock's arrangement, a - // floating window's layer), no handle or context rebuilt each pass, so it replays at rest. - MemoView::new(("tabs", cx.id), row, tab_row::

).show(ui) - }) - .inner; + let strip = crate::runtime::env::flow_scope(ui, egui::UiBuilder::new().layout(layout), |ui| { + ui.spacing_mut().item_spacing.x = gap; // design: allow[style-mutation](the header lays its title cells with an egui layout and sets that layout's gap) + if let Some(hook) = extra.as_mut() { + hook.run(ui, cx); + } + let row = TabRow { + anchor: self.anchor.clone(), + panels: self.panels.clone(), + active: self.active, + closable: self.closable, + wheel_selects: self.wheel_selects_tabs, + dock: self.dock, + sent: Outbox::new(&mut sent), + }; + // The env around the tab row holds only values its key follows (the shell, the dock's arrangement, a + // floating window's layer), no handle or context rebuilt each pass, so it replays at rest. + MemoView::new(("tabs", cx.id), row, tab_row::

).show(ui) + }) + .inner; self.extra = extra; // A press on the group's empty body focuses its panel, as in Photoshop. if surface_pressed && let Some(strip) = strip { diff --git a/crates/ui-widgets/src/layout_components/mod.rs b/crates/ui-widgets/src/layout_components/mod.rs index a869191b7..300c0996e 100644 --- a/crates/ui-widgets/src/layout_components/mod.rs +++ b/crates/ui-widgets/src/layout_components/mod.rs @@ -212,6 +212,10 @@ layout_component!( ); layout_component!( /// Children row-major over a column list: a child in flow takes the next cell. + /// + /// The cells are the grid's craft children. An egui container shown in the body (`ui.horizontal`, an + /// `egui::Grid`) is no cell: egui places it below the cells placed so far, its children are not cells, and the + /// next child takes the next cell. Content that should take a cell goes in a craft body (`Row`). Grid, GridStyleKey, [] diff --git a/crates/ui-widgets/src/pickers/date.rs b/crates/ui-widgets/src/pickers/date.rs index d8819c941..0ebb83cd1 100644 --- a/crates/ui-widgets/src/pickers/date.rs +++ b/crates/ui-widgets/src/pickers/date.rs @@ -18,8 +18,9 @@ use crate::buttons::{IconButton, Link}; use crate::choice::Segmented; use crate::contract::{ Access, Bind, BindMut, Common, Component, Cx, Edit as _, EditResponse, Entry, Input, MeasureCx, Overlay, OverlayUi, Overlays as _, PaintUi, - PartsCx, Props as _, Remember as _, Role, Show as _, WordSetCx as _, clicked, + PartsCx, Props as _, Remember as _, Role, Show as _, ShowContainer as _, WordSetCx as _, clicked, }; +use crate::layout_components::Row; use crate::primitives::{IconSpec, TextKey, TextOptions, TextRun}; use crate::runtime::env::Label; use crate::text::{Adornment, FieldStyle, place, reserve}; @@ -338,7 +339,7 @@ impl Component for DatePicker<'_> { view = next; } let mut pick = page.picked.map(Some); - ui.horizontal(|ui| { + let _ = Row::new().show(ui, |ui, _| { let at_precision = today.map(|today| match view.precision { DatePrecision::Day => today, DatePrecision::Month => PickedDate::month(today.year, today.month.unwrap_or(1)), diff --git a/crates/ui-widgets/src/pickers/select.rs b/crates/ui-widgets/src/pickers/select.rs index 8aec2a378..f23620475 100644 --- a/crates/ui-widgets/src/pickers/select.rs +++ b/crates/ui-widgets/src/pickers/select.rs @@ -16,6 +16,7 @@ use crate::contract::{ MeasureCx, Numeric, Overlay, OverlayUi, Overlays as _, PaintUi, PartsCx, Pointer as _, PopupHandle, PopupKind, Props as _, Role, Show as _, ShowCollection as _, ShowContainer as _, Sizing, WordSetCx as _, step_stop, step_stops, }; +use crate::layout_components::Row; use crate::list::{List, ListRow, ThumbAspect}; use crate::menu::Menu; use crate::primitives::ContentPainterCx; @@ -318,7 +319,7 @@ impl Component for Select<'_, T> { let list = ui.scope_id().with("options"); let mut query = String::new(); if searchable || header_menu.is_some() { - ui.horizontal(|ui| { + let _ = Row::new().show(ui, |ui, _| { if searchable { query = search::show(ui, body, popup, cx, &name, list); }