From 2469c6b611ae0dedaf7b122e4cdc8bdfbd75cd1a Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Sun, 22 Nov 2026 21:04:25 -0500 Subject: [PATCH] MenuBarUi::icons / MenuItem::no_icon: text-only menus without icon column (trial) --- crates/menu/src/bar.rs | 12 ++++++++++-- crates/menu/src/context.rs | 11 +++++++++-- crates/ui-widgets/src/menu/item.rs | 7 +++++++ crates/ui-widgets/tests/it/menu/mod.rs | 24 ++++++++++++++++++++++++ 4 files changed, 50 insertions(+), 4 deletions(-) diff --git a/crates/menu/src/bar.rs b/crates/menu/src/bar.rs index ed436e6e1..2c0426365 100644 --- a/crates/menu/src/bar.rs +++ b/crates/menu/src/bar.rs @@ -75,12 +75,13 @@ pub struct MenuBarUi<'a> { preferred: &'a [(&'a str, char)], keyboard: Option, help_search: bool, + icons: 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 } + MenuBarUi { bar, target: Target::current(), english: None, preferred: &[], keyboard: None, help_search: false, icons: true } } /// The platform the bar is on, for the shortcut column ([`craft_keymap::Chord::display`]): ⌘ glyphs on @@ -122,12 +123,19 @@ impl<'a> MenuBarUi<'a> { self } + /// Whether the rows show their commands' icons (on by default). Off, the menus are text only and their rows a + /// line shorter, as a classic menu bar's. + pub fn icons(mut self, on: bool) -> MenuBarUi<'a> { + self.icons = 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); + let rows = Rows::new(self.target).english(self.english).preferred(self.preferred).icons(self.icons); 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 d851943c1..1f81e39ed 100644 --- a/crates/menu/src/context.rs +++ b/crates/menu/src/context.rs @@ -35,11 +35,18 @@ pub(crate) struct Rows<'a> { target: Target, english: Option<&'a EnglishHook<'a>>, preferred: &'a [(&'a str, char)], + /// Whether rows show their command's icon. + icons: bool, } impl<'a> Rows<'a> { pub(crate) fn new(target: Target) -> Self { - Rows { target, english: None, preferred: &[] } + Rows { target, english: None, preferred: &[], icons: true } + } + + pub(crate) fn icons(mut self, on: bool) -> Self { + self.icons = on; + self } pub(crate) fn english(mut self, english: Option<&'a EnglishHook<'a>>) -> Self { @@ -156,7 +163,7 @@ impl<'a> Rows<'a> { } } _ => { - command_row(ui, item, label, shortcut).show(ui); + command_row(ui, item, label, shortcut).no_icon(!self.icons).show(ui); } } } diff --git a/crates/ui-widgets/src/menu/item.rs b/crates/ui-widgets/src/menu/item.rs index e92656d50..7eab4f2e5 100644 --- a/crates/ui-widgets/src/menu/item.rs +++ b/crates/ui-widgets/src/menu/item.rs @@ -213,6 +213,9 @@ pub struct MenuItem<'a> { /// An icon before the label. #[prop(into)] icon: Option>, + /// 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, /// A check mark, shown while the value is true; the row reserves the mark column either way. #[prop] check: Option, @@ -254,6 +257,7 @@ impl<'a> MenuItem<'a> { submenu: false, heading: false, icon: None, + no_icon: false, check: None, radio_mark: None, mnemonic: None, @@ -304,6 +308,9 @@ impl<'a> MenuItem<'a> { } fn lead_icon(&self, command: Option<&ResolvedCommand>) -> Option> { + if self.no_icon { + return None; + } self.icon.clone().or_else(|| MenuItem::command_icon(command)) } diff --git a/crates/ui-widgets/tests/it/menu/mod.rs b/crates/ui-widgets/tests/it/menu/mod.rs index 2f9a6c6ed..4ab013cb2 100644 --- a/crates/ui-widgets/tests/it/menu/mod.rs +++ b/crates/ui-widgets/tests/it/menu/mod.rs @@ -52,3 +52,27 @@ fn rows_of_an_inline_menu_shown_from_its_container_record_take_the_columns_of_la fixture.frame(vec![]); } } + +/// A text-only row shows no icon and reserves no icon column, even with an icon set: its menu's labels start where a +/// menu with no icons at all starts them. +#[trial] +fn a_row_with_no_icon_takes_no_icon_column() { + let redo_left = |with_icon: bool| { + let mut fixture = Fixture::new(move |ui| { + Stack::new().show(ui, |ui, _| { + Menu::inline("Edit").show(ui, |ui, _| { + let undo = MenuItem::new("Undo"); + if with_icon { + undo.icon(IconSpec::svg(IconId::new("history"))).no_icon(true).show(ui); + } else { + undo.show(ui); + } + MenuItem::new("Redo").show(ui); + }); + }); + }); + fixture.settle(); + fixture.texts().iter().find(|text| text.text == "Redo").map(|text| text.rect.left() - fixture.shown().left()) + }; + assert_eq!(redo_left(true), redo_left(false), "the iconless row reserves no icon column"); +}