with_rem_size.rs

 1use gpui::{AnyElement, Hsla, Render};
 2use story::Story;
 3
 4use ui::{prelude::*, utils::WithRemSize};
 5
 6pub struct WithRemSizeStory;
 7
 8impl Render for WithRemSizeStory {
 9    fn render(&mut self, _window: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
10        Story::container(cx).child(
11            Example::new(16., gpui::red())
12                .child(
13                    Example::new(24., gpui::green())
14                        .child(Example::new(8., gpui::blue()))
15                        .child(Example::new(16., gpui::yellow())),
16                )
17                .child(
18                    Example::new(12., gpui::green())
19                        .child(Example::new(48., gpui::blue()))
20                        .child(Example::new(16., gpui::yellow())),
21                ),
22        )
23    }
24}
25
26#[derive(IntoElement)]
27struct Example {
28    rem_size: Pixels,
29    border_color: Hsla,
30    children: Vec<AnyElement>,
31}
32
33impl Example {
34    pub fn new(rem_size: impl Into<Pixels>, border_color: Hsla) -> Self {
35        Self {
36            rem_size: rem_size.into(),
37            border_color,
38            children: Vec::new(),
39        }
40    }
41}
42
43impl ParentElement for Example {
44    fn extend(&mut self, elements: impl IntoIterator<Item = AnyElement>) {
45        self.children.extend(elements);
46    }
47}
48
49impl RenderOnce for Example {
50    fn render(self, _window: &mut Window, _cx: &mut App) -> impl IntoElement {
51        WithRemSize::new(self.rem_size).child(
52            v_flex()
53                .gap_2()
54                .p_2()
55                .border_2()
56                .border_color(self.border_color)
57                .child(Label::new(format!("1rem = {}px", self.rem_size.0)))
58                .children(self.children),
59        )
60    }
61}