use gpui_kit::component::text::{TextView as KitTextView, TextViewState};
use gpui_kit::{AppContext as _, Context, Entity, IntoElement, ParentElement as _, Render, Styled as _, Window, div};
pub struct TextView {
text: String,
state: Entity<TextViewState>,
}
impl TextView {
pub fn new(markdown: impl Into<String>, cx: &mut Context<Self>) -> Self {
let text = markdown.into();
let state = cx.new(|cx| TextViewState::markdown(&text, cx).selectable(true));
Self { text, state }
}
pub fn push_str(&mut self, more: &str, cx: &mut Context<Self>) {
self.text.push_str(more);
self.state.update(cx, |s, cx| s.push_str(more, cx));
cx.notify();
}
pub fn set_text(&mut self, markdown: impl Into<String>, cx: &mut Context<Self>) {
self.text = markdown.into();
let text = self.text.clone();
self.state.update(cx, |s, cx| s.set_text(&text, cx));
cx.notify();
}
pub fn text(&self) -> &str {
&self.text
}
pub fn selected_text(&self, cx: &gpui_kit::App) -> String {
self.state.read(cx).selected_text()
}
}
impl Render for TextView {
fn render(&mut self, _: &mut Window, _: &mut Context<Self>) -> impl IntoElement {
div().w_full().min_w_0().text_sm().child(KitTextView::new(&self.state).selectable(true).w_full())
}
}