with_rem_size.rs

 1use gpui::{
 2    div, AnyElement, Bounds, Div, DivFrameState, Element, ElementId, GlobalElementId, Hitbox,
 3    IntoElement, LayoutId, ParentElement, Pixels, StyleRefinement, Styled, WindowContext,
 4};
 5
 6/// An element that sets a particular rem size for its children.
 7pub struct WithRemSize {
 8    div: Div,
 9    rem_size: Pixels,
10}
11
12impl WithRemSize {
13    /// Create a new [WithRemSize] element, which sets a
14    /// particular rem size for its children.
15    pub fn new(rem_size: impl Into<Pixels>) -> Self {
16        Self {
17            div: div(),
18            rem_size: rem_size.into(),
19        }
20    }
21}
22
23impl Styled for WithRemSize {
24    fn style(&mut self) -> &mut StyleRefinement {
25        self.div.style()
26    }
27}
28
29impl ParentElement for WithRemSize {
30    fn extend(&mut self, elements: impl IntoIterator<Item = AnyElement>) {
31        self.div.extend(elements)
32    }
33}
34
35impl Element for WithRemSize {
36    type RequestLayoutState = DivFrameState;
37    type PrepaintState = Option<Hitbox>;
38
39    fn id(&self) -> Option<ElementId> {
40        self.div.id()
41    }
42
43    fn request_layout(
44        &mut self,
45        id: Option<&GlobalElementId>,
46        cx: &mut WindowContext,
47    ) -> (LayoutId, Self::RequestLayoutState) {
48        cx.with_rem_size(Some(self.rem_size), |cx| self.div.request_layout(id, cx))
49    }
50
51    fn prepaint(
52        &mut self,
53        id: Option<&GlobalElementId>,
54        bounds: Bounds<Pixels>,
55        request_layout: &mut Self::RequestLayoutState,
56        cx: &mut WindowContext,
57    ) -> Self::PrepaintState {
58        cx.with_rem_size(Some(self.rem_size), |cx| {
59            self.div.prepaint(id, bounds, request_layout, cx)
60        })
61    }
62
63    fn paint(
64        &mut self,
65        id: Option<&GlobalElementId>,
66        bounds: Bounds<Pixels>,
67        request_layout: &mut Self::RequestLayoutState,
68        prepaint: &mut Self::PrepaintState,
69        cx: &mut WindowContext,
70    ) {
71        cx.with_rem_size(Some(self.rem_size), |cx| {
72            self.div.paint(id, bounds, request_layout, prepaint, cx)
73        })
74    }
75}
76
77impl IntoElement for WithRemSize {
78    type Element = Self;
79
80    fn into_element(self) -> Self::Element {
81        self
82    }
83}