gpuikit 0.7.0

A UI toolkit for GPUI 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
use gpui::{actions, App, KeyBinding};

actions!(
    input,
    [
        /// Delete the character before the cursor.
        Backspace,
        /// Delete the character after the cursor.
        Delete,
        /// Blur focus from the input.
        Escape,
        /// Delete the word before the cursor.
        DeleteWordLeft,
        /// Delete the word after the cursor.
        DeleteWordRight,
        /// Delete from the cursor to the beginning of the line.
        DeleteToBeginningOfLine,
        /// Delete from the cursor to the end of the line.
        DeleteToEndOfLine,
        /// Insert a tab character at the cursor position.
        Tab,
        /// Move the cursor one character to the left.
        Left,
        /// Move the cursor one character to the right.
        Right,
        /// Move the cursor up one visual line.
        Up,
        /// Move the cursor down one visual line.
        Down,
        /// Extend selection one character to the left.
        SelectLeft,
        /// Extend selection one character to the right.
        SelectRight,
        /// Extend selection up one visual line.
        SelectUp,
        /// Extend selection down one visual line.
        SelectDown,
        /// Select all text content.
        SelectAll,
        /// Move cursor to the start of the current line.
        Home,
        /// Move cursor to the end of the current line.
        End,
        /// Extend selection to the beginning of the content.
        SelectToBeginning,
        /// Extend selection to the end of the content.
        SelectToEnd,
        /// Move cursor to the beginning of the content.
        MoveToBeginning,
        /// Move cursor to the end of the content.
        MoveToEnd,
        /// Paste from clipboard at the cursor position.
        Paste,
        /// Cut selected text to clipboard.
        Cut,
        /// Copy selected text to clipboard.
        Copy,
        /// Insert a newline at the cursor position, or submit when the input's
        /// submit mode is `SubmitOn::Enter`.
        Enter,
        /// Submit the input's content (fires `InputStateEvent::Submit` on
        /// inputs configured with a submit mode).
        Submit,
        /// Insert a newline at the cursor position regardless of submit mode.
        InsertNewline,
        /// Move cursor one word to the left.
        WordLeft,
        /// Move cursor one word to the right.
        WordRight,
        /// Extend selection one word to the left.
        SelectWordLeft,
        /// Extend selection one word to the right.
        SelectWordRight,
        /// Undo the last edit.
        Undo,
        /// Redo the last undone edit.
        Redo,
    ]
);

/// The key context used for input element keybindings.
pub const INPUT_CONTEXT: &str = "Input";

/// Keybindings configuration for input elements.
///
/// Each field is an `Option<KeyBinding>` to allow:
/// - Using defaults (via `Default::default()`)
/// - Overriding with custom bindings
/// - Unbinding keys by setting fields to `None`
///
/// The `Default` implementation returns platform-specific keybindings.
#[derive(Clone)]
pub struct InputBindings {
    /// Binding for deleting the character before the cursor.
    /// Default: `backspace`
    pub backspace: Option<KeyBinding>,

    /// Binding for deleting the character after the cursor.
    /// Default: `delete`
    pub delete: Option<KeyBinding>,

    /// Binding for deleting the word before the cursor.
    /// Default: `alt-backspace` (macOS) / `ctrl-backspace` (other platforms)
    pub delete_word_left: Option<KeyBinding>,

    /// Binding for deleting the word after the cursor.
    /// Default: `alt-delete` (macOS) / `ctrl-delete` (other platforms)
    pub delete_word_right: Option<KeyBinding>,

    /// Binding for deleting from cursor to beginning of line.
    /// Default: `cmd-backspace` (macOS) / `ctrl-shift-backspace` (other platforms)
    pub delete_to_beginning_of_line: Option<KeyBinding>,

    /// Binding for deleting from cursor to end of line.
    /// Default: `ctrl-k` (macOS) / `ctrl-shift-delete` (other platforms)
    pub delete_to_end_of_line: Option<KeyBinding>,

    /// Binding for inserting a tab character.
    /// Default: `tab`
    pub tab: Option<KeyBinding>,

    /// Binding for inserting a newline (multi-line) or confirming input (single-line).
    /// Default: `enter`
    pub enter: Option<KeyBinding>,

