text.rs

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