diff --git a/crates/ui-widgets/src/menu/bar.rs b/crates/ui-widgets/src/menu/bar.rs index 99e4e86c2..ba83634f2 100644 --- a/crates/ui-widgets/src/menu/bar.rs +++ b/crates/ui-widgets/src/menu/bar.rs @@ -37,7 +37,7 @@ use crate::runtime::env::{EnvView, Label}; use crate::runtime::focus::{self, FocusTarget}; use crate::runtime::memory::{Facts, Remember as _}; use crate::runtime::words::common::CommonWords; -use crate::style::{ComponentStyle, FocusLook, IconLook, StyleCx, SurfaceLook, TextLook, TypeRole}; +use crate::style::{ComponentStyle, FocusLook, IconLook, StyleCx, SurfaceLook, TextLook, TypeRole, UiTokens}; use crate::text::TextField; use craft_behavior::fit::{FitItem, FitKeep, fit_items}; use craft_behavior::{Arrows, Flow, Intent, Metrics, NavMove, NavShape, PartKey, PartRef, Proposal, Roving, Sense}; @@ -63,6 +63,19 @@ pub enum MenuTitlePad { Control, } +/// How a menu bar's titles stand. An app sets it once at its root (`RootEnv::values`); the default is a menu row's. +#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash, crate::runtime::env::EnvValue, crate::contract::MemoProps)] +#[env(style)] +#[non_exhaustive] +pub enum MenuTitleLook { + /// A menu row's: square corners, a control high, [`MenuTitlePad`] beside the text. + #[default] + Row, + /// A bare push button's, as egui's menu bar draws its titles: a push button high with the button's corners + /// (`radius.button`) and `space.gap.tight` beside the text, the titles `space.pad.control` apart. + Button, +} + /// The style of a title's face: a menu row's surface and ink (flat, square corners, no border), with the command /// highlight (`CommandHighlight`) under the pointer and while its menu is open or the keyboard is on it. #[derive(Clone, Debug, PartialEq, ComponentStyle)] @@ -114,15 +127,14 @@ impl Component for TitleFace { let surface = cx.command_row_look(); let ink = cx.command_row_ink(tokens.color.text.normal); let control = cx.control(); + let (surface, pad, height) = match (cx.env::(), cx.env::()) { + (MenuTitleLook::Button, _) => (surface.radius(UiTokens::corner(tokens.shape.radius.button)), tokens.space.gap.tight, cx.button_height()), + (MenuTitleLook::Row, MenuTitlePad::Regular) => (surface, tokens.space.pad.regular, control.height), + (MenuTitleLook::Row, MenuTitlePad::Control) => (surface, tokens.space.pad.control, control.height), + }; TitleFaceStyle { surface, - metrics: Metrics::new() - .padding(craft_behavior::Insets::horizontal(match cx.env::() { - MenuTitlePad::Regular => tokens.space.pad.regular, - MenuTitlePad::Control => tokens.space.pad.control, - })) - .min_size(Vec2::new(0.0, control.height)) - .gap(control.gap), + metrics: Metrics::new().padding(craft_behavior::Insets::horizontal(pad)).min_size(Vec2::new(0.0, height)).gap(control.gap), focus: cx.focus_look(), text: TextLook::new(TypeRole::Label, ink.clone()), icon: IconLook::new(control.icon, ink), @@ -387,13 +399,23 @@ impl<'a> MenuBar<'a> { if shown == 0 { words.bar_all.as_str().to_owned() } else { words.bar_more.as_str().to_owned() } } - /// Each title's width as its face measures, the face height, and the width of the "…" title. + /// Each title's width as its face measures, the face height, and the width of the "…" title, each with the gap + /// after it ([`Self::gap`]). fn widths(&self, cx: &MeasureCx<'_, Self>) -> (Vec, f32, f32) { let texts = self.texts(&cx.env); + let gap = Self::gap(cx); let sizes: Vec = texts.iter().map(|(text, _)| cx.face(Self::face(text, false, None))).collect(); let more = cx.face(Self::face(&Self::more_text(&cx.env.words::(), 1), false, None)); let height = sizes.iter().map(|size| size.y).fold(more.y, f32::max); - (sizes.iter().map(|size| size.x).collect(), height, more.x) + (sizes.iter().map(|size| size.x + gap).collect(), height, more.x + gap) + } + + /// The room after each title: `space.pad.control` between titles that stand as buttons, none between rows. + fn gap(cx: &MeasureCx<'_, Self>) -> f32 { + match cx.env.get::() { + MenuTitleLook::Button => cx.tokens.space.pad.control, + MenuTitleLook::Row => 0.0, + } } fn slot_part(open: Open) -> PartRef { @@ -500,14 +522,16 @@ impl Component for MenuBar<'_> { } let (widths, _, more) = self.widths(cx); let fit = fit_titles(&widths, more, rect.width()); + let gap = Self::gap(cx); let mut flow = Flow::new(rect, cx.flow.is_rtl()); for (at, width) in widths.iter().take(fit.shown).enumerate() { let (cell, rest) = flow.take_start(*width); flow = rest; + let cell = Flow::new(cell, cx.flow.is_rtl()).take_start(width - gap).0; cx.part(TITLE.at(at as u64), cell).sense(Sense::CLICK | Sense::FOCUS).focusable().toggles_at("menu", at); } if fit.overflow { - let (cell, _) = flow.take_start(more); + let cell = flow.take_start(more - gap).0; cx.part(MORE, cell).sense(Sense::CLICK | Sense::FOCUS).focusable().toggles_at("menu", fit.shown); } cx.roving(Roving::new(NavShape::Flat).arrows(Arrows::Horizontal)); diff --git a/crates/ui-widgets/src/menu/bar/stories.rs b/crates/ui-widgets/src/menu/bar/stories.rs index f617bd4b8..c56fab9a7 100644 --- a/crates/ui-widgets/src/menu/bar/stories.rs +++ b/crates/ui-widgets/src/menu/bar/stories.rs @@ -1,6 +1,6 @@ //! Stories of the menu bar, the edit menu and the panel menu. -use super::{MenuBar, MenuHelp, MenuTitle, MenuTitlePad}; +use super::{MenuBar, MenuHelp, MenuTitle, MenuTitleLook, MenuTitlePad}; use crate::buttons::{IconButton, PopupMark}; use crate::contract::{Entry, PopupKind, Props as _, Show as _, ShowContainer as _, Trigger}; use crate::display::Divider; @@ -103,6 +103,21 @@ fn menu_bar_close_titles(ui: &mut Ui, _: &mut StoryData) -> StoryOutput { .into() } +/// Titles that stand as bare push buttons (`MenuTitleLook::Button`), a push button high with its corners and the tight +/// gap beside the text, as egui's menu bar draws them; the open File menu's title shows the hover tint. +#[story(MenuBar<'static>, covers = [], drive = Drive::Custom(open_file))] +fn menu_bar_button_titles(ui: &mut Ui, _: &mut StoryData) -> StoryOutput { + with_env( + ui, + |overrides| { + overrides.values.set(MenuTitleLook::Button); + overrides.values.set(CommandHighlight::Hover); + }, + |ui| MenuBar::new("Main menu", titles()).show(ui), + ) + .into() +} + /// The open File menu under `CommandHighlight::Hover`: the open title and the active row show the hover tint with /// their own ink, as an app sets it at its root for a quiet command highlight. #[story(MenuBar<'static>, covers = [], drive = Drive::Custom(open_file))] diff --git a/crates/ui-widgets/src/menu/mod.rs b/crates/ui-widgets/src/menu/mod.rs index a151906f4..aad7b2ae1 100644 --- a/crates/ui-widgets/src/menu/mod.rs +++ b/crates/ui-widgets/src/menu/mod.rs @@ -15,7 +15,7 @@ pub(crate) mod stories; mod words; pub use alternate::Alternate; -pub use bar::{MenuBar, MenuBarOutput, MenuBarStyle, MenuHelp, MenuTitle, MenuTitlePad}; +pub use bar::{MenuBar, MenuBarOutput, MenuBarStyle, MenuHelp, MenuTitle, MenuTitleLook, MenuTitlePad}; pub(crate) use edit::text_menu; pub use edit::{EditMenu, EditMenuOutput}; pub use icon_row::{MenuIcon, MenuIconRow, MenuIconRowOutput};