Skip to main content

gpui_base/text/
mod.rs

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