with_rem_size.rs

 1use gpui::{
 2    AnyElement, App, Bounds, Div, DivFrameState, Element, ElementId, GlobalElementId, Hitbox,
 3    InteractiveElement as _, IntoElement, LayoutId, ParentElement, Pixels, StyleRefinement, Styled,
 4    Window, div,
 5};
 6
 7/// An element that sets a particular rem size for its children.
 8pub struct WithRemSize {
 9    div: Div,
10    rem_size: Pixels,
11}
12
13impl WithRemSize {
14    /// Create a new [WithRemSize] element, which sets a
15    /// particular rem size for its children.
16    pub fn new(rem_size: impl Into<Pixels>) -> Self {
17        Self {
18            div: div(),
19            rem_size: rem_size.into(),
20        }
21    }
22
23    /// Block the mouse from interacting with this element or any of its children
24    /// The fluent API equivalent to [`Interactivity::occlude_mouse`]
25    ///
26    /// [`Interactivity::occlude_mouse`]: gpui::Interactivity::occlude_mouse
27    pub fn occlude(mut self) -> Self {
28        self.div = self.div.occlude();
29        self
30    }
31}
32
33impl Styled for WithRemSize {
34    fn style(&mut self) -> &mut StyleRefinement {
35        self.div.style()
36    }
37}
38
39impl ParentElement for WithRemSize {
40    fn extend(&mut self, elements: impl IntoIterator<Item = AnyElement>) {
41        self.div.extend(elements)
42    }
43}
44
45impl Element for WithRemSize {
46    type RequestLayoutState = DivFrameState;
47    type PrepaintState = Option<Hitbox>;
48
49    fn id(&self) -> Option<ElementId> {
50        Element::id(&self.div)
51    }
52
53    fn request_layout(
54        &mut self,
55        id: Option<&GlobalElementId>,
56        window: &mut Window,
57        cx: &mut App,
58    ) -> (LayoutId, Self::RequestLayoutState) {
59        window.with_rem_size(Some(self.rem_size), |window| {
60            self.div.request_layout(id, window, cx)
61        })
62    }
63
64    fn prepaint(
65        &mut self,
66        id: Option<&GlobalElementId>,
67        bounds: Bounds<Pixels>,
68        request_layout: &mut Self::RequestLayoutState,
69        window: &mut Window,
70        cx: &mut App,
71    ) -> Self::PrepaintState {
72        window.with_rem_size(Some(self.rem_size), |window| {
73            self.div.prepaint(id, bounds, request_layout, window, cx)
74        })
75    }
76
77    fn paint(
78        &mut self,
79        id: Option<&GlobalElementId>,
80        bounds: Bounds<Pixels>,
81        request_layout: &mut Self::RequestLayoutState,
82        prepaint: &mut Self::PrepaintState,
83        window: &mut Window,
84        cx: &mut App,
85    ) {
86        window.with_rem_size(Some(self.rem_size), |window| {
87            self.div
88                .paint(id, bounds, request_layout, prepaint, window, cx)
89        })
90    }
91}
92
93impl IntoElement for WithRemSize {
94    type Element = Self;
95
96    fn into_element(self) -> Self::Element {
97        self
98    }
99}