text_layout.rs

 1use gpui::{
 2    App, Bounds, Context, FontStyle, FontWeight, StyledText, Window, WindowBounds, WindowOptions,
 3    div, prelude::*, px, size,
 4};
 5use gpui_platform::application;
 6
 7struct HelloWorld {}
 8
 9impl Render for HelloWorld {
10    fn render(&mut self, _window: &mut Window, _cx: &mut Context<Self>) -> impl IntoElement {
11        div()
12            .bg(gpui::white())
13            .flex()
14            .flex_col()
15            .gap_2()
16            .p_4()
17            .size_full()
18            .child(div().child("Text left"))
19            .child(div().text_center().child("Text center"))
20            .child(div().text_right().child("Text right"))
21            .child(div().text_decoration_1().child("Text left (underline)"))
22            .child(
23                div()
24                    .text_center()
25                    .text_decoration_1()
26                    .child("Text center (underline)"),
27            )
28            .child(
29                div()
30                    .text_right()
31                    .text_decoration_1()
32                    .child("Text right (underline)"),
33            )
34            .child(div().line_through().child("Text left (line_through)"))
35            .child(
36                div()
37                    .text_center()
38                    .line_through()
39                    .child("Text center (line_through)"),
40            )
41            .child(
42                div()
43                    .text_right()
44                    .line_through()
45                    .child("Text right (line_through)"),
46            )
47            .child(
48                div()
49                    .flex()
50                    .gap_2()
51                    .justify_between()
52                    .child(
53                        div()
54                            .w(px(400.))
55                            .border_1()
56                            .border_color(gpui::blue())
57                            .p_1()
58                            .whitespace_nowrap()
59                            .overflow_hidden()
60                            .text_center()
61                            .child("A long non-wrapping text align center"),
62                    )
63                    .child(
64                        div()
65                            .w_32()
66                            .border_1()
67                            .border_color(gpui::blue())
68                            .p_1()
69                            .whitespace_nowrap()
70                            .overflow_hidden()
71                            .text_right()
72                            .child("100%"),
73                    ),
74            )
75            .child(div().flex().gap_2().justify_between().child(
76                StyledText::new("ABCD").with_highlights([
77                    (0..1, FontWeight::EXTRA_BOLD.into()),
78                    (2..3, FontStyle::Italic.into()),
79                ]),
80            ))
81    }
82}
83
84fn main() {
85    application().run(|cx: &mut App| {
86        let bounds = Bounds::centered(None, size(px(800.0), px(600.0)), cx);
87        cx.open_window(
88            WindowOptions {
89                window_bounds: Some(WindowBounds::Windowed(bounds)),
90                ..Default::default()
91            },
92            |_, cx| cx.new(|_| HelloWorld {}),
93        )
94        .unwrap();
95        cx.activate(true);
96    });
97}