fc-ui 0.9.7

A comprehensive, professional UI component library for GPUI inspired by shadcn/ui. 85+ polished components for building beautiful desktop applications.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
//! Textarea component - Multi-line text input component with full editing capabilities.

use crate::capabilities::controls::input::InputVariant;
pub use crate::capabilities::controls::textarea_state::wire_actions;
use crate::capabilities::controls::textarea_state::TextareaEvent;
use crate::capabilities::controls::textarea_state::TextareaState;
use crate::capabilities::controls::textarea_state::{ValidationError, ValidationRules};
use crate::capabilities::controls::validation::{validation_footer, CharacterCount, FooterMessage};
use crate::capabilities::foundation::theme::use_theme;
use crate::capabilities::layout::VStack;
use gpui::{prelude::FluentBuilder as _, *};
use std::rc::Rc;
use std::sync::Arc;

type TextareaValidator = Rc<dyn Fn(&str) -> Result<(), String>>;
type TextareaValidationCallback = Rc<dyn Fn(Result<(), ValidationError>, &mut App)>;

/// Initialize textarea component with key bindings
pub fn init(cx: &mut App) {
    if !crate::capabilities::foundation::initialization::begin(cx, "textarea") {
        return;
    }
    crate::capabilities::controls::textarea_state::init(cx);
}

#[derive(IntoElement)]
pub struct Textarea {
    state: Entity<TextareaState>,
    placeholder: SharedString,
    variant: InputVariant,
    disabled: bool,
    error: bool,
    rows: usize,
    min_rows: Option<usize>,
    max_rows: Option<usize>,
    auto_grow: bool,
    initial_value: Option<SharedString>,

    aria_label: Option<SharedString>,
    aria_description: Option<SharedString>,

    on_change: Option<Rc<dyn Fn(SharedString, &mut App)>>,
    on_enter: Option<Rc<dyn Fn(SharedString, &mut App)>>,
    on_shift_enter: Option<Rc<dyn Fn(SharedString, &mut App)>>,
    on_focus: Option<Rc<dyn Fn(SharedString, &mut App)>>,
    on_blur: Option<Rc<dyn Fn(SharedString, &mut App)>>,

    validation_rules: Option<ValidationRules>,
    custom_validator: Option<TextareaValidator>,
    helper_text: Option<SharedString>,
    show_character_count: bool,
    on_validate: Option<TextareaValidationCallback>,

    style: StyleRefinement,
}

impl Textarea {
    pub fn new(state: &Entity<TextareaState>) -> Self {
        Self {
            state: state.clone(),
            placeholder: "".into(),
            variant: InputVariant::Default,
            disabled: false,
            error: false,
            rows: 3,
            min_rows: None,
            max_rows: None,
            auto_grow: false,
            initial_value: None,
            aria_label: None,
            aria_description: None,
            on_change: None,
            on_enter: None,
            on_shift_enter: None,
            on_focus: None,
            on_blur: None,
            validation_rules: None,
            custom_validator: None,
            helper_text: None,
            show_character_count: false,
            on_validate: None,
            style: StyleRefinement::default(),
        }
    }

    pub fn value(mut self, value: impl Into<SharedString>) -> Self {
        self.initial_value = Some(value.into());
        self
    }

    pub fn placeholder(mut self, placeholder: impl Into<SharedString>) -> Self {
        self.placeholder = placeholder.into();
        self
    }

    pub fn variant(mut self, variant: InputVariant) -> Self {
        self.variant = variant;
        self
    }

    pub fn disabled(mut self, disabled: bool) -> Self {
        self.disabled = disabled;
        self
    }

    pub fn error(mut self, error: bool) -> Self {
        self.error = error;
        self
    }

    pub fn rows(mut self, rows: usize) -> Self {
        self.rows = rows.max(1);
        self
    }

    pub fn min_rows(mut self, min_rows: usize) -> Self {
        self.min_rows = Some(min_rows.max(1));
        self
    }

    pub fn max_rows(mut self, max_rows: usize) -> Self {
        self.max_rows = Some(max_rows.max(1));
        self
    }

