ZIm/crates/ui/src/with_rem_size.rs
Marshall Bowers 2f3102672c
ui: Don't break flex layout when using WithRemSize (#12076)
This PR fixes an issue where the flex hierarchy wasn't getting broken by
the use of `WithRemSize`.

Release Notes:

- N/A
2024-05-20 21:39:18 -04:00

81 lines
2 KiB
Rust

use gpui::{
div, AnyElement, Bounds, Div, DivFrameState, Element, ElementId, GlobalElementId, Hitbox,
IntoElement, LayoutId, ParentElement, Pixels, StyleRefinement, Styled, WindowContext,
};
/// An element that sets a particular rem size for its children.
pub struct WithRemSize {
div: Div,
rem_size: Pixels,
}
impl WithRemSize {
pub fn new(rem_size: impl Into<Pixels>) -> Self {
Self {
div: div(),
rem_size: rem_size.into(),
}
}
}
impl Styled for WithRemSize {
fn style(&mut self) -> &mut StyleRefinement {
self.div.style()
}
}
impl ParentElement for WithRemSize {
fn extend(&mut self, elements: impl IntoIterator<Item = AnyElement>) {
self.div.extend(elements)
}
}
impl Element for WithRemSize {
type RequestLayoutState = DivFrameState;
type PrepaintState = Option<Hitbox>;
fn id(&self) -> Option<ElementId> {
self.div.id()
}
fn request_layout(
&mut self,
id: Option<&GlobalElementId>,
cx: &mut WindowContext,
) -> (LayoutId, Self::RequestLayoutState) {
cx.with_rem_size(Some(self.rem_size), |cx| self.div.request_layout(id, cx))
}
fn prepaint(
&mut self,
id: Option<&GlobalElementId>,
bounds: Bounds<Pixels>,
request_layout: &mut Self::RequestLayoutState,
cx: &mut WindowContext,
) -> Self::PrepaintState {
cx.with_rem_size(Some(self.rem_size), |cx| {
self.div.prepaint(id, bounds, request_layout, cx)
})
}
fn paint(
&mut self,
id: Option<&GlobalElementId>,
bounds: Bounds<Pixels>,
request_layout: &mut Self::RequestLayoutState,
prepaint: &mut Self::PrepaintState,
cx: &mut WindowContext,
) {
cx.with_rem_size(Some(self.rem_size), |cx| {
self.div.paint(id, bounds, request_layout, prepaint, cx)
})
}
}
impl IntoElement for WithRemSize {
type Element = Self;
fn into_element(self) -> Self::Element {
self
}
}