ccf-gpui-widgets 0.1.0

Reusable GPUI widgets for building 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
//! Core editing logic for text input widgets
//!
//! This module provides `EditingCore<S>`, a generic editing engine that handles
//! cursor movement, selection, and text manipulation. The storage type `S` must
//! implement `ContentStorage`, allowing different backing stores (e.g., `String`
//! for regular text, or a zeroizing type for sensitive data).

use std::ops::Range;

/// Trait for content storage backends
///
/// This trait abstracts over the string storage, allowing different implementations
/// for regular text (`String`) and sensitive data (`Zeroizing<String>`).
#[allow(dead_code)]
pub trait ContentStorage: Default {
    /// Get the content as a string slice
    fn as_str(&self) -> &str;

    /// Get the length in bytes
    fn len(&self) -> usize {
        self.as_str().len()
    }

    /// Check if empty
    fn is_empty(&self) -> bool {
        self.as_str().is_empty()
    }

    /// Set the content from a string slice
    fn set(&mut self, value: &str);

    /// Insert a string at the given byte position
    fn insert_str(&mut self, pos: usize, text: &str);

    /// Remove a range of bytes
    fn remove_range(&mut self, range: Range<usize>);

    /// Replace a range with new text
    fn replace_range(&mut self, range: Range<usize>, replacement: &str);

    /// Get a substring
    fn get(&self, range: Range<usize>) -> Option<&str> {
        self.as_str().get(range)
    }

    /// Iterate over char indices
    fn char_indices(&self) -> std::str::CharIndices<'_> {
        self.as_str().char_indices()
    }

    /// Count characters
    fn chars_count(&self) -> usize {
        self.as_str().chars().count()
    }
}

impl ContentStorage for String {
    fn as_str(&self) -> &str {
        self
    }

    fn set(&mut self, value: &str) {
        self.clear();
        self.push_str(value);
    }

    fn insert_str(&mut self, pos: usize, text: &str) {
        String::insert_str(self, pos, text);
    }

    fn remove_range(&mut self, range: Range<usize>) {
        self.replace_range(range, "");
    }

    fn replace_range(&mut self, range: Range<usize>, replacement: &str) {
        String::replace_range(self, range, replacement);
    }
}

/// Core editing state and operations
///
/// This struct handles all the cursor, selection, and text manipulation logic
/// independent of the UI framework. It's generic over the storage type to support
/// both regular strings and secure/zeroizing storage for passwords.
pub struct EditingCore<S: ContentStorage> {
    /// The text content
    content: S,
    /// Cursor position (byte index into content)
    cursor: usize,
    /// Selection range (start, end) where start <= end
    selection: Option<(usize, usize)>,
    /// The anchor point for selection extension
    selection_anchor: Option<usize>,
    /// Whether this input masks content (for password fields)
    masked: bool,
}

impl<S: ContentStorage> Default for EditingCore<S> {
    fn default() -> Self {
        Self::new()
    }
}

#[allow(dead_code)]
impl<S: ContentStorage> EditingCore<S> {
    /// Create a new editing core with empty content
    pub fn new() -> Self {
        Self {
            content: S::default(),
            cursor: 0,
            selection: None,
            selection_anchor: None,
            masked: false,
        }
    }

    /// Create with initial content
    #[must_use]
    pub fn with_content(mut self, content: &str) -> Self {
        self.content.set(content);
        self.cursor = self.content.len();
        self
    }

    /// Set masked mode (for password input)
    #[must_use]
    pub fn with_masked(mut self, masked: bool) -> Self {
        self.masked = masked;
        self
    }

    // ========================================================================
    // Getters
    // ========================================================================

    /// Get the content as a string slice
    pub fn content(&self) -> &str {
        self.content.as_str()
    }

    /// Get the cursor position
    pub fn cursor(&self) -> usize {
        self.cursor
    }

    /// Get the selection range
    pub fn selection(&self) -> Option<(usize, usize)> {
        self.selection
    }

    /// Check if masked mode is enabled
    pub fn is_masked(&self) -> bool {
        self.masked
    }

    /// Get selected text
    pub fn selected_text(&self) -> Option<&str> {
        if let Some((start, end)) = self.selection {
            if start != end {
                return self.content.get(start..end);
            }
        }
        None
    }

    // ========================================================================
    // Setters
    // ========================================================================