    /// Binding for submitting the input via `InputStateEvent::Submit`.
    /// Only fires on inputs configured with a submit mode ([`InputState::submit_on`]).
    /// Default: `cmd-enter` (macOS) / `ctrl-enter` (other platforms)
    pub submit: Option<KeyBinding>,

    /// Binding for inserting a newline regardless of submit mode — the escape
    /// hatch when `enter` is configured to submit.
    /// Default: `shift-enter`
    pub insert_newline: Option<KeyBinding>,

    /// Binding for moving the cursor one character to the left.
    /// Default: `left`
    pub left: Option<KeyBinding>,

    /// Binding for moving the cursor one character to the right.
    /// Default: `right`
    pub right: Option<KeyBinding>,

    /// Binding for moving the cursor up one line (multi-line) or to the start of the line (single-line).
    /// Default: `up`
    pub up: Option<KeyBinding>,

    /// Binding for moving the cursor down one line (multi-line) or to the end of the line (single-line).
    /// Default: `down`
    pub down: Option<KeyBinding>,

    /// Binding for extending selection one character to the left.
    /// Default: `shift-left`
    pub select_left: Option<KeyBinding>,

    /// Binding for extending selection one character to the right.
    /// Default: `shift-right`
    pub select_right: Option<KeyBinding>,

    /// Binding for extending selection up one line (multi-line) or to the start (single-line).
    /// Default: `shift-up`
    pub select_up: Option<KeyBinding>,

    /// Binding for extending selection down one line (multi-line) or to the end (single-line).
    /// Default: `shift-down`
    pub select_down: Option<KeyBinding>,

    /// Binding for selecting all text content.
    /// Default: `cmd-a` (macOS) / `ctrl-a` (other platforms)
    pub select_all: Option<KeyBinding>,

    /// Binding for moving cursor to the start of the current line.
    /// Default: `home`
    pub home: Option<KeyBinding>,

    /// Binding for moving cursor to the end of the current line.
    /// Default: `end`
    pub end: Option<KeyBinding>,

    /// Binding for moving cursor to the beginning of all content.
    /// Default: `cmd-up` (macOS) / `ctrl-home` (other platforms)
    pub move_to_beginning: Option<KeyBinding>,

    /// Binding for moving cursor to the end of all content.
    /// Default: `cmd-down` (macOS) / `ctrl-end` (other platforms)
    pub move_to_end: Option<KeyBinding>,

    /// Binding for extending selection to the beginning of all content.
    /// Default: `cmd-shift-up` (macOS) / `ctrl-shift-home` (other platforms)
    pub select_to_beginning: Option<KeyBinding>,

    /// Binding for extending selection to the end of all content.
    /// Default: `cmd-shift-down` (macOS) / `ctrl-shift-end` (other platforms)
    pub select_to_end: Option<KeyBinding>,

    /// Binding for moving cursor one word to the left.
    /// Default: `alt-left` (macOS) / `ctrl-left` (other platforms)
    pub word_left: Option<KeyBinding>,

    /// Binding for moving cursor one word to the right.
    /// Default: `alt-right` (macOS) / `ctrl-right` (other platforms)
    pub word_right: Option<KeyBinding>,

    /// Binding for extending selection one word to the left.
    /// Default: `alt-shift-left` (macOS) / `ctrl-shift-left` (other platforms)
    pub select_word_left: Option<KeyBinding>,

    /// Binding for extending selection one word to the right.
    /// Default: `alt-shift-right` (macOS) / `ctrl-shift-right` (other platforms)
    pub select_word_right: Option<KeyBinding>,

    /// Binding for copying selected text to clipboard.
    /// Default: `cmd-c` (macOS) / `ctrl-c` (other platforms)
    pub copy: Option<KeyBinding>,

    /// Binding for cutting selected text to clipboard.
    /// Default: `cmd-x` (macOS) / `ctrl-x` (other platforms)
    pub cut: Option<KeyBinding>,

    /// Binding for pasting from clipboard.
    /// Default: `cmd-v` (macOS) / `ctrl-v` (other platforms)
    pub paste: Option<KeyBinding>,

    /// Binding for undoing the last edit.
    /// Default: `cmd-z` (macOS) / `ctrl-z` (other platforms)
    pub undo: Option<KeyBinding>,

    /// Binding for redoing the last undone edit.
    /// Default: `cmd-shift-z` (macOS) / `ctrl-shift-z` (other platforms)
    pub redo: Option<KeyBinding>,

