Skip to main content

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