oxiui_theme/
breakpoint.rs1#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
16pub enum Breakpoint {
17 Xs,
19 Sm,
21 Md,
23 Lg,
25 Xl,
27 Xxl,
29}
30
31impl Breakpoint {
32 pub fn for_width(width: f32) -> Self {
35 if width < 576.0 {
36 Breakpoint::Xs
37 } else if width < 768.0 {
38 Breakpoint::Sm
39 } else if width < 992.0 {
40 Breakpoint::Md
41 } else if width < 1200.0 {
42 Breakpoint::Lg
43 } else if width < 1536.0 {
44 Breakpoint::Xl
45 } else {
46 Breakpoint::Xxl
47 }
48 }
49
50 pub fn min_width(self) -> f32 {
53 match self {
54 Breakpoint::Xs => 0.0,
55 Breakpoint::Sm => 576.0,
56 Breakpoint::Md => 768.0,
57 Breakpoint::Lg => 992.0,
58 Breakpoint::Xl => 1200.0,
59 Breakpoint::Xxl => 1536.0,
60 }
61 }
62
63 pub fn matches_width(self, width: f32) -> bool {
67 width >= self.min_width()
68 }
69}
70
71#[cfg(test)]
72mod tests {
73 use super::*;
74
75 #[test]
76 fn breakpoint_xs_below_576() {
77 assert_eq!(Breakpoint::for_width(400.0), Breakpoint::Xs);
78 assert_eq!(Breakpoint::for_width(0.0), Breakpoint::Xs);
79 }
80
81 #[test]
82 fn breakpoint_sm_at_576() {
83 assert_eq!(Breakpoint::for_width(576.0), Breakpoint::Sm);
84 assert_eq!(Breakpoint::for_width(767.9), Breakpoint::Sm);
85 }
86
87 #[test]
88 fn breakpoint_md_768_to_991() {
89 assert_eq!(Breakpoint::for_width(900.0), Breakpoint::Md);
90 }
91
92 #[test]
93 fn breakpoint_xxl_at_1536() {
94 assert_eq!(Breakpoint::for_width(1536.0), Breakpoint::Xxl);
95 assert_eq!(Breakpoint::for_width(2000.0), Breakpoint::Xxl);
96 }
97
98 #[test]
99 fn breakpoint_ordering() {
100 assert!(Breakpoint::Xs < Breakpoint::Sm);
101 assert!(Breakpoint::Sm < Breakpoint::Md);
102 assert!(Breakpoint::Md < Breakpoint::Lg);
103 assert!(Breakpoint::Lg < Breakpoint::Xl);
104 assert!(Breakpoint::Xl < Breakpoint::Xxl);
105 }
106
107 #[test]
108 fn min_width_xs_is_zero() {
109 assert_eq!(Breakpoint::Xs.min_width(), 0.0);
110 }
111
112 #[test]
113 fn matches_width_true_and_false() {
114 assert!(Breakpoint::Md.matches_width(900.0));
115 assert!(!Breakpoint::Xl.matches_width(900.0));
116 }
117}