crisol_ui_kit/foundation/
style.rs1use egui::{Color32, Context, CornerRadius, FontId, Id, Stroke, TextStyle, Vec2};
30
31use crate::components::Size;
32use crate::foundation::metrics;
33
34#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
36pub enum Density {
37 Compact,
39 #[default]
41 Default,
42 Comfortable,
44}
45
46impl Density {
47 pub const fn factor(self) -> f32 {
49 match self {
50 Self::Compact => 0.86,
51 Self::Default => 1.0,
52 Self::Comfortable => 1.16,
53 }
54 }
55
56 pub const ALL: [Self; 3] = [Self::Compact, Self::Default, Self::Comfortable];
58
59 pub const fn as_setting(self) -> &'static str {
61 match self {
62 Self::Compact => "compact",
63 Self::Default => "default",
64 Self::Comfortable => "comfortable",
65 }
66 }
67
68 pub fn from_setting(value: &str) -> Self {
70 match value {
71 "compact" => Self::Compact,
72 "comfortable" => Self::Comfortable,
73 _ => Self::Default,
74 }
75 }
76}
77
78#[derive(Clone, Copy, Debug, PartialEq)]
80pub struct WorkbenchStyle {
81 pub density: Density,
83 pub control_radius: u8,
85 pub item_radius: u8,
87 pub inner_radius: u8,
90 pub container_radius: u8,
92 pub dialog_radius: u8,
94 pub pill_radius: u8,
97 pub border_width: f32,
100 pub text_scale: f32,
103}
104
105impl Default for WorkbenchStyle {
106 fn default() -> Self {
107 Self {
108 density: Density::Default,
109 control_radius: 6,
110 item_radius: 5,
111 inner_radius: 4,
112 container_radius: 8,
113 dialog_radius: 10,
114 pill_radius: 12,
115 border_width: 1.0,
116 text_scale: 1.0,
117 }
118 }
119}
120
121impl WorkbenchStyle {
122 pub fn sharp() -> Self {
124 Self {
125 control_radius: 0,
126 item_radius: 0,
127 inner_radius: 0,
128 container_radius: 0,
129 dialog_radius: 0,
130 pill_radius: 2,
131 ..Self::default()
132 }
133 }
134
135 pub fn rounded() -> Self {
137 Self {
138 control_radius: 9,
139 item_radius: 7,
140 inner_radius: 5,
141 container_radius: 12,
142 dialog_radius: 14,
143 pill_radius: u8::MAX,
144 ..Self::default()
145 }
146 }
147
148 pub fn with_density(mut self, density: Density) -> Self {
150 self.density = density;
151 self
152 }
153
154 pub fn scaled(&self, height: f32) -> f32 {
156 (height * self.density.factor()).round()
157 }
158
159 pub fn control_height(&self) -> f32 {
161 self.scaled(metrics::CONTROL_HEIGHT)
162 }
163
164 pub fn size_height(&self, size: Size) -> f32 {
166 self.scaled(size.height())
167 }
168
169 pub fn size_padding(&self, size: Size) -> Vec2 {
171 let padding = size.padding();
172 Vec2::new(padding.x, (padding.y * self.density.factor()).round())
173 }
174
175 pub fn menu_row_height(&self) -> f32 {
177 self.scaled(metrics::MENU_ROW_HEIGHT)
178 }
179
180 pub fn list_row_height(&self) -> f32 {
182 self.scaled(metrics::LIST_ROW_HEIGHT)
183 }
184
185 pub fn tree_row_height(&self) -> f32 {
187 self.scaled(metrics::TREE_ROW_HEIGHT)
188 }
189
190 pub fn control(&self) -> CornerRadius {
192 CornerRadius::same(self.control_radius)
193 }
194
195 pub fn item(&self) -> CornerRadius {
197 CornerRadius::same(self.item_radius)
198 }
199
200 pub fn container(&self) -> CornerRadius {
202 CornerRadius::same(self.container_radius)
203 }
204
205 pub fn dialog(&self) -> CornerRadius {
207 CornerRadius::same(self.dialog_radius)
208 }
209
210 pub fn inner(&self) -> CornerRadius {
212 CornerRadius::same(self.inner_radius)
213 }
214
215 pub fn tight(&self) -> CornerRadius {
218 CornerRadius::same(self.inner_radius.saturating_sub(1))
219 }
220
221 pub fn mark(&self) -> CornerRadius {
223 CornerRadius::same(self.inner_radius.min(1))
224 }
225
226 pub fn pill(&self, height: f32) -> CornerRadius {
228 let half = (height / 2.0).round().clamp(0.0, 255.0) as u8;
229 CornerRadius::same(self.pill_radius.min(half))
230 }
231
232 pub fn border(&self, color: Color32) -> Stroke {
234 Stroke::new(self.border_width.max(0.0), color)
235 }
236
237 pub fn text_scale(&self) -> f32 {
239 if self.text_scale.is_finite() {
240 self.text_scale.clamp(0.75, 1.5)
241 } else {
242 1.0
243 }
244 }
245
246 pub fn font_size(&self, points: f32) -> f32 {
248 points * self.text_scale()
249 }
250
251 pub fn font(&self, points: f32) -> FontId {
253 FontId::proportional(self.font_size(points))
254 }
255
256 pub fn mono(&self, points: f32) -> FontId {
258 FontId::monospace(self.font_size(points))
259 }
260
261 pub fn text(&self, size: Size) -> f32 {
263 self.font_size(size.text())
264 }
265}
266
267pub const TYPE_SCALE: [(TextStyle, f32); 5] = [
270 (TextStyle::Body, 14.0),
271 (TextStyle::Button, 12.5),
272 (TextStyle::Small, 11.0),
273 (TextStyle::Monospace, 13.0),
274 (TextStyle::Heading, 20.0),
275];
276
277fn key() -> Id {
278 Id::unique("crisol::style")
279}
280
281pub fn get(ctx: &Context) -> WorkbenchStyle {
283 ctx.data(|data| data.get_temp::<WorkbenchStyle>(key()))
284 .unwrap_or_default()
285}
286
287pub fn set(ctx: &Context, style: WorkbenchStyle) {
290 ctx.data_mut(|data| data.insert_temp(key(), style));
291 apply_to_egui(ctx, &style);
292}
293
294pub(crate) fn apply_to_egui(ctx: &Context, style: &WorkbenchStyle) {
297 let radius = style.control();
298 let border = style.border_width.max(0.0);
299 ctx.all_styles_mut(|egui_style| {
300 let widgets = &mut egui_style.visuals.widgets;
301 for state in [
302 &mut widgets.noninteractive,
303 &mut widgets.inactive,
304 &mut widgets.hovered,
305 &mut widgets.active,
306 &mut widgets.open,
307 ] {
308 state.corner_radius = radius;
309 }
310 for state in [&mut widgets.noninteractive, &mut widgets.inactive, &mut widgets.hovered] {
312 state.bg_stroke.width = border;
313 }
314 egui_style.visuals.window_stroke.width = border;
315 egui_style.visuals.window_corner_radius = style.container();
316 egui_style.visuals.menu_corner_radius = style.control();
317 egui_style.spacing.interact_size.y = style.scaled(24.0);
318 egui_style.spacing.button_padding.y = style.scaled(3.0);
319 for (text_style, points) in TYPE_SCALE {
320 let font = if text_style == TextStyle::Monospace {
321 style.mono(points)
322 } else {
323 style.font(points)
324 };
325 egui_style.text_styles.insert(text_style, font);
326 }
327 });
328}
329
330#[cfg(test)]
331mod tests {
332 use super::*;
333
334 #[test]
335 fn the_default_style_reproduces_the_metrics() {
336 let style = WorkbenchStyle::default();
337 assert_eq!(style.control_height(), metrics::CONTROL_HEIGHT);
338 assert_eq!(style.menu_row_height(), metrics::MENU_ROW_HEIGHT);
339 assert_eq!(style.list_row_height(), metrics::LIST_ROW_HEIGHT);
340 assert_eq!(style.tree_row_height(), metrics::TREE_ROW_HEIGHT);
341 for size in [Size::Sm, Size::Md, Size::Lg] {
342 assert_eq!(style.size_height(size), size.height());
343 assert_eq!(style.size_padding(size), size.padding());
344 }
345 }
346
347 #[test]
348 fn capsules_clamp_to_half_the_height() {
349 let style = WorkbenchStyle::default();
350 assert_eq!(style.pill(20.0), CornerRadius::same(10));
351 assert_eq!(style.pill(24.0), CornerRadius::same(12));
352 assert_eq!(WorkbenchStyle::sharp().pill(24.0), CornerRadius::same(2));
353 assert_eq!(WorkbenchStyle::rounded().pill(30.0), CornerRadius::same(15));
354 }
355
356 #[test]
357 fn the_text_scale_reaches_egui_and_is_clamped() {
358 let ctx = Context::default();
359 set(
360 &ctx,
361 WorkbenchStyle {
362 text_scale: 1.2,
363 border_width: 0.0,
364 ..WorkbenchStyle::default()
365 },
366 );
367 let style = ctx.style_of(egui::Theme::Light);
368 assert_eq!(style.text_styles[&TextStyle::Body].size, 14.0 * 1.2);
369 assert_eq!(style.visuals.widgets.inactive.bg_stroke.width, 0.0);
370 let huge = WorkbenchStyle {
371 text_scale: 9.0,
372 ..WorkbenchStyle::default()
373 };
374 assert_eq!(huge.font_size(10.0), 15.0);
375 }
376
377 #[test]
378 fn densities_order_the_heights() {
379 let heights: Vec<f32> = Density::ALL
380 .iter()
381 .map(|density| WorkbenchStyle::default().with_density(*density).control_height())
382 .collect();
383 assert!(heights[0] < heights[1] && heights[1] < heights[2], "{heights:?}");
384 for density in Density::ALL {
385 assert_eq!(Density::from_setting(density.as_setting()), density);
386 }
387 }
388
389 #[test]
390 fn the_style_survives_in_the_context_and_reaches_egui() {
391 let ctx = Context::default();
392 set(&ctx, WorkbenchStyle::sharp());
393 assert_eq!(get(&ctx).control_radius, 0);
394 assert_eq!(
395 ctx.style_of(egui::Theme::Light).visuals.widgets.inactive.corner_radius,
396 CornerRadius::ZERO
397 );
398 }
399}