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