1mod directory_context_picker;
2mod fetch_context_picker;
3mod file_context_picker;
4mod thread_context_picker;
5
6use std::sync::Arc;
7
8use gpui::{
9 AppContext, DismissEvent, EventEmitter, FocusHandle, FocusableView, SharedString, Task, View,
10 WeakModel, WeakView,
11};
12use picker::{Picker, PickerDelegate};
13use ui::{prelude::*, ListItem, ListItemSpacing};
14use util::ResultExt;
15use workspace::Workspace;
16
17use crate::context::ContextKind;
18use crate::context_picker::directory_context_picker::DirectoryContextPicker;
19use crate::context_picker::fetch_context_picker::FetchContextPicker;
20use crate::context_picker::file_context_picker::FileContextPicker;
21use crate::context_picker::thread_context_picker::ThreadContextPicker;
22use crate::context_store::ContextStore;
23use crate::thread_store::ThreadStore;
24
25#[derive(Debug, Clone, Copy)]
26pub enum ConfirmBehavior {
27 KeepOpen,
28 Close,
29}
30
31#[derive(Debug, Clone)]
32enum ContextPickerMode {
33 Default,
34 File(View<FileContextPicker>),
35 Directory(View<DirectoryContextPicker>),
36 Fetch(View<FetchContextPicker>),
37 Thread(View<ThreadContextPicker>),
38}
39
40pub(super) struct ContextPicker {
41 mode: ContextPickerMode,
42 picker: View<Picker<ContextPickerDelegate>>,
43}
44
45impl ContextPicker {
46 pub fn new(
47 workspace: WeakView<Workspace>,
48 thread_store: Option<WeakModel<ThreadStore>>,
49 context_store: WeakModel<ContextStore>,
50 confirm_behavior: ConfirmBehavior,
51 cx: &mut ViewContext<Self>,
52 ) -> Self {
53 let mut entries = vec![
54 ContextPickerEntry {
55 name: "File".into(),
56 kind: ContextKind::File,
57 icon: IconName::File,
58 },
59 ContextPickerEntry {
60 name: "Folder".into(),
61 kind: ContextKind::Directory,
62 icon: IconName::Folder,
63 },
64 ContextPickerEntry {
65 name: "Fetch".into(),
66 kind: ContextKind::FetchedUrl,
67 icon: IconName::Globe,
68 },
69 ];
70
71 if thread_store.is_some() {
72 entries.push(ContextPickerEntry {
73 name: "Thread".into(),
74 kind: ContextKind::Thread,
75 icon: IconName::MessageCircle,
76 });
77 }
78
79 let delegate = ContextPickerDelegate {
80 context_picker: cx.view().downgrade(),
81 workspace,
82 thread_store,
83 context_store,
84 confirm_behavior,
85 entries,
86 selected_ix: 0,
87 };
88
89 let picker = cx.new_view(|cx| {
90 Picker::nonsearchable_uniform_list(delegate, cx).max_height(Some(rems(20.).into()))
91 });
92
93 ContextPicker {
94 mode: ContextPickerMode::Default,
95 picker,
96 }
97 }
98
99 pub fn reset_mode(&mut self) {
100 self.mode = ContextPickerMode::Default;
101 }
102}
103
104impl EventEmitter<DismissEvent> for ContextPicker {}
105
106impl FocusableView for ContextPicker {
107 fn focus_handle(&self, cx: &AppContext) -> FocusHandle {
108 match &self.mode {
109 ContextPickerMode::Default => self.picker.focus_handle(cx),
110 ContextPickerMode::File(file_picker) => file_picker.focus_handle(cx),
111 ContextPickerMode::Directory(directory_picker) => directory_picker.focus_handle(cx),
112 ContextPickerMode::Fetch(fetch_picker) => fetch_picker.focus_handle(cx),
113 ContextPickerMode::Thread(thread_picker) => thread_picker.focus_handle(cx),
114 }
115 }
116}
117
118impl Render for ContextPicker {
119 fn render(&mut self, _cx: &mut ViewContext<Self>) -> impl IntoElement {
120 v_flex()
121 .w(px(400.))
122 .min_w(px(400.))
123 .map(|parent| match &self.mode {
124 ContextPickerMode::Default => parent.child(self.picker.clone()),
125 ContextPickerMode::File(file_picker) => parent.child(file_picker.clone()),
126 ContextPickerMode::Directory(directory_picker) => {
127 parent.child(directory_picker.clone())
128 }
129 ContextPickerMode::Fetch(fetch_picker) => parent.child(fetch_picker.clone()),
130 ContextPickerMode::Thread(thread_picker) => parent.child(thread_picker.clone()),
131 })
132 }
133}
134
135#[derive(Clone)]
136struct ContextPickerEntry {
137 name: SharedString,
138 kind: ContextKind,
139 icon: IconName,
140}
141
142pub(crate) struct ContextPickerDelegate {
143 context_picker: WeakView<ContextPicker>,
144 workspace: WeakView<Workspace>,
145 thread_store: Option<WeakModel<ThreadStore>>,
146 context_store: WeakModel<ContextStore>,
147 confirm_behavior: ConfirmBehavior,
148 entries: Vec<ContextPickerEntry>,
149 selected_ix: usize,
150}
151
152impl PickerDelegate for ContextPickerDelegate {
153 type ListItem = ListItem;
154
155 fn match_count(&self) -> usize {
156 self.entries.len()
157 }
158
159 fn selected_index(&self) -> usize {
160 self.selected_ix
161 }
162
163 fn set_selected_index(&mut self, ix: usize, cx: &mut ViewContext<Picker<Self>>) {
164 self.selected_ix = ix.min(self.entries.len().saturating_sub(1));
165 cx.notify();
166 }
167
168 fn placeholder_text(&self, _cx: &mut WindowContext) -> Arc<str> {
169 "Select a context source…".into()
170 }
171
172 fn update_matches(&mut self, _query: String, _cx: &mut ViewContext<Picker<Self>>) -> Task<()> {
173 Task::ready(())
174 }
175
176 fn confirm(&mut self, _secondary: bool, cx: &mut ViewContext<Picker<Self>>) {
177 if let Some(entry) = self.entries.get(self.selected_ix) {
178 self.context_picker
179 .update(cx, |this, cx| {
180 match entry.kind {
181 ContextKind::File => {
182 this.mode = ContextPickerMode::File(cx.new_view(|cx| {
183 FileContextPicker::new(
184 self.context_picker.clone(),
185 self.workspace.clone(),
186 self.context_store.clone(),
187 self.confirm_behavior,
188 cx,
189 )
190 }));
191 }
192 ContextKind::Directory => {
193 this.mode = ContextPickerMode::Directory(cx.new_view(|cx| {
194 DirectoryContextPicker::new(
195 self.context_picker.clone(),
196 self.workspace.clone(),
197 self.context_store.clone(),
198 self.confirm_behavior,
199 cx,
200 )
201 }));
202 }
203 ContextKind::FetchedUrl => {
204 this.mode = ContextPickerMode::Fetch(cx.new_view(|cx| {
205 FetchContextPicker::new(
206 self.context_picker.clone(),
207 self.workspace.clone(),
208 self.context_store.clone(),
209 self.confirm_behavior,
210 cx,
211 )
212 }));
213 }
214 ContextKind::Thread => {
215 if let Some(thread_store) = self.thread_store.as_ref() {
216 this.mode = ContextPickerMode::Thread(cx.new_view(|cx| {
217 ThreadContextPicker::new(
218 thread_store.clone(),
219 self.context_picker.clone(),
220 self.context_store.clone(),
221 self.confirm_behavior,
222 cx,
223 )
224 }));
225 }
226 }
227 }
228
229 cx.focus_self();
230 })
231 .log_err();
232 }
233 }
234
235 fn dismissed(&mut self, cx: &mut ViewContext<Picker<Self>>) {
236 self.context_picker
237 .update(cx, |this, cx| match this.mode {
238 ContextPickerMode::Default => cx.emit(DismissEvent),
239 ContextPickerMode::File(_)
240 | ContextPickerMode::Directory(_)
241 | ContextPickerMode::Fetch(_)
242 | ContextPickerMode::Thread(_) => {}
243 })
244 .log_err();
245 }
246
247 fn render_match(
248 &self,
249 ix: usize,
250 selected: bool,
251 _cx: &mut ViewContext<Picker<Self>>,
252 ) -> Option<Self::ListItem> {
253 let entry = &self.entries[ix];
254
255 Some(
256 ListItem::new(ix)
257 .inset(true)
258 .spacing(ListItemSpacing::Dense)
259 .toggle_state(selected)
260 .child(
261 h_flex()
262 .min_w(px(250.))
263 .max_w(px(400.))
264 .gap_2()
265 .child(Icon::new(entry.icon).size(IconSize::Small))
266 .child(Label::new(entry.name.clone()).single_line()),
267 ),
268 )
269 }
270}