Skip to main content

gpui_form_component/infinite_select/
value.rs

1use super::*;
2
3pub trait InfiniteSelectValue: Sized + Clone + Default + PartialEq + 'static {
4    /// Returns all possible variants at this level with default inner values.
5    fn variants() -> Vec<Self>;
6
7    /// Returns the variant name/discriminant as a string.
8    fn variant_name(&self) -> &'static str;
9
10    /// Returns a stable key for this variant.
11    ///
12    /// The derived implementation currently mirrors `variant_name()`, which is
13    /// stable across enum reordering and suitable for persisted paths.
14    fn variant_key(&self) -> &'static str;
15
16    /// Returns the localized label for this specific variant.
17    fn variant_label(&self, cx: &impl std::borrow::Borrow<App>) -> SharedString;
18
19    /// Returns true if this variant contains an inner value.
20    fn has_inner(&self) -> bool;
21
22    /// Returns the variant names of the children for this specific variant.
23    /// Returns an empty vec for unit variants or variants without children.
24    fn child_variant_names(&self) -> Vec<&'static str>;
25
26    /// Returns the stable keys of the children for this specific variant.
27    fn child_variant_keys(&self) -> Vec<&'static str>;
28
29    /// Returns the localized labels of the children for this specific variant.
30    fn child_variant_labels(&self, cx: &impl std::borrow::Borrow<App>) -> Vec<SharedString>;
31
32    /// Creates a new instance with the child at the given index.
33    /// Returns None if the variant doesn't have children or the index is out of bounds.
34    fn set_child_by_index(&self, index: usize) -> Option<Self>;
35
36    /// Creates a new instance with the child that matches the given key.
37    fn set_child_by_key(&self, key: &str) -> Option<Self>;
38
39    /// Sets the child at a given path depth recursively.
40    /// `path[0]` is the child index at this level, `path[1]` the grandchild, and so on.
41    fn set_child_by_path(&self, path: &[usize]) -> Option<Self>;
42
43    /// Sets the child at a given key path recursively.
44    fn set_child_by_key_path(&self, path: &[String]) -> Option<Self>;
45
46    /// Returns the depth of nesting for this variant's children.
47    /// Returns 0 for leaf variants.
48    fn child_depth(&self) -> usize;
49
50    /// Returns the maximum depth of nesting for this enum type.
51    fn depth() -> usize;
52
53    /// Returns the current selection path for this concrete value.
54    ///
55    /// The returned path includes the root variant index at depth 0.
56    fn selection_path(&self) -> InfiniteSelectPath;
57
58    /// Returns the current stable key path for this concrete value.
59    fn selection_key_path(&self) -> InfiniteSelectKeyPath;
60
61    /// Returns the variant names of the inner value's children.
62    fn inner_child_variant_names(&self) -> Vec<&'static str>;
63
64    /// Returns the stable keys of the inner value's children.
65    fn inner_child_variant_keys(&self) -> Vec<&'static str>;
66
67    /// Returns the localized labels of the inner value's children.
68    fn inner_child_variant_labels(&self, cx: &impl std::borrow::Borrow<App>) -> Vec<SharedString>;
69
70    /// Sets a child on the inner value and wraps it back.
71    fn inner_set_child_by_index(&self, index: usize) -> Option<Self>;
72
73    /// Sets a child on the inner value by stable key and wraps it back.
74    fn inner_set_child_by_key(&self, key: &str) -> Option<Self>;
75
76    /// Returns true if the inner value itself has children.
77    fn inner_has_inner(&self) -> bool;
78
79    /// Returns the localized label for this type (level).
80    fn type_label(&self, cx: &impl std::borrow::Borrow<App>) -> SharedString;
81
82    /// Returns the localized description for this type (level).
83    fn type_description(&self, cx: &impl std::borrow::Borrow<App>) -> SharedString;
84
85    /// Returns the localized label for the child at the given depth relative to this node.
86    /// `depth = 0` is the immediate child.
87    fn child_label_at_depth(
88        &self,
89        depth: usize,
90        cx: &impl std::borrow::Borrow<App>,
91    ) -> Option<SharedString>;
92
93    /// Returns the localized description for the child at the given depth.
94    fn child_description_at_depth(
95        &self,
96        depth: usize,
97        cx: &impl std::borrow::Borrow<App>,
98    ) -> Option<SharedString>;
99
100    /// Internal method to delegate label lookup to the inner value.
101    fn inner_child_label_at_depth(
102        &self,
103        depth: usize,
104        cx: &impl std::borrow::Borrow<App>,
105    ) -> Option<SharedString>;
106
107    /// Internal method to delegate description lookup to the inner value.
108    fn inner_child_description_at_depth(
109        &self,
110        depth: usize,
111        cx: &impl std::borrow::Borrow<App>,
112    ) -> Option<SharedString>;
113}
114
115/// A wrapper for infinite-select enum variants that implements `SelectItem`.
116///
117/// This allows infinite-select enum variants to be displayed in a select dropdown
118/// while preserving access to the nested value.
119#[derive(Clone)]
120pub struct InfiniteSelectItem<T: InfiniteSelectValue> {
121    value: T,
122    title: SharedString,
123}
124
125impl<T: InfiniteSelectValue> InfiniteSelectItem<T> {
126    /// Creates a new item with a custom title.
127    pub fn new(value: T, title: impl Into<SharedString>) -> Self {
128        Self {
129            value,
130            title: title.into(),
131        }
132    }
133
134    /// Creates an item using `variant_label(cx)` as the title.
135    pub fn from_variant(value: T, cx: &impl std::borrow::Borrow<App>) -> Self {
136        let title = value.variant_label(cx);
137        Self { value, title }
138    }
139
140    /// Returns a reference to the wrapped value.
141    pub fn get_value(&self) -> &T {
142        &self.value
143    }
144
145    /// Consumes the item and returns the wrapped value.
146    pub fn into_value(self) -> T {
147        self.value
148    }
149
150    /// Returns true if the wrapped value has nested inner values.
151    pub fn has_inner(&self) -> bool {
152        self.value.has_inner()
153    }
154
155    /// Returns the child variant names if the wrapped value has children.
156    pub fn child_variant_names(&self) -> Vec<&'static str> {
157        self.value.child_variant_names()
158    }
159
160    /// Returns the child variant keys if the wrapped value has children.
161    pub fn child_variant_keys(&self) -> Vec<&'static str> {
162        self.value.child_variant_keys()
163    }
164
165    /// Returns the child variant labels if the wrapped value has children.
166    pub fn child_variant_labels(&self, cx: &impl std::borrow::Borrow<App>) -> Vec<SharedString> {
167        self.value.child_variant_labels(cx)
168    }
169
170    /// Returns a new item with a child selected at the given index.
171    pub fn with_child_at(&self, index: usize, cx: &impl std::borrow::Borrow<App>) -> Option<Self> {
172        let title = self.value.child_variant_labels(cx).get(index).cloned()?;
173        self.value
174            .set_child_by_index(index)
175            .map(|value| Self::new(value, title))
176    }
177}
178
179impl<T: InfiniteSelectValue> SelectItem for InfiniteSelectItem<T> {
180    type Value = T;
181
182    fn title(&self) -> SharedString {
183        self.title.clone()
184    }
185
186    fn value(&self) -> &Self::Value {
187        &self.value
188    }
189}
190
191/// Creates root select items from `T::variants()`.
192pub fn to_select_items<T>(cx: &impl std::borrow::Borrow<App>) -> Vec<InfiniteSelectItem<T>>
193where
194    T: InfiniteSelectValue,
195{
196    T::variants()
197        .into_iter()
198        .map(|value| InfiniteSelectItem::from_variant(value, cx))
199        .collect()
200}