text_layout.rs

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