|
|
|
@@ -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<bool> {
|
|
|
|
|