text.rs

  1use crate::{
  2    AnyElement, Bounds, Element, IntoAnyElement, LayoutId, Line, Pixels, Result, Size, ViewContext,
  3};
  4use parking_lot::Mutex;
  5use std::{marker::PhantomData, sync::Arc};
  6use util::{arc_cow::ArcCow, ResultExt};
  7
  8impl<S: 'static> 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> 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> Element for Text<S> {
 34    type State = S;
 35    type FrameState = Arc<Mutex<Option<TextFrameState>>>;
 36
 37    fn layout(
 38        &mut self,
 39        _view: &mut S,
 40        cx: &mut ViewContext<S>,
 41    ) -> Result<(LayoutId, Self::FrameState)> {
 42        let text_system = cx.text_system().clone();
 43        let text_style = cx.text_style();
 44        let font_size = text_style.font_size * cx.rem_size();
 45        let line_height = text_style
 46            .line_height
 47            .to_pixels(font_size.into(), cx.rem_size());
 48        let text = self.text.clone();
 49        let frame_state = Arc::new(Mutex::new(None));
 50
 51        let rem_size = cx.rem_size();
 52        let layout_id = cx.request_measured_layout(Default::default(), rem_size, {
 53            let frame_state = frame_state.clone();
 54            move |_, _| {
 55                let Some(line_layout) = text_system
 56                    .layout_line(
 57                        text.as_ref(),
 58                        font_size,
 59                        &[(text.len(), text_style.to_run())],
 60                    )
 61                    .log_err()
 62                else {
 63                    return Size::default();
 64                };
 65
 66                let size = Size {
 67                    width: line_layout.width(),
 68                    height: line_height,
 69                };
 70
 71                frame_state.lock().replace(TextFrameState {
 72                    line: Arc::new(line_layout),
 73                    line_height,
 74                });
 75
 76                size
 77            }
 78        });
 79
 80        Ok((layout_id?, frame_state))
 81    }
 82
 83    fn paint<'a>(
 84        &mut self,
 85        bounds: Bounds<Pixels>,
 86        _: &mut Self::State,
 87        frame_state: &mut Self::FrameState,
 88        cx: &mut ViewContext<S>,
 89    ) -> Result<()> {
 90        let line;
 91        let line_height;
 92        {
 93            let frame_state = frame_state.lock();
 94            let frame_state = frame_state
 95                .as_ref()
 96                .expect("measurement has not been performed");
 97            line = frame_state.line.clone();
 98            line_height = frame_state.line_height;
 99        }
100
101        // todo!("We haven't added visible bounds to the new element system yet, so this is a placeholder.");
102        line.paint(bounds, bounds, line_height, cx)?;
103
104        Ok(())
105    }
106}
107
108pub struct TextFrameState {
109    line: Arc<Line>,
110    line_height: Pixels,
111}