gpui-form-component 0.8.1

GPUI component runtimes for gpui-form, including InfiniteSelect for nested enum support
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
//! Infinite-select runtime support for nested enum trees.
//!
//! This module exposes both the low-level trait and path helpers used by
//! generated code and a higher-level `Select` entity that owns the
//! cascading `SelectState`s for a field.

mod path;
mod state_helpers;
mod value;

pub use path::{
    InfiniteSelectKeyPath, InfiniteSelectKeyPathParseError, InfiniteSelectKeyPathParseErrorReason,
    InfiniteSelectPath, InfiniteSelectPathError, InfiniteSelectPathErrorReason,
    InfiniteSelectPathSegment, build_from_key_path, build_from_path, key_path_from_value,
    path_from_value,
};
use state_helpers::{
    build_child_selects, build_levels, build_select_state, first_changed_depth, selected_index,
};
pub use value::{InfiniteSelectItem, InfiniteSelectValue, to_select_items};

use gpui_kit::component::{
    IndexPath,
    form::{Field, field},
    select::{Select as GpuiSelect, SelectDelegate, SelectEvent, SelectItem, SelectState},
};
use gpui_kit::{
    App, AppContext as _, Context, Empty, Entity, EventEmitter, FocusHandle, Focusable,
    IntoElement, ParentElement as _, Render, RenderOnce, SharedString, Styled as _, Subscription,
    Window, div,
};

/// Options for the runtime `Select`.
#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
#[cfg_attr(feature = "component-shape", derive(bon::Builder))]
pub struct InfiniteSelectOptions {
    #[cfg_attr(feature = "component-shape", builder(default))]
    searchable: bool,
    max_depth: Option<usize>,
}

impl InfiniteSelectOptions {
    /// Creates runtime options for `Select`.
    pub const fn new(searchable: bool, max_depth: Option<usize>) -> Self {
        Self {
            searchable,
            max_depth,
        }
    }
}

/// Event emitted by `Select` whenever the selection changes.
#[derive(Clone)]
pub struct InfiniteSelectEvent<T: InfiniteSelectValue> {
    previous_value: T,
    previous_path: InfiniteSelectPath,
    previous_key_path: InfiniteSelectKeyPath,
    value: T,
    path: InfiniteSelectPath,
    key_path: InfiniteSelectKeyPath,
    changed_depth: usize,
}

impl<T: InfiniteSelectValue> InfiniteSelectEvent<T> {
    /// Returns the concrete selection value before this change.
    pub fn previous_value(&self) -> &T {
        &self.previous_value
    }

    /// Returns the index path before this change.
    pub fn previous_path(&self) -> &InfiniteSelectPath {
        &self.previous_path
    }

    /// Returns the key path before this change.
    pub fn previous_key_path(&self) -> &InfiniteSelectKeyPath {
        &self.previous_key_path
    }

    /// Returns the rebuilt concrete selection value.
    pub fn value(&self) -> &T {
        &self.value
    }

    /// Consumes the event and returns the rebuilt concrete selection value.
    pub fn into_value(self) -> T {
        self.value
    }

    /// Returns the current index path.
    pub fn path(&self) -> &InfiniteSelectPath {
        &self.path
    }

    /// Returns the current key path.
    pub fn key_path(&self) -> &InfiniteSelectKeyPath {
        &self.key_path
    }

    /// Returns the first depth that changed for this selection event.
    pub fn changed_depth(&self) -> usize {
        self.changed_depth
    }
}

/// A single rendered level of an infinite-select field.
#[derive(Clone)]
pub struct InfiniteSelectLevel<D>
where
    D: SelectDelegate + 'static,
{
    depth: usize,
    label: SharedString,
    description: SharedString,
    select: Entity<SelectState<D>>,
    selected_index: Option<usize>,
    selected_key: Option<String>,
}

