fluix 0.1.9

A comprehensive UI component library for GPUI 0.2 - Modern, performant, and type-safe
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
use gpui::*;
use gpui::prelude::FluentBuilder;

// ============================================================================
// Events
// ============================================================================

/// Events emitted by TextArea
#[derive(Clone, Debug)]
pub enum TextAreaEvent {
    /// The textarea value has changed
    Change(String),
    /// Submit event (when Enter is pressed without Shift)
    Submit(String),
    /// Focus event
    Focus,
    /// Blur event
    Blur,
}

// ============================================================================
// TextArea Component
// ============================================================================

/// A multi-line text area component
pub struct TextArea {
    /// Current text value
    value: String,
    /// Cursor position (character index)
    cursor_pos: usize,
    /// Selection start position (character index, None if no selection)
    selection_start: Option<usize>,
    /// Selection end position (character index, None if no selection)
    selection_end: Option<usize>,
    /// Placeholder text when empty
    placeholder: String,
    /// Focus handle for keyboard input
    focus_handle: FocusHandle,
    /// Whether the textarea is disabled
    disabled: bool,
    /// Maximum length (None for unlimited)
    max_length: Option<usize>,
    /// Minimum height in pixels
    min_height: f32,
    /// Maximum height in pixels (None for unlimited)
    max_height: Option<f32>,
    /// Custom background color
    bg_color: Option<Rgba>,
    /// Custom border color (None for default)
    custom_border_color: Option<Rgba>,
    /// Custom focus border color (None for default)
    focus_border_color: Option<Rgba>,
    /// Whether to show border
    show_border: bool,
}

impl TextArea {
    /// Create a new TextArea
    pub fn new(cx: &mut Context<Self>) -> Self {
        Self {
            value: String::new(),
            cursor_pos: 0,
            selection_start: None,
            selection_end: None,
            placeholder: String::new(),
            focus_handle: cx.focus_handle(),
            disabled: false,
            max_length: None,
            min_height: 100.0,
            max_height: None,
            bg_color: None,
            custom_border_color: None,
            focus_border_color: None,
            show_border: true,
        }
    }

    /// Set the placeholder text
    pub fn placeholder(mut self, placeholder: impl Into<String>) -> Self {
        self.placeholder = placeholder.into();
        self
    }

    /// Set the initial value
    pub fn value(mut self, value: impl Into<String>) -> Self {
        self.value = value.into();
        self
    }

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

    /// Set maximum length
    pub fn max_length(mut self, max_length: usize) -> Self {
        self.max_length = Some(max_length);
        self
    }

    /// Set minimum height
    pub fn min_height(mut self, height: f32) -> Self {
        self.min_height = height;
        self
    }

    /// Set maximum height
    pub fn max_height(mut self, height: f32) -> Self {
        self.max_height = Some(height);
        self
    }

    /// Set custom background color
    pub fn bg_color(mut self, color: Rgba) -> Self {
        self.bg_color = Some(color);
        self
    }

    /// Set custom border color
    pub fn border_color(mut self, color: Rgba) -> Self {
        self.custom_border_color = Some(color);
        self
    }

    /// Set custom focus border color
    pub fn focus_border_color(mut self, color: Rgba) -> Self {
        self.focus_border_color = Some(color);
        self
    }

    /// Remove border
    pub fn no_border(mut self) -> Self {
        self.show_border = false;
        self
    }

    /// Get the current value
    pub fn get_value(&self) -> &str {
        &self.value
    }

    /// Set the value programmatically
    pub fn set_value(&mut self, value: String, cx: &mut Context<Self>) {
        if let Some(max_len) = self.max_length {
            if value.len() > max_len {
                return;
            }
        }

        self.cursor_pos = value.len();
        self.value = value.clone();
        cx.emit(TextAreaEvent::Change(value));
        cx.notify();
    }

    /// Clear the textarea
    pub fn clear(&mut self, cx: &mut Context<Self>) {
        self.value.clear();
        self.cursor_pos = 0;
        cx.emit(TextAreaEvent::Change(String::new()));
        cx.notify();
    }

