use waft_embedder::Size;
use waft_foundation::App;
use waft_widgets::{BuildContext, MediaQuery};
#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub enum WindowWidthSizeClass {
Compact,
Medium,
Expanded,
Large,
ExtraLarge,
}
impl WindowWidthSizeClass {
pub fn margin(self) -> f64 {
match self {
WindowWidthSizeClass::Compact => 16.0,
WindowWidthSizeClass::Medium
| WindowWidthSizeClass::Expanded
| WindowWidthSizeClass::Large
| WindowWidthSizeClass::ExtraLarge => 24.0,
}
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub enum WindowHeightSizeClass {
Compact,
Medium,
Expanded,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub struct WindowSizeClass {
pub width: WindowWidthSizeClass,
pub height: WindowHeightSizeClass,
}
impl WindowSizeClass {
pub const WIDTH_MEDIUM_LOWER_BOUND: f64 = 600.0;
pub const WIDTH_EXPANDED_LOWER_BOUND: f64 = 840.0;
pub const WIDTH_LARGE_LOWER_BOUND: f64 = 1200.0;
pub const WIDTH_EXTRA_LARGE_LOWER_BOUND: f64 = 1600.0;
pub const HEIGHT_MEDIUM_LOWER_BOUND: f64 = 480.0;
pub const HEIGHT_EXPANDED_LOWER_BOUND: f64 = 900.0;
pub fn from_size(size: Size) -> WindowSizeClass {
WindowSizeClass {
width: width_class(size.width()),
height: height_class(size.height()),
}
}
pub fn of(app: &mut App, context: BuildContext) -> WindowSizeClass {
WindowSizeClass::from_size(MediaQuery::size_of(app, context))
}
pub fn maybe_of(app: &mut App, context: BuildContext) -> Option<WindowSizeClass> {
MediaQuery::maybe_size_of(app, context).map(WindowSizeClass::from_size)
}
}
fn width_class(width: f64) -> WindowWidthSizeClass {
if width < WindowSizeClass::WIDTH_MEDIUM_LOWER_BOUND {
WindowWidthSizeClass::Compact
} else if width < WindowSizeClass::WIDTH_EXPANDED_LOWER_BOUND {
WindowWidthSizeClass::Medium
} else if width < WindowSizeClass::WIDTH_LARGE_LOWER_BOUND {
WindowWidthSizeClass::Expanded
} else if width < WindowSizeClass::WIDTH_EXTRA_LARGE_LOWER_BOUND {
WindowWidthSizeClass::Large
} else {
WindowWidthSizeClass::ExtraLarge
}
}
fn height_class(height: f64) -> WindowHeightSizeClass {
if height < WindowSizeClass::HEIGHT_MEDIUM_LOWER_BOUND {
WindowHeightSizeClass::Compact
} else if height < WindowSizeClass::HEIGHT_EXPANDED_LOWER_BOUND {
WindowHeightSizeClass::Medium
} else {
WindowHeightSizeClass::Expanded
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn each_width_breakpoint_starts_its_class() {
for (width, class) in [
(0.0, WindowWidthSizeClass::Compact),
(599.0, WindowWidthSizeClass::Compact),
(600.0, WindowWidthSizeClass::Medium),
(839.0, WindowWidthSizeClass::Medium),
(840.0, WindowWidthSizeClass::Expanded),
(1199.0, WindowWidthSizeClass::Expanded),
(1200.0, WindowWidthSizeClass::Large),
(1599.0, WindowWidthSizeClass::Large),
(1600.0, WindowWidthSizeClass::ExtraLarge),
(2000.0, WindowWidthSizeClass::ExtraLarge),
] {
assert_eq!(
WindowSizeClass::from_size(Size::new(width, 0.0)).width,
class,
"width {width}"
);
}
}
#[test]
fn each_height_breakpoint_starts_its_class() {
for (height, class) in [
(0.0, WindowHeightSizeClass::Compact),
(479.0, WindowHeightSizeClass::Compact),
(480.0, WindowHeightSizeClass::Medium),
(899.0, WindowHeightSizeClass::Medium),
(900.0, WindowHeightSizeClass::Expanded),
(2000.0, WindowHeightSizeClass::Expanded),
] {
assert_eq!(
WindowSizeClass::from_size(Size::new(0.0, height)).height,
class,
"height {height}"
);
}
}
#[test]
fn the_margin_is_16_when_compact_and_24_from_medium() {
assert_eq!(WindowWidthSizeClass::Compact.margin(), 16.0);
assert_eq!(WindowWidthSizeClass::Medium.margin(), 24.0);
assert_eq!(WindowWidthSizeClass::ExtraLarge.margin(), 24.0);
}
}