Skip to main content

gpui_base/input/textarea/
mod.rs

1use gpui::{App, Entity, IntoElement, RenderOnce, Window};
2
3use super::{InputBaseState, TextareaMode};
4
5/// State for editing ordinary multi-line text.
6///
7/// This is the shared editing engine in its multi-line kind. Code-editor
8/// facilities such as languages, diagnostics, folding, and LSP do not exist on
9/// this type — those methods live on [`super::EditorState`].
10pub type TextareaState = InputBaseState<TextareaMode>;
11
12/// An unstyled ordinary multi-line text input.
13#[derive(IntoElement)]
14pub struct Textarea {
15    presentation: super::InlineTokenPresentation,
16    state: Entity<TextareaState>,
17}
18
19impl Textarea {
20    pub fn new(state: &Entity<TextareaState>) -> Self {
21        Self {
22            state: state.clone(),
23            presentation: Default::default(),
24        }
25    }
26    /// The element each atomic token renders as; the input keeps editing and history.
27    pub fn token<R: IntoElement>(
28        mut self,
29        render: impl Fn(&super::InlineTokenContext, &mut Window, &mut App) -> R + 'static,
30    ) -> Self {
31        self.presentation = self.presentation.token(render);
32        self
33    }
34    /// Open a reference after a completed, unconsumed token click.
35    pub fn on_token_click(
36        mut self,
37        listener: impl Fn(&super::InlineTokenClickEvent, &mut Window, &mut App) + 'static,
38    ) -> Self {
39        self.presentation = self.presentation.on_token_click(listener);
40        self
41    }
42}
43
44impl RenderOnce for Textarea {
45    fn render(self, _: &mut Window, cx: &mut App) -> impl IntoElement {
46        self.state.update(cx, |state, _| {
47            state.set_token_presentation(self.presentation)
48        });
49        self.state
50    }
51}