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
 28pub struct Text<S> {
 29    text: ArcCow<'static, str>,
 30    state_type: PhantomData<S>,
 31}
 32
 33impl<S: 'static + Send + Sync> Element for Text<S> {
 34    type ViewState = S;
 35    type ElementState = Arc<Mutex<Option<TextElementState>>>;
 36
 37    fn layout(
 38        &mut self,
 39        _view: &mut S,
 40        _element_state: Option<Self::ElementState>,
 41        cx: &mut ViewContext<S>,
 42    ) -> (LayoutId, Self::ElementState) {
 43        let text_system = cx.text_system().clone();
 44        let text_style = cx.text_style();
 45        let font_size = text_style.font_size * cx.rem_size();
 46        let line_height = text_style
 47            .line_height
 48            .to_pixels(font_size.into(), cx.rem_size());
 49        let text = self.text.clone();
 50        let element_state = Arc::new(Mutex::new(None));
 51
 52        let rem_size = cx.rem_size();
 53        let layout_id = cx.request_measured_layout(Default::default(), rem_size, {
 54            let element_state = element_state.clone();
 55            move |_, _| {
 56                let Some(line_layout) = text_system
 57                    .layout_line(
 58                        text.as_ref(),
 59                        font_size,
 60                        &[(text.len(), text_style.to_run())],
 61                    )
 62                    .log_err()
 63                else {
 64                    return Size::default();
 65                };
 66
 67                let size = Size {
 68                    width: line_layout.width(),
 69                    height: line_height,
 70                };
 71
 72                element_state.lock().replace(TextElementState {
 73                    line: Arc::new(line_layout),
 74                    line_height,
 75                });
 76
 77                size
 78            }
 79        });
 80
 81        (layout_id, element_state)
 82    }
 83
 84    fn paint<'a>(
 85        &mut self,
 86        bounds: Bounds<Pixels>,
 87        _: &mut Self::ViewState,
 88        element_state: &mut Self::ElementState,
 89        cx: &mut ViewContext<S>,
 90    ) {
 91        let line;
 92        let line_height;
 93        {
 94            let element_state = element_state.lock();
 95            let element_state = element_state
 96                .as_ref()
 97                .expect("measurement has not been performed");
 98            line = element_state.line.clone();
 99            line_height = element_state.line_height;
100        }
101
102        line.paint(bounds, bounds, line_height, cx).log_err();
103    }
104}
105
106pub struct TextElementState {
107    line: Arc<Line>,
108    line_height: Pixels,
109}