diff --git a/crates/ui-contract/src/layout/body.rs b/crates/ui-contract/src/layout/body.rs index 1483647d6..26fe05874 100644 --- a/crates/ui-contract/src/layout/body.rs +++ b/crates/ui-contract/src/layout/body.rs @@ -679,6 +679,41 @@ fn level_of(ctx: &egui::Context, row: Id, unit: Id) -> (usize, Option) { .map_or((0, None), |(found, _)| (found.took.y.max(0.0) as usize, Some(found.took.x).filter(|width| width.is_finite() && *width > 0.0))) } +/// Shows `show`, the end group of the unwrapped `Row` body around it, at the row's end edge: the room the row's members +/// leave lies between the content before the group and the group. Show it last in the row. +/// +/// The group's children are members of the row as any other child is: they sit on its line and are units of its shrink +/// step, so those that carry a rank (`ContainerProps::fit_rank`) leave whole, the lowest rank first, before the row's +/// other units narrow or drop, and the room they leave goes to the free room. The group is placed from the width it +/// took in this pass's measuring run; without one, and outside an unwrapped row, it follows the content before it. +pub fn end_group(ui: &mut Ui, show: impl FnOnce(&mut Ui) -> R) -> R { + let env = Env::of(ui); + let Some(row) = env.flow_body.filter(|_| env.row_room.is_some() && env.row_line.is_some()) else { return show(ui) }; + let (ctx, rtl, key) = (ui.ctx().clone(), env.direction.is_rtl(), sub(row, "end_group")); + let sizing = measure::active(&ctx); + let room = ui.available_rect_before_wrap().width(); + if let Some(took) = this_pass(&ctx, sizing, key).filter(|took| took.x > 0.0 && room.is_finite()) { + ui.add_space((room - took.x).max(0.0)); + } + // The cursor's edge, which stays finite in a room unbounded at its end. + let cursor = |ui: &Ui| { + let rest = ui.available_rect_before_wrap(); + if rtl { rest.max.x } else { rest.min.x } + }; + let start = cursor(ui); + // In the row's counting sweep each member measures as in no room: one that the content before it crowds still + // reports its whole width, and leaves by its rank instead of narrowing. + let inner = if matches!(env.shrink, Some(Shrink::Count(_))) { + Env::run_in(ui, env.derive_local(|scoped| scoped.unbounded.0 = true), show) + } else { + show(ui) + }; + // The cursor stands a gap past the group's last child. + let took = ((cursor(ui) - start).abs() - ui.spacing().item_spacing.x).max(0.0); + found(&ctx, sizing, key, Vec2::new(room, 0.0), Vec2::new(took, 0.0)); + inner +} + /// Reports the least width of the body unit `body` of the row `row` that asks for it. pub(crate) fn report_least(ctx: &egui::Context, row: Id, body: Id, least: f32) { pass_state::with::(ctx, |reports| { diff --git a/crates/ui-contract/src/layout/mod.rs b/crates/ui-contract/src/layout/mod.rs index c9afb1e1d..bf217a002 100644 --- a/crates/ui-contract/src/layout/mod.rs +++ b/crates/ui-contract/src/layout/mod.rs @@ -7,7 +7,7 @@ mod label; mod slot; pub use arrange::{Placed, arrange}; -pub use body::{BodyLayout, CellAlign, GridFit, RowAlign, ShrinkOrder, ShrinkUnit, empty_cell, levelled_unit, plan_levels, shrink_widths}; +pub use body::{BodyLayout, CellAlign, GridFit, RowAlign, ShrinkOrder, ShrinkUnit, empty_cell, end_group, levelled_unit, plan_levels, shrink_widths}; pub use column_group::ColumnGroup; #[doc(hidden)] pub use column_group::ambient_width; diff --git a/crates/ui-shell/src/shell/title_bar/bar.rs b/crates/ui-shell/src/shell/title_bar/bar.rs index 027f366b8..7af19dc3d 100644 --- a/crates/ui-shell/src/shell/title_bar/bar.rs +++ b/crates/ui-shell/src/shell/title_bar/bar.rs @@ -10,7 +10,7 @@ use crate::contract::{ Access, BodyCx, BodyLayout, Common, Component, Container, ContainerProps, Cx, DragBand, EndRegion, Gap, HeaderRegion, NoValue, Outcome, Pad, PartsCx, Role, ShowContainer as _, Sizing, WordSetCx as _, }; -use crate::layout::{Placed, arrange}; +use crate::layout::{Placed, arrange, end_group}; use crate::runtime::access::Name; use crate::runtime::egui::pass; use crate::runtime::env::Env; @@ -70,7 +70,9 @@ impl Outcome for TitleBarOutput { /// In an embedded shell (an app preview, a story) the bar is a picture of a window: it acts on no window and reads /// none. The captions show hover and report their clicks, but send no window command, and the bar has no move band. The body is the leading content; the /// trailing content ([`trailing`](TitleBar::trailing)) sits before the end's captions. [`show_titled`](TitleBar::show_titled) -/// adds the title in the gap between them. Close never closes the window; see [`TitleBarOutput`]. +/// adds the title in the gap between them; [`show_fitted`](TitleBar::show_fitted) puts the trailing content on the +/// body's line, where its ranked members leave before the leading content narrows. Close never closes the window; see +/// [`TitleBarOutput`]. #[derive(crate::contract::Component, crate::contract::Props)] #[component(container)] pub struct TitleBar<'a> { @@ -249,6 +251,24 @@ impl<'a> TitleBar<'a> { out } + /// Shows the bar with `leading` content at its start and `trailing` content in its place of + /// [`trailing`](TitleBar::trailing), before the end's captions, both on the body's line: the trailing content is + /// the body's end group (`layout::end_group`). It takes the room the leading content's natural width leaves, so on + /// a short bar its members that carry a `fit_rank` leave whole, lowest rank first, before the leading content + /// narrows: an app bar whose menus stay whole while its end controls go. + pub fn show_fitted( + self, + ui: &mut Ui, + mut leading: impl FnMut(&mut Ui, &mut BodyCx<'_>) + 'a, + mut trailing: impl FnMut(&mut Ui, &mut BodyCx<'_>) + 'a, + ) -> TitleBarOutput { + self.show(ui, |ui, cx| { + leading(ui, cx); + end_group(ui, |ui| trailing(ui, cx)); + }) + .output + } + /// Shows one end of the bar and records its clicked caption: Close is reported, never acted on. fn end_of(&mut self, ui: &mut Ui, cx: &BodyCx<'_>, end: &End, window: &Window) { if let Some(caption) = show_end(ui, cx, end, window) { diff --git a/crates/ui-shell/src/shell/title_bar/bar/stories.rs b/crates/ui-shell/src/shell/title_bar/bar/stories.rs index 603af173a..810ceef2a 100644 --- a/crates/ui-shell/src/shell/title_bar/bar/stories.rs +++ b/crates/ui-shell/src/shell/title_bar/bar/stories.rs @@ -1,8 +1,10 @@ //! Stories of `TitleBar`: one per frame mode, and a maximized window. use super::TitleBar; -use crate::contract::Show as _; +use crate::buttons::Button; +use crate::contract::{Show as _, ShowContainer as _}; use crate::display::Text; +use crate::layout_components::Row; use crate::shell::Title; use crate::shell::{Caption, CaptionFamily, Captions, Frame, MacInset}; use crate::style::SurfaceKind; @@ -22,6 +24,20 @@ fn tools(ui: &mut Ui, _: &mut crate::contract::BodyCx<'_>) { Text::new("Share").show(ui); } +/// The width of the narrow fitted bar: the menus and Share fit, the ranked end controls do not. +const FITTED_NARROW: f32 = 420.0; + +/// End controls that leave a short bar by rank, then one that stays. +fn ranked_tools(ui: &mut Ui, _: &mut crate::contract::BodyCx<'_>) { + let _ = Row::new().fit_rank(1).show(ui, |ui, _| { + Button::new("Community").show(ui); + }); + let _ = Row::new().fit_rank(2).show(ui, |ui, _| { + Button::new("Learn").show(ui); + }); + Text::new("Share").show(ui); +} + #[story(TitleBar<'static>, covers = [Default], drive = Drive::Never)] fn title_bar_windows(ui: &mut Ui, _: &mut StoryData) -> StoryOutput { let frame = captions(CaptionFamily::Windows, &[], &[Caption::Minimize, Caption::Maximize, Caption::Close]); @@ -66,3 +82,12 @@ fn title_bar_on_background(ui: &mut Ui, _: &mut StoryData) -> StoryOutput { let frame = captions(CaptionFamily::Windows, &[], &[Caption::Minimize, Caption::Maximize, Caption::Close]); TitleBar::new().frame(frame).surface(SurfaceKind::Background).show_titled(ui, Title::new("Landscape.psd"), menu, tools).into() } + +/// A bar whose end controls fit after its menus: in a narrow window the menus stay whole and the end controls leave by +/// rank, the lowest first (Community, then Learn), while the unranked Share stays at the end, before the captions. +#[story(TitleBar<'static>, covers = [], drive = Drive::Never)] +fn title_bar_fitted_narrow(ui: &mut Ui, _: &mut StoryData) -> StoryOutput { + ui.set_max_width(FITTED_NARROW); + let frame = captions(CaptionFamily::Windows, &[], &[Caption::Minimize, Caption::Maximize, Caption::Close]); + TitleBar::new().frame(frame).show_fitted(ui, menu, ranked_tools).into() +} diff --git a/crates/ui-shell/tests/it/shell/title_bar.rs b/crates/ui-shell/tests/it/shell/title_bar.rs index fdbadce91..a44a27375 100644 --- a/crates/ui-shell/tests/it/shell/title_bar.rs +++ b/crates/ui-shell/tests/it/shell/title_bar.rs @@ -353,3 +353,78 @@ fn the_lights_room_follows_the_lights_without_mirroring_the_bar() { assert!(rect("Home").left() >= lights - 0.5, "Home {:?} after the lights at the left", rect("Home")); assert!(rect("Preferences").right() >= width - 20.0, "Preferences {:?} at the right end", rect("Preferences")); } + +/// What a bar with Windows captions `width` wide shows in `direction`, with its trailing content fitted after the +/// leading content or, without `fitted`, in the end region, by label: the leading button, the trailing buttons ranked 1 +/// and 2 when they are there, the unranked trailing button and the Minimize caption. +fn fitted(width: f32, direction: craft_i18n::Direction, fitted: bool) -> impl Fn(&str) -> Option { + use craft_ui::buttons::Button; + use craft_ui::contract::{BodyCx, Show as _, ShowContainer as _}; + use craft_ui::layout_components::Row; + let mut fixture = Fixture::new(move |ui| { + let layout = craft_window::Layout { left: vec![], right: vec![Caption::Minimize, Caption::Maximize, Caption::Close] }; + let frame = Frame::Integrated(Captions { family: CaptionFamily::Windows, layout }); + let leading = |ui: &mut Ui, _: &mut BodyCx<'_>| { + Button::new(MENUS).show(ui); + }; + let trailing = |ui: &mut Ui, _: &mut BodyCx<'_>| { + let _ = Row::new().fit_rank(1).show(ui, |ui, _| { + Button::new("Community").show(ui); + }); + let _ = Row::new().fit_rank(2).show(ui, |ui, _| { + Button::new("Learn").show(ui); + }); + Button::new("Share").show(ui); + }; + let _ = if fitted { + TitleBar::new().frame(frame).show_fitted(ui, leading, trailing) + } else { + TitleBar::new().frame(frame).trailing(trailing).show(ui, leading).output + }; + }); + fixture.direction(direction).room(vec2(width, 80.0)).settle(); + move |label: &str| fixture.by_label(label).map(|node| node.rect) +} + +/// The leading button of the fitted bar trial: menus wider than the trailing content. +const MENUS: &str = "File Edit Object Type Select Effect View Window Help"; + +/// A fitted bar's trailing content takes what the leading content leaves: as the bar narrows, its ranked members +/// leave whole, lowest rank first, while the leading content keeps its width; a ranked member that stays keeps its +/// whole width, even where the member before it took the room; and the rest of the trailing content stays at the end, +/// clear of the captions and no further from them than a wide unfitted bar puts it. Right to left places it the same, +/// mirrored. +#[trial] +fn a_fitted_bar_drops_its_trailing_members_by_rank_before_the_leading_content_narrows() { + use craft_i18n::Direction; + let wide = fitted(1400.0, Direction::Ltr, true); + let (whole, learn) = (wide(MENUS).expect("the leading button"), wide("Learn").expect("Learn on a wide bar")); + assert!(wide("Community").is_some(), "a wide bar shows every trailing member"); + let unfitted = fitted(1400.0, Direction::Ltr, false); + let end_gap = unfitted("Minimize").expect("Minimize").left() - unfitted("Share").expect("Share").right(); + let mut dropped_alone = false; + for width in (360..=900).step_by(30).map(|width| width as f32) { + let rect = fitted(width, Direction::Ltr, true); + let (leading, share, minimize) = + (rect(MENUS).expect("the leading button"), rect("Share").expect("Share"), rect("Minimize").expect("Minimize")); + let (low, high) = (rect("Community").is_some(), rect("Learn")); + assert!(!(low && high.is_none()), "rank 2 left before rank 1 at {width}"); + dropped_alone |= !low && high.is_some(); + if let Some(high) = high { + assert!(leading.width() >= whole.width() - 0.5, "the leading content narrowed to {leading:?} at {width} while a ranked member stayed"); + assert!(high.width() >= learn.width() - 0.5, "Learn narrowed to {high:?} at {width} instead of leaving"); + } + let gap = minimize.left() - share.right(); + assert!((-0.5..=end_gap + 0.5).contains(&gap), "Share {share:?} stands {gap} before the captions at {width}, an unfitted bar {end_gap}"); + let mirrored = fitted(width, Direction::Rtl, true); + assert_eq!( + [mirrored("Community").is_some(), mirrored("Learn").is_some()], + [low, high.is_some()], + "right to left keeps the same members at {width}" + ); + let (rtl_leading, rtl_share) = (mirrored(MENUS).expect("the leading button"), mirrored("Share").expect("Share")); + assert!((rtl_share.left() - (width - share.right())).abs() <= 0.5, "Share {rtl_share:?} mirrors {share:?} at {width}"); + assert!((rtl_leading.right() - (width - leading.left())).abs() <= 0.5, "the leading content {rtl_leading:?} mirrors {leading:?} at {width}"); + } + assert!(dropped_alone, "rank 1 never left alone"); +}