1use gpui::{
2 color::Color,
3 fonts::{Properties, Weight},
4 text_layout::RunStyle,
5 DebugContext, Element as _, MeasurementContext, Quad,
6};
7use log::LevelFilter;
8use pathfinder_geometry::rect::RectF;
9use simplelog::SimpleLogger;
10use std::ops::Range;
11
12fn main() {
13 SimpleLogger::init(LevelFilter::Info, Default::default()).expect("could not initialize logger");
14
15 gpui::App::new(()).unwrap().run(|cx| {
16 cx.platform().activate(true);
17 cx.add_window(Default::default(), |_| TextView);
18 });
19}
20
21struct TextView;
22struct TextElement;
23
24impl gpui::Entity for TextView {
25 type Event = ();
26}
27
28impl gpui::View for TextView {
29 fn ui_name() -> &'static str {
30 "View"
31 }
32
33 fn render(&mut self, _: &mut gpui::RenderContext<Self>) -> gpui::ElementBox {
34 TextElement.boxed()
35 }
36}
37
38impl gpui::Element for TextElement {
39 type LayoutState = ();
40
41 type PaintState = ();
42
43 fn layout(
44 &mut self,
45 constraint: gpui::SizeConstraint,
46 _: &mut gpui::LayoutContext,
47 ) -> (pathfinder_geometry::vector::Vector2F, Self::LayoutState) {
48 (constraint.max, ())
49 }
50
51 fn paint(
52 &mut self,
53 bounds: RectF,
54 visible_bounds: RectF,
55 _: &mut Self::LayoutState,
56 cx: &mut gpui::PaintContext,
57 ) -> Self::PaintState {
58 let font_size = 12.;
59 let family = cx
60 .font_cache
61 .load_family(&["SF Pro Display"], &Default::default())
62 .unwrap();
63 let normal = RunStyle {
64 font_id: cx
65 .font_cache
66 .select_font(family, &Default::default())
67 .unwrap(),
68 color: Color::default(),
69 underline: Default::default(),
70 };
71 let bold = RunStyle {
72 font_id: cx
73 .font_cache
74 .select_font(
75 family,
76 &Properties {
77 weight: Weight::BOLD,
78 ..Default::default()
79 },
80 )
81 .unwrap(),
82 color: Color::default(),
83 underline: Default::default(),
84 };
85
86 let text = "Hello world!";
87 let line = cx.text_layout_cache.layout_str(
88 text,
89 font_size,
90 &[
91 (1, normal),
92 (1, bold),
93 (1, normal),
94 (1, bold),
95 (text.len() - 4, normal),
96 ],
97 );
98
99 cx.scene.push_quad(Quad {
100 bounds,
101 background: Some(Color::white()),
102 ..Default::default()
103 });
104 line.paint(bounds.origin(), visible_bounds, bounds.height(), cx);
105 }
106
107 fn rect_for_text_range(
108 &self,
109 _: Range<usize>,
110 _: RectF,
111 _: RectF,
112 _: &Self::LayoutState,
113 _: &Self::PaintState,
114 _: &MeasurementContext,
115 ) -> Option<RectF> {
116 None
117 }
118
119 fn debug(
120 &self,
121 _: RectF,
122 _: &Self::LayoutState,
123 _: &Self::PaintState,
124 _: &DebugContext,
125 ) -> gpui::json::Value {
126 todo!()
127 }
128}