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, 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<TextLayout>>>;
 36
 37    fn layout(
 38        &mut self,
 39        _view: &mut S,
 40        cx: &mut ViewContext<S>,
 41    ) -> Result<(LayoutId, Self::FrameState)> {
 42        dbg!("layout text");
 43
 44        let text_system = cx.text_system().clone();
 45        let text_style = cx.text_style();
 46        let font_size = text_style.font_size * cx.rem_size();
 47        let line_height = text_style
 48            .line_height
 49            .to_pixels(font_size.into(), cx.rem_size());
 50        let text = self.text.clone();
 51        let paint_state = Arc::new(Mutex::new(None));
 52
 53        let rem_size = cx.rem_size();
 54        let layout_id = cx.request_measured_layout(Default::default(), rem_size, {
 55            let frame_state = paint_state.clone();
 56            move |_, _| {
 57                dbg!("starting measurement");
 58                let Some(line_layout) = text_system
 59                    .layout_line(
 60                        text.as_ref(),
 61                        font_size,
 62                        &[(text.len(), text_style.to_run())],
 63                    )
 64                    .log_err()
 65                else {
 66                    return Size::default();
 67                };
 68                dbg!("bbbb");
 69
 70                let size = Size {
 71                    width: line_layout.width(),
 72                    height: line_height,
 73                };
 74
 75                frame_state.lock().replace(TextLayout {
 76                    line: Arc::new(line_layout),
 77                    line_height,
 78                });
 79
 80                dbg!(size)
 81            }
 82        });
 83
 84        dbg!("got to end of text layout");
 85        Ok((layout_id?, paint_state))
 86    }
 87
 88    fn paint<'a>(
 89        &mut self,
 90        layout: Layout,
 91        _: &mut Self::State,
 92        paint_state: &mut Self::FrameState,
 93        cx: &mut ViewContext<S>,
 94    ) -> Result<()> {
 95        let bounds = layout.bounds;
 96
 97        let line;
 98        let line_height;
 99        {
100            let paint_state = paint_state.lock();
101            let paint_state = paint_state
102                .as_ref()
103                .expect("measurement has not been performed");
104            line = paint_state.line.clone();
105            line_height = paint_state.line_height;
106        }
107
108        let _text_style = cx.text_style();
109
110        // todo!("We haven't added visible bounds to the new element system yet, so this is a placeholder.");
111        let visible_bounds = bounds;
112        line.paint(bounds.origin, visible_bounds, line_height, cx)?;
113
114        Ok(())
115    }
116}
117
118pub struct TextLayout {
119    line: Arc<Line>,
120    line_height: Pixels,
121}