text.rs

  1use crate::{
  2    AnyElement, BorrowWindow, Bounds, Component, Element, LayoutId, Line, Pixels, SharedString,
  3    Size, ViewContext,
  4};
  5use parking_lot::Mutex;
  6use smallvec::SmallVec;
  7use std::{marker::PhantomData, sync::Arc};
  8use util::ResultExt;
  9
 10impl<V: 'static> Component<V> for SharedString {
 11    fn render(self) -> AnyElement<V> {
 12        Text {
 13            text: self,
 14            state_type: PhantomData,
 15        }
 16        .render()
 17    }
 18}
 19
 20impl<V: 'static> Component<V> for &'static str {
 21    fn render(self) -> AnyElement<V> {
 22        Text {
 23            text: self.into(),
 24            state_type: PhantomData,
 25        }
 26        .render()
 27    }
 28}
 29
 30// TODO: Figure out how to pass `String` to `child` without this.
 31// This impl doesn't exist in the `gpui2` crate.
 32impl<V: 'static> Component<V> for String {
 33    fn render(self) -> AnyElement<V> {
 34        Text {
 35            text: self.into(),
 36            state_type: PhantomData,
 37        }
 38        .render()
 39    }
 40}
 41
 42pub struct Text<V> {
 43    text: SharedString,
 44    state_type: PhantomData<V>,
 45}
 46
 47impl<V: 'static> Component<V> for Text<V> {
 48    fn render(self) -> AnyElement<V> {
 49        AnyElement::new(self)
 50    }
 51}
 52
 53impl<V: 'static> Element<V> for Text<V> {
 54    type ElementState = Arc<Mutex<Option<TextElementState>>>;
 55
 56    fn id(&self) -> Option<crate::ElementId> {
 57        None
 58    }
 59
 60    fn initialize(
 61        &mut self,
 62        _view_state: &mut V,
 63        element_state: Option<Self::ElementState>,
 64        _cx: &mut ViewContext<V>,
 65    ) -> Self::ElementState {
 66        element_state.unwrap_or_default()
 67    }
 68
 69    fn layout(
 70        &mut self,
 71        _view: &mut V,
 72        element_state: &mut Self::ElementState,
 73        cx: &mut ViewContext<V>,
 74    ) -> LayoutId {
 75        let text_system = cx.text_system().clone();
 76        let text_style = cx.text_style();
 77        let font_size = text_style.font_size.to_pixels(cx.rem_size());
 78        let line_height = text_style
 79            .line_height
 80            .to_pixels(font_size.into(), cx.rem_size());
 81        let text = self.text.clone();
 82
 83        let rem_size = cx.rem_size();
 84        let layout_id = cx.request_measured_layout(Default::default(), rem_size, {
 85            let element_state = element_state.clone();
 86            move |known_dimensions, _| {
 87                let Some(lines) = text_system
 88                    .layout_text(
 89                        &text,
 90                        font_size,
 91                        &[text_style.to_run(text.len())],
 92                        known_dimensions.width, // Wrap if we know the width.
 93                    )
 94                    .log_err()
 95                else {
 96                    return Size::default();
 97                };
 98
 99                let line_count = lines
100                    .iter()
101                    .map(|line| line.wrap_count() + 1)
102                    .sum::<usize>();
103                let size = Size {
104                    width: lines
105                        .iter()
106                        .map(|line| line.layout.width)
107                        .max()
108                        .unwrap()
109                        .ceil(),
110                    height: line_height * line_count,
111                };
112
113                element_state
114                    .lock()
115                    .replace(TextElementState { lines, line_height });
116
117                size
118            }
119        });
120
121        layout_id
122    }
123
124    fn paint(
125        &mut self,
126        bounds: Bounds<Pixels>,
127        _: &mut V,
128        element_state: &mut Self::ElementState,
129        cx: &mut ViewContext<V>,
130    ) {
131        let element_state = element_state.lock();
132        let element_state = element_state
133            .as_ref()
134            .expect("measurement has not been performed");
135
136        let line_height = element_state.line_height;
137        let mut line_origin = bounds.origin;
138        for line in &element_state.lines {
139            line.paint(line_origin, line_height, cx).log_err();
140            line_origin.y += line.size(line_height).height;
141        }
142    }
143}
144
145pub struct TextElementState {
146    lines: SmallVec<[Line; 1]>,
147    line_height: Pixels,
148}