rgpui_component/text/
mod.rs1mod document;
2mod format;
3mod inline;
4mod node;
5pub(crate) mod selection;
6mod state;
7mod style;
8mod text_view;
9mod utils;
10mod window_selection;
11
12use rgpui::{App, ElementId, IntoElement, RenderOnce, SharedString, Window};
13pub use state::*;
14pub use style::*;
15pub use text_view::*;
16pub(crate) use window_selection::TextSelectionController;
17pub(crate) use window_selection::WindowTextSelection;
18
19pub(crate) fn init(cx: &mut App) {
20 state::init(cx);
21}
22
23#[track_caller]
25pub fn markdown(source: impl Into<SharedString>) -> TextView {
26 let id: ElementId = ElementId::CodeLocation(*std::panic::Location::caller());
27 TextView::markdown(id, source)
28}
29
30#[track_caller]
32pub fn html(source: impl Into<SharedString>) -> TextView {
33 let id: ElementId = ElementId::CodeLocation(*std::panic::Location::caller());
34 TextView::html(id, source)
35}
36
37#[track_caller]
41pub fn plain(source: impl Into<SharedString>) -> TextView {
42 let id: ElementId = ElementId::CodeLocation(*std::panic::Location::caller());
43 TextView::plain(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 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 pub(crate) fn get_text(&self, cx: &App) -> SharedString {
89 match self {
90 Self::String(s) => s.clone(),
91 Self::TextView(view) => {
92 if let Some(state) = &view.state {
93 state.read(cx).source()
94 } else {
95 SharedString::default()
96 }
97 }
98 }
99 }
100}
101
102impl RenderOnce for Text {
103 fn render(self, _: &mut Window, _: &mut App) -> impl IntoElement {
104 match self {
105 Self::String(s) => s.into_any_element(),
106 Self::TextView(e) => e.into_any_element(),
107 }
108 }
109}