    pub fn auto_grow(mut self, auto_grow: bool) -> Self {
        self.auto_grow = auto_grow;
        self
    }

    pub fn on_change<F>(mut self, callback: F) -> Self
    where
        F: Fn(SharedString, &mut App) + 'static,
    {
        self.on_change = Some(Rc::new(callback));
        self
    }

    pub fn on_enter<F>(mut self, callback: F) -> Self
    where
        F: Fn(SharedString, &mut App) + 'static,
    {
        self.on_enter = Some(Rc::new(callback));
        self
    }

    pub fn on_shift_enter<F>(mut self, callback: F) -> Self
    where
        F: Fn(SharedString, &mut App) + 'static,
    {
        self.on_shift_enter = Some(Rc::new(callback));
        self
    }

    pub fn on_focus<F>(mut self, callback: F) -> Self
    where
        F: Fn(SharedString, &mut App) + 'static,
    {
        self.on_focus = Some(Rc::new(callback));
        self
    }

    pub fn on_blur<F>(mut self, callback: F) -> Self
    where
        F: Fn(SharedString, &mut App) + 'static,
    {
        self.on_blur = Some(Rc::new(callback));
        self
    }

    /// Set the accessible label for the textarea.
    pub fn aria_label(mut self, label: impl Into<SharedString>) -> Self {
        self.aria_label = Some(label.into());
        self
    }

    /// Set the accessible description for the textarea.
    pub fn aria_description(mut self, description: impl Into<SharedString>) -> Self {
        self.aria_description = Some(description.into());
        self
    }

    /// Set validation rules
    pub fn validation_rules(mut self, rules: ValidationRules) -> Self {
        self.validation_rules = Some(rules);
        self
    }

    /// Set minimum length requirement
    pub fn min_length(mut self, min: usize) -> Self {
        self.rules_mut().min_length = Some(min);
        self
    }

    /// Set maximum length requirement (also truncates user input)
    pub fn max_length(mut self, max: usize) -> Self {
        self.rules_mut().max_length = Some(max);
        self
    }

    /// Mark field as required
    pub fn required(mut self, required: bool) -> Self {
        self.rules_mut().required = required;
        self
    }

    /// Set helper text displayed below the textarea
    pub fn helper_text(mut self, text: impl Into<SharedString>) -> Self {
        self.helper_text = Some(text.into());
        self
    }

    /// Show character count indicator
    pub fn show_character_count(mut self, show: bool) -> Self {
        self.show_character_count = show;
        self
    }

    /// Set a custom validation function
    pub fn custom_validator<F>(mut self, validator: F) -> Self
    where
        F: Fn(&str) -> Result<(), String> + 'static,
    {
        self.custom_validator = Some(Rc::new(validator));
        self
    }

    /// Set callback for validation events
    pub fn on_validate<F>(mut self, callback: F) -> Self
    where
        F: Fn(Result<(), ValidationError>, &mut App) + 'static,
    {
        self.on_validate = Some(Rc::new(callback));
        self
    }

    fn rules_mut(&mut self) -> &mut ValidationRules {
        self.validation_rules
            .get_or_insert_with(ValidationRules::default)
    }

    #[allow(clippy::arc_with_non_send_sync)]
    fn validation_rules_for_state(&self) -> Option<ValidationRules> {
        let mut rules = self.validation_rules.clone().or_else(|| {
            self.custom_validator
                .is_some()
                .then(ValidationRules::default)
        })?;

        if let Some(custom_validator) = self.custom_validator.as_ref() {
            let validator = custom_validator.clone();
            rules.custom_validator = Some(Arc::new(move |value| match validator(value) {
                Ok(()) => Ok(()),
                Err(message) => Err(ValidationError {
                    message: message.into(),
                    field_name: None,
                }),
            }));
        }

        Some(rules)
    }

