Skip to main content

gpui_component/text/
mod.rs

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