From ef11aa243167ef147afb89edcc4f8cfac6a22554 Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Sun, 22 Nov 2026 22:03:59 -0500 Subject: [PATCH] MenuBarUi::compact / MenuItem::compact: rows a text line tall (trial) --- crates/menu/src/bar.rs | 11 +++++++++-- crates/menu/src/context.rs | 11 +++++++++-- crates/ui-widgets/src/menu/item.rs | 9 +++++++++ crates/ui-widgets/tests/it/menu/mod.rs | 20 ++++++++++++++++++++ 4 files changed, 47 insertions(+), 4 deletions(-) diff --git a/crates/menu/src/bar.rs b/crates/menu/src/bar.rs index 2c0426365..41b153347 100644 --- a/crates/menu/src/bar.rs +++ b/crates/menu/src/bar.rs @@ -76,12 +76,13 @@ pub struct MenuBarUi<'a> { keyboard: Option, help_search: bool, icons: bool, + compact: bool, } impl<'a> MenuBarUi<'a> { /// The bar for `bar`, already laid out ([`crate::layout`]), on [`Target::current`]. pub fn new(bar: &'a MenuBar) -> MenuBarUi<'a> { - MenuBarUi { bar, target: Target::current(), english: None, preferred: &[], keyboard: None, help_search: false, icons: true } + MenuBarUi { bar, target: Target::current(), english: None, preferred: &[], keyboard: None, help_search: false, icons: true, compact: false } } /// The platform the bar is on, for the shortcut column ([`craft_keymap::Chord::display`]): ⌘ glyphs on @@ -130,12 +131,18 @@ impl<'a> MenuBarUi<'a> { self } + /// Whether the rows are a line of text tall instead of a control's height (off by default): a dense classic menu. + pub fn compact(mut self, on: bool) -> MenuBarUi<'a> { + self.compact = on; + self + } + /// Draws the bar and its open menu. Disabled rows never run. A chosen item is a fired command in the frame's /// output; [`Builtin::Quit`](crate::Builtin::Quit) and [`Builtin::Minimize`](crate::Builtin::Minimize) act on /// the root window instead. pub fn show(self, ui: &mut Ui) -> MenuBarOutput { let env = Env::of(ui); - let rows = Rows::new(self.target).english(self.english).preferred(self.preferred).icons(self.icons); + let rows = Rows::new(self.target).english(self.english).preferred(self.preferred).icons(self.icons).compact(self.compact); let labels = rows.labels(&self.bar.menus, |menu| menu.title.as_str(), |menu| menu.title.as_str(), |menu| rows.english_of(Labeled::Title(&menu.title))); let shared = &rows; diff --git a/crates/menu/src/context.rs b/crates/menu/src/context.rs index 1f81e39ed..00be14654 100644 --- a/crates/menu/src/context.rs +++ b/crates/menu/src/context.rs @@ -37,11 +37,13 @@ pub(crate) struct Rows<'a> { preferred: &'a [(&'a str, char)], /// Whether rows show their command's icon. icons: bool, + /// Whether rows are a text line tall. + compact: bool, } impl<'a> Rows<'a> { pub(crate) fn new(target: Target) -> Self { - Rows { target, english: None, preferred: &[], icons: true } + Rows { target, english: None, preferred: &[], icons: true, compact: false } } pub(crate) fn icons(mut self, on: bool) -> Self { @@ -49,6 +51,11 @@ impl<'a> Rows<'a> { self } + pub(crate) fn compact(mut self, on: bool) -> Self { + self.compact = on; + self + } + pub(crate) fn english(mut self, english: Option<&'a EnglishHook<'a>>) -> Self { self.english = english; self @@ -163,7 +170,7 @@ impl<'a> Rows<'a> { } } _ => { - command_row(ui, item, label, shortcut).no_icon(!self.icons).show(ui); + command_row(ui, item, label, shortcut).no_icon(!self.icons).compact(self.compact).show(ui); } } } diff --git a/crates/ui-widgets/src/menu/item.rs b/crates/ui-widgets/src/menu/item.rs index 7eab4f2e5..e29424c61 100644 --- a/crates/ui-widgets/src/menu/item.rs +++ b/crates/ui-widgets/src/menu/item.rs @@ -216,6 +216,10 @@ pub struct MenuItem<'a> { /// The row shows no icon, not even the one its command names: a text-only menu's row, which is also a line shorter. #[prop] no_icon: bool, + /// The row is its label's line plus the menu's padding, not a control's height: a dense classic menu. + #[style_key] + #[prop] + compact: bool, /// A check mark, shown while the value is true; the row reserves the mark column either way. #[prop] check: Option, @@ -258,6 +262,7 @@ impl<'a> MenuItem<'a> { heading: false, icon: None, no_icon: false, + compact: false, check: None, radio_mark: None, mnemonic: None, @@ -408,6 +413,10 @@ impl Component for MenuItem<'_> { // A row's content sits `space.pad.regular` in from the popup's own padding, so a label starts clear of the edge. let mut metrics = control.metrics(); metrics.padding = Insets::new(tokens.space.pad.regular, tokens.space.pad.regular, metrics.padding.top, metrics.padding.bottom); + if key.compact { + // The text line sets the height, as in a classic menu: no control height floor. + metrics.min_size.y = 0.0; + } MenuItemStyle { surface, metrics, diff --git a/crates/ui-widgets/tests/it/menu/mod.rs b/crates/ui-widgets/tests/it/menu/mod.rs index 4ab013cb2..acb0dccb8 100644 --- a/crates/ui-widgets/tests/it/menu/mod.rs +++ b/crates/ui-widgets/tests/it/menu/mod.rs @@ -76,3 +76,23 @@ fn a_row_with_no_icon_takes_no_icon_column() { }; assert_eq!(redo_left(true), redo_left(false), "the iconless row reserves no icon column"); } + +/// A compact row is its label's line tall, not a control's height: the rows of a dense classic menu stand closer. +#[trial] +fn a_compact_row_is_shorter_than_a_control_row() { + let pitch = |compact: bool| { + let mut fixture = Fixture::new(move |ui| { + Stack::new().show(ui, |ui, _| { + Menu::inline("Edit").show(ui, |ui, _| { + MenuItem::new("Undo").compact(compact).show(ui); + MenuItem::new("Redo").compact(compact).show(ui); + }); + }); + }); + fixture.settle(); + let top = |name: &str| fixture.texts().iter().find(|text| text.text == name).map(|text| text.rect.top()); + top("Redo").zip(top("Undo")).map(|(redo, undo)| redo - undo) + }; + let (control, compact) = (pitch(false), pitch(true)); + assert!(compact < control, "compact rows stand closer: {compact:?} against {control:?}"); +}