gpui_form_component/infinite_select/
value.rs1use super::*;
2
3pub trait InfiniteSelectValue: Sized + Clone + Default + PartialEq + 'static {
4 fn variants() -> Vec<Self>;
6
7 fn variant_name(&self) -> &'static str;
9
10 fn variant_key(&self) -> &'static str;
15
16 fn variant_label(&self, cx: &impl std::borrow::Borrow<App>) -> SharedString;
18
19 fn has_inner(&self) -> bool;
21
22 fn child_variant_names(&self) -> Vec<&'static str>;
25
26 fn child_variant_keys(&self) -> Vec<&'static str>;
28
29 fn child_variant_labels(&self, cx: &impl std::borrow::Borrow<App>) -> Vec<SharedString>;
31
32 fn set_child_by_index(&self, index: usize) -> Option<Self>;
35
36 fn set_child_by_key(&self, key: &str) -> Option<Self>;
38
39 fn set_child_by_path(&self, path: &[usize]) -> Option<Self>;
42
43 fn set_child_by_key_path(&self, path: &[String]) -> Option<Self>;
45
46 fn child_depth(&self) -> usize;
49
50 fn depth() -> usize;
52
53 fn selection_path(&self) -> InfiniteSelectPath;
57
58 fn selection_key_path(&self) -> InfiniteSelectKeyPath;
60
61 fn inner_child_variant_names(&self) -> Vec<&'static str>;
63
64 fn inner_child_variant_keys(&self) -> Vec<&'static str>;
66
67 fn inner_child_variant_labels(&self, cx: &impl std::borrow::Borrow<App>) -> Vec<SharedString>;
69
70 fn inner_set_child_by_index(&self, index: usize) -> Option<Self>;
72
73 fn inner_set_child_by_key(&self, key: &str) -> Option<Self>;
75
76 fn inner_has_inner(&self) -> bool;
78
79 fn type_label(&self, cx: &impl std::borrow::Borrow<App>) -> SharedString;
81
82 fn type_description(&self, cx: &impl std::borrow::Borrow<App>) -> SharedString;
84
85 fn child_label_at_depth(
88 &self,
89 depth: usize,
90 cx: &impl std::borrow::Borrow<App>,
91 ) -> Option<SharedString>;
92
93 fn child_description_at_depth(
95 &self,
96 depth: usize,
97 cx: &impl std::borrow::Borrow<App>,
98 ) -> Option<SharedString>;
99
100 fn inner_child_label_at_depth(
102 &self,
103 depth: usize,
104 cx: &impl std::borrow::Borrow<App>,
105 ) -> Option<SharedString>;
106
107 fn inner_child_description_at_depth(
109 &self,
110 depth: usize,
111 cx: &impl std::borrow::Borrow<App>,
112 ) -> Option<SharedString>;
113}
114
115#[derive(Clone)]
120pub struct InfiniteSelectItem<T: InfiniteSelectValue> {
121 value: T,
122 title: SharedString,
123}
124
125impl<T: InfiniteSelectValue> InfiniteSelectItem<T> {
126 pub fn new(value: T, title: impl Into<SharedString>) -> Self {
128 Self {
129 value,
130 title: title.into(),
131 }
132 }
133
134 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 pub fn get_value(&self) -> &T {
142 &self.value
143 }
144
145 pub fn into_value(self) -> T {
147 self.value
148 }
149
150 pub fn has_inner(&self) -> bool {
152 self.value.has_inner()
153 }
154
155 pub fn child_variant_names(&self) -> Vec<&'static str> {
157 self.value.child_variant_names()
158 }
159
160 pub fn child_variant_keys(&self) -> Vec<&'static str> {
162 self.value.child_variant_keys()
163 }
164
165 pub fn child_variant_labels(&self, cx: &impl std::borrow::Borrow<App>) -> Vec<SharedString> {
167 self.value.child_variant_labels(cx)
168 }
169
170 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
191pub 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}