text.rs

 1use gpui::{
 2    blue, div, green, red, white, Div, InteractiveText, ParentElement, Render, Styled, StyledText,
 3    TextRun, View, VisualContext, WindowContext,
 4};
 5use ui::v_stack;
 6
 7pub struct TextStory;
 8
 9impl TextStory {
10    pub fn view(cx: &mut WindowContext) -> View<Self> {
11        cx.build_view(|_cx| Self)
12    }
13}
14
15impl Render for TextStory {
16    type Element = Div;
17
18    fn render(&mut self, cx: &mut gpui::ViewContext<Self>) -> Self::Element {
19        v_stack()
20            .bg(blue())
21            .child(
22                div()
23                    .flex()
24                    .child(div().max_w_96().bg(white()).child(concat!(
25        "max-width: 96. The quick brown fox jumps over the lazy dog. ",
26        "Meanwhile, the lazy dog decided it was time for a change. ",
27        "He started daily workout routines, ate healthier and became the fastest dog in town.",
28    ))),
29            )
30            .child(div().h_5())
31            .child(div().flex().flex_col().w_96().bg(white()).child(concat!(
32        "flex-col. width: 96; The quick brown fox jumps over the lazy dog. ",
33        "Meanwhile, the lazy dog decided it was time for a change. ",
34        "He started daily workout routines, ate healthier and became the fastest dog in town.",
35    )))
36            .child(div().h_5())
37            .child(
38                div()
39                    .flex()
40                    .child(div().min_w_96().bg(white()).child(concat!(
41    "min-width: 96. The quick brown fox jumps over the lazy dog. ",
42    "Meanwhile, the lazy dog decided it was time for a change. ",
43    "He started daily workout routines, ate healthier and became the fastest dog in town.",
44))))
45            .child(div().h_5())
46            .child(div().flex().w_96().bg(white()).child(div().overflow_hidden().child(concat!(
47        "flex-row. width 96. overflow-hidden. The quick brown fox jumps over the lazy dog. ",
48        "Meanwhile, the lazy dog decided it was time for a change. ",
49        "He started daily workout routines, ate healthier and became the fastest dog in town.",
50    ))))
51            // NOTE: When rendering text in a horizonal flex container,
52            // Taffy will not pass width constraints down from the parent.
53            // To fix this, render text in a praent with overflow: hidden, which
54                    .child(div().h_5())
55                    .child(div().flex().w_96().bg(red()).child(concat!(
56                "flex-row. width 96. The quick brown fox jumps over the lazy dog. ",
57                "Meanwhile, the lazy dog decided it was time for a change. ",
58                "He started daily workout routines, ate healthier and became the fastest dog in town.",
59            ))).child(
60                InteractiveText::new(
61                    "interactive",
62                    StyledText::new("Hello world, how is it going?").with_runs(vec![
63                        cx.text_style().to_run(6),
64                        TextRun {
65                            background_color: Some(green()),
66                            ..cx.text_style().to_run(5)
67                        },
68                        cx.text_style().to_run(18),
69                    ]),
70                )
71                .on_click(vec![2..4, 1..3, 7..9], |range_ix, _cx| {
72                    println!("Clicked range {range_ix}");
73                })
74            )
75    }
76}