    /// Set the content, moving cursor to end
    pub fn set_content(&mut self, value: &str) {
        self.content.set(value);
        self.cursor = self.content.len();
        self.selection = None;
        self.selection_anchor = None;
    }

    /// Set cursor position (clamped to valid range)
    pub fn set_cursor(&mut self, pos: usize) {
        self.cursor = pos.min(self.content.len());
    }

    /// Set masked mode
    pub fn set_masked(&mut self, masked: bool) {
        self.masked = masked;
    }

    /// Clear selection state
    pub fn clear_selection(&mut self) {
        self.selection = None;
        self.selection_anchor = None;
    }

    // ========================================================================
    // Text modification
    // ========================================================================

    /// Delete selected text and return whether deletion occurred
    pub fn delete_selection(&mut self) -> bool {
        if let Some((start, end)) = self.selection {
            if start != end {
                self.content.remove_range(start..end);
                self.cursor = start;
                self.selection = None;
                self.selection_anchor = None;
                return true;
            }
        }
        false
    }

    /// Insert text at cursor (replacing selection if any)
    /// Returns true if content changed
    pub fn insert_text(&mut self, text: &str) -> bool {
        self.delete_selection();
        self.content.insert_str(self.cursor, text);
        self.cursor += text.len();
        true
    }

    /// Delete character before cursor
    /// Returns true if content changed
    pub fn delete_backward(&mut self) -> bool {
        if self.delete_selection() {
            return true;
        }
        if self.cursor > 0 {
            let prev = self.prev_char_boundary(self.cursor);
            self.content.remove_range(prev..self.cursor);
            self.cursor = prev;
            return true;
        }
        false
    }

    /// Delete character after cursor
    /// Returns true if content changed
    pub fn delete_forward(&mut self) -> bool {
        if self.delete_selection() {
            return true;
        }
        if self.cursor < self.content.len() {
            let next = self.next_char_boundary(self.cursor);
            self.content.remove_range(self.cursor..next);
            return true;
        }
        false
    }

    /// Delete word before cursor (falls back to single char when masked)
    /// Returns true if content changed
    pub fn delete_word_backward(&mut self) -> bool {
        if self.masked {
            return self.delete_backward();
        }
        if self.delete_selection() {
            return true;
        }
        if self.cursor > 0 {
            let prev = self.prev_word_boundary(self.cursor);
            self.content.remove_range(prev..self.cursor);
            self.cursor = prev;
            return true;
        }
        false
    }

    /// Delete word after cursor (falls back to single char when masked)
    /// Returns true if content changed
    pub fn delete_word_forward(&mut self) -> bool {
        if self.masked {
            return self.delete_forward();
        }
        if self.delete_selection() {
            return true;
        }
        if self.cursor < self.content.len() {
            let next = self.next_word_boundary(self.cursor);
            self.content.remove_range(self.cursor..next);
            return true;
        }
        false
    }

    // ========================================================================
    // Cursor movement
    // ========================================================================

    /// Move cursor left by one character
    pub fn move_left(&mut self) {
        self.clear_selection();
        if self.cursor > 0 {
            self.cursor = self.prev_char_boundary(self.cursor);
        }
    }

    /// Move cursor right by one character
    pub fn move_right(&mut self) {
        self.clear_selection();
        if self.cursor < self.content.len() {
            self.cursor = self.next_char_boundary(self.cursor);
        }
    }

    /// Move cursor to start
    pub fn move_to_start(&mut self) {
        self.clear_selection();
        self.cursor = 0;
    }

    /// Move cursor to end
    pub fn move_to_end(&mut self) {
        self.clear_selection();
        self.cursor = self.content.len();
    }

    /// Move cursor to previous word (falls back to single char when masked)
    pub fn move_word_left(&mut self) {
        if self.masked {
            self.move_left();
            return;
        }
        self.clear_selection();
        self.cursor = self.prev_word_boundary(self.cursor);
    }

    /// Move cursor to next word (falls back to single char when masked)
    pub fn move_word_right(&mut self) {
        if self.masked {
            self.move_right();
            return;
        }
        self.clear_selection();
        self.cursor = self.next_word_boundary(self.cursor);
    }

    // ========================================================================
    // Selection
    // ========================================================================