impl<D> InfiniteSelectLevel<D>
where
    D: SelectDelegate + 'static,
{
    /// Returns the rendered depth for this select level.
    pub fn depth(&self) -> usize {
        self.depth
    }

    /// Returns true when this level is the root select.
    pub fn is_root(&self) -> bool {
        self.depth == 0
    }

    /// Returns the field label for this level.
    pub fn label(&self) -> &SharedString {
        &self.label
    }

    /// Returns the field description for this level.
    pub fn description(&self) -> &SharedString {
        &self.description
    }

    /// Returns the backing select entity for this level.
    pub fn select(&self) -> Entity<SelectState<D>> {
        self.select.clone()
    }

    /// Returns the selected index for this level, when available.
    pub fn selected_index(&self) -> Option<usize> {
        self.selected_index
    }

    /// Returns the selected stable key for this level, when available.
    pub fn selected_key(&self) -> Option<&str> {
        self.selected_key.as_deref()
    }

    /// Builds a `gpui_kit::component::form::Field` for this select level.
    pub fn to_form_field(&self) -> Field {
        let label = self.label.clone();
        let description = self.description.clone();
        let select = self.select.clone();

        field()
            .label(label)
            .description_fn(move |_, _| {
                div()
                    .flex()
                    .flex_col()
                    .gap_1()
                    .child(div().child(description.clone()))
            })
            .child(GpuiSelect::new(&select))
    }
}

/// An owned snapshot of the current infinite-select runtime state.
#[derive(Clone)]
pub struct InfiniteSelectSnapshot<T, D>
where
    D: SelectDelegate + 'static,
{
    value: T,
    path: InfiniteSelectPath,
    key_path: InfiniteSelectKeyPath,
    levels: Vec<InfiniteSelectLevel<D>>,
}

impl<T, D> InfiniteSelectSnapshot<T, D>
where
    T: InfiniteSelectValue,
    D: SelectDelegate + 'static,
{
    /// Returns the concrete selected value.
    pub fn value(&self) -> &T {
        &self.value
    }

    /// Returns the current index-based selection path.
    pub fn path(&self) -> &InfiniteSelectPath {
        &self.path
    }

    /// Returns the current key-based selection path.
    pub fn key_path(&self) -> &InfiniteSelectKeyPath {
        &self.key_path
    }

    /// Returns the rendered select levels in root-to-leaf order.
    pub fn levels(&self) -> &[InfiniteSelectLevel<D>] {
        &self.levels
    }

    /// Consumes the snapshot and returns the owned level list.
    pub fn into_levels(self) -> Vec<InfiniteSelectLevel<D>> {
        self.levels
    }

    /// Returns render-ready GPUI form fields for each select level.
    pub fn form_fields(&self) -> Vec<Field> {
        self.levels
            .iter()
            .map(InfiniteSelectLevel::to_form_field)
            .collect()
    }
}

/// Runtime state for a cascading infinite-select field.
pub struct InfiniteSelectState<T, D = Vec<InfiniteSelectItem<T>>>
where
    T: InfiniteSelectValue,
    D: SelectDelegate<Item = InfiniteSelectItem<T>> + From<Vec<InfiniteSelectItem<T>>> + 'static,
{
    value: T,
    path: InfiniteSelectPath,
    key_path: InfiniteSelectKeyPath,
    master_select: Entity<SelectState<D>>,
    child_selects: Vec<Entity<SelectState<D>>>,
    options: InfiniteSelectOptions,
    _master_subscription: Subscription,
    _child_subscriptions: Vec<Subscription>,
}

impl<T, D> Focusable for InfiniteSelectState<T, D>
where
    T: InfiniteSelectValue,
    D: SelectDelegate<Item = InfiniteSelectItem<T>> + From<Vec<InfiniteSelectItem<T>>> + 'static,
{
    fn focus_handle(&self, cx: &App) -> FocusHandle {
        self.master_select.read(cx).focus_handle(cx)
    }
}

impl<T, D> EventEmitter<InfiniteSelectEvent<T>> for InfiniteSelectState<T, D>
where
    T: InfiniteSelectValue,
    D: SelectDelegate<Item = InfiniteSelectItem<T>> + From<Vec<InfiniteSelectItem<T>>> + 'static,
{
}

