dioxus_use_window/layouts/
mod.rs

1use std::{
2    fmt::{Debug, Display, Formatter},
3    num::ParseIntError,
4    str::FromStr,
5};
6
7/// Responsive layout system for mainstream screen
8///
9/// ## epx
10/// Note that mobile devices does not depend on the actual pixel, but on the equivalent pixel.
11///
12/// For example `Phone 11Pro Max` has a `1242px × 2688px @ 3x` screen, the actual size is `414epx × 896epx`.
13///
14/// For 2K screen, which has a `2560px × 1440px @ 1.5x` screen, the actual size is `1706epx × 960epx`.
15///
16/// For 4K screen, which has a `4096px × 2160px @ 2x` screen, the actual size is `2048epx × 1024epx`.
17///
18/// ## Example
19///
20/// see: [`crate::use_window_layout`]
21#[derive(Debug, Copy, Clone)]
22pub enum ResponsiveLayout {
23    /// `width ⩽ 375epx`
24    ///
25    /// ## Devices
26    /// - ios: `3GS`, `4/4S`, `Phone X,Xs,11Pro`
27    Tiny,
28    /// `width ⩽ 640epx`
29    /// ## Devices
30    /// - ios: `Phone 6p,6sp,7p,8p`, `Phone 11,Xr,Phone 11Pro Max,Xs Max`
31    Small,
32    /// `width ⩽ 992epx`
33    /// ## Devices
34    /// - Vertical ipad
35    Medium,
36    /// `width ⩽ 1366epx`
37    /// ## Devices
38    /// - Horizontal ipad
39    Large,
40    /// `width ⩽ 2048epx`
41    /// ## Devices
42    /// - Mainstream Desktop Screen(16:10,16:9)
43    ExtraLarge,
44    /// `width > 2048epx`
45    /// ## Devices
46    /// - Hairtail Screen(21:9)
47    UltraLarge,
48}
49
50impl Default for ResponsiveLayout {
51    fn default() -> Self {
52        Self::Large
53    }
54}
55
56impl Display for ResponsiveLayout {
57    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
58        match self {
59            Self::Tiny => f.write_str("xs"),
60            Self::Small => f.write_str("sm"),
61            Self::Medium => f.write_str("md"),
62            Self::Large => f.write_str("lg"),
63            Self::ExtraLarge => f.write_str("xl"),
64            Self::UltraLarge => f.write_str("ul"),
65        }
66    }
67}
68
69impl From<usize> for ResponsiveLayout {
70    fn from(n: usize) -> Self {
71        match n {
72            n if n <= 375 => Self::Tiny,
73            n if n <= 640 => Self::Small,
74            n if n <= 992 => Self::Medium,
75            n if n <= 1366 => Self::Large,
76            n if n <= 2048 => Self::ExtraLarge,
77            _ => Self::UltraLarge,
78        }
79    }
80}
81
82impl FromStr for ResponsiveLayout {
83    type Err = ParseIntError;
84
85    #[inline]
86    fn from_str(s: &str) -> Result<Self, Self::Err> {
87        let out = match s.to_ascii_lowercase().as_str() {
88            "t" | "xs" | "tiny" => Self::Tiny,
89            "s" | "sm" | "small" => Self::Small,
90            "m" | "md" | "medium" => Self::Medium,
91            "l" | "lg" | "large" => Self::Large,
92            "x" | "xl" | "extra" => Self::ExtraLarge,
93            "u" | "ul" | "xxl" | "2xl" | "ultra" => Self::UltraLarge,
94            s => Self::from(s.parse::<usize>()?),
95        };
96        Ok(out)
97    }
98}