text.rs

 1use gpui::{
 2    color::Color,
 3    elements::Text,
 4    fonts::{HighlightStyle, TextStyle},
 5    platform::{CursorStyle, MouseButton},
 6    AnyElement, CursorRegion, Element, MouseRegion,
 7};
 8use log::LevelFilter;
 9use simplelog::SimpleLogger;
10
11fn main() {
12    SimpleLogger::init(LevelFilter::Info, Default::default()).expect("could not initialize logger");
13
14    gpui::App::new(()).unwrap().run(|cx| {
15        cx.platform().activate(true);
16        cx.add_window(Default::default(), |_| TextView);
17    });
18}
19
20struct TextView;
21
22impl gpui::Entity for TextView {
23    type Event = ();
24}
25
26impl gpui::View for TextView {
27    fn ui_name() -> &'static str {
28        "View"
29    }
30
31    fn render(&mut self, cx: &mut gpui::ViewContext<Self>) -> AnyElement<TextView> {
32        let font_size = 12.;
33        let family = cx
34            .font_cache
35            .load_family(&["Monaco"], &Default::default())
36            .unwrap();
37        let font_id = cx
38            .font_cache
39            .select_font(family, &Default::default())
40            .unwrap();
41        let view_id = cx.view_id();
42
43        let underline = HighlightStyle {
44            underline: Some(gpui::fonts::Underline {
45                thickness: 1.0.into(),
46                ..Default::default()
47            }),
48            ..Default::default()
49        };
50
51        Text::new(
52            "The text:\nHello, beautiful world, hello!",
53            TextStyle {
54                font_id,
55                font_size,
56                color: Color::red(),
57                font_family_name: "".into(),
58                font_family_id: family,
59                underline: Default::default(),
60                font_properties: Default::default(),
61                soft_wrap: false,
62            },
63        )
64        .with_highlights(vec![(17..26, underline), (34..40, underline)])
65        .with_custom_runs(vec![(17..26), (34..40)], move |ix, bounds, cx| {
66            cx.scene().push_cursor_region(CursorRegion {
67                bounds,
68                style: CursorStyle::PointingHand,
69            });
70            cx.scene().push_mouse_region(
71                MouseRegion::new::<Self>(view_id, ix, bounds).on_click::<Self, _>(
72                    MouseButton::Left,
73                    move |_, _, _| {
74                        eprintln!("clicked link {ix}");
75                    },
76                ),
77            );
78        })
79        .into_any()
80    }
81}