text.rs

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