diff --git a/crates/ui-widgets/src/buttons/icon_button.rs b/crates/ui-widgets/src/buttons/icon_button.rs index 2b4eb7380..4ed1ae5d5 100644 --- a/crates/ui-widgets/src/buttons/icon_button.rs +++ b/crates/ui-widgets/src/buttons/icon_button.rs @@ -155,7 +155,7 @@ impl Component for IconButton<'_> { // 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 arrow = DisclosureGlyph::dropdown_form(); 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(); diff --git a/crates/ui-widgets/src/controls/disclosure.rs b/crates/ui-widgets/src/controls/disclosure.rs index 5eb58420f..8f306a17f 100644 --- a/crates/ui-widgets/src/controls/disclosure.rs +++ b/crates/ui-widgets/src/controls/disclosure.rs @@ -1,4 +1,5 @@ -//! `Disclosure`: the open/closed toggle of a section, a title row's lead and a tree row's expander. +//! `Disclosure`: the open/closed toggle of a section, a title row's lead, a tree row's expander and a row's trailing +//! disclosure. use crate::contract::{ Animate as _, Bind, BindMut, Common, Component, Cx, Edit as _, EditResponse, MeasureCx, PaintUi, Pointer as _, Role, Styled as _, spec, @@ -18,43 +19,60 @@ const ARM: f32 = 0.1; /// The submenu arrow's base and depth, as shares of the glyph rect's side. const ARROW_BASE: f32 = 0.55; const ARROW_DEPTH: f32 = 0.35; -/// The tree row triangle's base and depth, as shares of the glyph rect's side. -const TREE_BASE: f32 = 0.52; -const TREE_DEPTH: f32 = 0.31; +/// The disclosure triangle's base and depth, as shares of the glyph rect's side. +const TRIANGLE_BASE: f32 = 0.52; +const TRIANGLE_DEPTH: f32 = 0.31; -/// The arrow every component shows for something that opens or unfolds: a chevron that turns from pointing to the -/// end of the line (closed) to pointing down (open) as `open` goes from 0 to 1. A dropdown's arrow is the open -/// chevron; a submenu's is the filled arrow to the end of the line. Components name these through -/// [`DisclosureGlyph::dropdown`] and [`DisclosureGlyph::submenu`] and never an icon of their own. +/// Which arrow a [`DisclosureGlyph`] draws. +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +#[non_exhaustive] +pub enum ArrowForm { + /// The filled triangle of everything that opens or unfolds in place: a section, a tree row, a row's trailing + /// disclosure. + Disclosure, + /// The larger filled arrow at the end of a menu row that opens a submenu. + Submenu, + /// The stroked chevron after the text of a field, a picker or a button that opens a menu. + Dropdown, +} + +/// The arrow every component shows for something that opens or unfolds. The disclosure is a filled triangle that +/// turns from pointing to the end of the line (closed) to pointing down (open) as `open` goes from 0 to 1; a +/// dropdown's arrow is the open chevron; a submenu's is the filled arrow to the end of the line. Components name these +/// through [`DisclosureGlyph::new`], [`DisclosureGlyph::dropdown`], [`DisclosureGlyph::menu_button`] and +/// [`DisclosureGlyph::submenu`] and never an icon of their own. #[derive(Clone, Copy, Debug, PartialEq)] #[non_exhaustive] pub struct DisclosureGlyph { /// The turn, 0 closed to 1 open. pub open: f32, - /// A filled arrow in place of the stroked chevron. - pub filled: bool, - /// The smaller filled triangle of a tree row (with `filled`). - pub tree: bool, - /// The chevron's line width: a field's or picker's arrow is a fine line, a section's or a button's a firmer one. + /// Which arrow it is. + pub form: ArrowForm, + /// The dropdown chevron's line width: a field's or picker's arrow is a fine line, a button's a firmer one. pub stroke: StrokeWidth, } /// The arrow of a field or picker. -static DROPDOWN: DisclosureGlyph = DisclosureGlyph { open: 1.0, filled: false, tree: false, stroke: StrokeWidth::Thin }; +static DROPDOWN: DisclosureGlyph = DisclosureGlyph::dropdown_form(); /// The arrow of a button that opens a menu. -static MENU_BUTTON: DisclosureGlyph = DisclosureGlyph { open: 1.0, filled: false, tree: false, stroke: StrokeWidth::Medium }; +static MENU_BUTTON: DisclosureGlyph = DisclosureGlyph { open: 1.0, form: ArrowForm::Dropdown, stroke: StrokeWidth::Medium }; /// The submenu arrow. -static SUBMENU: DisclosureGlyph = DisclosureGlyph { open: 0.0, filled: true, tree: false, stroke: StrokeWidth::Thin }; +static SUBMENU: DisclosureGlyph = DisclosureGlyph { open: 0.0, form: ArrowForm::Submenu, stroke: StrokeWidth::Thin }; impl DisclosureGlyph { - /// A chevron `open` of the way turned. + /// The disclosure triangle `open` of the way turned. pub const fn new(open: f32) -> Self { - DisclosureGlyph { open, filled: false, tree: false, stroke: StrokeWidth::Medium } + DisclosureGlyph { open, form: ArrowForm::Disclosure, stroke: StrokeWidth::Thin } } - /// A filled arrow `open` of the way turned; `tree` picks the smaller triangle of a tree row over a submenu's arrow. - pub const fn filled(open: f32, tree: bool) -> Self { - DisclosureGlyph { open, filled: true, tree, stroke: StrokeWidth::Thin } + /// The dropdown chevron in the fine line, for a component that draws the arrow's path itself. + pub(crate) const fn dropdown_form() -> Self { + DisclosureGlyph { open: 1.0, form: ArrowForm::Dropdown, stroke: StrokeWidth::Thin } + } + + /// Whether the arrow is a filled shape rather than a stroked chevron. + const fn filled(&self) -> bool { + !matches!(self.form, ArrowForm::Dropdown) } /// The icon of the arrow after the text of a picker's face or a field with a list. @@ -83,8 +101,8 @@ impl DisclosureGlyph { let (sin, cos) = turn.sin_cos(); let flip = if rtl { -1.0 } else { 1.0 }; let center = rect.center(); - let points: [Vec2; 3] = if self.filled { - let (base, depth) = if self.tree { (TREE_BASE, TREE_DEPTH) } else { (ARROW_BASE, ARROW_DEPTH) }; + let points: [Vec2; 3] = if self.filled() { + let (base, depth) = if self.form == ArrowForm::Submenu { (ARROW_BASE, ARROW_DEPTH) } else { (TRIANGLE_BASE, TRIANGLE_DEPTH) }; let (half, depth) = (side * base / 2.0, side * depth / 2.0); [Vec2::new(-depth, -half), Vec2::new(depth, 0.0), Vec2::new(-depth, half)] } else { @@ -102,16 +120,18 @@ impl DisclosureGlyph { impl DisclosureGlyph { /// The side of the square the glyph is drawn in, from the tokens and never from the slot it is placed in: the - /// icon side for the chevron, the menu text size for the submenu arrow. Every placement shows the same shape. + /// icon side for the disclosure and the chevron (a `Disclosure` at the standard size), the menu text size for the + /// submenu arrow. Every placement shows the same shape. fn side(&self, tokens: &UiTokens) -> f32 { - if self.filled { tokens.typography.body.size } else { tokens.size.icon.md } + if self.form == ArrowForm::Submenu { tokens.typography.body.size } else { tokens.size.icon.md } } /// The glyph's colour: the muted text colour in the place of the normal one. A state that recolours the ink (a /// disabled control, a selected row, text on a fill) keeps its colour. The submenu arrow reads in its row's text /// colour. fn tone(&self, tokens: &UiTokens, ink: Color32) -> Color32 { - if !self.filled && ink == ink_color(tokens, crate::style::Ink::OnPanel) { rgba_color(tokens.color.text.muted) } else { ink } + let quiet = self.form != ArrowForm::Submenu; + if quiet && ink == ink_color(tokens, crate::style::Ink::OnPanel) { rgba_color(tokens.color.text.muted) } else { ink } } } @@ -124,7 +144,7 @@ impl Glyph for DisclosureGlyph { let rect = Rect::from_center_size(rect.center(), Vec2::splat(self.side(tokens))); let mut points = self.outline(rect, mirrored); let color = self.tone(tokens, color); - if self.filled { + if self.filled() { if self.open == 0.0 { // The base is the arrow's vertical edge: on a pixel boundary it paints no half-covered column. let x = points[0].x.round_to_pixels(painter.pixels_per_point()); @@ -145,7 +165,7 @@ impl Glyph for DisclosureGlyph { pub struct DisclosureStyle { #[style(metrics)] metrics: Metrics, - /// The chevron. + /// The triangle. #[style(icon)] glyph: IconLook, } @@ -159,15 +179,12 @@ pub struct Disclosure<'a> { /// Whether the disclosure is its own AccessKit node; a part of another component's node turns it off. #[prop] node: bool, - /// The filled triangle of a tree row's expander in place of a section's chevron. - #[prop] - triangle: bool, } impl<'a> Disclosure<'a> { /// A disclosure of `open`. pub fn new(open: Bind<'a, bool>) -> Self { - Disclosure { common: Common::new(), open, node: true, triangle: false } + Disclosure { common: Common::new(), open, node: true } } } @@ -212,15 +229,9 @@ impl Component for Disclosure<'_> { fn paint(&mut self, ui: &mut PaintUi<'_>, cx: &Cx<'_, Self>) { let open = cx.anim_bool("open", cx.value().copied().unwrap_or(false), spec::OPEN_RISE); - let glyph = DisclosureGlyph { open, filled: self.triangle, tree: self.triangle, stroke: StrokeWidth::Medium }; let rect = Rect::from_center_size(cx.rect.center(), Vec2::splat(cx.style.glyph.side)); - let points = glyph.path(rect, cx.env.direction()); - let color = cx.resolve(&cx.style.glyph.color); - if self.triangle { - ui.draw().fill(&PaintShape::Polygon(points), color); - } else { - ui.draw().stroke(&PaintShape::Path(points), glyph.stroke, color); - } + let points = DisclosureGlyph::new(open).path(rect, cx.env.direction()); + ui.draw().fill(&PaintShape::Polygon(points), cx.resolve(&cx.style.glyph.color)); } fn finish(self, cx: Cx<'_, Self>) -> EditResponse { diff --git a/crates/ui-widgets/src/list/row/show.rs b/crates/ui-widgets/src/list/row/show.rs index 2b180c7dd..0495aa3f5 100644 --- a/crates/ui-widgets/src/list/row/show.rs +++ b/crates/ui-widgets/src/list/row/show.rs @@ -524,7 +524,7 @@ impl Component for ListRow<'_> { && placed.disclosure.is_some() && !self.disclosure_by_host { - ui.face(DISCLOSURE, Disclosure::new(Bind::given(open)).node(false).triangle(true).label(cx.resolved.label.to_owned())); + ui.face(DISCLOSURE, Disclosure::new(Bind::given(open)).node(false).label(cx.resolved.label.to_owned())); } { let draw = ui.draw(); diff --git a/crates/ui-widgets/src/list/row/trailing.rs b/crates/ui-widgets/src/list/row/trailing.rs index 91041cc06..9c7087fa1 100644 --- a/crates/ui-widgets/src/list/row/trailing.rs +++ b/crates/ui-widgets/src/list/row/trailing.rs @@ -33,10 +33,10 @@ impl TrailingPart { pub enum TrailingFace { /// An icon button face. Icon(IconSpec<'static>), - /// The disclosure face of an open or closed section. + /// The disclosure triangle of what the row unfolds under it (a layer's effects), as its group disclosure shows. #[non_exhaustive] Disclosure { - /// Whether the section is open. + /// Whether what it unfolds is open. open: bool, }, /// A short text in the caption role's semibold face, as wide as it measures (a layer's `fx`). diff --git a/crates/ui-widgets/src/tile_grid/grid.rs b/crates/ui-widgets/src/tile_grid/grid.rs index d860697b8..9e52bdae3 100644 --- a/crates/ui-widgets/src/tile_grid/grid.rs +++ b/crates/ui-widgets/src/tile_grid/grid.rs @@ -675,7 +675,7 @@ impl Component for TileGrid<'_, K> let lead = RowState { emphasized: lead == Some(Slot::Header(placed.group)), ..RowState::default() }; ui.face(section_part(&group.key), self.header_row(group, lead)); if placed.toggle { - let disclosure = Disclosure::new(Bind::given(group.open)).node(false).triangle(true).label(group.title.clone()); + let disclosure = Disclosure::new(Bind::given(group.open)).node(false).label(group.title.clone()); ui.face(TOGGLE.at(placed.group as u64), disclosure); } } diff --git a/crates/ui-widgets/tests/it/list.rs b/crates/ui-widgets/tests/it/list.rs index ce98fd642..e9eee0738 100644 --- a/crates/ui-widgets/tests/it/list.rs +++ b/crates/ui-widgets/tests/it/list.rs @@ -855,3 +855,40 @@ fn a_revealed_row_past_unbuilt_rows_taller_than_the_estimate_shows_wholly_in_one fixture.idle(1); assert!(in_view(&fixture, "Photo 00"), "the first row scrolls back wholly into view"); } + +/// One disclosure glyph: a section's disclosure and a row's trailing disclosure each paint one filled triangle, the +/// same size, and no stroked chevron. +#[trial] +fn a_sections_disclosure_and_a_rows_trailing_disclosure_paint_the_same_triangle() { + use craft_ui::contract::{Bind, Show as _}; + use craft_ui::controls::disclosure::Disclosure; + use craft_ui::list::{TrailingFace, TrailingPart}; + let triangles = |fixture: &mut Fixture| -> Vec { + let shapes = fixture.frame_shapes(); + assert!(!shapes.iter().any(|shape| matches!(shape, egui::Shape::Path(path) if !path.closed)), "no stroked chevron paints"); + shapes + .iter() + .filter_map(|shape| match shape { + egui::Shape::Mesh(mesh) => Some(mesh.calc_bounds().size()), + _ => None, + }) + .collect() + }; + let mut section = Fixture::new(|ui| { + let _ = Disclosure::new(Bind::given(true)).label("Details").show(ui); + }); + section.room(Vec2::new(300.0, 200.0)); + section.settle(); + let mut row = Fixture::new(|ui| { + let keys = [0_usize]; + let source = keyed(&keys, |key: &usize| *key); + let _ = List::new("Layers").show_items(ui, &source, &mut |key, _| { + ListRow::new(NAMES[*key]).trailing_part(TrailingPart::new(1), TrailingFace::disclosure(true), "Effects") + }); + }); + row.room(Vec2::new(300.0, 200.0)); + row.settle(); + let (section, row) = (triangles(&mut section), triangles(&mut row)); + assert_eq!((section.len(), row.len()), (1, 1), "each paints one filled triangle: {section:?} {row:?}"); + assert!((section[0] - row[0]).length() < 0.01, "the two triangles are the same size: {section:?} {row:?}"); +}