Skip to main content

gpui_base/text/
inline_element.rs

1use gpui::{AnyElement, IntoElement, Pixels, TextStyle};
2
3/// A GPUI element participating as one atomic object in a text line.
4///
5/// Use native GPUI styles, events and components on the element. TextView
6/// measures its intrinsic size and wraps around the whole object. Fixed-width
7/// elements keep their actual size; constrain them with GPUI styles as needed.
8pub struct InlineElement {
9    pub(crate) element: AnyElement,
10    pub(crate) baseline: Option<Pixels>,
11}
12
13impl InlineElement {
14    pub fn new(element: impl IntoElement) -> Self {
15        Self {
16            element: element.into_any_element(),
17            baseline: None,
18        }
19    }
20
21    /// Distance from the top edge to the alphabetic baseline, in logical pixels.
22    /// By default the box aligns its bottom with the surrounding text's descent.
23    pub fn with_baseline(mut self, baseline: Pixels) -> Self {
24        self.baseline = Some(baseline);
25        self
26    }
27}
28
29/// Inherited typography for a format-independent inline renderer.
30///
31/// TextView builds this while laying a line out; renderers only read it.
32#[derive(Clone)]
33pub struct InlineRenderContext {
34    text_style: TextStyle,
35    font_size: Pixels,
36    line_height: Pixels,
37    rem_size: Pixels,
38}
39
40impl InlineRenderContext {
41    pub(crate) fn new(
42        text_style: TextStyle,
43        font_size: Pixels,
44        line_height: Pixels,
45        rem_size: Pixels,
46    ) -> Self {
47        Self {
48            text_style,
49            font_size,
50            line_height,
51            rem_size,
52        }
53    }
54
55    /// Effective text style at the object's position, marks already applied.
56    pub fn text_style(&self) -> &TextStyle {
57        &self.text_style
58    }
59
60    /// Font size of the surrounding text, in logical pixels.
61    pub fn font_size(&self) -> Pixels {
62        self.font_size
63    }
64
65    /// Line height of the surrounding text, in logical pixels.
66    pub fn line_height(&self) -> Pixels {
67        self.line_height
68    }
69
70    /// Root font size, for resolving rem-relative lengths.
71    pub fn rem_size(&self) -> Pixels {
72        self.rem_size
73    }
74}