Skip to main content

gpui_base/text/
mod.rs

1mod document;
2pub mod editor_host;
3mod editor_seam;
4mod format;
5mod inline;
6mod inline_element;
7mod inline_flow;
8mod inline_object;
9#[cfg(test)]
10mod inline_virtual_tests;
11mod markdown_ext;
12mod node;
13mod range_highlight;
14pub(crate) mod selection;
15mod selection_adapter;
16mod state;
17mod stream_fade;
18mod style;
19mod text_view;
20mod utils;
21
22use gpui::{App, ElementId, IntoElement, RenderOnce, SharedString, Window};
23pub use inline_element::*;
24pub use markdown_ext::*;
25pub use node::{CodeBlock, TableData};
26pub use range_highlight::{RangeHighlight, RangeHighlightError, RenderedText};
27pub use state::*;
28pub use stream_fade::TextViewMotion;
29pub use style::*;
30pub use text_view::*;
31
32pub(crate) fn init(cx: &mut App) {
33    state::init(cx);
34}
35
36/// Create a new markdown text view with code location as id.
37#[track_caller]
38pub fn markdown(source: impl Into<SharedString>) -> TextView {
39    let id: ElementId = ElementId::CodeLocation(*std::panic::Location::caller());
40    TextView::markdown(id, source)
41}
42
43/// Create a new html text view with code location as id.
44#[track_caller]
45pub fn html(source: impl Into<SharedString>) -> TextView {
46    let id: ElementId = ElementId::CodeLocation(*std::panic::Location::caller());
47    TextView::html(id, source)
48}
49
50#[derive(IntoElement, Clone)]
51pub enum Text {
52    String(SharedString),
53    TextView(Box<TextView>),
54}
55
56impl From<SharedString> for Text {
57    fn from(s: SharedString) -> Self {
58        Self::String(s)
59    }
60}
61
62impl From<&str> for Text {
63    fn from(s: &str) -> Self {
64        Self::String(SharedString::from(s.to_string()))
65    }
66}
67
68impl From<String> for Text {
69    fn from(s: String) -> Self {
70        Self::String(s.into())
71    }
72}
73
74impl From<TextView> for Text {
75    fn from(e: TextView) -> Self {
76        Self::TextView(Box::new(e))
77    }
78}
79
80impl Text {
81    /// Set the style for [`TextView`].
82    ///
83    /// Do nothing if this is `String`.
84    pub fn style(self, style: TextViewStyle) -> Self {
85        match self {
86            Self::String(s) => Self::String(s),
87            Self::TextView(e) => Self::TextView(Box::new(e.style(style))),
88        }
89    }
90
91    /// Get the text content.
92    #[doc(hidden)]
93    pub fn get_text(&self, cx: &App) -> SharedString {
94        match self {
95            Self::String(s) => s.clone(),
96            Self::TextView(view) => {
97                if let Some(state) = &view.state {
98                    state.read(cx).source()
99                } else {
100                    SharedString::default()
101                }
102            }
103        }
104    }
105}
106
107impl RenderOnce for Text {
108    fn render(self, _: &mut Window, _: &mut App) -> impl IntoElement {
109        match self {
110            Self::String(s) => s.into_any_element(),
111            Self::TextView(e) => e.into_any_element(),
112        }
113    }
114}