impl<T, D> InfiniteSelectState<T, D>
where
    T: InfiniteSelectValue,
    D: SelectDelegate<Item = InfiniteSelectItem<T>> + From<Vec<InfiniteSelectItem<T>>> + 'static,
{
    /// Creates a new state from `T::default()`.
    pub fn new_default(window: &mut Window, cx: &mut Context<Self>) -> Self {
        Self::new(T::default(), window, cx)
    }

    /// Creates a new state from the given initial value.
    pub fn new(initial_value: T, window: &mut Window, cx: &mut Context<Self>) -> Self {
        Self::new_with_options(initial_value, InfiniteSelectOptions::default(), window, cx)
    }

    /// Creates a new state with explicit options.
    pub fn new_with_options(
        initial_value: T,
        options: InfiniteSelectOptions,
        window: &mut Window,
        cx: &mut Context<Self>,
    ) -> Self {
        let path = path_from_value(&initial_value);
        let key_path = key_path_from_value(&initial_value);
        let root_selected = path.get(0);
        let master_select = cx.new(|cx| {
            build_select_state::<T, D>(
                to_select_items::<T>(cx),
                root_selected,
                options.searchable,
                window,
                cx,
            )
        });
        let master_subscription = cx.subscribe_in(&master_select, window, Self::on_select_event);

        let mut this = Self {
            value: initial_value,
            path,
            key_path,
            master_select,
            child_selects: Vec::new(),
            options,
            _master_subscription: master_subscription,
            _child_subscriptions: Vec::new(),
        };
        this.rebuild_child_selects(window, cx);
        this
    }

    /// Returns the current concrete selection.
    pub fn value(&self) -> &T {
        &self.value
    }

    /// Returns the current index-based selection path.
    pub fn path(&self) -> &InfiniteSelectPath {
        &self.path
    }

    /// Returns the current key-based selection path.
    pub fn key_path(&self) -> &InfiniteSelectKeyPath {
        &self.key_path
    }

    /// Returns the selected index at the given depth.
    pub fn selected_index_at_depth(&self, depth: usize) -> Option<usize> {
        self.path.get(depth)
    }

    /// Returns the selected key at the given depth.
    pub fn selected_key_at_depth(&self, depth: usize) -> Option<&str> {
        self.key_path.get(depth)
    }

    /// Returns the current rendered select levels in root-to-leaf order.
    pub fn levels(&self, cx: &impl std::borrow::Borrow<App>) -> Vec<InfiniteSelectLevel<D>> {
        build_levels(
            &self.value,
            &self.path,
            &self.key_path,
            &self.master_select,
            &self.child_selects,
            cx,
        )
    }

    /// Returns an owned snapshot of the value, paths, and rendered levels.
    pub fn snapshot(&self, cx: &impl std::borrow::Borrow<App>) -> InfiniteSelectSnapshot<T, D> {
        InfiniteSelectSnapshot {
            value: self.value.clone(),
            path: self.path.clone(),
            key_path: self.key_path.clone(),
            levels: self.levels(cx),
        }
    }

    /// Returns render-ready GPUI form fields for each visible select level.
    pub fn form_fields(&self, cx: &impl std::borrow::Borrow<App>) -> Vec<Field> {
        self.levels(cx)
            .into_iter()
            .map(|level| level.to_form_field())
            .collect()
    }

    /// Returns the root select entity.
    pub fn master_select(&self) -> Entity<SelectState<D>> {
        self.master_select.clone()
    }

    /// Returns the currently visible child selects.
    pub fn child_selects(&self) -> Vec<Entity<SelectState<D>>> {
        self.child_selects.clone()
    }

    /// Programmatically sets the current selection.
    pub fn set_value(&mut self, value: T, window: &mut Window, cx: &mut Context<Self>) {
        self.apply_selection(value, None, false, window, cx);
    }

    /// Programmatically sets the current selection from an index path.
    pub fn set_path(
        &mut self,
        path: &InfiniteSelectPath,
        window: &mut Window,
        cx: &mut Context<Self>,
    ) -> Result<(), InfiniteSelectPathError> {
        let value = build_from_path::<T>(path)?;
        self.apply_selection(value, None, false, window, cx);
        Ok(())
    }

    /// Programmatically sets the current selection from a key path.
    pub fn set_key_path(
        &mut self,
        key_path: &InfiniteSelectKeyPath,
        window: &mut Window,
        cx: &mut Context<Self>,
    ) -> Result<(), InfiniteSelectPathError> {
        let value = build_from_key_path::<T>(key_path)?;
        self.apply_selection(value, None, false, window, cx);
        Ok(())
    }

    /// Programmatically changes one selection index and resets deeper levels to defaults.
    pub fn set_selected_index_at_depth(
        &mut self,
        depth: usize,
        index: usize,
        window: &mut Window,
        cx: &mut Context<Self>,
    ) -> Result<(), InfiniteSelectPathError> {
        if depth > self.path.len() {
            return Err(InfiniteSelectPathError::missing_selection_options(
                depth,
                InfiniteSelectPathSegment::Index(index),
            ));
        }

        let mut path = self.path.clone();
        path.set(depth, index);
        self.set_path(&path, window, cx)
    }

    /// Programmatically changes one selection key and resets deeper levels to defaults.
    pub fn set_selected_key_at_depth(
        &mut self,
        depth: usize,
        key: impl Into<String>,
        window: &mut Window,
        cx: &mut Context<Self>,
    ) -> Result<(), InfiniteSelectPathError> {
        let key = key.into();
        if depth > self.key_path.len() {
            return Err(InfiniteSelectPathError::missing_selection_options(
                depth,
                InfiniteSelectPathSegment::Key(key),
            ));
        }

        let mut key_path = self.key_path.clone();
        key_path.set(depth, key);
        self.set_key_path(&key_path, window, cx)
    }

    fn resolved_max_depth(&self) -> usize {
        match self.options.max_depth {
            Some(max_depth) => max_depth.clamp(1, T::depth()),
            None => T::depth(),
        }
    }

    fn on_select_event(
        &mut self,
        this: &Entity<SelectState<D>>,
        event: &SelectEvent<D>,
        window: &mut Window,
        cx: &mut Context<Self>,
    ) {
        let SelectEvent::Confirm(Some(selected)) = event else {
            return;
        };

        let changed_depth = self
            .select_depth(this)
            .or_else(|| Some(first_changed_depth(&self.path, &selected.selection_path())));

        self.apply_selection(selected.clone(), changed_depth, true, window, cx);
    }

    fn select_depth(&self, this: &Entity<SelectState<D>>) -> Option<usize> {
        if &self.master_select == this {
            Some(0)
        } else {
            self.child_selects
                .iter()
                .position(|child| child == this)
                .map(|position| position + 1)
        }
    }

    fn apply_selection(
        &mut self,
        value: T,
        changed_depth: Option<usize>,
        emit: bool,
        window: &mut Window,
        cx: &mut Context<Self>,
    ) {
        let new_path = path_from_value(&value);
        let new_key_path = key_path_from_value(&value);
        let changed_depth =
            changed_depth.unwrap_or_else(|| first_changed_depth(&self.path, &new_path));

        let previous_value = std::mem::replace(&mut self.value, value);
        let previous_path = std::mem::replace(&mut self.path, new_path);
        let previous_key_path = std::mem::replace(&mut self.key_path, new_key_path);
        self.sync_master_select(window, cx);
        self.rebuild_child_selects(window, cx);
        if emit {
            cx.emit(InfiniteSelectEvent {
                previous_value,
                previous_path,
                previous_key_path,
                value: self.value.clone(),
                path: self.path.clone(),
                key_path: self.key_path.clone(),
                changed_depth,
            });
        }
        cx.notify();
    }

    fn sync_master_select(&mut self, window: &mut Window, cx: &mut Context<Self>) {
        let selected_index = selected_index(self.path.get(0).unwrap_or(0));
        self.master_select.update(cx, |state, cx| {
            state.set_selected_index(selected_index, window, cx);
        });
    }

    fn rebuild_child_selects(&mut self, window: &mut Window, cx: &mut Context<Self>) {
        let child_selects = build_child_selects::<T, D>(
            &self.value,
            &self.path,
            self.resolved_max_depth(),
            self.options.searchable,
            window,
            cx,
        );

        self._child_subscriptions = child_selects
            .iter()
            .map(|child| cx.subscribe_in(child, window, Self::on_select_event))
            .collect();
        self.child_selects = child_selects;
    }
}

