diff --git a/crates/ui-panels/src/rails/flyout.rs b/crates/ui-panels/src/rails/flyout.rs index 30fd2e0e23..34ae103c7a 100644 --- a/crates/ui-panels/src/rails/flyout.rs +++ b/crates/ui-panels/src/rails/flyout.rs @@ -1,7 +1,7 @@ //! `FlyoutButton`: a rail button that stands for a group of tools and opens them as a flyout. use super::tool::{RailTool, SlotDrop, SlotId, ToolDrag, ToolDropped}; -use crate::buttons::IconButton; +use crate::buttons::{IconButton, PopupMark}; use crate::contract::overlay::{Anchor, Place}; use crate::contract::{ Access, Common, Component, Cx, DragPreview, MeasureCx, Name, NoValue, Outcome, OverlayUi, Overlays as _, PaintUi, PartsCx, Pointer as _, @@ -161,7 +161,7 @@ impl<'a, T: Value + 'static> FlyoutButton<'a, T> { }; let mut face = face.selected(selected).glyphs(glyphs); if self.has_flyout() { - face = face.popup(PopupKind::Menu).popup_mark(true); + face = face.popup(PopupKind::Menu).popup_mark(PopupMark::ToolGroup); } face } diff --git a/crates/ui-panels/src/sections/footer.rs b/crates/ui-panels/src/sections/footer.rs index 3c9f94d33f..dc3f8d3d93 100644 --- a/crates/ui-panels/src/sections/footer.rs +++ b/crates/ui-panels/src/sections/footer.rs @@ -1,7 +1,7 @@ //! `FooterBar` (16.4): the container that fills a container's footer region. use super::words::SectionWords; -use crate::buttons::{Button, IconButton}; +use crate::buttons::{Button, IconButton, PopupMark}; use crate::contract::{ Access, BodyLayout, Common, Component, Container, ContainerProps, Cx, DropMark, DropOperation, DropTarget, LeastWidth, NoValue, Outcome, Pad, PaintUi, Pointer as _, PopupKind, Props as _, Role, RowAlign, Show as _, ShowContainer as _, Sizing, TooltipSpec, WordSetCx as _, @@ -147,7 +147,7 @@ impl FooterAction { self } - /// The kind of popup the action opens: the action reports it, and a mark shows at its end-bottom corner. + /// The kind of popup the action opens: the action reports it, and the dropdown arrow shows under its icon. #[must_use] pub fn popup(mut self, kind: PopupKind) -> Self { self.popup = Some(kind); @@ -199,7 +199,7 @@ impl FooterAction { button = button.variant(Variant::look(Emphasis::Ghost, tone)); } if let Some(kind) = self.popup { - button = button.popup(kind).popup_mark(true); + button = button.popup(kind).popup_mark(PopupMark::Menu); } if let Some(open) = self.expanded { button = button.expanded(open); diff --git a/crates/ui-widgets/src/buttons/icon_button.rs b/crates/ui-widgets/src/buttons/icon_button.rs index f8d2ca0a30..2b4eb7380a 100644 --- a/crates/ui-widgets/src/buttons/icon_button.rs +++ b/crates/ui-widgets/src/buttons/icon_button.rs @@ -3,10 +3,12 @@ use super::button::ButtonOutput; use super::face::{self, tinted}; use crate::contract::{Access, Common, Component, Cx, MeasureCx, PaintUi, PopupKind, Props, Role, Styled as _}; +use crate::controls::disclosure::DisclosureGlyph; use crate::primitives::{IconSpec, PaintShape, StrokeWidth}; use crate::runtime::commands::CommandRef; use crate::style::{ComponentStyle, FocusLook, IconLook, StyleColor, StyleCx, SurfaceLook}; use craft_behavior::{Metrics, Sense}; +use craft_i18n::Direction; use craft_theme::{ Variant, variant::{Emphasis, Tone}, @@ -40,12 +42,27 @@ pub struct IconButtonStyle { marker_color: StyleColor, /// The ring around the marker dot. marker_edge: StyleColor, - /// The popup mark at the end-bottom corner: its side, which is also its inset from the corner. + /// The tool group's triangle at the end-bottom corner: its side, which is also its inset from the corner. mark: f32, - /// The popup mark's colour. + /// The side of the square the menu arrow is drawn in. + arrow: f32, + /// The colour of the triangle and the menu arrow. mark_color: StyleColor, } +/// The mark an icon button shows for the popup it opens. +#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)] +#[non_exhaustive] +pub enum PopupMark { + /// No mark. + #[default] + None, + /// A rail button that stands for a group of tools: a filled triangle at its end-bottom corner. + ToolGroup, + /// A button that opens a menu: the dropdown arrow under the end edge of its glyph. + Menu, +} + /// A square button showing only an icon. Its label is its accessible name; a tooltip is the caller's. A selected /// button (`selected(true)`) shows the on look: the host owns the state and a click is reported in the response. #[derive(crate::contract::Component, crate::contract::Props)] @@ -57,14 +74,13 @@ pub struct IconButton<'a> { /// field's clear button repeats Escape. #[prop] focusable: bool, - /// The kind of popup the button opens: AccessKit reports it, and a mark shows at the end-bottom corner when - /// [`popup_mark`](Self::popup_mark) is on. + /// The kind of popup the button opens: AccessKit reports it, and [`popup_mark`](Self::popup_mark) shows it. #[prop(into)] popup: Option, - /// Whether the popup mark shows; on for a rail button with a flyout and a panel footer button that opens a menu, - /// the places a mark tells that a button opens more than it shows. + /// The mark that tells the button opens more than it shows: a rail button with a flyout and a panel footer button + /// that opens a menu have one. #[prop] - popup_mark: bool, + popup_mark: PopupMark, /// Whether the popup the button opens is open, as the host knows it. #[prop(into)] expanded: Option, @@ -73,7 +89,7 @@ pub struct IconButton<'a> { impl<'a> IconButton<'a> { /// An icon button showing `icon`; `label` it with the accessible name. pub fn new(icon: impl Into>) -> Self { - IconButton { common: Common::new(), icon: icon.into(), focusable: true, popup: None, popup_mark: false, expanded: None } + IconButton { common: Common::new(), icon: icon.into(), focusable: true, popup: None, popup_mark: PopupMark::None, expanded: None } } /// An icon button bound to `command`: its label, enabled state and activation follow the command. @@ -101,6 +117,7 @@ impl Component for IconButton<'_> { marker_color: cx.color(cx.tokens.color.accent.solid), marker_edge: cx.color(cx.tokens.color.line.mark), mark: cx.tokens.space.gap.tight, + arrow: cx.tokens.size.icon.glyph_sm, mark_color: cx.color(cx.tokens.color.text.faint), } } @@ -121,16 +138,30 @@ impl Component for IconButton<'_> { let glyph = egui::Rect::from_center_size(cx.rect.center(), Vec2::splat(cx.style.glyph.side)); let draw = ui.draw(); draw.icon(glyph, &tinted(&self.icon, cx.resolve(&cx.style.glyph.color))); - if self.popup.is_some() && self.popup_mark { - let (end, bottom) = if cx.flow.is_rtl() { - (cx.rect.left() + cx.style.mark, cx.rect.bottom() - cx.style.mark) - } else { - (cx.rect.right() - cx.style.mark, cx.rect.bottom() - cx.style.mark) - }; - let reach = if cx.flow.is_rtl() { cx.style.mark } else { -cx.style.mark }; - let corner = Pos2::new(end, bottom); - let triangle = PaintShape::Polygon(vec![corner, Pos2::new(end, bottom - cx.style.mark), Pos2::new(end + reach, bottom)]); - draw.fill(&triangle, cx.style.mark_color); + let rtl = cx.flow.is_rtl(); + match self.popup.map(|_| self.popup_mark) { + Some(PopupMark::ToolGroup) => { + let (end, bottom) = if rtl { + (cx.rect.left() + cx.style.mark, cx.rect.bottom() - cx.style.mark) + } else { + (cx.rect.right() - cx.style.mark, cx.rect.bottom() - cx.style.mark) + }; + let reach = if rtl { cx.style.mark } else { -cx.style.mark }; + let corner = Pos2::new(end, bottom); + let triangle = PaintShape::Polygon(vec![corner, Pos2::new(end, bottom - cx.style.mark), Pos2::new(end + reach, bottom)]); + draw.fill(&triangle, cx.style.mark_color); + } + Some(PopupMark::Menu) => { + // The arrow hangs from the glyph's bottom edge, centred on its end edge, so it never covers the glyph. + let end = if rtl { glyph.left() } else { glyph.right() }; + let square = egui::Rect::from_center_size(Pos2::new(end, glyph.bottom()), Vec2::splat(cx.style.arrow)); + let arrow = DisclosureGlyph::new(1.0); + let points = arrow.path(square, if rtl { Direction::Rtl } else { Direction::Ltr }); + let top = points.iter().map(|point| point.y).fold(f32::INFINITY, f32::min); + let points = points.into_iter().map(|point| point + Vec2::new(0.0, glyph.bottom() - top)).collect(); + draw.stroke(&PaintShape::Path(points), StrokeWidth::Thin, cx.style.mark_color); + } + _ => {} } // A mark tells that the command's content has something to show, so it shows while that content is not // shown: a rail's panel that is closed. diff --git a/crates/ui-widgets/src/buttons/mod.rs b/crates/ui-widgets/src/buttons/mod.rs index ba91744c62..fc6299fb7a 100644 --- a/crates/ui-widgets/src/buttons/mod.rs +++ b/crates/ui-widgets/src/buttons/mod.rs @@ -11,7 +11,7 @@ pub mod toggle_button; mod words; pub use button::{Button, ButtonOutput, ButtonStyle}; -pub use icon_button::{IconButton, IconButtonStyle}; +pub use icon_button::{IconButton, IconButtonStyle, PopupMark}; pub use link::{Link, LinkStyle}; pub use split_button::{SplitButton, SplitButtonStyle, SplitOutput}; pub use toggle_button::{ToggleButton, ToggleButtonStyle}; diff --git a/crates/ui-widgets/src/menu/bar/stories.rs b/crates/ui-widgets/src/menu/bar/stories.rs index 3394575a5d..0a44d16c82 100644 --- a/crates/ui-widgets/src/menu/bar/stories.rs +++ b/crates/ui-widgets/src/menu/bar/stories.rs @@ -1,7 +1,7 @@ //! Stories of the menu bar, the edit menu and the panel menu. use super::{MenuBar, MenuHelp, MenuTitle}; -use crate::buttons::IconButton; +use crate::buttons::{IconButton, PopupMark}; use crate::contract::{Bind, Entry, PopupKind, Props as _, Show as _, ShowContainer as _, Trigger}; use crate::display::Divider; use crate::menu::{EditMenu, GroupItem, HelpEntry, Menu, MenuItem, PanelMenu, PanelRow}; @@ -106,7 +106,7 @@ fn panel_menu(ui: &mut Ui, data: &mut StoryData) -> StoryOutput { ]; PanelMenu::new(host, group) }; - let opener = IconButton::new(IconId::new("menu")).popup(PopupKind::Menu).popup_mark(true).label("Panel menu").show(ui); + let opener = IconButton::new(IconId::new("menu")).popup(PopupKind::Menu).popup_mark(PopupMark::Menu).label("Panel menu").show(ui); let mut picked = None; let shown = Menu::new("Panel menu") .open(Bind::bound(data.value("open", false))) diff --git a/crates/ui-widgets/tests/it/menu/openers.rs b/crates/ui-widgets/tests/it/menu/openers.rs index 996537da0e..888f4165eb 100644 --- a/crates/ui-widgets/tests/it/menu/openers.rs +++ b/crates/ui-widgets/tests/it/menu/openers.rs @@ -1,10 +1,13 @@ //! A menu with several openers: a context menu over a popover. -use craft_ui::buttons::Button; -use craft_ui::contract::{Bind, Role, Show as _, ShowContainer as _, Trigger}; +use craft_behavior::IconId; +use craft_ui::buttons::{Button, IconButton, PopupMark}; +use craft_ui::contract::{Bind, PopupKind, Props as _, Role, Show as _, ShowContainer as _, Trigger}; use craft_ui::menu::{Menu, MenuItem}; use craft_ui::popover::Popover; +use craft_ui::runtime::env::Direction; use craft_ui::testing::{Fixture, pointer, trial}; +use egui::{Rect, Shape, pos2}; use std::cell::Cell; use std::rc::Rc; @@ -97,3 +100,51 @@ fn a_new_right_click_moves_an_open_context_menu_to_the_pointer() { let after = menu_rect(&fixture).expect("the menu is open after the second right-click"); assert!(after.min.x > before.min.x + 40.0, "the menu moved to the second point: {before:?} then {after:?}"); } + +/// An icon button that opens a menu hangs the dropdown arrow under the end edge of its glyph, inside the button and +/// clear of the glyph, in either reading direction; a tool group's button shows the corner triangle instead. +#[trial] +fn a_menu_button_hangs_its_arrow_under_the_end_of_its_glyph() { + const BUTTON: Rect = Rect { min: pos2(20.0, 20.0), max: pos2(44.0, 44.0) }; + for direction in [Direction::Ltr, Direction::Rtl] { + let at_end = |x: f32| if direction == Direction::Rtl { x < BUTTON.center().x } else { x > BUTTON.center().x }; + for mark in [PopupMark::Menu, PopupMark::ToolGroup] { + let mut fixture = Fixture::new(move |ui| { + IconButton::new(IconId::new("plus")).label("Add").popup(PopupKind::Menu).popup_mark(mark).show_at(ui, BUTTON); + }); + fixture.direction(direction).settle(); + let shapes = fixture.frame_shapes(); + let meshes = |textured: bool| { + shapes + .iter() + .filter_map(move |shape| match shape { + Shape::Mesh(mesh) if (mesh.texture_id != egui::TextureId::default()) == textured => Some(mesh.calc_bounds()), + _ => None, + }) + .collect::>() + }; + let glyph = *meshes(true).first().expect("the glyph"); + let arrows: Vec<_> = shapes + .iter() + .filter_map(|shape| match shape { + Shape::Path(path) if !path.closed => Some((Rect::from_points(&path.points), path.visual_bounding_rect())), + _ => None, + }) + .collect(); + match mark { + PopupMark::Menu => { + let [(points, painted)] = arrows[..] else { panic!("{direction:?}: one arrow, not {arrows:?}") }; + assert!(BUTTON.contains_rect(painted), "{direction:?}: the arrow {painted:?} leaves the button"); + assert!(points.top() >= glyph.bottom() - 0.5, "{direction:?}: the arrow {points:?} covers the glyph {glyph:?}"); + assert!(at_end(points.center().x), "{direction:?}: the arrow {points:?} is not at the end edge"); + assert!(meshes(false).is_empty(), "{direction:?}: a menu button shows no corner triangle"); + } + _ => { + assert!(arrows.is_empty(), "{direction:?}: a tool group shows no arrow"); + let triangle = *meshes(false).first().expect("the corner triangle"); + assert!(BUTTON.contains_rect(triangle) && at_end(triangle.center().x), "{direction:?}: the triangle {triangle:?}"); + } + } + } + } +}