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