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    inline::init(cx);
34    state::init(cx);
35}
36
37/// Create a new markdown text view with code location as id.
38#[track_caller]
39pub fn markdown(source: impl Into<SharedString>) -> TextView {
40    let id: ElementId = ElementId::CodeLocation(*std::panic::Location::caller());
41    TextView::markdown(id, source)
42}
43
44/// Create a new html text view with code location as id.
45#[track_caller]
46pub fn html(source: impl Into<SharedString>) -> TextView {
47    let id: ElementId = ElementId::CodeLocation(*std::panic::Location::caller());
48    TextView::html(id, source)
49}
50
51#[derive(IntoElement, Clone)]
52pub enum Text {
53    String(SharedString),
54    TextView(Box<TextView>),
55}
56
57impl From<SharedString> for Text {
58    fn from(s: SharedString) -> Self {
59        Self::String(s)
60    }
61}
62
63impl From<&str> for Text {
64    fn from(s: &str) -> Self {
65        Self::String(SharedString::from(s.to_string()))
66    }
67}
68
69impl From<String> for Text {
70    fn from(s: String) -> Self {
71        Self::String(s.into())
72    }
73}
74
75impl From<TextView> for Text {
76    fn from(e: TextView) -> Self {
77        Self::TextView(Box::new(e))
78    }
79}
80
81impl Text {
82    /// Set the style for [`TextView`].
83    ///
84    /// Do nothing if this is `String`.
85    pub fn style(self, style: TextViewStyle) -> Self {
86        match self {
87            Self::String(s) => Self::String(s),
88            Self::TextView(e) => Self::TextView(Box::new(e.style(style))),
89        }
90    }
91
92    /// Get the text content.
93    #[doc(hidden)]
94    pub fn get_text(&self, cx: &App) -> SharedString {
95        match self {
96            Self::String(s) => s.clone(),
97            Self::TextView(view) => {
98                if let Some(state) = &view.state {
99                    state.read(cx).source()
100                } else {
101                    SharedString::default()
102                }
103            }
104        }
105    }
106}
107
108impl RenderOnce for Text {
109    fn render(self, _: &mut Window, _: &mut App) -> impl IntoElement {
110        match self {
111            Self::String(s) => s.into_any_element(),
112            Self::TextView(e) => e.into_any_element(),
113        }
114    }
115}