    /// Ensure selection anchor is set
    fn ensure_selection_anchor(&mut self) {
        if self.selection_anchor.is_none() {
            self.selection_anchor = Some(self.cursor);
        }
    }

    /// Update selection from anchor to cursor
    fn update_selection(&mut self) {
        if let Some(anchor) = self.selection_anchor {
            if anchor == self.cursor {
                self.selection = None;
            } else {
                self.selection = Some((anchor.min(self.cursor), anchor.max(self.cursor)));
            }
        }
    }

    /// Extend selection left by one character
    pub fn select_left(&mut self) {
        if self.cursor > 0 {
            self.ensure_selection_anchor();
            self.cursor = self.prev_char_boundary(self.cursor);
            self.update_selection();
        }
    }

    /// Extend selection right by one character
    pub fn select_right(&mut self) {
        if self.cursor < self.content.len() {
            self.ensure_selection_anchor();
            self.cursor = self.next_char_boundary(self.cursor);
            self.update_selection();
        }
    }

    /// Extend selection left by one word (falls back to single char when masked)
    pub fn select_word_left(&mut self) {
        if self.masked {
            self.select_left();
            return;
        }
        if self.cursor > 0 {
            self.ensure_selection_anchor();
            self.cursor = self.prev_word_boundary(self.cursor);
            self.update_selection();
        }
    }

    /// Extend selection right by one word (falls back to single char when masked)
    pub fn select_word_right(&mut self) {
        if self.masked {
            self.select_right();
            return;
        }
        if self.cursor < self.content.len() {
            self.ensure_selection_anchor();
            self.cursor = self.next_word_boundary(self.cursor);
            self.update_selection();
        }
    }

    /// Select to start of line
    pub fn select_to_start(&mut self) {
        self.ensure_selection_anchor();
        self.cursor = 0;
        self.update_selection();
    }

    /// Select to end of line
    pub fn select_to_end(&mut self) {
        self.ensure_selection_anchor();
        self.cursor = self.content.len();
        self.update_selection();
    }

    /// Select all text
    pub fn select_all(&mut self) {
        self.selection_anchor = Some(0);
        self.cursor = self.content.len();
        self.selection = Some((0, self.content.len()));
    }

    /// Set selection range and update cursor/anchor accordingly
    pub fn set_selection(&mut self, start: usize, end: usize) {
        let start = start.min(self.content.len());
        let end = end.min(self.content.len());
        if start == end {
            self.selection = None;
            self.selection_anchor = None;
            self.cursor = start;
        } else {
            let (min, max) = (start.min(end), start.max(end));
            self.selection = Some((min, max));
            self.selection_anchor = Some(min);
            self.cursor = max;
        }
    }

    /// Start selection at current cursor position (for shift+click)
    pub fn start_selection_from_cursor(&mut self) {
        self.ensure_selection_anchor();
    }

    /// Extend selection to new cursor position
    pub fn extend_selection_to(&mut self, pos: usize) {
        self.cursor = pos.min(self.content.len());
        self.update_selection();
    }

    // ========================================================================
    // Character boundary helpers
    // ========================================================================

    /// Find the previous character boundary
    pub fn prev_char_boundary(&self, pos: usize) -> usize {
        if pos == 0 {
            return 0;
        }
        self.content
            .as_str()[..pos]
            .char_indices()
            .last()
            .map(|(i, _)| i)
            .unwrap_or(0)
    }

    /// Find the next character boundary
    pub fn next_char_boundary(&self, pos: usize) -> usize {
        if pos >= self.content.len() {
            return self.content.len();
        }
        self.content.as_str()[pos..]
            .char_indices()
            .nth(1)
            .map(|(i, _)| pos + i)
            .unwrap_or(self.content.len())
    }

    /// Find the start of the previous word
    /// Uses an iterator-based approach to avoid Vec allocation
    pub fn prev_word_boundary(&self, pos: usize) -> usize {
        if pos == 0 {
            return 0;
        }

        let s = &self.content.as_str()[..pos];
        if s.is_empty() {
            return 0;
        }

        // Iterate backwards by collecting positions, then walking back
        // Find the last word start before pos
        let mut last_word_start = 0;
        let mut in_word = false;
        let mut last_non_alnum_after_word = None;

        for (i, c) in s.char_indices() {
            if c.is_alphanumeric() && !in_word {
                last_word_start = i;
                in_word = true;
            } else if !c.is_alphanumeric() {
                if in_word {
                    last_non_alnum_after_word = Some(i);
                }
                in_word = false;
            }
        }

        // If we're currently in a word (at the end of s), return the start of that word
        if in_word {
            return last_word_start;
        }

        // Otherwise, we're in non-alphanumeric chars, return start of previous word
        // If there was a word before, return its start
        if last_non_alnum_after_word.is_some() {
            return last_word_start;
        }

        // No word found, return 0
        0
    }