    fn install_event_subscription(&self, cx: &mut App) {
        let on_change = self.on_change.clone();
        let on_enter = self.on_enter.clone();
        let on_shift_enter = self.on_shift_enter.clone();
        let on_focus = self.on_focus.clone();
        let on_blur = self.on_blur.clone();
        let on_validate = self.on_validate.clone();

        let has_callbacks = on_change.is_some()
            || on_enter.is_some()
            || on_shift_enter.is_some()
            || on_focus.is_some()
            || on_blur.is_some()
            || on_validate.is_some();
        let subscription = has_callbacks.then(|| {
            cx.subscribe(
                &self.state,
                move |emitter, event: &TextareaEvent, cx| match event {
                    TextareaEvent::Change => {
                        if let Some(callback) = on_change.as_ref() {
                            callback(emitter.read(cx).content.clone(), cx);
                        }
                    }
                    TextareaEvent::Enter => {
                        if let Some(callback) = on_enter.as_ref() {
                            callback(emitter.read(cx).content.clone(), cx);
                        }
                    }
                    TextareaEvent::ShiftEnter => {
                        if let Some(callback) = on_shift_enter.as_ref() {
                            callback(emitter.read(cx).content.clone(), cx);
                        }
                    }
                    TextareaEvent::Focus => {
                        if let Some(callback) = on_focus.as_ref() {
                            callback(emitter.read(cx).content.clone(), cx);
                        }
                    }
                    TextareaEvent::Blur => {
                        if let Some(callback) = on_blur.as_ref() {
                            callback(emitter.read(cx).content.clone(), cx);
                        }
                    }
                    TextareaEvent::Validate(result) => {
                        if let Some(callback) = on_validate.as_ref() {
                            callback(result.clone(), cx);
                        }
                    }
                    TextareaEvent::Tab | TextareaEvent::ShiftTab => {}
                },
            )
        });

        self.state.update(cx, |state, _| {
            state.replace_event_subscription(subscription);
        });
    }

    fn calculate_height(&self, window: &Window, cx: &App) -> Pixels {
        let line_height = window.line_height().to_f64() as f32;
        let padding_y = 8.0;
        let base_height = self.rows as f32 * line_height + padding_y * 2.0;

        if self.auto_grow {
            let visual_lines = self.state.read(cx).visual_line_count_for_layout().max(1);
            let content_height = visual_lines as f32 * line_height + padding_y * 2.0;

            let final_height = if let Some(min_rows) = self.min_rows {
                let min_height = min_rows as f32 * line_height + padding_y * 2.0;
                content_height.max(min_height)
            } else {
                content_height.max(base_height)
            };

            if let Some(max_rows) = self.max_rows {
                let max_height = max_rows as f32 * line_height + padding_y * 2.0;
                px(final_height.min(max_height))
            } else {
                px(final_height)
            }
        } else {
            px(base_height)
        }
    }
}

impl Styled for Textarea {
    fn style(&mut self) -> &mut StyleRefinement {
        &mut self.style
    }
}

