Skip to main content

fc_ui/capabilities/foundation/
responsive.rs

1use gpui::*;
2
3#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord)]
4pub enum Breakpoint {
5    Xs,
6    Sm,
7    Md,
8    Lg,
9    Xl,
10    Xxl,
11}
12
13impl Breakpoint {
14    pub fn min_width(&self) -> f32 {
15        match self {
16            Self::Xs => 0.0,
17            Self::Sm => 640.0,
18            Self::Md => 768.0,
19            Self::Lg => 1024.0,
20            Self::Xl => 1280.0,
21            Self::Xxl => 1536.0,
22        }
23    }
24
25    pub fn from_width(width: f32) -> Self {
26        if width >= 1536.0 {
27            Self::Xxl
28        } else if width >= 1280.0 {
29            Self::Xl
30        } else if width >= 1024.0 {
31            Self::Lg
32        } else if width >= 768.0 {
33            Self::Md
34        } else if width >= 640.0 {
35            Self::Sm
36        } else {
37            Self::Xs
38        }
39    }
40}
41
42pub struct Responsive {
43    builders: Vec<(
44        Breakpoint,
45        Box<dyn FnOnce(&mut Window, &mut App) -> AnyElement>,
46    )>,
47}
48
49impl Responsive {
50    pub fn new() -> Self {
51        Self {
52            builders: Vec::new(),
53        }
54    }
55
56    pub fn xs(self, builder: impl FnOnce(&mut Window, &mut App) -> AnyElement + 'static) -> Self {
57        self.at(Breakpoint::Xs, builder)
58    }
59
60    pub fn sm(self, builder: impl FnOnce(&mut Window, &mut App) -> AnyElement + 'static) -> Self {
61        self.at(Breakpoint::Sm, builder)
62    }
63
64    pub fn md(self, builder: impl FnOnce(&mut Window, &mut App) -> AnyElement + 'static) -> Self {
65        self.at(Breakpoint::Md, builder)
66    }
67
68    pub fn lg(self, builder: impl FnOnce(&mut Window, &mut App) -> AnyElement + 'static) -> Self {
69        self.at(Breakpoint::Lg, builder)
70    }
71
72    pub fn xl(self, builder: impl FnOnce(&mut Window, &mut App) -> AnyElement + 'static) -> Self {
73        self.at(Breakpoint::Xl, builder)
74    }
75
76    pub fn xxl(self, builder: impl FnOnce(&mut Window, &mut App) -> AnyElement + 'static) -> Self {
77        self.at(Breakpoint::Xxl, builder)
78    }
79
80    fn at(
81        mut self,
82        breakpoint: Breakpoint,
83        builder: impl FnOnce(&mut Window, &mut App) -> AnyElement + 'static,
84    ) -> Self {
85        self.builders.push((breakpoint, Box::new(builder)));
86        self
87    }
88
89    pub fn build(mut self, window: &mut Window, cx: &mut App) -> AnyElement {
90        let viewport = window.viewport_size();
91        let width = f32::from(viewport.width);
92        let current = Breakpoint::from_width(width);
93
94        self.builders.sort_by_key(|b| std::cmp::Reverse(b.0));
95
96        for (breakpoint, builder) in self.builders {
97            if current >= breakpoint {
98                return (builder)(window, cx);
99            }
100        }
101
102        div().into_any_element()
103    }
104}
105
106impl Default for Responsive {
107    fn default() -> Self {
108        Self::new()
109    }
110}
111
112pub fn current_breakpoint(window: &Window) -> Breakpoint {
113    let viewport = window.viewport_size();
114    Breakpoint::from_width(f32::from(viewport.width))
115}
116
117pub fn responsive_value<T: Clone>(window: &Window, xs: T, sm: T, md: T, lg: T) -> T {
118    match current_breakpoint(window) {
119        Breakpoint::Xs => xs,
120        Breakpoint::Sm => sm,
121        Breakpoint::Md | Breakpoint::Lg => md,
122        Breakpoint::Xl | Breakpoint::Xxl => lg,
123    }
124}
125
126pub fn responsive_columns(window: &Window) -> usize {
127    match current_breakpoint(window) {
128        Breakpoint::Xs => 1,
129        Breakpoint::Sm => 2,
130        Breakpoint::Md => 3,
131        Breakpoint::Lg => 4,
132        Breakpoint::Xl | Breakpoint::Xxl => 6,
133    }
134}