with_rem_size.rs

  1use gpui::{
  2    AnyElement, App, Bounds, Div, DivFrameState, Element, ElementId, GlobalElementId, Hitbox,
  3    InteractiveElement as _, IntoElement, LayoutId, ParentElement, Pixels, StyleRefinement, Styled,
  4    Window, div,
  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    ///
 26    /// [`Interactivity::occlude_mouse`]: gpui::Interactivity::occlude_mouse
 27    pub fn occlude(mut self) -> Self {
 28        self.div = self.div.occlude();
 29        self
 30    }
 31}
 32
 33impl Styled for WithRemSize {
 34    fn style(&mut self) -> &mut StyleRefinement {
 35        self.div.style()
 36    }
 37}
 38
 39impl ParentElement for WithRemSize {
 40    fn extend(&mut self, elements: impl IntoIterator<Item = AnyElement>) {
 41        self.div.extend(elements)
 42    }
 43}
 44
 45impl Element for WithRemSize {
 46    type RequestLayoutState = DivFrameState;
 47    type PrepaintState = Option<Hitbox>;
 48
 49    fn id(&self) -> Option<ElementId> {
 50        Element::id(&self.div)
 51    }
 52
 53    fn source_location(&self) -> Option<&'static core::panic::Location<'static>> {
 54        Element::source_location(&self.div)
 55    }
 56
 57    fn request_layout(
 58        &mut self,
 59        id: Option<&GlobalElementId>,
 60        inspector_id: Option<&gpui::InspectorElementId>,
 61        window: &mut Window,
 62        cx: &mut App,
 63    ) -> (LayoutId, Self::RequestLayoutState) {
 64        window.with_rem_size(Some(self.rem_size), |window| {
 65            self.div.request_layout(id, inspector_id, window, cx)
 66        })
 67    }
 68
 69    fn prepaint(
 70        &mut self,
 71        id: Option<&GlobalElementId>,
 72        inspector_id: Option<&gpui::InspectorElementId>,
 73        bounds: Bounds<Pixels>,
 74        request_layout: &mut Self::RequestLayoutState,
 75        window: &mut Window,
 76        cx: &mut App,
 77    ) -> Self::PrepaintState {
 78        window.with_rem_size(Some(self.rem_size), |window| {
 79            self.div
 80                .prepaint(id, inspector_id, bounds, request_layout, window, cx)
 81        })
 82    }
 83
 84    fn paint(
 85        &mut self,
 86        id: Option<&GlobalElementId>,
 87        inspector_id: Option<&gpui::InspectorElementId>,
 88        bounds: Bounds<Pixels>,
 89        request_layout: &mut Self::RequestLayoutState,
 90        prepaint: &mut Self::PrepaintState,
 91        window: &mut Window,
 92        cx: &mut App,
 93    ) {
 94        window.with_rem_size(Some(self.rem_size), |window| {
 95            self.div.paint(
 96                id,
 97                inspector_id,
 98                bounds,
 99                request_layout,
100                prepaint,
101                window,
102                cx,
103            )
104        })
105    }
106}
107
108impl IntoElement for WithRemSize {
109    type Element = Self;
110
111    fn into_element(self) -> Self::Element {
112        self
113    }
114}