impl RenderOnce for Textarea {
    fn render(self, window: &mut Window, cx: &mut App) -> impl IntoElement {
        let theme = use_theme(cx);
        let height = self.calculate_height(window, cx);

        // Update state with properties
        self.state.update(cx, |state, cx| {
            state.disabled = self.disabled;
            state.placeholder = self.placeholder.clone();

            // Apply rules before the initial value so `set_value` truncates
            // against `max_length` on first render too.
            if let Some(rules) = self.validation_rules_for_state() {
                state.validation_rules = rules;
            }

            if !state.initialized {
                state.initialized = true;
                if let Some(value) = self.initial_value.clone() {
                    state.set_value(value, window, cx);
                }
            }
        });

        self.install_event_subscription(cx);

        let (validation_error, success_message, content_length, max_length, has_value, composing) = {
            let state = self.state.read(cx);
            let composing = state.composition_char_count();
            (
                state.validation_error.clone(),
                state.success_message.clone(),
                // Exclude the in-flight IME composition from the committed
                // count; the footer shows it separately.
                state.content.chars().count() - composing,
                state.validation_rules.max_length,
                !state.content.is_empty(),
                composing,
            )
        };

        // A failed validation (e.g. on blur) shows the destructive border just
        // like an explicit `.error(true)`.
        let has_error = self.error || validation_error.is_some();

        let (bg_color, border_color, text_color) =
            crate::capabilities::controls::input_shell_colors(
                self.variant,
                self.disabled,
                has_error,
                &theme.tokens,
            );

        let is_focused = self.state.read(cx).focus_handle(cx).is_focused(window);
        let user_style = self.style;

        let has_footer = self.helper_text.is_some()
            || validation_error.is_some()
            || success_message.is_some()
            || self.show_character_count;
        let footer_message = if let Some(error) = validation_error.as_ref() {
            FooterMessage::Error(error)
        } else if let Some(success) = success_message.as_ref() {
            if has_value {
                FooterMessage::Success(success)
            } else {
                FooterMessage::Blank
            }
        } else if let Some(helper) = self.helper_text.as_ref() {
            FooterMessage::Helper(helper)
        } else {
            FooterMessage::Blank
        };
        let character_count = self.show_character_count.then_some(CharacterCount {
            committed: content_length,
            composing,
            max: max_length,
        });

        let field = wire_actions(&self.state, window, cx)
            .debug_selector(|| "textarea-field".into())
            .role(Role::MultilineTextInput)
            .when_some(self.aria_label.clone(), |this, label| {
                this.aria_label(label)
            })
            .when_some(self.aria_description.clone(), |this, description| {
                this.aria_description(description)
            })
            .aria_value(self.state.read(cx).content.clone())
            .aria_placeholder(self.placeholder.clone())
            .aria_disabled(self.disabled)
            .aria_required(self.state.read(cx).validation_rules.required)
            .when(has_error, |this| {
                this.aria_invalid(gpui::accesskit::Invalid::True)
            })
            .w_full()
            .h(height)
            .flex_shrink_0()
            .when(self.auto_grow, |this| this.min_h(height))
            .px(px(12.0))
            .py(px(8.0))
            .bg(bg_color)
            .border_1()
            .border_color(border_color)
            .rounded(theme.tokens.radius_md)
            .text_size(px(14.0))
            .font_family(theme.tokens.font_mono.clone())
            .text_color(text_color)
            .when(!self.disabled, |this| {
                this.hover(|style| {
                    style.border_color(if has_error {
                        theme.tokens.destructive
                    } else {
                        theme.tokens.ring
                    })
                })
            })
            .when(is_focused && !self.disabled, |this| {
                this.border_color(if has_error {
                    theme.tokens.destructive
                } else {
                    theme.tokens.ring
                })
            })
            .when(!self.disabled, |this| this.cursor(gpui::CursorStyle::IBeam))
            .child(self.state.clone());

        if has_footer {
            VStack::new()
                .debug_selector(|| "textarea-root".into())
                .w_full()
                .gap(px(4.0))
                .child(field)
                .child(validation_footer(
                    &theme.tokens,
                    footer_message,
                    character_count,
                ))
                .map(|this| {
                    let mut vstack = this;
                    vstack.style().refine(&user_style);
                    vstack
                })
                .into_any_element()
        } else {
            field
                .map(|this| {
                    let mut div = this;
                    div.style().refine(&user_style);
                    div
                })
                .into_any_element()
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::capabilities::foundation::theme::{install_theme, Theme};
    use std::{cell::Cell, rc::Rc};

    struct TextareaRenderTestView {
        state: Entity<TextareaState>,
        callback_calls: Option<Rc<Cell<usize>>>,
    }

    impl Render for TextareaRenderTestView {
        fn render(&mut self, _window: &mut Window, _cx: &mut Context<Self>) -> impl IntoElement {
            Textarea::new(&self.state).when_some(self.callback_calls.clone(), |textarea, calls| {
                textarea.on_change(move |_, _| calls.set(calls.get() + 1))
            })
        }
    }

    #[gpui::test]
    fn repeated_render_replaces_textarea_event_subscription() {
        let mut app = TestApp::new();
        app.update(|cx| install_theme(cx, Theme::light()));
        let stale_calls = Rc::new(Cell::new(0));
        let current_calls = Rc::new(Cell::new(0));
        let mut window = app.open_window(|_, cx| TextareaRenderTestView {
            state: cx.new(TextareaState::new),
            callback_calls: Some(stale_calls.clone()),
        });

        window.draw();
        window.update(|view, _, cx| {
            view.callback_calls = Some(current_calls.clone());
            cx.notify();
        });
        window.draw();
        window.update(|view, _, cx| {
            view.state
                .update(cx, |_, cx| cx.emit(TextareaEvent::Change));
        });
        assert_eq!(stale_calls.get(), 0);
        assert_eq!(current_calls.get(), 1);

        window.update(|view, _, cx| {
            view.callback_calls = None;
            cx.notify();
        });
        window.draw();
        window.update(|view, _, cx| {
            view.state
                .update(cx, |_, cx| cx.emit(TextareaEvent::Change));
        });
        assert_eq!(current_calls.get(), 1);
    }

    struct BareTextareaTestView {
        state: Entity<TextareaState>,
    }

    impl Render for BareTextareaTestView {
        fn render(&mut self, window: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
            wire_actions(&self.state, window, cx)
                .h(px(96.0))
                .px(px(12.0))
                .rounded(px(8.0))
                .child(self.state.clone())
        }
    }

    #[gpui::test]
    fn wire_actions_gives_custom_textarea_containers_full_keyboard_editing() {
        let mut app = TestApp::new();
        app.update(|cx| {
            install_theme(cx, Theme::light());
            crate::capabilities::controls::textarea_state::init(cx);
        });
        let state = app.update(|cx| cx.new(|cx| TextareaState::new(cx).value("hello")));
        let mut window = app.open_window(|_, _| BareTextareaTestView {
            state: state.clone(),
        });

        window.draw();
        window.update(|_, window, cx| state.read(cx).focus_handle(cx).focus(window));
        window.draw();
        let handle: AnyWindowHandle = window.handle().into();
        app.raw_context_mut()
            .simulate_keystrokes(handle, "backspace");
        app.flush();

        app.update(|cx| {
            assert_eq!(state.read(cx).content(), "hell");
        });
    }
    struct FooterTextareaTestView {
        state: Entity<TextareaState>,
        validate_errors: Rc<Cell<usize>>,
    }

    impl Render for FooterTextareaTestView {
        fn render(&mut self, _window: &mut Window, _cx: &mut Context<Self>) -> impl IntoElement {
            let validate_errors = self.validate_errors.clone();
            Textarea::new(&self.state)
                .required(true)
                .max_length(3)
                .helper_text("hint")
                .show_character_count(true)
                .on_validate(move |result, _| {
                    if result.is_err() {
                        validate_errors.set(validate_errors.get() + 1);
                    }
                })
        }
    }

    #[gpui::test]
    fn footer_renders_and_blur_validation_reaches_on_validate() {
        let mut app = TestApp::new();
        app.update(|cx| {
            install_theme(cx, Theme::light());
            crate::capabilities::controls::textarea_state::init(cx);
        });
        let validate_errors = Rc::new(Cell::new(0));
        let state = app.update(|cx| cx.new(TextareaState::new));
        let mut window = app.open_window(|_, _| FooterTextareaTestView {
            state: state.clone(),
            validate_errors: validate_errors.clone(),
        });
        window.draw();
        window.draw();
        window.update(|_, window, cx| state.read(cx).focus_handle(cx).focus(window));
        window.draw();
        // Moving focus elsewhere drives the blur -> validate_on_blur path.
        window.update(|_, window, cx| cx.focus_handle().focus(window));
        window.draw();

        assert_eq!(validate_errors.get(), 1);
        app.update(|cx| assert!(state.read(cx).validation_error.is_some()));
    }

    #[gpui::test]
    fn max_length_truncates_typed_input() {
        let mut app = TestApp::new();
        app.update(|cx| {
            install_theme(cx, Theme::light());
            crate::capabilities::controls::textarea_state::init(cx);
        });
        let state = app.update(|cx| cx.new(|cx| TextareaState::new(cx).max_length(3)));
        let mut window = app.open_window(|_, _| BareTextareaTestView {
            state: state.clone(),
        });
        window.draw();
        window.update(|_, window, cx| state.read(cx).focus_handle(cx).focus(window));
        window.draw();
        let handle: AnyWindowHandle = window.handle().into();
        app.raw_context_mut().simulate_keystrokes(handle, "abcde");
        app.flush();

        app.update(|cx| assert_eq!(state.read(cx).content(), "abc"));
    }

    #[gpui::test]
    fn set_value_truncates_to_max_length() {
        let mut app = TestApp::new();
        app.update(|cx| {
            install_theme(cx, Theme::light());
            crate::capabilities::controls::textarea_state::init(cx);
        });
        let state = app.update(|cx| cx.new(|cx| TextareaState::new(cx).max_length(3)));
        let mut window = app.open_window(|_, _| BareTextareaTestView {
            state: state.clone(),
        });
        window.draw();
        window.update(|_, window, cx| {
            state.update(cx, |state, cx| state.set_value("abcdef", window, cx));
        });

        app.update(|cx| assert_eq!(state.read(cx).content(), "abc"));
    }

    #[gpui::test]
    fn rejected_max_length_input_does_not_emit_change() {
        let mut app = TestApp::new();
        app.update(|cx| {
            install_theme(cx, Theme::light());
            crate::capabilities::controls::textarea_state::init(cx);
        });
        let state = app.update(|cx| cx.new(|cx| TextareaState::new(cx).max_length(3).value("abc")));
        let changes = Rc::new(Cell::new(0));
        let changes_for_subscription = changes.clone();
        app.update(|cx| {
            cx.subscribe(&state, move |_, event: &TextareaEvent, _| {
                if matches!(event, TextareaEvent::Change) {
                    changes_for_subscription.set(changes_for_subscription.get() + 1);
                }
            })
            .detach();
        });

        let mut window = app.open_window(|_, _| BareTextareaTestView {
            state: state.clone(),
        });
        window.draw();
        window.update(|_, window, cx| {
            state.update(cx, |state, cx| {
                state.replace_text_in_range(None, "d", window, cx)
            });
        });

        app.update(|cx| {
            assert_eq!(state.read(cx).content(), "abc");
            assert_eq!(changes.get(), 0);
        });
    }

    #[gpui::test]
    fn validate_on_change_runs_after_set_value() {
        let mut app = TestApp::new();
        app.update(|cx| {
            install_theme(cx, Theme::light());
            crate::capabilities::controls::textarea_state::init(cx);
        });
        let state = app.update(|cx| {
            cx.new(|cx| {
                let mut state = TextareaState::new(cx).required(true).value("x");
                state.validate_on_change = true;
                state
            })
        });
        let mut window = app.open_window(|_, _| BareTextareaTestView {
            state: state.clone(),
        });
        window.draw();
        window.update(|_, window, cx| {
            state.update(cx, |state, cx| state.set_value("", window, cx));
        });

        app.update(|cx| {
            assert_eq!(
                state
                    .read(cx)
                    .validation_error
                    .as_ref()
                    .unwrap()
                    .message
                    .as_ref(),
                "This field is required"
            );
        });
    }

    #[gpui::test]
    fn auto_validation_stays_silent_without_rules() {
        let mut app = TestApp::new();
        app.update(|cx| {
            install_theme(cx, Theme::light());
            crate::capabilities::controls::textarea_state::init(cx);
        });
        // No rules configured: blur and change must not produce validation
        // messages, so existing plain textareas render exactly as before.
        let state = app.update(|cx| cx.new(|cx| TextareaState::new(cx).value("hello")));
        let mut window = app.open_window(|_, _| BareTextareaTestView {
            state: state.clone(),
        });
        window.draw();
        window.update(|_, window, cx| state.read(cx).focus_handle(cx).focus(window));
        window.draw();
        window.update(|_, window, cx| cx.focus_handle().focus(window));
        window.draw();
        window.update(|_, window, cx| {
            state.update(cx, |state, cx| state.set_value("changed", window, cx));
        });

        app.update(|cx| {
            let state = state.read(cx);
            assert!(state.validation_error.is_none());
            assert!(state.success_message.is_none());
        });
    }
}