Skip to main content

waft_material/compose/
window_size_class.rs

1//! The window's width and height size classes, from androidx.window's V2 breakpoints.
2
3use waft_embedder::Size;
4use waft_foundation::App;
5use waft_widgets::{BuildContext, MediaQuery};
6
7/// Material's width breakpoints, in logical pixels.
8#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
9pub enum WindowWidthSizeClass {
10    /// Below 600 logical pixels.
11    Compact,
12    /// From 600 to 840.
13    Medium,
14    /// From 840 to 1200.
15    Expanded,
16    /// From 1200 to 1600.
17    Large,
18    /// From 1600.
19    ExtraLarge,
20}
21
22impl WindowWidthSizeClass {
23    /// Material 3's window margin at this width, in logical pixels: the space between the
24    /// window's edge, or its navigation, and the panes. 16 when Compact and 24 from Medium, from
25    /// the margins of m3.material.io/foundations/layout/applying-layout/window-size-classes.
26    ///
27    /// Compose leaves margins to the app; a pane takes them with [`Pane::margins`].
28    ///
29    /// [`Pane::margins`]: crate::compose::Pane::margins
30    pub fn margin(self) -> f64 {
31        match self {
32            WindowWidthSizeClass::Compact => 16.0,
33            WindowWidthSizeClass::Medium
34            | WindowWidthSizeClass::Expanded
35            | WindowWidthSizeClass::Large
36            | WindowWidthSizeClass::ExtraLarge => 24.0,
37        }
38    }
39}
40
41/// Material's height breakpoints, in logical pixels.
42#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
43pub enum WindowHeightSizeClass {
44    /// Below 480 logical pixels.
45    Compact,
46    /// From 480 to 900.
47    Medium,
48    /// From 900.
49    Expanded,
50}
51
52/// The classes of a window's width and height.
53///
54/// The classes say how much room a layout has, not how the window is used; posture and hinges,
55/// which would refine that, wait for the embedders to report them.
56#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
57pub struct WindowSizeClass {
58    /// The width class.
59    pub width: WindowWidthSizeClass,
60    /// The height class.
61    pub height: WindowHeightSizeClass,
62}
63
64impl WindowSizeClass {
65    /// The lower bound of [`WindowWidthSizeClass::Medium`].
66    pub const WIDTH_MEDIUM_LOWER_BOUND: f64 = 600.0;
67    /// The lower bound of [`WindowWidthSizeClass::Expanded`].
68    pub const WIDTH_EXPANDED_LOWER_BOUND: f64 = 840.0;
69    /// The lower bound of [`WindowWidthSizeClass::Large`].
70    pub const WIDTH_LARGE_LOWER_BOUND: f64 = 1200.0;
71    /// The lower bound of [`WindowWidthSizeClass::ExtraLarge`].
72    pub const WIDTH_EXTRA_LARGE_LOWER_BOUND: f64 = 1600.0;
73    /// The lower bound of [`WindowHeightSizeClass::Medium`].
74    pub const HEIGHT_MEDIUM_LOWER_BOUND: f64 = 480.0;
75    /// The lower bound of [`WindowHeightSizeClass::Expanded`].
76    pub const HEIGHT_EXPANDED_LOWER_BOUND: f64 = 900.0;
77
78    /// The class of a window of this logical size.
79    pub fn from_size(size: Size) -> WindowSizeClass {
80        WindowSizeClass {
81            width: width_class(size.width()),
82            height: height_class(size.height()),
83        }
84    }
85
86    /// The class of the nearest [`MediaQuery`]'s size.
87    ///
88    /// Reading it makes the caller rebuild when the window size changes.
89    pub fn of(app: &mut App, context: BuildContext) -> WindowSizeClass {
90        WindowSizeClass::from_size(MediaQuery::size_of(app, context))
91    }
92
93    /// The class of the nearest [`MediaQuery`]'s size, or `None` without one.
94    pub fn maybe_of(app: &mut App, context: BuildContext) -> Option<WindowSizeClass> {
95        MediaQuery::maybe_size_of(app, context).map(WindowSizeClass::from_size)
96    }
97}
98
99fn width_class(width: f64) -> WindowWidthSizeClass {
100    if width < WindowSizeClass::WIDTH_MEDIUM_LOWER_BOUND {
101        WindowWidthSizeClass::Compact
102    } else if width < WindowSizeClass::WIDTH_EXPANDED_LOWER_BOUND {
103        WindowWidthSizeClass::Medium
104    } else if width < WindowSizeClass::WIDTH_LARGE_LOWER_BOUND {
105        WindowWidthSizeClass::Expanded
106    } else if width < WindowSizeClass::WIDTH_EXTRA_LARGE_LOWER_BOUND {
107        WindowWidthSizeClass::Large
108    } else {
109        WindowWidthSizeClass::ExtraLarge
110    }
111}
112
113fn height_class(height: f64) -> WindowHeightSizeClass {
114    if height < WindowSizeClass::HEIGHT_MEDIUM_LOWER_BOUND {
115        WindowHeightSizeClass::Compact
116    } else if height < WindowSizeClass::HEIGHT_EXPANDED_LOWER_BOUND {
117        WindowHeightSizeClass::Medium
118    } else {
119        WindowHeightSizeClass::Expanded
120    }
121}
122
123#[cfg(test)]
124mod tests {
125    use super::*;
126
127    #[test]
128    fn each_width_breakpoint_starts_its_class() {
129        for (width, class) in [
130            (0.0, WindowWidthSizeClass::Compact),
131            (599.0, WindowWidthSizeClass::Compact),
132            (600.0, WindowWidthSizeClass::Medium),
133            (839.0, WindowWidthSizeClass::Medium),
134            (840.0, WindowWidthSizeClass::Expanded),
135            (1199.0, WindowWidthSizeClass::Expanded),
136            (1200.0, WindowWidthSizeClass::Large),
137            (1599.0, WindowWidthSizeClass::Large),
138            (1600.0, WindowWidthSizeClass::ExtraLarge),
139            (2000.0, WindowWidthSizeClass::ExtraLarge),
140        ] {
141            assert_eq!(
142                WindowSizeClass::from_size(Size::new(width, 0.0)).width,
143                class,
144                "width {width}"
145            );
146        }
147    }
148
149    #[test]
150    fn each_height_breakpoint_starts_its_class() {
151        for (height, class) in [
152            (0.0, WindowHeightSizeClass::Compact),
153            (479.0, WindowHeightSizeClass::Compact),
154            (480.0, WindowHeightSizeClass::Medium),
155            (899.0, WindowHeightSizeClass::Medium),
156            (900.0, WindowHeightSizeClass::Expanded),
157            (2000.0, WindowHeightSizeClass::Expanded),
158        ] {
159            assert_eq!(
160                WindowSizeClass::from_size(Size::new(0.0, height)).height,
161                class,
162                "height {height}"
163            );
164        }
165    }
166
167    #[test]
168    fn the_margin_is_16_when_compact_and_24_from_medium() {
169        assert_eq!(WindowWidthSizeClass::Compact.margin(), 16.0);
170        assert_eq!(WindowWidthSizeClass::Medium.margin(), 24.0);
171        assert_eq!(WindowWidthSizeClass::ExtraLarge.margin(), 24.0);
172    }
173}