Skip to main content

gpui_base/text/
mod.rs

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