example_text_area.rs

  1//! The `ExampleTextArea` view — a multi-line text area component.
  2//!
  3//! Same `ExampleEditor` entity, different presentation: taller box with configurable
  4//! row count. Demonstrates that the same entity type can back different `View`
  5//! components with different props and layouts.
  6
  7use gpui::{
  8    App, BoxShadow, CursorStyle, Entity, Hsla, IntoViewElement, ViewElement, Window, div, hsla,
  9    point, prelude::*, px, white,
 10};
 11
 12use crate::example_editor::ExampleEditor;
 13use crate::{Backspace, Delete, End, Enter, Home, Left, Right};
 14
 15#[derive(Hash, IntoViewElement)]
 16pub struct ExampleTextArea {
 17    editor: Entity<ExampleEditor>,
 18    rows: usize,
 19    color: Option<Hsla>,
 20}
 21
 22impl ExampleTextArea {
 23    pub fn new(editor: Entity<ExampleEditor>, rows: usize) -> Self {
 24        Self {
 25            editor,
 26            rows,
 27            color: None,
 28        }
 29    }
 30
 31    pub fn color(mut self, color: Hsla) -> Self {
 32        self.color = Some(color);
 33        self
 34    }
 35}
 36
 37impl gpui::ComponentView for ExampleTextArea {
 38    fn render(self, window: &mut Window, cx: &mut App) -> impl IntoElement {
 39        let focus_handle = self.editor.read(cx).focus_handle.clone();
 40        let is_focused = focus_handle.is_focused(window);
 41        let text_color = self.color.unwrap_or(hsla(0., 0., 0.1, 1.));
 42        let row_height = px(20.);
 43        let box_height = row_height * self.rows as f32 + px(16.);
 44        let editor = self.editor;
 45
 46        div()
 47            .id("text-area")
 48            .key_context("TextInput")
 49            .track_focus(&focus_handle)
 50            .cursor(CursorStyle::IBeam)
 51            .on_action({
 52                let editor = editor.clone();
 53                move |action: &Backspace, _window, cx| {
 54                    editor.update(cx, |state, cx| state.backspace(action, _window, cx));
 55                }
 56            })
 57            .on_action({
 58                let editor = editor.clone();
 59                move |action: &Delete, _window, cx| {
 60                    editor.update(cx, |state, cx| state.delete(action, _window, cx));
 61                }
 62            })
 63            .on_action({
 64                let editor = editor.clone();
 65                move |action: &Left, _window, cx| {
 66                    editor.update(cx, |state, cx| state.left(action, _window, cx));
 67                }
 68            })
 69            .on_action({
 70                let editor = editor.clone();
 71                move |action: &Right, _window, cx| {
 72                    editor.update(cx, |state, cx| state.right(action, _window, cx));
 73                }
 74            })
 75            .on_action({
 76                let editor = editor.clone();
 77                move |action: &Home, _window, cx| {
 78                    editor.update(cx, |state, cx| state.home(action, _window, cx));
 79                }
 80            })
 81            .on_action({
 82                let editor = editor.clone();
 83                move |action: &End, _window, cx| {
 84                    editor.update(cx, |state, cx| state.end(action, _window, cx));
 85                }
 86            })
 87            .on_action({
 88                let editor = editor.clone();
 89                move |_: &Enter, _window, cx| {
 90                    editor.update(cx, |state, cx| state.insert_newline(cx));
 91                }
 92            })
 93            .w(px(400.))
 94            .h(box_height)
 95            .p(px(8.))
 96            .bg(white())
 97            .border_1()
 98            .border_color(if is_focused {
 99                hsla(220. / 360., 0.8, 0.5, 1.)
100            } else {
101                hsla(0., 0., 0.75, 1.)
102            })
103            .when(is_focused, |this| {
104                this.shadow(vec![BoxShadow {
105                    color: hsla(220. / 360., 0.8, 0.5, 0.3),
106                    offset: point(px(0.), px(0.)),
107                    blur_radius: px(4.),
108                    spread_radius: px(1.),
109                }])
110            })
111            .rounded(px(4.))
112            .overflow_hidden()
113            .line_height(row_height)
114            .text_size(px(14.))
115            .text_color(text_color)
116            .child(ViewElement::new(editor))
117    }
118}