with_rem_size.rs

 1use gpui::{
 2    div, AnyElement, Bounds, Div, DivFrameState, Element, ElementId, GlobalElementId, Hitbox,
 3    IntoElement, LayoutId, ParentElement, Pixels, 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    pub fn new(rem_size: impl Into<Pixels>) -> Self {
14        Self {
15            div: div(),
16            rem_size: rem_size.into(),
17        }
18    }
19}
20
21impl ParentElement for WithRemSize {
22    fn extend(&mut self, elements: impl IntoIterator<Item = AnyElement>) {
23        self.div.extend(elements)
24    }
25}
26
27impl Element for WithRemSize {
28    type RequestLayoutState = DivFrameState;
29    type PrepaintState = Option<Hitbox>;
30
31    fn id(&self) -> Option<ElementId> {
32        self.div.id()
33    }
34
35    fn request_layout(
36        &mut self,
37        id: Option<&GlobalElementId>,
38        cx: &mut WindowContext,
39    ) -> (LayoutId, Self::RequestLayoutState) {
40        cx.with_rem_size(Some(self.rem_size), |cx| self.div.request_layout(id, cx))
41    }
42
43    fn prepaint(
44        &mut self,
45        id: Option<&GlobalElementId>,
46        bounds: Bounds<Pixels>,
47        request_layout: &mut Self::RequestLayoutState,
48        cx: &mut WindowContext,
49    ) -> Self::PrepaintState {
50        cx.with_rem_size(Some(self.rem_size), |cx| {
51            self.div.prepaint(id, bounds, request_layout, cx)
52        })
53    }
54
55    fn paint(
56        &mut self,
57        id: Option<&GlobalElementId>,
58        bounds: Bounds<Pixels>,
59        request_layout: &mut Self::RequestLayoutState,
60        prepaint: &mut Self::PrepaintState,
61        cx: &mut WindowContext,
62    ) {
63        cx.with_rem_size(Some(self.rem_size), |cx| {
64            self.div.paint(id, bounds, request_layout, prepaint, cx)
65        })
66    }
67}
68
69impl IntoElement for WithRemSize {
70    type Element = Self;
71
72    fn into_element(self) -> Self::Element {
73        self
74    }
75}