diff --git a/crates/ui-contract/src/contract/body/hooks.rs b/crates/ui-contract/src/contract/body/hooks.rs index 6ea4318ea..95906aa1e 100644 --- a/crates/ui-contract/src/contract/body/hooks.rs +++ b/crates/ui-contract/src/contract/body/hooks.rs @@ -288,7 +288,7 @@ impl ShapeHooks for BodyHooks<'_> { self.overflow = self.wanted_width(props).map_or(0.0, |wanted| (wanted - room.max(0.0)).max(0.0)); let (header, end) = (self.measured_header.unwrap_or(Vec2::ZERO), self.measured_end.unwrap_or(Vec2::ZERO)); let height = self.regions_size(props, body, header, self.measured_footer, end).y; - Vec2::new(width, height) + Vec2::splat(2.0 * pad_of(scx.tokens, props.pad)) + Vec2::new(width, height) + 2.0 * self.pads(props, scx.tokens) } /// The regions run once in an invisible sizing pass over the room, as `content` runs them, and the container takes @@ -371,7 +371,7 @@ impl ShapeHooks for BodyHooks<'_> { if at.ui.is_sizing_pass() && self.given == (true, true) && self.room.is_finite() && !scrolled_share { return self.room; } - let pad = Vec2::splat(2.0 * pad_of(at.scx.tokens, props.pad)); + let pad = 2.0 * self.pads(&props, at.scx.tokens); let vertical = scroll_of(&props).is_some_and(|scroll| scroll.vertical_on()); let tolerance = measure_tolerance(crate::runtime::egui::scale::pixels_per_point(&ctx)); // A record a run of this pass left serves as before; a kept one serves only while the inputs it was measured @@ -500,7 +500,13 @@ impl ShapeHooks for BodyHooks<'_> { // where it is shown: its measure never sets the column's width. let held = !self.given.0 && self.held(props, &Env::of(ui)); let room = if held { room.min(crate::layout::flow_room(ui).x) } else { room }; - Vec2::new(crate::layout::fill_size(ui, id, craft_behavior::Axis::X, size.x, room, self.given.0 || held), size.y) + // Given the height of the row line it sits on, it fills that line (`given_axes`). + let height = if self.given.1 { + crate::layout::fill_size(ui, id, craft_behavior::Axis::Y, size.y, crate::layout::flow_room(ui).y, true) + } else { + size.y + }; + Vec2::new(crate::layout::fill_size(ui, id, craft_behavior::Axis::X, size.x, room, self.given.0 || held), height) } Sizing::Content => size, } @@ -520,6 +526,12 @@ impl ShapeHooks for BodyHooks<'_> { layout_memory::measured(ctx, sub(id, "first")).map(|found| found.took.y) } + /// A container with no header whose body is an unwrapped row reports that row's line as its first line. + fn first_is_row_line(&self, comp: &C) -> bool { + let props = comp.props(Internal::new()); + props.header.is_none() && super::size::unwrapped_row(props) + } + fn member(&self, comp: &C) -> bool { comp.props(Internal::new()).label.is_some() } @@ -539,7 +551,7 @@ impl ShapeHooks for BodyHooks<'_> { return None; } let tokens = cx.tokens; - let inner = content.shrink(pad_of(tokens, props.pad)); + let inner = content.shrink2(self.pads(&props, tokens)); let regions = self.regions(&props, inner, pad_of(tokens, props.pad), self.room_size(rect, inner), rtl, self.measured_sizes()); let (_, slots) = self.header_line(regions.header, pad_of(tokens, header.inset), parts, rtl); Some(std::rc::Rc::new(slots)) @@ -550,7 +562,7 @@ impl ShapeHooks for BodyHooks<'_> { let ctx = pcx.ui.ctx().clone(); let rtl = pcx.flow.is_rtl(); let tokens = pcx.tokens; - let inner = pcx.content_rect.shrink(pad_of(tokens, props.pad)); + let inner = pcx.content_rect.shrink2(self.pads(&props, tokens)); let room = self.room_size(rect, inner); let regions = self.regions(&props, inner, pad_of(tokens, props.pad), room, rtl, self.measured_sizes()); let scroll = scroll_of(&props); @@ -678,7 +690,7 @@ impl ShapeHooks for BodyHooks<'_> { let node = matches!(C::ROLE, Role::Node(_)); let scroll = scroll_of(&props); let pad = pad_of(&env.tokens, props.pad); - let inner = cx.core.content_rect.shrink(pad); + let inner = cx.core.content_rect.shrink2(self.pads(&props, &env.tokens)); let room = self.room_size(rect, inner); let mut regions = self.regions(&props, inner, pad, room, rtl, self.measured_sizes()); let surface_slot = SurfaceSlot::reserve(ui, matches!(props.mount, Mount::Layer(_))); @@ -781,6 +793,8 @@ impl ShapeHooks for BodyHooks<'_> { (regions.body, regions.beside) = (placed.body, placed.beside); } let line = shared_line(&props, beside); + // A row given its height sets its line at the middle of its band: the line is as high as the band. + let line = if self.centres_line(&props) { Some(line.unwrap_or(0.0).max(regions.body.height())) } else { line }; // B2 label: the control gets the rect the label leaves; the label column adds to the container's extent. let mut body_rect = regions.body; let mut label_rect = Rect::NOTHING; @@ -1246,7 +1260,7 @@ impl BodyHooks<'_> { ) -> Vec2 { let props = Arc::clone(&self.props); let (id, ctx) = (cx.core.id, at.ui.ctx().clone()); - let pad = Vec2::splat(2.0 * pad_of(at.scx.tokens, props.pad)); + let pad = 2.0 * self.pads(&props, at.scx.tokens); #[cfg(feature = "testing")] crate::runtime::measure::count_body(&ctx, id); layout_memory::begin_measure(&ctx, id); diff --git a/crates/ui-contract/src/contract/body/size.rs b/crates/ui-contract/src/contract/body/size.rs index ac0bee7c6..155d84b24 100644 --- a/crates/ui-contract/src/contract/body/size.rs +++ b/crates/ui-contract/src/contract/body/size.rs @@ -117,9 +117,13 @@ impl<'b> BodyHooks<'b> { /// The axes the container is given where `env` is its parent's: a rect it is shown at gives both; an inline /// container that fills an axis takes the room only where its parent was given that axis. + /// + /// A fill-width container whose body is a row, shown on the line of a row given its height, is given that height + /// too: the line is a fixed cross extent, and the container fills it (a bar in a shell slot). pub(super) fn given_axes(&self, props: &ContainerProps, env: &Env, at: ShowAt) -> (bool, bool) { let sizing = self.sizing(props); - let (x, y) = (sizing != Sizing::Content, sizing == Sizing::Fill); + let fills_line = sizing == Sizing::FillWidth && unwrapped_row(props) && env.row_line.is_some(); + let (x, y) = (sizing != Sizing::Content, sizing == Sizing::Fill || fills_line); match (at, &props.mount) { (ShowAt::Rect(_), _) => (true, true), (ShowAt::Flow, Mount::Inline) => (x && env.gives_width(), y && !env.content.1), @@ -134,6 +138,20 @@ impl<'b> BodyHooks<'b> { matches!((at, &props.mount), (ShowAt::Flow, Mount::Inline)) && self.sizing(props) == Sizing::Content && env.given_column } + /// Whether the container centres its body's line in the height it is given: its body is a row that does not scroll + /// along the height, and the container is given its height (a rect it is shown at, a fill, a fixed row line it + /// fills). Its padding then keeps the members off the start and end edges only (`pads`). + pub(super) fn centres_line(&self, props: &ContainerProps) -> bool { + self.given.1 && unwrapped_row(props) && !scroll_of(props).is_some_and(|scroll| scroll.vertical_on()) + } + + /// The container's padding along each axis: `ContainerProps::pad` on both, or along the line only where the + /// container centres its line in its height. + pub(super) fn pads(&self, props: &ContainerProps, tokens: &crate::style::UiTokens) -> Vec2 { + let pad = super::regions::pad_of(tokens, props.pad); + Vec2::new(pad, if self.centres_line(props) { 0.0 } else { pad }) + } + /// Whether the container's body is laid out across the width it is offered: the container is given the width, /// passes it on, or is held across the width its column settled on. pub(super) fn wide(&self) -> bool { @@ -293,3 +311,8 @@ impl<'b> BodyHooks<'b> { } } } + +/// Whether the container's body is a `Row` that does not wrap: its children sit on one line. +pub(super) fn unwrapped_row(props: &ContainerProps) -> bool { + matches!(props.layout, crate::layout::BodyLayout::Row { wrap: false, .. }) +} diff --git a/crates/ui-contract/src/contract/run.rs b/crates/ui-contract/src/contract/run.rs index d86c8ea5d..7e0acd15b 100644 --- a/crates/ui-contract/src/contract/run.rs +++ b/crates/ui-contract/src/contract/run.rs @@ -144,6 +144,13 @@ pub(crate) trait ShapeHooks { Some(height / 2.0) } + /// Whether the root's first line is the line of its own unwrapped `Row` body: it sits on a `Row`'s line by that + /// line even where a child of more than one line makes it taller, so the members of a row nested in a row share + /// the outer row's line. + fn first_is_row_line(&self, _comp: &C) -> bool { + false + } + /// Whether the root is one member of a bar's line, shown whole or not at all: a leaf is; a body is when it has a /// side label (a captioned field), and otherwise only lays out the members it holds. fn member(&self, _comp: &C) -> bool { @@ -557,6 +564,7 @@ pub(crate) trait ErasedPass<'a> { fn sized(&self, ui: &Ui, id: Id, size: Vec2) -> Vec2; fn fills_width(&self) -> bool; fn first_line(&self, ctx: &Context, id: Id, height: f32) -> Option; + fn first_is_row_line(&self) -> bool; fn member(&self) -> bool; fn fills_width_only(&self) -> bool; fn arena(&self) -> bool; @@ -804,7 +812,8 @@ fn place_root(p: &Pass<'_, '_, '_>, ui: &mut Ui, at: ShowAt, env: &Env, size: Ve .then(|| crate::layout::flow_room(ui).x) .filter(|width| width.is_finite()) .unwrap_or(0.0); - let flowed = steps::place(ui, at, Vec2::new(p.measured.x.min(size.x), size.y), InFlow { fill: false, first: first(size.y) }); + let line = p.p.first_is_row_line(); + let flowed = steps::place(ui, at, Vec2::new(p.measured.x.min(size.x), size.y), InFlow { fill: false, first: first(size.y), line }); // The column's first sweep learns which of its held children measured narrower than it settles on. if let Some(ColumnSweep::Count(column)) = env.column { crate::layout::report_held(&ctx, column, flowed.width()); @@ -814,7 +823,7 @@ fn place_root(p: &Pass<'_, '_, '_>, ui: &mut Ui, at: ShowAt, env: &Env, size: Ve } else { let fill = p.p.fills_width() && !p.p.fills_width_only(); let size = if inert { Vec2::ZERO } else { size }; - steps::place(ui, at, size, InFlow { fill, first: first(size.y) }) + steps::place(ui, at, size, InFlow { fill, first: first(size.y), line: p.p.first_is_row_line() }) }; (rect, held) } diff --git a/crates/ui-contract/src/contract/run/typed.rs b/crates/ui-contract/src/contract/run/typed.rs index 4086d4135..1ef59e1ad 100644 --- a/crates/ui-contract/src/contract/run/typed.rs +++ b/crates/ui-contract/src/contract/run/typed.rs @@ -405,6 +405,10 @@ impl<'a, C: Component> ErasedPass<'a> for TypedPass<'_, 'a, C> { self.hooks.first_line(self.comp, ctx, id, height) } + fn first_is_row_line(&self) -> bool { + self.hooks.first_is_row_line(self.comp) + } + fn member(&self) -> bool { self.hooks.member(self.comp) } diff --git a/crates/ui-contract/src/layout/body.rs b/crates/ui-contract/src/layout/body.rs index 88ee769c6..4f0a14fa6 100644 --- a/crates/ui-contract/src/layout/body.rs +++ b/crates/ui-contract/src/layout/body.rs @@ -898,6 +898,9 @@ pub(crate) struct InFlow { /// rows run along Y, or a container whose first child is one. The child is one line when that centre lies at /// half its height (`one_line`). pub first: Option, + /// `first` is the line of the child's own unwrapped `Row` body: the child sits on a row's line by it even when it + /// is more than one line (a nested row with a child of two lines). + pub line: bool, } /// How far a one-line child's first-line centre may lie from half its height: half a pixel at the scales the box runs. @@ -919,7 +922,7 @@ fn one_line(first: Option, height: f32) -> Option { /// sits at the vertical centre of its cell. #[doc(hidden)] pub fn place_in_flow(ui: &mut Ui, size: Vec2) -> Rect { - place_in_cell(ui, size, InFlow { fill: false, first: Some(size.y / 2.0) }) + place_in_cell(ui, size, InFlow { fill: false, first: Some(size.y / 2.0), line: false }) } /// `place_in_flow` for a child that is `child`. In a measuring run the child reports its line to the row around it @@ -927,7 +930,7 @@ pub fn place_in_flow(ui: &mut Ui, size: Vec2) -> Rect { 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, env.first_line); - let child = InFlow { first: one_line(child.first, size.y), ..child }; + let child = InFlow { first: if child.line { child.first } else { one_line(child.first, size.y) }, ..child }; let above = line.and_then(|line| line.centre).zip(child.first).map_or(0.0, |(centre, first)| (centre - first).max(0.0)); let slot = Vec2::new(size.x, line.and_then(|line| line.height).map_or(above + size.y, |height| height.max(above + size.y))); let (rect, in_cell) = take_in(ui, env.grid, slot); diff --git a/crates/ui-shell/src/shell/app_shell.rs b/crates/ui-shell/src/shell/app_shell.rs index 46cb00933..0bb389849 100644 --- a/crates/ui-shell/src/shell/app_shell.rs +++ b/crates/ui-shell/src/shell/app_shell.rs @@ -14,7 +14,7 @@ use super::screen_mode::{ScreenMode, ScreenModeCommands, ScreenModeFollow}; use super::words::ShellWords; use crate::contract::overlay::Resize; use crate::contract::{ - Bind, BodyCx, Common, Component, Container, ContainerProps, Cx, Mount, NoValue, Outcome, PaneEdge, Props as _, Remember as _, Role, ShellPane, + Bind, BodyCx, BodyLayout, Common, Component, Container, ContainerProps, Cx, Mount, NoValue, Outcome, PaneEdge, Props as _, Remember as _, Role, ShellPane, ShowContainer as _, Sizing, }; use crate::notices::ToastStack; @@ -120,9 +120,12 @@ impl ShellRegion { ShellRegion { common: Common::new(), props, shell: Some(shell), senses_surface: false } } - /// A pane of the window; it fills the room its edge gives it. + /// A pane of the window; it fills the room its edge gives it. A top or bottom pane lays its content along its edge + /// on one line at the middle of its height, so a bar in it fills that height and centres its members there; a + /// start, end or centre pane stacks its content. pub fn pane(pane: ShellPane) -> Self { - let props = ContainerProps::new().mount(Mount::Pane(pane)).focus(FocusPolicy::PANE); + let layout = if matches!(pane.edge, PaneEdge::Top | PaneEdge::Bottom) { BodyLayout::row() } else { BodyLayout::stack() }; + let props = ContainerProps::new().mount(Mount::Pane(pane)).focus(FocusPolicy::PANE).layout(layout); ShellRegion { common: Common::new(), props, shell: None, senses_surface: false } } } @@ -520,26 +523,13 @@ impl<'a, S> AppShell<'a, S> { continue; } let extent = placed.extent(&tokens); - let Placed { slot, edge, named, add, title, .. } = placed; + let Placed { slot, edge, named, add, .. } = placed; let pane = ShellPane::new(slot.key()).edge(*edge).extent(extent).at(rect_of(slot.key())); let mut region = ShellRegion::pane(pane).id_salt(("slot", slot.key())); - // A bar is as high as its content and the slot as high as its token, so the slot paints the bar's - // surface under it, and the bar sits at the slot's centre line: no band of the backdrop shows - // between the bar and the body. - let centred = !*title && matches!(edge, PaneEdge::Top | PaneEdge::Bottom); - if centred { - region = region.surface(SurfaceKind::Chrome); - } if let Some(label) = slot.name().cloned().or_else(|| named.map(|name| name(&words))) { region = region.label(label); } - region.show(ui, |ui, bcx| { - if centred { - ui.with_layout(egui::Layout::left_to_right(egui::Align::Center), |ui| add(ui, bcx, app)); - } else { - add(ui, bcx, app); - } - }); + region.show(ui, |ui, bcx| add(ui, bcx, app)); } }; show_slots(ui, false, app); diff --git a/crates/ui-shell/tests/it/shell/app_shell.rs b/crates/ui-shell/tests/it/shell/app_shell.rs index 79517abae..dcaba0203 100644 --- a/crates/ui-shell/tests/it/shell/app_shell.rs +++ b/crates/ui-shell/tests/it/shell/app_shell.rs @@ -1,10 +1,17 @@ -//! The app shell's named slots: a slot keeps its region name on either edge. +//! The app shell's named slots: a slot keeps its region name on either edge, and a bar in a top or bottom slot fills +//! it with its members on the slot's middle. -use craft_ui::contract::{PaneEdge, Role, Show as _}; +use craft_ui::bars::{Bar, BarKind}; +use craft_ui::buttons::Button; +use craft_ui::contract::{PaneEdge, Role, Show as _, ShowContainer as _}; use craft_ui::display::Text; +use craft_ui::layout_components::{Row, Stack}; use craft_ui::runtime::env::WindowKey; use craft_ui::shell::AppShell; -use craft_ui::testing::{Frames, trial}; +use craft_ui::testing::{Fixture, Frames, trial}; +use egui::{Rect, Vec2}; +use std::cell::Cell; +use std::rc::Rc; /// A tool rail placed at the end edge sits at the window's end and screen readers still find it as the Tools region. #[trial] @@ -29,3 +36,53 @@ fn the_tool_rail_at_the_end_edge_is_the_tools_region() { let names: Vec<&str> = tree.with_role(Role::GROUP).into_iter().filter_map(|key| tree.name_of(key)).collect(); assert!(names.contains(&"Tools"), "the end-edge rail is the Tools region: {names:?}"); } + +/// What the options-bar trial saw: the slot, the bar, a member of the bar and a member of a row nested in it. +#[derive(Clone, Copy)] +struct InSlot { + slot: Rect, + bar: Rect, + member: Rect, + nested: Rect, +} + +/// A bar in the options slot is as high as the slot, whatever its own padding and members take, and its members sit +/// on the slot's middle: a member of the bar's own row and a member of a row nested in it beside a child of two lines. +#[trial] +fn a_bar_in_a_top_slot_fills_it_and_centres_its_members_on_one_line() { + let seen = Rc::new(Cell::new(None::)); + let out = Rc::clone(&seen); + let mut fixture = Fixture::new(move |ui| { + let (mut bar, mut member, mut nested) = (Rect::NOTHING, Rect::NOTHING, Rect::NOTHING); + let shown = AppShell::new(WindowKey::main()) + .options_bar(true, |ui, _, _: &mut ()| { + bar = Bar::new("Options", BarKind::Options) + .show(ui, |ui, _| { + member = Button::new("Alpha").show(ui).rect; + Row::new().show(ui, |ui, _| { + nested = Button::new("Beta").show(ui).rect; + Stack::new().show(ui, |ui, _| { + Text::new("Above").show(ui); + Text::new("Below").show(ui); + }); + }); + }) + .output + .response + .rect; + }) + .body(|_, _, _| {}) + .show(ui, &mut ()); + let slot = shown.regions.slots.iter().find(|(key, _)| *key == "options").map(|(_, rect)| *rect); + if let Some(slot) = slot { + out.set(Some(InSlot { slot, bar, member, nested })); + } + }); + fixture.room(Vec2::new(600.0, 300.0)).settle(); + let InSlot { slot, bar, member, nested } = seen.get().expect("the options slot shows"); + const SLACK: f32 = 1.0; + assert!((bar.min.y - slot.min.y).abs() <= SLACK && (bar.max.y - slot.max.y).abs() <= SLACK, "the bar {bar:?} fills the slot {slot:?}"); + for (name, rect) in [("the bar's member", member), ("the nested row's member", nested)] { + assert!((rect.center().y - slot.center().y).abs() <= SLACK, "{name} {rect:?} sits on the middle of the slot {slot:?}"); + } +} diff --git a/crates/ui-widgets/src/bars/bar.rs b/crates/ui-widgets/src/bars/bar.rs index ce3854112..a968c9a83 100644 --- a/crates/ui-widgets/src/bars/bar.rs +++ b/crates/ui-widgets/src/bars/bar.rs @@ -77,7 +77,8 @@ impl Outcome for BarOutput { /// A bar over a chrome surface whose body holds the members in reading order: a tool bar's at the standard control /// size, an options or status bar's at the small one. They are one roving group unless the bar is made of /// [`tab_stops`](Bar::tab_stops). The bar is as tall as its tallest member plus the control padding, and takes the -/// width it is offered. A bar of one row lays its body out as a row; a bar of several rows as a stack whose rows the +/// width it is offered. On the line of a row given its height (a top or bottom slot of the shell) it fills that height +/// and its members sit on the line's middle, with its padding at the start and end only. A bar of one row lays its body out as a row; a bar of several rows as a stack whose rows the /// caller makes with `Row`. A member that fills (a leaf with `Sizing::FillWidth`, a fill-width `Row`) takes the room the view /// leaves. A row too long for the bar first narrows its selects and elidable text, the last first, down to their /// readable least (`TextRun::READABLE_CHARACTERS`), then scrolls, so a member past the edge is reached by Tab and