1use crate::{
2 AnyElement, BorrowWindow, Bounds, Element, IntoAnyElement, LayoutId, Line, Pixels,
3 SharedString, Size, ViewContext,
4};
5use parking_lot::Mutex;
6use smallvec::SmallVec;
7use std::{marker::PhantomData, sync::Arc};
8use util::ResultExt;
9
10impl<S: 'static + Send + Sync> IntoAnyElement<S> for SharedString {
11 fn into_any(self) -> AnyElement<S> {
12 Text {
13 text: self,
14 state_type: PhantomData,
15 }
16 .into_any()
17 }
18}
19
20impl<V: 'static + Send + Sync> IntoAnyElement<V> for &'static str {
21 fn into_any(self) -> AnyElement<V> {
22 Text {
23 text: self.into(),
24 state_type: PhantomData,
25 }
26 .into_any()
27 }
28}
29
30// TODO: Figure out how to pass `String` to `child` without this.
31// This impl doesn't exist in the `gpui2` crate.
32impl<S: 'static + Send + Sync> IntoAnyElement<S> for String {
33 fn into_any(self) -> AnyElement<S> {
34 Text {
35 text: self.into(),
36 state_type: PhantomData,
37 }
38 .into_any()
39 }
40}
41
42pub struct Text<V> {
43 text: SharedString,
44 state_type: PhantomData<V>,
45}
46
47impl<V: 'static + Send + Sync> IntoAnyElement<V> for Text<V> {
48 fn into_any(self) -> AnyElement<V> {
49 AnyElement::new(self)
50 }
51}
52
53impl<V: 'static + Send + Sync> Element for Text<V> {
54 type ViewState = V;
55 type ElementState = Arc<Mutex<Option<TextElementState>>>;
56
57 fn id(&self) -> Option<crate::ElementId> {
58 None
59 }
60
61 fn initialize(
62 &mut self,
63 _view_state: &mut V,
64 element_state: Option<Self::ElementState>,
65 _cx: &mut ViewContext<V>,
66 ) -> Self::ElementState {
67 element_state.unwrap_or_default()
68 }
69
70 fn layout(
71 &mut self,
72 _view: &mut V,
73 element_state: &mut Self::ElementState,
74 cx: &mut ViewContext<V>,
75 ) -> LayoutId {
76 let text_system = cx.text_system().clone();
77 let text_style = cx.text_style();
78 let font_size = text_style.font_size * cx.rem_size();
79 let line_height = text_style
80 .line_height
81 .to_pixels(font_size.into(), cx.rem_size());
82 let text = self.text.clone();
83
84 let rem_size = cx.rem_size();
85 let layout_id = cx.request_measured_layout(Default::default(), rem_size, {
86 let element_state = element_state.clone();
87 move |known_dimensions, _| {
88 let Some(lines) = text_system
89 .layout_text(
90 &text,
91 font_size,
92 &[text_style.to_run(text.len())],
93 known_dimensions.width, // Wrap if we know the width.
94 )
95 .log_err()
96 else {
97 return Size::default();
98 };
99
100 let line_count = lines
101 .iter()
102 .map(|line| line.wrap_count() + 1)
103 .sum::<usize>();
104 let size = Size {
105 width: lines.iter().map(|line| line.layout.width).max().unwrap(),
106 height: line_height * line_count,
107 };
108
109 element_state
110 .lock()
111 .replace(TextElementState { lines, line_height });
112
113 size
114 }
115 });
116
117 layout_id
118 }
119
120 fn paint(
121 &mut self,
122 bounds: Bounds<Pixels>,
123 _: &mut V,
124 element_state: &mut Self::ElementState,
125 cx: &mut ViewContext<V>,
126 ) {
127 let element_state = element_state.lock();
128 let element_state = element_state
129 .as_ref()
130 .expect("measurement has not been performed");
131 let line_height = element_state.line_height;
132 let mut line_origin = bounds.origin;
133 for line in &element_state.lines {
134 line.paint(line_origin, line_height, cx).log_err();
135 line_origin.y += line.size(line_height).height;
136 }
137 }
138}
139
140pub struct TextElementState {
141 lines: SmallVec<[Line; 1]>,
142 line_height: Pixels,
143}