    /// Find the end of the next word
    /// Uses an iterator-based approach to avoid Vec allocation
    pub fn next_word_boundary(&self, pos: usize) -> usize {
        if pos >= self.content.len() {
            return self.content.len();
        }

        let s = &self.content.as_str()[pos..];
        if s.is_empty() {
            return self.content.len();
        }

        let mut in_word = false;

        for (i, c) in s.char_indices() {
            if c.is_alphanumeric() {
                in_word = true;
            } else if in_word {
                return pos + i; // Just exited a word
            }
        }

        // If we went through a word (or any text) and reached the end, return end
        self.content.len()
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_basic_insert() {
        let mut core = EditingCore::<String>::new();
        core.insert_text("hello");
        assert_eq!(core.content(), "hello");
        assert_eq!(core.cursor(), 5);
    }

    #[test]
    fn test_cursor_movement() {
        let mut core = EditingCore::<String>::new().with_content("hello");
        assert_eq!(core.cursor(), 5);

        core.move_to_start();
        assert_eq!(core.cursor(), 0);

        core.move_right();
        assert_eq!(core.cursor(), 1);

        core.move_to_end();
        assert_eq!(core.cursor(), 5);

        core.move_left();
        assert_eq!(core.cursor(), 4);
    }

    #[test]
    fn test_selection() {
        let mut core = EditingCore::<String>::new().with_content("hello world");

        core.select_all();
        assert_eq!(core.selection(), Some((0, 11)));
        assert_eq!(core.selected_text(), Some("hello world"));

        core.clear_selection();
        assert_eq!(core.selection(), None);
    }

    #[test]
    fn test_delete_selection() {
        let mut core = EditingCore::<String>::new().with_content("hello world");

        core.set_selection(0, 6); // Select "hello "
        assert!(core.delete_selection());
        assert_eq!(core.content(), "world");
        assert_eq!(core.cursor(), 0);
    }

    #[test]
    fn test_insert_replaces_selection() {
        let mut core = EditingCore::<String>::new().with_content("hello world");

        core.set_selection(0, 5); // Select "hello"
        core.insert_text("hi");
        assert_eq!(core.content(), "hi world");
    }

    #[test]
    fn test_word_boundaries() {
        let core = EditingCore::<String>::new().with_content("hello world test");

        assert_eq!(core.next_word_boundary(0), 5); // "hello" ends at 5
        assert_eq!(core.next_word_boundary(5), 11); // "world" ends at 11
        assert_eq!(core.prev_word_boundary(11), 6); // "world" starts at 6
        assert_eq!(core.prev_word_boundary(6), 0); // "hello" starts at 0
    }

    #[test]
    fn test_masked_mode_disables_word_operations() {
        let mut core = EditingCore::<String>::new()
            .with_content("hello world")
            .with_masked(true);

        core.move_to_start();
        core.move_word_right(); // Should move one char, not one word
        assert_eq!(core.cursor(), 1);

        core.move_to_end();
        core.move_word_left(); // Should move one char, not one word
        assert_eq!(core.cursor(), 10);
    }

    #[test]
    fn test_delete_backward() {
        let mut core = EditingCore::<String>::new().with_content("hello");

        assert!(core.delete_backward());
        assert_eq!(core.content(), "hell");
        assert_eq!(core.cursor(), 4);
    }

    #[test]
    fn test_delete_forward() {
        let mut core = EditingCore::<String>::new().with_content("hello");
        core.move_to_start();

        assert!(core.delete_forward());
        assert_eq!(core.content(), "ello");
        assert_eq!(core.cursor(), 0);
    }

    #[test]
    fn test_unicode_handling() {
        let mut core = EditingCore::<String>::new().with_content("cafe\u{0301}"); // café with combining accent

        // Should handle multi-byte characters correctly
        core.move_left();
        assert!(core.cursor() < 5); // Cursor moved before the combining character
    }
}