text_wrapper.rs

  1use gpui::{
  2    App, Bounds, Context, TextOverflow, Window, WindowBounds, WindowOptions, div, prelude::*, px,
  3    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        let text = "The longest word 你好世界这段是中文,こんにちはこの段落は日本語です in any of the major \
 12            English language dictionaries is pneumonoultramicroscopicsilicovolcanoconiosis, a word that \
 13            refers to a lung disease contracted from the inhalation of very fine silica particles, \
 14            a url https://github.com/zed-industries/zed/pull/35724?query=foo&bar=2, \
 15            specifically from a volcano; medically, it is the same as silicosis.";
 16        div()
 17            .id("page")
 18            .size_full()
 19            .flex()
 20            .flex_col()
 21            .p_2()
 22            .gap_2()
 23            .bg(gpui::white())
 24            .child(
 25                div()
 26                    .flex()
 27                    .flex_row()
 28                    .flex_shrink_0()
 29                    .gap_2()
 30                    .child(
 31                        div()
 32                            .flex()
 33                            .border_1()
 34                            .border_color(gpui::red())
 35                            .text_ellipsis()
 36                            .child("longer text in flex 1"),
 37                    )
 38                    .child(
 39                        div()
 40                            .flex()
 41                            .border_1()
 42                            .border_color(gpui::red())
 43                            .text_ellipsis()
 44                            .child("short flex"),
 45                    )
 46                    .child(
 47                        div()
 48                            .overflow_hidden()
 49                            .border_1()
 50                            .border_color(gpui::red())
 51                            .text_ellipsis()
 52                            .w_full()
 53                            .child("A short text in normal div"),
 54                    ),
 55            )
 56            .child(
 57                div()
 58                    .flex_shrink_0()
 59                    .text_xl()
 60                    .truncate()
 61                    .border_1()
 62                    .border_color(gpui::blue())
 63                    .child("ELLIPSIS: ".to_owned() + text),
 64            )
 65            .child(
 66                div()
 67                    .flex_shrink_0()
 68                    .text_xl()
 69                    .overflow_hidden()
 70                    .text_ellipsis()
 71                    .line_clamp(2)
 72                    .border_1()
 73                    .border_color(gpui::blue())
 74                    .child("ELLIPSIS 2 lines: ".to_owned() + text),
 75            )
 76            .child(
 77                div()
 78                    .flex_shrink_0()
 79                    .text_xl()
 80                    .overflow_hidden()
 81                    .text_overflow(TextOverflow::Truncate("".into()))
 82                    .border_1()
 83                    .border_color(gpui::green())
 84                    .child("TRUNCATE: ".to_owned() + text),
 85            )
 86            .child(
 87                div()
 88                    .flex_shrink_0()
 89                    .text_xl()
 90                    .overflow_hidden()
 91                    .text_overflow(TextOverflow::Truncate("".into()))
 92                    .line_clamp(3)
 93                    .border_1()
 94                    .border_color(gpui::green())
 95                    .child("TRUNCATE 3 lines: ".to_owned() + text),
 96            )
 97            .child(
 98                div()
 99                    .flex_shrink_0()
100                    .text_xl()
101                    .whitespace_nowrap()
102                    .overflow_hidden()
103                    .border_1()
104                    .border_color(gpui::black())
105                    .child("NOWRAP: ".to_owned() + text),
106            )
107            .child(div().text_xl().w_full().child(text))
108    }
109}
110
111fn main() {
112    application().run(|cx: &mut App| {
113        let bounds = Bounds::centered(None, size(px(800.0), px(600.0)), cx);
114        cx.open_window(
115            WindowOptions {
116                window_bounds: Some(WindowBounds::Windowed(bounds)),
117                ..Default::default()
118            },
119            |_, cx| cx.new(|_| HelloWorld {}),
120        )
121        .unwrap();
122        cx.activate(true);
123    });
124}