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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
//! Shared size, variant, and tone vocabulary for the component layer.
//!
//! Every builder in [`super`] uses these values, so a new component does not invent its
//! own scale. Colors still come from [`egui::Visuals`] (and thus from the active theme).
use egui::{Color32, RichText, Ui, Vec2, Visuals};
/// Control height and type scale.
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[cfg_attr(feature = "serde", serde(rename_all = "snake_case"))]
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
#[non_exhaustive]
pub enum Size {
/// 24 pt high, 11.5 pt text. Dense rows and toolbars.
Sm,
/// 30 pt high, 13 pt text. The default control height.
#[default]
Md,
/// 36 pt high, 14 pt text. Dialog and hero actions.
Lg,
}
impl Size {
/// Height in points.
pub const fn height(self) -> f32 {
match self {
Self::Sm => 24.0,
Self::Md => 30.0,
Self::Lg => 36.0,
}
}
/// Label size in points.
pub const fn text(self) -> f32 {
match self {
Self::Sm => 11.5,
Self::Md => 13.0,
Self::Lg => 14.0,
}
}
/// Minimum width for a text button.
pub const fn min_width(self) -> f32 {
match self {
Self::Sm => 0.0,
Self::Md => 88.0,
Self::Lg => 104.0,
}
}
/// Horizontal and vertical button padding.
pub const fn padding(self) -> Vec2 {
match self {
Self::Sm => Vec2::new(8.0, 2.0),
Self::Md => Vec2::new(12.0, 4.0),
Self::Lg => Vec2::new(16.0, 6.0),
}
}
}
/// The visual role of a button.
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
#[non_exhaustive]
pub enum ButtonVariant {
/// Selection fill, no border, selection text color.
#[default]
Primary,
/// Surface fill with a 1 pt border.
Secondary,
/// Transparent fill with a 1 pt border.
Outline,
/// Transparent, no border; hover fill only.
Ghost,
/// Error fill with white text.
Destructive,
/// Hyperlink color with no frame.
Link,
}
/// The square sizes used by the shell.
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
#[non_exhaustive]
pub enum IconButtonSize {
/// 28 pt title-bar action with a 16 pt glyph.
#[default]
Action,
/// 26 pt tab-strip tool with an 18 pt glyph.
Tool,
/// 21 pt explorer-header button with a 16 pt glyph.
Small,
}
impl IconButtonSize {
/// Side length in points.
pub const fn side(self) -> f32 {
match self {
Self::Action => 28.0,
Self::Tool => 26.0,
Self::Small => 21.0,
}
}
/// Glyph size in points.
pub const fn glyph(self) -> f32 {
match self {
Self::Action => 16.0,
Self::Tool => 18.0,
Self::Small => 16.0,
}
}
/// Corner radius in points.
pub const fn radius(self) -> u8 {
match self {
Self::Small => 4,
Self::Action | Self::Tool => 5,
}
}
}
/// Semantic color roles for badges, banners, and labels.
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
#[non_exhaustive]
pub enum Tone {
/// Primary text.
#[default]
Neutral,
/// Secondary text.
Muted,
/// Selection/brand color.
Accent,
/// Warning color.
Warning,
/// Error color.
Danger,
/// A caller-supplied theme token, such as `theme.success`.
Custom(Color32),
}
impl Tone {
/// Foreground color for this tone.
pub fn text(self, visuals: &Visuals) -> Color32 {
match self {
Self::Neutral => visuals.text_color(),
Self::Muted => visuals.weak_text_color(),
Self::Accent => visuals.selection.stroke.color,
Self::Warning => visuals.warn_fg_color,
Self::Danger => visuals.error_fg_color,
Self::Custom(color) => color,
}
}
/// Subtle background for this tone.
pub fn fill(self, visuals: &Visuals) -> Color32 {
match self {
Self::Neutral => visuals.extreme_bg_color,
Self::Muted => visuals.widgets.inactive.weak_bg_fill,
Self::Accent => visuals.selection.bg_fill,
Self::Warning => visuals.warn_fg_color.gamma_multiply(0.15),
Self::Danger => visuals.error_fg_color.gamma_multiply(0.15),
Self::Custom(color) => color.gamma_multiply(0.15),
}
}
/// A rich text with this tone and optional emphasis.
pub fn rich(self, ui: &Ui, text: impl Into<String>, size: f32, strong: bool) -> RichText {
let mut rich = RichText::new(text).size(size).color(self.text(ui.visuals()));
if strong {
rich = rich.strong();
}
rich
}
}