/// Render wrapper used by generated form code for infinite-select fields.
#[cfg_attr(
    feature = "component-shape",
    derive(component_shape_gpui::GpuiComponentShape)
)]
#[cfg_attr(
    feature = "component-shape",
    gpui_component_shape(
        state = InfiniteSelectState<T, D>,
        new = InfiniteSelectState::<T, D>::new_default,
        value = T,
        field_suffix = "infinite_select",
        value_binding
    )
)]
#[derive(IntoElement)]
pub struct InfiniteSelect<T, D = Vec<InfiniteSelectItem<T>>>
where
    T: InfiniteSelectValue,
    D: SelectDelegate<Item = InfiniteSelectItem<T>> + From<Vec<InfiniteSelectItem<T>>> + 'static,
{
    state: Entity<InfiniteSelectState<T, D>>,
}

#[cfg(feature = "component-shape")]
impl<T, D> gpui_form_runtime::shape::GpuiFormComponentShapePolicy for InfiniteSelect<T, D>
where
    T: InfiniteSelectValue,
    D: SelectDelegate<Item = InfiniteSelectItem<T>> + From<Vec<InfiniteSelectItem<T>>> + 'static,
{
    type ValueStoragePolicy = gpui_form_runtime::shape::DirectValueStorage;
}

impl<T, D> InfiniteSelect<T, D>
where
    T: InfiniteSelectValue,
    D: SelectDelegate<Item = InfiniteSelectItem<T>> + From<Vec<InfiniteSelectItem<T>>> + 'static,
{
    #[cfg(feature = "component-shape")]
    pub fn searchable(searchable: bool) -> InfiniteSelectOptions {
        InfiniteSelectOptions::builder()
            .searchable(searchable)
            .build()
    }

    #[cfg(feature = "component-shape")]
    pub fn from(options: InfiniteSelectOptions) -> InfiniteSelectOptions {
        options
    }

    pub fn new(state: &Entity<InfiniteSelectState<T, D>>) -> Self {
        Self {
            state: state.clone(),
        }
    }
}

