text.rs

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