with_rem_size.rs

  1use gpui::{
  2    AnyElement, App, Bounds, Div, DivFrameState, Element, ElementId, GlobalElementId, Hitbox, InteractiveElement as _, IntoElement, LayoutId, ParentElement, Pixels, Role, StyleRefinement, Styled, Window, div
  3};
  4
  5/// An element that sets a particular rem size for its children.
  6pub struct WithRemSize {
  7    div: Div,
  8    rem_size: Pixels,
  9}
 10
 11impl WithRemSize {
 12    /// Create a new [WithRemSize] element, which sets a
 13    /// particular rem size for its children.
 14    pub fn new(rem_size: impl Into<Pixels>) -> Self {
 15        Self {
 16            div: div(),
 17            rem_size: rem_size.into(),
 18        }
 19    }
 20
 21    /// Block the mouse from interacting with this element or any of its children
 22    /// The fluent API equivalent to [`Interactivity::occlude_mouse`]
 23    ///
 24    /// [`Interactivity::occlude_mouse`]: gpui::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 source_location(&self) -> Option<&'static core::panic::Location<'static>> {
 52        Element::source_location(&self.div)
 53    }
 54
 55    fn request_layout(
 56        &mut self,
 57        id: Option<&GlobalElementId>,
 58        inspector_id: Option<&gpui::InspectorElementId>,
 59        window: &mut Window,
 60        cx: &mut App,
 61    ) -> (LayoutId, Self::RequestLayoutState) {
 62        window.with_rem_size(Some(self.rem_size), |window| {
 63            self.div.request_layout(id, inspector_id, window, cx)
 64        })
 65    }
 66
 67    fn prepaint(
 68        &mut self,
 69        id: Option<&GlobalElementId>,
 70        inspector_id: Option<&gpui::InspectorElementId>,
 71        bounds: Bounds<Pixels>,
 72        request_layout: &mut Self::RequestLayoutState,
 73        window: &mut Window,
 74        cx: &mut App,
 75    ) -> Self::PrepaintState {
 76        window.with_rem_size(Some(self.rem_size), |window| {
 77            self.div
 78                .prepaint(id, inspector_id, bounds, request_layout, window, cx)
 79        })
 80    }
 81
 82    fn paint(
 83        &mut self,
 84        id: Option<&GlobalElementId>,
 85        inspector_id: Option<&gpui::InspectorElementId>,
 86        bounds: Bounds<Pixels>,
 87        request_layout: &mut Self::RequestLayoutState,
 88        prepaint: &mut Self::PrepaintState,
 89        window: &mut Window,
 90        cx: &mut App,
 91    ) {
 92        window.with_rem_size(Some(self.rem_size), |window| {
 93            self.div.paint(
 94                id,
 95                inspector_id,
 96                bounds,
 97                request_layout,
 98                prepaint,
 99                window,
100                cx,
101            )
102        })
103    }
104    
105    fn a11y_role(&self) -> Option<Role> {
106        self.div.a11y_role()
107    }
108}
109
110impl IntoElement for WithRemSize {
111    type Element = Self;
112
113    fn into_element(self) -> Self::Element {
114        self
115    }
116}