MenuBarUi::icons / MenuItem::no_icon: text-only menus without icon column (trial)

This commit is contained in:
Dylan M. Taylor committed 2026-11-22 21:04:25 -05:00
1 parent b5e0109bfe
commit 2469c6b611
4 files changed
+50 -4

No files matched your search

+10 -2
View File
@@ -75,12 +75,13 @@ pub struct MenuBarUi<'a> {
preferred: &'a [(&'a str, char)],
keyboard: Option<bool>,
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;
+9 -2
View File
@@ -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);
}
}
}
+7
View File
@@ -213,6 +213,9 @@ pub struct MenuItem<'a> {
/// An icon before the label.
#[prop(into)]
icon: Option<IconSpec<'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,
/// A check mark, shown while the value is true; the row reserves the mark column either way.
#[prop]
check: Option<bool>,
@@ -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<IconSpec<'_>> {
if self.no_icon {
return None;
}
self.icon.clone().or_else(|| MenuItem::command_icon(command))
}
+24
View File
@@ -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");
}