MenuBarUi::compact / MenuItem::compact: rows a text line tall (trial)

This commit is contained in:
Dylan M. Taylor
2026-11-22 22:03:59 -05:00
parent cfb37fb71f
commit ef11aa2431
4 changed files with 47 additions and 4 deletions
+9 -2
View File
@@ -76,12 +76,13 @@ pub struct MenuBarUi<'a> {
keyboard: Option<bool>,
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;
+9 -2
View File
@@ -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);
}
}
}
+9
View File
@@ -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<bool>,
@@ -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,
+20
View File
@@ -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:?}");
}