with_rem_size.rs

 1use gpui::{
 2    div, AnyElement, App, Bounds, Div, DivFrameState, Element, ElementId, GlobalElementId, Hitbox,
 3    InteractiveElement as _, IntoElement, LayoutId, ParentElement, Pixels, StyleRefinement, Styled,
 4    Window,
 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    pub fn occlude(mut self) -> Self {
26        self.div = self.div.occlude();
27        self
28    }
29}
30
31impl Styled for WithRemSize {
32    fn style(&mut self) -> &mut StyleRefinement {
33        self.div.style()
34    }
35}
36
37impl ParentElement for WithRemSize {
38    fn extend(&mut self, elements: impl IntoIterator<Item = AnyElement>) {
39        self.div.extend(elements)
40    }
41}
42
43impl Element for WithRemSize {
44    type RequestLayoutState = DivFrameState;
45    type PrepaintState = Option<Hitbox>;
46
47    fn id(&self) -> Option<ElementId> {
48        Element::id(&self.div)
49    }
50
51    fn request_layout(
52        &mut self,
53        id: Option<&GlobalElementId>,
54        window: &mut Window,
55        cx: &mut App,
56    ) -> (LayoutId, Self::RequestLayoutState) {
57        window.with_rem_size(Some(self.rem_size), |window| {
58            self.div.request_layout(id, window, cx)
59        })
60    }
61
62    fn prepaint(
63        &mut self,
64        id: Option<&GlobalElementId>,
65        bounds: Bounds<Pixels>,
66        request_layout: &mut Self::RequestLayoutState,
67        window: &mut Window,
68        cx: &mut App,
69    ) -> Self::PrepaintState {
70        window.with_rem_size(Some(self.rem_size), |window| {
71            self.div.prepaint(id, bounds, request_layout, window, cx)
72        })
73    }
74
75    fn paint(
76        &mut self,
77        id: Option<&GlobalElementId>,
78        bounds: Bounds<Pixels>,
79        request_layout: &mut Self::RequestLayoutState,
80        prepaint: &mut Self::PrepaintState,
81        window: &mut Window,
82        cx: &mut App,
83    ) {
84        window.with_rem_size(Some(self.rem_size), |window| {
85            self.div
86                .paint(id, bounds, request_layout, prepaint, window, cx)
87        })
88    }
89}
90
91impl IntoElement for WithRemSize {
92    type Element = Self;
93
94    fn into_element(self) -> Self::Element {
95        self
96    }
97}