text.rs

  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 size = Size {
101                    width: lines.iter().map(|line| line.layout.width).max().unwrap(),
102                    height: line_height * lines.len(),
103                };
104
105                element_state
106                    .lock()
107                    .replace(TextElementState { lines, line_height });
108
109                size
110            }
111        });
112
113        layout_id
114    }
115
116    fn paint(
117        &mut self,
118        bounds: Bounds<Pixels>,
119        _: &mut V,
120        element_state: &mut Self::ElementState,
121        cx: &mut ViewContext<V>,
122    ) {
123        let element_state = element_state.lock();
124        let element_state = element_state
125            .as_ref()
126            .expect("measurement has not been performed");
127        let line_height = element_state.line_height;
128        let mut line_origin = bounds.origin;
129        for line in &element_state.lines {
130            line.paint(line_origin, line_height, cx).log_err();
131            line_origin.y += line.size(line_height).height;
132        }
133    }
134}
135
136pub struct TextElementState {
137    lines: SmallVec<[Line; 1]>,
138    line_height: Pixels,
139}