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