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> IntoAnyElement<S> for Text<S> {
 46    fn into_any(self) -> AnyElement<S> {
 47        AnyElement::new(self)
 48    }
 49}
 50
 51impl<S: 'static + Send + Sync> Element for Text<S> {
 52    type ViewState = S;
 53    type ElementState = Arc<Mutex<Option<TextElementState>>>;
 54
 55    fn element_id(&self) -> Option<crate::ElementId> {
 56        None
 57    }
 58
 59    fn layout(
 60        &mut self,
 61        _view: &mut S,
 62        _element_state: Option<Self::ElementState>,
 63        cx: &mut ViewContext<S>,
 64    ) -> (LayoutId, Self::ElementState) {
 65        let text_system = cx.text_system().clone();
 66        let text_style = cx.text_style();
 67        let font_size = text_style.font_size * cx.rem_size();
 68        let line_height = text_style
 69            .line_height
 70            .to_pixels(font_size.into(), cx.rem_size());
 71        let text = self.text.clone();
 72        let element_state = Arc::new(Mutex::new(None));
 73
 74        let rem_size = cx.rem_size();
 75        let layout_id = cx.request_measured_layout(Default::default(), rem_size, {
 76            let element_state = element_state.clone();
 77            move |_, _| {
 78                let Some(line_layout) = text_system
 79                    .layout_line(
 80                        text.as_ref(),
 81                        font_size,
 82                        &[(text.len(), text_style.to_run())],
 83                    )
 84                    .log_err()
 85                else {
 86                    return Size::default();
 87                };
 88
 89                let size = Size {
 90                    width: line_layout.width(),
 91                    height: line_height,
 92                };
 93
 94                element_state.lock().replace(TextElementState {
 95                    line: Arc::new(line_layout),
 96                    line_height,
 97                });
 98
 99                size
100            }
101        });
102
103        (layout_id, element_state)
104    }
105
106    fn paint<'a>(
107        &mut self,
108        bounds: Bounds<Pixels>,
109        _: &mut Self::ViewState,
110        element_state: &mut Self::ElementState,
111        cx: &mut ViewContext<S>,
112    ) {
113        let line;
114        let line_height;
115        {
116            let element_state = element_state.lock();
117            let element_state = element_state
118                .as_ref()
119                .expect("measurement has not been performed");
120            line = element_state.line.clone();
121            line_height = element_state.line_height;
122        }
123
124        line.paint(bounds, bounds, line_height, cx).log_err();
125    }
126}
127
128pub struct TextElementState {
129    line: Arc<Line>,
130    line_height: Pixels,
131}