1use gpui::{
2 div, AnyElement, Bounds, Div, DivFrameState, Element, ElementId, GlobalElementId, Hitbox,
3 InteractiveElement as _, IntoElement, LayoutId, ParentElement, Pixels, StyleRefinement, Styled,
4 WindowContext,
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 cx: &mut WindowContext,
55 ) -> (LayoutId, Self::RequestLayoutState) {
56 cx.with_rem_size(Some(self.rem_size), |cx| self.div.request_layout(id, cx))
57 }
58
59 fn prepaint(
60 &mut self,
61 id: Option<&GlobalElementId>,
62 bounds: Bounds<Pixels>,
63 request_layout: &mut Self::RequestLayoutState,
64 cx: &mut WindowContext,
65 ) -> Self::PrepaintState {
66 cx.with_rem_size(Some(self.rem_size), |cx| {
67 self.div.prepaint(id, bounds, request_layout, cx)
68 })
69 }
70
71 fn paint(
72 &mut self,
73 id: Option<&GlobalElementId>,
74 bounds: Bounds<Pixels>,
75 request_layout: &mut Self::RequestLayoutState,
76 prepaint: &mut Self::PrepaintState,
77 cx: &mut WindowContext,
78 ) {
79 cx.with_rem_size(Some(self.rem_size), |cx| {
80 self.div.paint(id, bounds, request_layout, prepaint, cx)
81 })
82 }
83}
84
85impl IntoElement for WithRemSize {
86 type Element = Self;
87
88 fn into_element(self) -> Self::Element {
89 self
90 }
91}