    /// Binding for blurring focus from the input.
    /// Default: `escape`
    pub escape: Option<KeyBinding>,
}

impl Default for InputBindings {
    /// Returns platform-specific default keybindings for input elements.
    fn default() -> Self {
        let context = Some(INPUT_CONTEXT);

        #[cfg(target_os = "macos")]
        {
            Self {
                backspace: Some(KeyBinding::new("backspace", Backspace, context)),
                delete: Some(KeyBinding::new("delete", Delete, context)),
                delete_word_left: Some(KeyBinding::new("alt-backspace", DeleteWordLeft, context)),
                delete_word_right: Some(KeyBinding::new("alt-delete", DeleteWordRight, context)),
                delete_to_beginning_of_line: Some(KeyBinding::new(
                    "cmd-backspace",
                    DeleteToBeginningOfLine,
                    context,
                )),
                delete_to_end_of_line: Some(KeyBinding::new("ctrl-k", DeleteToEndOfLine, context)),
                tab: Some(KeyBinding::new("tab", Tab, context)),
                enter: Some(KeyBinding::new("enter", Enter, context)),
                submit: Some(KeyBinding::new("cmd-enter", Submit, context)),
                insert_newline: Some(KeyBinding::new("shift-enter", InsertNewline, context)),
                left: Some(KeyBinding::new("left", Left, context)),
                right: Some(KeyBinding::new("right", Right, context)),
                up: Some(KeyBinding::new("up", Up, context)),
                down: Some(KeyBinding::new("down", Down, context)),
                select_left: Some(KeyBinding::new("shift-left", SelectLeft, context)),
                select_right: Some(KeyBinding::new("shift-right", SelectRight, context)),
                select_up: Some(KeyBinding::new("shift-up", SelectUp, context)),
                select_down: Some(KeyBinding::new("shift-down", SelectDown, context)),
                select_all: Some(KeyBinding::new("cmd-a", SelectAll, context)),
                home: Some(KeyBinding::new("home", Home, context)),
                end: Some(KeyBinding::new("end", End, context)),
                move_to_beginning: Some(KeyBinding::new("cmd-up", MoveToBeginning, context)),
                move_to_end: Some(KeyBinding::new("cmd-down", MoveToEnd, context)),
                select_to_beginning: Some(KeyBinding::new(
                    "cmd-shift-up",
                    SelectToBeginning,
                    context,
                )),
                select_to_end: Some(KeyBinding::new("cmd-shift-down", SelectToEnd, context)),
                word_left: Some(KeyBinding::new("alt-left", WordLeft, context)),
                word_right: Some(KeyBinding::new("alt-right", WordRight, context)),
                select_word_left: Some(KeyBinding::new("alt-shift-left", SelectWordLeft, context)),
                select_word_right: Some(KeyBinding::new(
                    "alt-shift-right",
                    SelectWordRight,
                    context,
                )),
                copy: Some(KeyBinding::new("cmd-c", Copy, context)),
                cut: Some(KeyBinding::new("cmd-x", Cut, context)),
                paste: Some(KeyBinding::new("cmd-v", Paste, context)),
                undo: Some(KeyBinding::new("cmd-z", Undo, context)),
                redo: Some(KeyBinding::new("cmd-shift-z", Redo, context)),
                escape: Some(KeyBinding::new("escape", Escape, context)),
            }
        }

        #[cfg(not(target_os = "macos"))]
        {
            Self {
                backspace: Some(KeyBinding::new("backspace", Backspace, context)),
                delete: Some(KeyBinding::new("delete", Delete, context)),
                delete_word_left: Some(KeyBinding::new("ctrl-backspace", DeleteWordLeft, context)),
                delete_word_right: Some(KeyBinding::new("ctrl-delete", DeleteWordRight, context)),
                delete_to_beginning_of_line: Some(KeyBinding::new(
                    "ctrl-shift-backspace",
                    DeleteToBeginningOfLine,
                    context,
                )),
                delete_to_end_of_line: Some(KeyBinding::new(
                    "ctrl-shift-delete",
                    DeleteToEndOfLine,
                    context,
                )),
                tab: Some(KeyBinding::new("tab", Tab, context)),
                enter: Some(KeyBinding::new("enter", Enter, context)),
                submit: Some(KeyBinding::new("ctrl-enter", Submit, context)),
                insert_newline: Some(KeyBinding::new("shift-enter", InsertNewline, context)),
                left: Some(KeyBinding::new("left", Left, context)),
                right: Some(KeyBinding::new("right", Right, context)),
                up: Some(KeyBinding::new("up", Up, context)),
                down: Some(KeyBinding::new("down", Down, context)),
                select_left: Some(KeyBinding::new("shift-left", SelectLeft, context)),
                select_right: Some(KeyBinding::new("shift-right", SelectRight, context)),
                select_up: Some(KeyBinding::new("shift-up", SelectUp, context)),
                select_down: Some(KeyBinding::new("shift-down", SelectDown, context)),
                select_all: Some(KeyBinding::new("ctrl-a", SelectAll, context)),
                home: Some(KeyBinding::new("home", Home, context)),
                end: Some(KeyBinding::new("end", End, context)),
                move_to_beginning: Some(KeyBinding::new("ctrl-home", MoveToBeginning, context)),
                move_to_end: Some(KeyBinding::new("ctrl-end", MoveToEnd, context)),
                select_to_beginning: Some(KeyBinding::new(
                    "ctrl-shift-home",
                    SelectToBeginning,
                    context,
                )),
                select_to_end: Some(KeyBinding::new("ctrl-shift-end", SelectToEnd, context)),
                word_left: Some(KeyBinding::new("ctrl-left", WordLeft, context)),
                word_right: Some(KeyBinding::new("ctrl-right", WordRight, context)),
                select_word_left: Some(KeyBinding::new("ctrl-shift-left", SelectWordLeft, context)),
                select_word_right: Some(KeyBinding::new(
                    "ctrl-shift-right",
                    SelectWordRight,
                    context,
                )),
                copy: Some(KeyBinding::new("ctrl-c", Copy, context)),
                cut: Some(KeyBinding::new("ctrl-x", Cut, context)),
                paste: Some(KeyBinding::new("ctrl-v", Paste, context)),
                undo: Some(KeyBinding::new("ctrl-z", Undo, context)),
                redo: Some(KeyBinding::new("ctrl-shift-z", Redo, context)),
                escape: Some(KeyBinding::new("escape", Escape, context)),
            }
        }
    }
}

