1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
use std::sync::Arc;
use crate::{
app::theme::TypographyStyle,
layout::text_style::TextStyle,
node::{Element, Node, TextColor},
};
/// What a `Select` draws for its chevron or an option's checkmark.
///
/// The default chevron is the text glyph `▾` and the default checkmark `✓`,
/// both drawn in the surrounding part's text style. Apps can supply a glyph in
/// a typography role (an icon-font code point with an `Extra` role that selects
/// the icon font and size) or an app-built element such as an SVG icon.
#[derive(Clone)]
pub enum SelectIcon {
/// A text glyph. With a typography role the glyph uses that role's font,
/// size and colour; without one it inherits the surrounding part's text
/// style at the configured size. A configured colour overrides both.
Glyph {
glyph: Arc<str>,
typography: Option<TypographyStyle>,
},
/// App-built content. The supplier receives the configured colour
/// (`chevron_color` / `checkmark_color`), or `None` when none is set. Built
/// menus are plain nodes: return an icon, SVG, text or layout element, not
/// a stateful component.
Element(Arc<dyn Fn(Option<TextColor>) -> Element + Send + Sync>),
}
impl SelectIcon {
/// A glyph drawn in the surrounding part's text style.
pub fn text(glyph: impl Into<Arc<str>>) -> Self {
Self::Glyph {
glyph: glyph.into(),
typography: None,
}
}
/// A glyph drawn in a typography role, e.g. an icon-font code point with
/// `TypographyStyle::extra("icon")`.
pub fn glyph(glyph: impl Into<Arc<str>>, typography: impl Into<TypographyStyle>) -> Self {
Self::Glyph {
glyph: glyph.into(),
typography: Some(typography.into()),
}
}
/// App-built content; see [`SelectIcon::Element`].
pub fn element(supplier: impl Fn(Option<TextColor>) -> Element + Send + Sync + 'static) -> Self {
Self::Element(Arc::new(supplier))
}
pub(crate) fn default_chevron() -> Self {
Self::text("\u{25BE}")
}
pub(crate) fn default_checkmark() -> Self {
Self::text("\u{2713}")
}
/// Whether the icon keeps its natural width (typography glyphs and
/// elements) instead of a slot sized from the configured size.
pub(crate) fn is_plain_text(&self) -> bool {
matches!(self, Self::Glyph { typography: None, .. })
}
/// Builds the icon node. `base` is the surrounding part's text style, used
/// by glyphs without a typography role, with `size` as their font size.
pub(crate) fn build(&self, base: Option<&TextStyle>, size: Option<f32>, color: Option<&TextColor>) -> Node {
match self {
Self::Glyph {
glyph,
typography: Some(typography),
} => with_color(Node::text(glyph).text_variant(*typography).text_wrap(false), color),
Self::Glyph {
glyph,
typography: None,
} => {
let mut style = base.cloned().unwrap_or_default();
if let Some(size) = size {
style.font_size = size;
}
with_color(Node::text_styled(glyph, style).text_wrap(false), color)
}
Self::Element(supplier) => supplier(color.cloned()).into_node(),
}
}
}
fn with_color(node: Node, color: Option<&TextColor>) -> Node {
match color {
Some(color) => node.text_color(color.clone()),
None => node,
}
}
/// Which side of an option row the checkmark slot sits on.
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub enum SelectCheckmarkPosition {
/// Before the label (the default).
#[default]
Leading,
/// After the label, at the row's trailing edge.
Trailing,
}