    /// Focus the textarea
    pub fn focus(&self, window: &mut Window) {
        self.focus_handle.focus(window);
    }
    
    /// Select all text
    pub fn select_all(&mut self, cx: &mut Context<Self>) {
        if !self.value.is_empty() {
            self.selection_start = Some(0);
            self.selection_end = Some(self.value.chars().count());
            cx.notify();
        }
    }
    
    /// Clear selection
    fn clear_selection(&mut self) {
        self.selection_start = None;
        self.selection_end = None;
    }
    
    /// Check if there is an active selection
    fn has_selection(&self) -> bool {
        self.selection_start.is_some() && self.selection_end.is_some()
    }
    
    /// Delete selected text and return the new cursor position
    fn delete_selection(&mut self) -> usize {
        if let (Some(start), Some(end)) = (self.selection_start, self.selection_end) {
            let (sel_start, sel_end) = if start <= end {
                (start, end)
            } else {
                (end, start)
            };
            
            let before = self.value.chars().take(sel_start).collect::<String>();
            let after = self.value.chars().skip(sel_end).collect::<String>();
            self.value = format!("{}{}", before, after);
            self.clear_selection();
            sel_start
        } else {
            self.cursor_pos
        }
    }

    fn handle_input(&mut self, text: &str, cx: &mut Context<Self>) {
        if self.disabled {
            return;
        }
        
        // If there's a selection, delete it first
        if self.has_selection() {
            self.cursor_pos = self.delete_selection();
        }

        // Insert text at cursor position
        let before = self.value.chars().take(self.cursor_pos).collect::<String>();
        let after = self.value.chars().skip(self.cursor_pos).collect::<String>();
        let new_value = format!("{}{}{}", before, text, after);

        // Check max length
        if let Some(max_len) = self.max_length {
            if new_value.chars().count() > max_len {
                return;
            }
        }

        self.value = new_value.clone();
        self.cursor_pos += text.chars().count();
        cx.emit(TextAreaEvent::Change(new_value));
        cx.notify();
    }

    fn handle_backspace(&mut self, cx: &mut Context<Self>) {
        if self.disabled {
            return;
        }
        
        // If there's a selection, delete it
        if self.has_selection() {
            self.cursor_pos = self.delete_selection();
            cx.emit(TextAreaEvent::Change(self.value.clone()));
            cx.notify();
            return;
        }
        
        if self.cursor_pos == 0 {
            return;
        }

        // Remove character before cursor
        let before = self.value.chars().take(self.cursor_pos - 1).collect::<String>();
        let after = self.value.chars().skip(self.cursor_pos).collect::<String>();
        self.value = format!("{}{}", before, after);
        self.cursor_pos -= 1;
        
        cx.emit(TextAreaEvent::Change(self.value.clone()));
        cx.notify();
    }

    fn handle_enter(&mut self, shift_pressed: bool, cx: &mut Context<Self>) {
        if self.disabled {
            return;
        }

        if shift_pressed {
            // Shift+Enter: insert newline
            self.handle_input("\n", cx);
        } else {
            // Enter: submit
            cx.emit(TextAreaEvent::Submit(self.value.clone()));
        }
    }

    fn count_lines(&self) -> usize {
        if self.value.is_empty() {
            1
        } else {
            self.value.lines().count().max(1)
        }
    }

    fn calculate_height(&self) -> f32 {
        let line_count = self.count_lines();
        let line_height = 20.0; // Approximate line height
        let padding = 12.0; // Vertical padding
        let calculated = (line_count as f32 * line_height) + padding;

        // Apply min and max constraints
        let height = calculated.max(self.min_height);
        if let Some(max_h) = self.max_height {
            height.min(max_h)
        } else {
            height
        }
    }
}

impl EventEmitter<TextAreaEvent> for TextArea {}

impl Focusable for TextArea {
    fn focus_handle(&self, _cx: &App) -> FocusHandle {
        self.focus_handle.clone()
    }
}

