From 6c33621a06715619e8a82d1bc6e01ccfdbaa32fb Mon Sep 17 00:00:00 2001 From: dotCooCoo Date: Sat, 21 Nov 2026 15:30:38 -0800 Subject: [PATCH] Menu bar: the output reports the width every title takes side by side craft_ui::menu::MenuBarOutput and craft_menu::MenuBarOutput gain a `natural: f32` field: the width of every title side by side with none in the "..." menu, whatever room the bar had. `titles` stays the rect of the titles drawn. A host that puts controls after the titles on the same row compares `natural` with the room it leaves the bar, and can narrow its own controls before titles fold. The field is zero while the bar shows nothing (ScreenTop), and a memoized replay keeps it. Trials: a bar in a wide and a narrow rect (craft-ui-widgets) and MenuBarUi in a wide and a narrow Stack (craft-menu) report the same natural width, equal to the titles' width when all fit and larger than the drawn titles when some fold. --- crates/menu/README.md | 4 ++- crates/menu/src/bar.rs | 12 ++++++--- crates/menu/tests/it/title_bar.rs | 30 ++++++++++++++++++++- crates/ui-widgets/src/menu/bar.rs | 14 +++++++--- crates/ui-widgets/tests/it/menu/bar.rs | 36 +++++++++++++++++++++++++- 5 files changed, 86 insertions(+), 10 deletions(-) diff --git a/crates/menu/README.md b/crates/menu/README.md index cb82dc8f9e..307cd092ad 100644 --- a/crates/menu/README.md +++ b/crates/menu/README.md @@ -132,7 +132,9 @@ app translates. The dialog never opens a file dialog or touches the file system. ## Windows, Linux and web: the in-window bar -`MenuBarUi` draws a laid-out bar and returns the titles' rect (`MenuBarOutput`). Show it inside a +`MenuBarUi` draws a laid-out bar and returns the titles' rect and the width every title takes side by +side (`MenuBarOutput`). A host that puts controls after the titles on the same row compares that +width with the room it leaves the bar. Show it inside a `Stack` (a top panel's body) or the title bar's row: there the titles that don't fit its room fold into "…". Each item is a command row: its enabled state, check mark and chord follow the command in the diff --git a/crates/menu/src/bar.rs b/crates/menu/src/bar.rs index 4b2c81e742..9ea75cec33 100644 --- a/crates/menu/src/bar.rs +++ b/crates/menu/src/bar.rs @@ -48,11 +48,11 @@ pub(crate) fn keymap_platform(target: Target, os: Platform) -> Platform { /// A replay at another place moves the titles' rect with the bar. impl craft_ui::runtime::Shift for MenuBarOutput { fn shifted(&self, by: &egui::RecordShift) -> Self { - MenuBarOutput { titles: by.rect(self.titles) } + MenuBarOutput { titles: by.rect(self.titles), natural: self.natural } } fn snapped(&self, to: &Self, within: f32) -> Self { - MenuBarOutput { titles: craft_ui::runtime::Shift::snapped(&self.titles, &to.titles, within) } + MenuBarOutput { titles: craft_ui::runtime::Shift::snapped(&self.titles, &to.titles, within), natural: self.natural } } } @@ -63,6 +63,9 @@ pub struct MenuBarOutput { /// The menu titles alone, without the room the bar leaves after them /// ([`craft_ui::menu::MenuBarOutput::titles`]). pub titles: Rect, + /// The width every title takes side by side with none in "…", whatever room the bar had + /// ([`craft_ui::menu::MenuBarOutput::natural`]). Zero when the bar shows nothing. + pub natural: f32, } /// The bar for one frame: `MenuBarUi::new(&bar).show(ui)`. Draw it at the top of the window, inside a `Stack` (a top @@ -145,7 +148,7 @@ impl<'a> MenuBarUi<'a> { help, label: env.words::().label.clone(), }; - MemoView::new("menu-bar", props, show_bar).show(ui).unwrap_or(MenuBarOutput { titles: Rect::NOTHING }) + MemoView::new("menu-bar", props, show_bar).show(ui).unwrap_or(MenuBarOutput { titles: Rect::NOTHING, natural: 0.0 }) } } @@ -182,7 +185,8 @@ fn show_bar(props: &mut BarProps<'_>, ui: &mut Ui) -> MenuBarOutput { if let Some(menu) = props.help { bar = bar.help(MenuHelp::new(menu, &entries)); } - MenuBarOutput { titles: bar.show(ui).titles } + let shown = bar.show(ui); + MenuBarOutput { titles: shown.titles, natural: shown.natural } } /// What the kept text of a bar is made of: the model, the platform that names its shortcuts, the preferred mnemonics, diff --git a/crates/menu/tests/it/title_bar.rs b/crates/menu/tests/it/title_bar.rs index 1e79bf5c18..15543e8d97 100644 --- a/crates/menu/tests/it/title_bar.rs +++ b/crates/menu/tests/it/title_bar.rs @@ -1,6 +1,7 @@ //! The in-window menu bar in an app's title bar: every title shows while the bar has room for them, the document title //! takes the rest of the bar, and only a bar too narrow for every title folds the last into "…". In a `Stack` -//! narrower than its titles the bar folds the same way and draws nothing past the stack's edge. +//! narrower than its titles the bar folds the same way and draws nothing past the stack's edge. In any room the bar +//! reports the width its titles take side by side. use std::cell::Cell; use std::rc::Rc; @@ -131,3 +132,30 @@ fn a_bar_in_a_stack_narrower_than_its_titles_folds_them_and_draws_nothing_past_i fixture.click(&more).settle(); assert!(fixture.by_label("Help").is_some(), "Help is a row of the \"…\" menu"); } + +/// The bar reports the width every title takes side by side: in a stack that holds them all it is the titles' width, +/// and in a stack too narrow for them it is the same width while the drawn titles are narrower. +#[trial] +fn the_bar_reports_the_width_every_title_takes_in_a_wide_and_a_narrow_stack() { + let mut reports = Vec::new(); + for width in [1520.0, 240.0] { + let bar = Rc::new(model()); + let seen = Rc::new(Cell::new((0.0, 0.0))); + let out = Rc::clone(&seen); + let mut fixture = Fixture::new(move |ui| { + let bar = Rc::clone(&bar); + let out = Rc::clone(&out); + Stack::new().show(ui, move |ui, _| { + let shown = MenuBarUi::new(&bar).target(Target::Windows).show(ui); + out.set((shown.natural, shown.titles.width())); + }); + }); + fixture.os(Os::Windows).room(vec2(width, 600.0)).settle(); + reports.push(seen.get()); + } + let [(wide, wide_titles), (narrow, narrow_titles)] = [reports[0], reports[1]]; + assert!(wide > 0.0, "the bar reports a width"); + assert!((wide - wide_titles).abs() < 0.5, "with room for every title the natural width {wide} is the titles' width {wide_titles}"); + assert!((narrow - wide).abs() < 0.5, "a narrow bar reports the same natural width {narrow} as a wide one {wide}"); + assert!(narrow_titles < narrow, "a narrow bar draws titles {narrow_titles} narrower than its natural width {narrow}"); +} diff --git a/crates/ui-widgets/src/menu/bar.rs b/crates/ui-widgets/src/menu/bar.rs index c74ef97b5c..dced87415a 100644 --- a/crates/ui-widgets/src/menu/bar.rs +++ b/crates/ui-widgets/src/menu/bar.rs @@ -209,6 +209,10 @@ pub struct MenuBarOutput { pub open: bool, /// The titles alone (with "…" when it shows), from the first title's reading start to the last title's end. pub titles: Rect, + /// The width every title takes side by side with none in "…", whatever room the bar had: a host that places + /// controls after the titles on the same row compares it with the room it leaves the bar. Zero when the bar + /// shows nothing (`MenuPlacement::ScreenTop`). + pub natural: f32, } impl Outcome for MenuBarOutput { @@ -304,12 +308,14 @@ pub struct MenuBar<'a> { #[prop] keyboard: bool, open: bool, + /// The width of every title side by side, from this pass's measure or parts step. + natural: f32, } impl<'a> MenuBar<'a> { /// A bar named `label` with one title per menu, in reading order. pub fn new(label: impl Into