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