impl Render for TextArea {
    fn render(&mut self, window: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
        let is_focused = self.focus_handle.is_focused(window);
        let show_placeholder = self.value.is_empty() && !is_focused;
        let disabled = self.disabled;
        let placeholder = self.placeholder.clone();
        let value = self.value.clone();
        let cursor_pos = self.cursor_pos;
        let height = self.calculate_height();
        let selection_start = self.selection_start;
        let selection_end = self.selection_end;
        
        // Determine colors based on customization or defaults
        let bg_color = if disabled {
            self.bg_color.unwrap_or(rgb(0xF5F5F5))
        } else {
            self.bg_color.unwrap_or(rgb(0xFFFFFF))
        };
        
        let border_color = if is_focused {
            self.focus_border_color
                .or(self.custom_border_color)
                .unwrap_or(rgb(0x696FC7))
        } else {
            self.custom_border_color.unwrap_or(rgb(0xE0E0E0))
        };

        div()
            .id("text-area")
            .track_focus(&self.focus_handle)
            .on_key_down(cx.listener(|this, event: &KeyDownEvent, _, cx| {
                if this.disabled {
                    return;
                }

                // Handle keyboard shortcuts
                let modifiers = &event.keystroke.modifiers;
                // On macOS use platform (Cmd), on others use control (Ctrl)
                let is_cmd_or_ctrl = if cfg!(target_os = "macos") {
                    modifiers.platform
                } else {
                    modifiers.control
                };
                
                // Check for Cmd/Ctrl + A (Select All)
                if is_cmd_or_ctrl && event.keystroke.key.as_str() == "a" {
                    this.select_all(cx);
                    return;
                }

                // Handle special keys
                match event.keystroke.key.as_str() {
                    "backspace" => {
                        this.handle_backspace(cx);
                    }
                    "enter" => {
                        let shift_pressed = event.keystroke.modifiers.shift;
                        this.handle_enter(shift_pressed, cx);
                    }
                    _ => {
                        // Handle regular character input
                        if let Some(ch) = &event.keystroke.key_char {
                            this.handle_input(ch, cx);
                        }
                    }
                }
            }))
            .on_mouse_down(MouseButton::Left, cx.listener(|this, event: &MouseDownEvent, window, cx| {
                cx.emit(TextAreaEvent::Focus);
                cx.focus_self(window);
                
                // Check for double-click to select all
                if event.click_count == 2 && !this.disabled {
                    this.select_all(cx);
                } else if !this.disabled {
                    // Single click: clear selection and position cursor at click
                    this.clear_selection();
                    // For now, just move cursor to end on click
                    // (Precise click positioning would require pixel-to-char calculation)
                    this.cursor_pos = this.value.chars().count();
                }
                
                cx.notify();
            }))
            .flex()
            .flex_col()
            .w_full()
            .min_h(px(height))
            .p_3()
            .bg(bg_color)
            .when(self.show_border, |this| {
                this.border_1().border_color(border_color)
            })
            .rounded(px(6.))
            .when(!disabled, |this| {
                this.cursor(CursorStyle::IBeam)
            })
            .children([
                // Render text with cursor
                div()
                    .flex()
                    .flex_row()
                    .items_start()
                    .w_full()
                    .text_sm()
                    .when(show_placeholder, |this| {
                        this.text_color(rgb(0x999999))
                            .child(placeholder)
                    })
                    .when(!show_placeholder && is_focused && value.is_empty(), |this| {
                        // Empty and focused: show just cursor
                        this.child(
                            div()
                                .w(px(2.))
                                .h(px(20.))
                                .bg(rgb(0x333333))
                        )
                    })
                    .when(!show_placeholder && !value.is_empty(), |this| {
                        // Calculate selection range if exists
                        let has_selection = selection_start.is_some() && selection_end.is_some();
                        let (sel_start, sel_end) = if let (Some(start), Some(end)) = (selection_start, selection_end) {
                            if start <= end {
                                (start, end)
                            } else {
                                (end, start)
                            }
                        } else {
                            (0, 0)
                        };
                        
                        // Split text into lines and find cursor position
                        let lines: Vec<&str> = value.split('\n').collect();
                        let mut chars_counted = 0;
                        let mut cursor_line_idx = 0;
                        let mut cursor_col = cursor_pos;
                        
                        // Find which line the cursor is on
                        for (idx, line) in lines.iter().enumerate() {
                            let line_len = line.chars().count();
                            // Check if cursor is within this line (including at the end)
                            if cursor_pos <= chars_counted + line_len {
                                cursor_line_idx = idx;
                                cursor_col = cursor_pos - chars_counted;
                                break;
                            }
                            // +1 for the newline character
                            chars_counted += line_len + 1;
                            
                            // If this is the last line and we haven't found the cursor yet
                            if idx == lines.len() - 1 {
                                cursor_line_idx = idx;
                                cursor_col = line_len;
                            }
                        }
                        
                        this.text_color(if disabled {
                            rgb(0x999999)
                        } else {
                            rgb(0x333333)
                        })
                        .child(
                            div()
                                .flex()
                                .flex_col()
                                .items_start()
                                .w_full()
                                .children(
                                    lines.iter().enumerate().map(|(line_idx, line)| {
                                        let line_str = line.to_string();
                                        let mut line_chars_before = 0;
                                        
                                        // Calculate character count before this line
                                        for (idx, l) in lines.iter().enumerate() {
                                            if idx < line_idx {
                                                line_chars_before += l.chars().count() + 1; // +1 for newline
                                            }
                                        }
                                        
                                        let line_start = line_chars_before;
                                        let line_end = line_start + line_str.chars().count();
                                        
                                        // Check if this line has selection
                                        let line_has_selection = has_selection && 
                                            !(sel_end <= line_start || sel_start >= line_end);
                                        
                                        if line_has_selection {
                                            // This line has selection
                                            let sel_start_in_line = if sel_start > line_start {
                                                sel_start - line_start
                                            } else {
                                                0
                                            };
                                            let sel_end_in_line = if sel_end < line_end {
                                                sel_end - line_start
                                            } else {
                                                line_str.chars().count()
                                            };
                                            
                                            let before_sel = line_str.chars().take(sel_start_in_line).collect::<String>();
                                            let selected = line_str.chars().skip(sel_start_in_line).take(sel_end_in_line - sel_start_in_line).collect::<String>();
                                            let after_sel = line_str.chars().skip(sel_end_in_line).collect::<String>();
                                            
                                            div()
                                                .flex()
                                                .flex_row()
                                                .items_start()
                                                .when(!before_sel.is_empty(), |el| el.child(before_sel))
                                                .when(!selected.is_empty(), |el| {
                                                    el.child(
                                                        div()
                                                            .bg(rgb(0x4A90E2))
                                                            .text_color(rgb(0xFFFFFF))
                                                            .child(selected)
                                                    )
                                                })
                                                .when(!after_sel.is_empty(), |el| el.child(after_sel))
                                                .when(line_idx == cursor_line_idx && is_focused && !disabled, |el| {
                                                    el.child(
                                                        div()
                                                            .w(px(2.))
                                                            .h(px(20.))
                                                            .bg(rgb(0x333333))
                                                            .flex_shrink_0()
                                                    )
                                                })
                                        } else if line_idx == cursor_line_idx {
                                            // This line contains the cursor but no selection
                                            let before = line_str.chars().take(cursor_col).collect::<String>();
                                            let after = line_str.chars().skip(cursor_col).collect::<String>();
                                            
                                            div()
                                                .flex()
                                                .flex_row()
                                                .items_start()
                                                .child(before)
                                                .when(is_focused && !disabled, |el| {
                                                    el.child(
                                                        div()
                                                            .w(px(2.))
                                                            .h(px(20.))
                                                            .bg(rgb(0x333333))
                                                            .flex_shrink_0()
                                                    )
                                                })
                                                .child(after)
                                        } else {
                                            // Regular line without cursor or selection
                                            div()
                                                .flex()
                                                .flex_row()
                                                .child(line_str)
                                        }
                                    })
                                )
                        )
                    })
            ])
    }
}