#[cfg(feature = "component-shape")]
impl<T, D> component_shape_gpui::GpuiComponentShapeBuilder<InfiniteSelect<T, D>>
    for InfiniteSelectOptions
where
    T: InfiniteSelectValue,
    D: SelectDelegate<Item = InfiniteSelectItem<T>> + From<Vec<InfiniteSelectItem<T>>> + 'static,
{
    fn build(
        self,
        window: &mut Window,
        cx: &mut Context<'_, InfiniteSelectState<T, D>>,
    ) -> InfiniteSelectState<T, D> {
        InfiniteSelectState::<T, D>::new_with_options(T::default(), self, window, cx)
    }
}

impl<T, D> RenderOnce for InfiniteSelect<T, D>
where
    T: InfiniteSelectValue,
    D: SelectDelegate<Item = InfiniteSelectItem<T>> + From<Vec<InfiniteSelectItem<T>>> + 'static,
{
    fn render(self, _window: &mut Window, cx: &mut App) -> impl IntoElement {
        div().children(self.state.read(cx).form_fields(cx))
    }
}

#[cfg(feature = "component-shape")]
impl<T, D> gpui_form_runtime::shape::GpuiComponentStateValueBinding<T> for InfiniteSelectState<T, D>
where
    T: InfiniteSelectValue,
    D: SelectDelegate<Item = InfiniteSelectItem<T>> + From<Vec<InfiniteSelectItem<T>>> + 'static,
{
    type Event = InfiniteSelectEvent<T>;

    fn seed_value_binding_state(
        state: &mut Self,
        value: Option<&T>,
        window: &mut Window,
        cx: &mut Context<'_, Self>,
    ) {
        if let Some(value) = value {
            state.set_value(value.clone(), window, cx);
        }
    }

    fn value_change(
        _state: &Self,
        event: &Self::Event,
    ) -> gpui_form_runtime::shape::ValueChange<T> {
        gpui_form_runtime::shape::ValueChange::Set(event.value().clone())
    }
}

impl<T, D> Render for InfiniteSelectState<T, D>
where
    T: InfiniteSelectValue,
    D: SelectDelegate<Item = InfiniteSelectItem<T>> + From<Vec<InfiniteSelectItem<T>>> + 'static,
{
    fn render(&mut self, _: &mut Window, _: &mut Context<Self>) -> impl IntoElement {
        Empty
    }
}