Skip to main content

guise/theme/
tokens.rs

1//! Sizing tokens: the `xs..xl` scale used for spacing, radius, and font size,
2//! authored in px.
3
4/// A named size on the `xs..xl` scale. The library default is `Md`.
5#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
6pub enum Size {
7  Xs,
8  Sm,
9  #[default]
10  Md,
11  Lg,
12  Xl,
13}
14
15impl Size {
16  /// The token's name, as the docs and the inspector spell it.
17  pub fn label(self) -> &'static str {
18    match self {
19      Size::Xs => "xs",
20      Size::Sm => "sm",
21      Size::Md => "md",
22      Size::Lg => "lg",
23      Size::Xl => "xl",
24    }
25  }
26}
27
28/// Five px values addressed by [`Size`]. Used for spacing, radius and font.
29#[derive(Debug, Clone, Copy)]
30pub struct Scale {
31  pub xs: f32,
32  pub sm: f32,
33  pub md: f32,
34  pub lg: f32,
35  pub xl: f32,
36}
37
38impl Scale {
39  pub const fn new(xs: f32, sm: f32, md: f32, lg: f32, xl: f32) -> Self {
40    Scale { xs, sm, md, lg, xl }
41  }
42
43  /// Resolve a [`Size`] to its px value.
44  pub fn get(&self, size: Size) -> f32 {
45    match size {
46      Size::Xs => self.xs,
47      Size::Sm => self.sm,
48      Size::Md => self.md,
49      Size::Lg => self.lg,
50      Size::Xl => self.xl,
51    }
52  }
53
54  pub fn spacing() -> Self {
55    Scale::new(10.0, 12.0, 16.0, 20.0, 32.0)
56  }
57
58  pub fn radius() -> Self {
59    Scale::new(2.0, 4.0, 8.0, 16.0, 32.0)
60  }
61
62  pub fn font_size() -> Self {
63    Scale::new(12.0, 14.0, 16.0, 18.0, 20.0)
64  }
65}