impl InputBindings {
    /// Creates an empty `InputBindings` with all fields set to `None`.
    ///
    /// Use this as a starting point when you want to override only specific bindings:
    ///
    /// ```ignore
    /// let bindings = InputBindings::empty();
    /// bindings.select_all = Some(KeyBinding::new("ctrl-shift-a", SelectAll, Some(INPUT_CONTEXT)));
    /// ```
    pub fn empty() -> Self {
        Self {
            backspace: None,
            delete: None,
            delete_word_left: None,
            delete_word_right: None,
            delete_to_beginning_of_line: None,
            delete_to_end_of_line: None,
            tab: None,
            enter: None,
            submit: None,
            insert_newline: None,
            left: None,
            right: None,
            up: None,
            down: None,
            select_left: None,
            select_right: None,
            select_up: None,
            select_down: None,
            select_all: None,
            home: None,
            end: None,
            move_to_beginning: None,
            move_to_end: None,
            select_to_beginning: None,
            select_to_end: None,
            word_left: None,
            word_right: None,
            select_word_left: None,
            select_word_right: None,
            copy: None,
            cut: None,
            paste: None,
            undo: None,
            redo: None,
            escape: None,
        }
    }

    /// Merges these bindings with defaults, using `self` values where `Some`,
    /// falling back to defaults for `None` values.
    pub fn merged_with_defaults(self) -> Self {
        let defaults = Self::default();
        Self {
            backspace: self.backspace.or(defaults.backspace),
            delete: self.delete.or(defaults.delete),
            delete_word_left: self.delete_word_left.or(defaults.delete_word_left),
            delete_word_right: self.delete_word_right.or(defaults.delete_word_right),
            delete_to_beginning_of_line: self
                .delete_to_beginning_of_line
                .or(defaults.delete_to_beginning_of_line),
            delete_to_end_of_line: self
                .delete_to_end_of_line
                .or(defaults.delete_to_end_of_line),
            tab: self.tab.or(defaults.tab),
            enter: self.enter.or(defaults.enter),
            submit: self.submit.or(defaults.submit),
            insert_newline: self.insert_newline.or(defaults.insert_newline),
            left: self.left.or(defaults.left),
            right: self.right.or(defaults.right),
            up: self.up.or(defaults.up),
            down: self.down.or(defaults.down),
            select_left: self.select_left.or(defaults.select_left),
            select_right: self.select_right.or(defaults.select_right),
            select_up: self.select_up.or(defaults.select_up),
            select_down: self.select_down.or(defaults.select_down),
            select_all: self.select_all.or(defaults.select_all),
            home: self.home.or(defaults.home),
            end: self.end.or(defaults.end),
            move_to_beginning: self.move_to_beginning.or(defaults.move_to_beginning),
            move_to_end: self.move_to_end.or(defaults.move_to_end),
            select_to_beginning: self.select_to_beginning.or(defaults.select_to_beginning),
            select_to_end: self.select_to_end.or(defaults.select_to_end),
            word_left: self.word_left.or(defaults.word_left),
            word_right: self.word_right.or(defaults.word_right),
            select_word_left: self.select_word_left.or(defaults.select_word_left),
            select_word_right: self.select_word_right.or(defaults.select_word_right),
            copy: self.copy.or(defaults.copy),
            cut: self.cut.or(defaults.cut),
            paste: self.paste.or(defaults.paste),
            undo: self.undo.or(defaults.undo),
            redo: self.redo.or(defaults.redo),
            escape: self.escape.or(defaults.escape),
        }
    }

