| 1 | use gpui::{App, Entity, IntoElement, RenderOnce, Window}; |
| 2 | |
| 3 | use 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`]. |
| 10 | pub type TextareaState = InputBaseState<TextareaMode>; |
| 11 | |
| 12 | /// An unstyled ordinary multi-line text input. |
| 13 | #[derive(IntoElement)] |
| 14 | pub struct Textarea { |
| 15 | presentation: super::InlineTokenPresentation, |
| 16 | state: Entity<TextareaState>, |
| 17 | } |
| 18 | |
| 19 | impl 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 | /// Report pointer presence over a token; hover never selects or edits. |
| 43 | pub fn on_token_hover( |
| 44 | mut self, |
| 45 | listener: impl Fn(&super::InlineTokenHoverEvent, &mut Window, &mut App) + 'static, |
| 46 | ) -> Self { |
| 47 | self.presentation = self.presentation.on_token_hover(listener); |
| 48 | self |
| 49 | } |
| 50 | } |
| 51 | |
| 52 | impl RenderOnce for Textarea { |
| 53 | fn render(self, _: &mut Window, cx: &mut App) -> impl IntoElement { |
| 54 | self.state.update(cx, |state, _| { |
| 55 | state.set_token_presentation(self.presentation) |
| 56 | }); |
| 57 | self.state |
| 58 | } |
| 59 | } |