Skip to main content

gpui_material_components/
insets.rs

1use gpui::{Edges, Pixels, px};
2use gpui_material_theme::Direction;
3
4/// Logical edges in pixels. Start and end follow the window direction.
5#[derive(Clone, Copy, Debug, Default, PartialEq)]
6pub struct ScaffoldInsets {
7    /// Top inset in logical pixels.
8    pub top: Pixels,
9    /// Leading edge inset (left in LTR, right in RTL).
10    pub start: Pixels,
11    /// Bottom inset in logical pixels.
12    pub bottom: Pixels,
13    /// Trailing edge inset (right in LTR, left in RTL).
14    pub end: Pixels,
15}
16
17impl ScaffoldInsets {
18    /// Converts logical start/end insets into physical left/right edges for the given layout direction.
19    pub fn resolve(self, direction: Direction) -> Edges<Pixels> {
20        let (left, right) = if direction == Direction::Rtl {
21            (self.end, self.start)
22        } else {
23            (self.start, self.end)
24        };
25        Edges {
26            top: self.top,
27            left,
28            bottom: self.bottom,
29            right,
30        }
31    }
32
33    #[cfg_attr(
34        not(any(feature = "feedback", feature = "navigation")),
35        allow(dead_code)
36    )]
37    pub(crate) fn normalized(self) -> Self {
38        let finite = |edge: Pixels| {
39            if f32::from(edge).is_finite() {
40                edge.max(px(0.))
41            } else {
42                px(0.)
43            }
44        };
45        Self {
46            top: finite(self.top),
47            start: finite(self.start),
48            bottom: finite(self.bottom),
49            end: finite(self.end),
50        }
51    }
52}