Skip to main content

mitsuami_core/
units.rs

1//! Abstract units, resolved to logical units at layout time.
2
3use crate::geometry::Size;
4
5/// A length in abstract units, CSS-style.
6///
7/// Everything resolves to *logical* units (points on macOS, effective pixels
8/// on Windows, logical pixels on GTK), so layouts are DPI-independent.
9#[derive(Clone, Copy, Debug, Default, PartialEq)]
10pub enum Length {
11    /// Logical pixels.
12    Px(f32),
13    /// Multiple of the node's inherited font size.
14    Em(f32),
15    /// Multiple of the platform body font size (follows the user's text size setting).
16    Rem(f32),
17    /// Percentage (0–100) of the containing block.
18    Percent(f32),
19    /// Percentage of the window content width.
20    Vw(f32),
21    /// Percentage of the window content height.
22    Vh(f32),
23    Vmin(f32),
24    Vmax(f32),
25    /// Fraction of the free space. Grid tracks only; elsewhere it acts as `Auto`.
26    Fr(f32),
27    /// A platform spacing token.
28    Token(Spacing),
29    #[default]
30    Auto,
31    /// The node's natural size: what it measures with no limit, as CSS's
32    /// `max-content`. For the least and most sizes of a measured node (a
33    /// list, as high as its rows; a label, its one line), so a list that
34    /// grows from nothing stops at its rows. Elsewhere it acts as `Auto`.
35    MaxContent,
36}
37
38/// Spacing tokens. Each backend decides what they mean, so spacing follows
39/// the platform's design language (e.g. HIG vs Fluent vs GNOME HIG).
40#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
41pub enum Spacing {
42    None,
43    Xs,
44    Sm,
45    Md,
46    Lg,
47    Xl,
48    /// How far the platform indents each level of a tree or outline: for
49    /// rows nested under others, as its own trees nest them.
50    Indent,
51}
52
53impl From<Spacing> for Length {
54    fn from(token: Spacing) -> Length {
55        Length::Token(token)
56    }
57}
58
59/// `16.px()`, `1.5.em()`, `50.pct()`, `1.fr()`, …
60pub trait LengthExt {
61    fn px(self) -> Length;
62    fn em(self) -> Length;
63    fn rem(self) -> Length;
64    fn pct(self) -> Length;
65    fn vw(self) -> Length;
66    fn vh(self) -> Length;
67    fn vmin(self) -> Length;
68    fn vmax(self) -> Length;
69    fn fr(self) -> Length;
70}
71
72macro_rules! length_ext {
73    ($($t:ty),*) => {$(
74        impl LengthExt for $t {
75            fn px(self) -> Length { Length::Px(self as f32) }
76            fn em(self) -> Length { Length::Em(self as f32) }
77            fn rem(self) -> Length { Length::Rem(self as f32) }
78            fn pct(self) -> Length { Length::Percent(self as f32) }
79            fn vw(self) -> Length { Length::Vw(self as f32) }
80            fn vh(self) -> Length { Length::Vh(self as f32) }
81            fn vmin(self) -> Length { Length::Vmin(self as f32) }
82            fn vmax(self) -> Length { Length::Vmax(self as f32) }
83            fn fr(self) -> Length { Length::Fr(self as f32) }
84        }
85    )*};
86}
87
88length_ext!(i32, u32, f32, f64);
89
90/// Everything needed to turn a [`Length`] into logical units.
91#[derive(Clone, Copy, Debug)]
92pub struct ResolveContext<'a> {
93    pub font_size: f32,
94    pub root_font_size: f32,
95    pub viewport: Size,
96    pub spacing: &'a SpacingScale,
97}
98
99/// Values of the spacing tokens, in logical units.
100#[derive(Clone, Copy, Debug, PartialEq)]
101pub struct SpacingScale {
102    pub xs: f32,
103    pub sm: f32,
104    pub md: f32,
105    pub lg: f32,
106    pub xl: f32,
107    /// A tree's indentation per level.
108    pub indent: f32,
109}
110
111impl SpacingScale {
112    pub fn get(&self, token: Spacing) -> f32 {
113        match token {
114            Spacing::None => 0.0,
115            Spacing::Xs => self.xs,
116            Spacing::Sm => self.sm,
117            Spacing::Md => self.md,
118            Spacing::Lg => self.lg,
119            Spacing::Xl => self.xl,
120            Spacing::Indent => self.indent,
121        }
122    }
123}
124
125/// A length after unit resolution: only what the layout engine understands.
126#[derive(Clone, Copy, Debug, PartialEq)]
127pub enum Resolved {
128    Length(f32),
129    /// Fraction, 0–1.
130    Percent(f32),
131    Fr(f32),
132    Auto,
133}
134
135impl Length {
136    pub fn resolve(&self, cx: &ResolveContext<'_>) -> Resolved {
137        let vw = cx.viewport.width / 100.0;
138        let vh = cx.viewport.height / 100.0;
139        match *self {
140            Length::Px(v) => Resolved::Length(v),
141            Length::Em(v) => Resolved::Length(v * cx.font_size),
142            Length::Rem(v) => Resolved::Length(v * cx.root_font_size),
143            Length::Percent(v) => Resolved::Percent(v / 100.0),
144            Length::Vw(v) => Resolved::Length(v * vw),
145            Length::Vh(v) => Resolved::Length(v * vh),
146            Length::Vmin(v) => Resolved::Length(v * vw.min(vh)),
147            Length::Vmax(v) => Resolved::Length(v * vw.max(vh)),
148            Length::Fr(v) => Resolved::Fr(v),
149            Length::Token(t) => Resolved::Length(cx.spacing.get(t)),
150            // Measured at layout (`Inner::content_limits`).
151            Length::Auto | Length::MaxContent => Resolved::Auto,
152        }
153    }
154}
155
156impl From<i32> for Length {
157    fn from(v: i32) -> Length {
158        Length::Px(v as f32)
159    }
160}
161
162impl From<f32> for Length {
163    fn from(v: f32) -> Length {
164        Length::Px(v)
165    }
166}
167
168/// Plain numbers are logical pixels: `.width(200)`.
169macro_rules! into_length_value {
170    ($($t:ty => $e:expr),*) => {$(
171        impl mitsuami_reactive::IntoValue<Length> for $t {
172            fn into_value(self) -> mitsuami_reactive::Value<Length> {
173                let f: fn($t) -> Length = $e;
174                mitsuami_reactive::Value::Static(f(self))
175            }
176        }
177    )*};
178}
179
180into_length_value!(
181    Length => |l| l,
182    Spacing => Length::Token,
183    i32 => |v| Length::Px(v as f32),
184    f32 => Length::Px
185);