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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
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,
}
}
/// What an option's leading or trailing element is built for: the state of
/// its row in the open menu, or the closed field showing the selected option.
/// `SelectOption::leading_with` and `trailing_with` receive it each time the
/// row or field is built, so the element can follow the label's look.
///
/// The pointer hover is not a state here: it repaints a row's fill, border
/// and shadow without rebuilding the row, as for the label.
#[derive(Clone, Debug, Default, PartialEq)]
#[non_exhaustive]
pub struct SelectAdornmentState {
/// The option is selected.
pub selected: bool,
/// The keyboard highlight is on the option's row. `false` on the field.
pub highlighted: bool,
/// The option is disabled.
pub disabled: bool,
/// The element is built for the closed field, not for a row of the menu.
pub in_field: bool,
/// The `text_color` of the row's resolved part (`option` with its state
/// parts merged), or of the trigger part on the field; `None` when the
/// part sets none and the label uses its text style's colour.
pub text_color: Option<TextColor>,
}
/// Builds an option's leading or trailing element for a state.
pub(crate) type SelectAdornment = Arc<dyn Fn(&SelectAdornmentState) -> Element + Send + Sync>;
/// 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,
}