    /// Collects all `Some` bindings into a `Vec<KeyBinding>`.
    pub fn into_bindings(self) -> Vec<KeyBinding> {
        let mut bindings: Vec<Option<KeyBinding>> = vec![
            self.backspace,
            self.delete,
            self.delete_word_left,
            self.delete_word_right,
            self.delete_to_beginning_of_line,
            self.delete_to_end_of_line,
            self.tab,
            self.enter,
            self.submit,
            self.insert_newline,
            self.left,
            self.right,
            self.up,
            self.down,
            self.select_left,
            self.select_right,
            self.select_up,
            self.select_down,
            self.select_all,
            self.home,
            self.end,
            self.move_to_beginning,
            self.move_to_end,
            self.select_to_beginning,
            self.select_to_end,
            self.word_left,
            self.word_right,
            self.select_word_left,
            self.select_word_right,
            self.copy,
            self.cut,
            self.paste,
            self.undo,
            self.redo,
            self.escape,
        ];

        // Add additional macOS-specific bindings for Home/End
        // Mac keyboards don't have Home/End keys, so cmd-left/right are standard
        #[cfg(target_os = "macos")]
        {
            let context = Some(INPUT_CONTEXT);
            bindings.push(Some(KeyBinding::new("cmd-left", Home, context)));
            bindings.push(Some(KeyBinding::new("cmd-right", End, context)));
        }

        bindings.into_iter().flatten().collect()
    }
}

/// Binds input keybindings to the application.
///
/// If no bindings are provided, platform defaults are used. When bindings are
/// provided, they are used exactly as-is - fields set to `None` will not have
/// any keybinding registered for that action.
///
/// # Examples
///
/// Use all platform defaults:
///
/// ```ignore
/// bind_input_keys(cx, None);
/// ```
///
/// Unbind a specific key while keeping other defaults:
///
/// ```ignore
/// bind_input_keys(cx, InputBindings {
///     up: None, // Unbind up arrow
///     ..Default::default()
/// });
/// ```
///
/// Override a specific binding while keeping other defaults:
///
/// ```ignore
/// bind_input_keys(cx, InputBindings {
///     select_all: Some(KeyBinding::new("ctrl-shift-a", SelectAll, Some(INPUT_CONTEXT))),
///     ..Default::default()
/// });
/// ```
///
/// Use [`InputBindings::empty()`] with [`merged_with_defaults()`](InputBindings::merged_with_defaults)
/// if you only want to specify a few custom bindings and fill in the rest with defaults:
///
/// ```ignore
/// let mut bindings = InputBindings::empty();
/// bindings.select_all = Some(KeyBinding::new("ctrl-shift-a", SelectAll, Some(INPUT_CONTEXT)));
/// bind_input_keys(cx, bindings.merged_with_defaults());
/// ```
pub fn bind_input_keys(cx: &mut App, bindings: impl Into<Option<InputBindings>>) {
    let bindings = bindings.into().unwrap_or_default();
    cx.bind_keys(bindings.into_bindings());
}