thread_context_picker.rs

  1use std::sync::Arc;
  2
  3use fuzzy::StringMatchCandidate;
  4use gpui::{App, DismissEvent, Entity, FocusHandle, Focusable, Task, WeakEntity};
  5use picker::{Picker, PickerDelegate};
  6use ui::{ListItem, prelude::*};
  7
  8use crate::context_picker::{ConfirmBehavior, ContextPicker};
  9use crate::context_store::{self, ContextStore};
 10use crate::thread::ThreadId;
 11use crate::thread_store::ThreadStore;
 12
 13pub struct ThreadContextPicker {
 14    picker: Entity<Picker<ThreadContextPickerDelegate>>,
 15}
 16
 17impl ThreadContextPicker {
 18    pub fn new(
 19        thread_store: WeakEntity<ThreadStore>,
 20        context_picker: WeakEntity<ContextPicker>,
 21        context_store: WeakEntity<context_store::ContextStore>,
 22        confirm_behavior: ConfirmBehavior,
 23        window: &mut Window,
 24        cx: &mut Context<Self>,
 25    ) -> Self {
 26        let delegate = ThreadContextPickerDelegate::new(
 27            thread_store,
 28            context_picker,
 29            context_store,
 30            confirm_behavior,
 31        );
 32        let picker = cx.new(|cx| Picker::uniform_list(delegate, window, cx));
 33
 34        ThreadContextPicker { picker }
 35    }
 36}
 37
 38impl Focusable for ThreadContextPicker {
 39    fn focus_handle(&self, cx: &App) -> FocusHandle {
 40        self.picker.focus_handle(cx)
 41    }
 42}
 43
 44impl Render for ThreadContextPicker {
 45    fn render(&mut self, _window: &mut Window, _cx: &mut Context<Self>) -> impl IntoElement {
 46        self.picker.clone()
 47    }
 48}
 49
 50#[derive(Debug, Clone)]
 51pub struct ThreadContextEntry {
 52    pub id: ThreadId,
 53    pub summary: SharedString,
 54}
 55
 56pub struct ThreadContextPickerDelegate {
 57    thread_store: WeakEntity<ThreadStore>,
 58    context_picker: WeakEntity<ContextPicker>,
 59    context_store: WeakEntity<context_store::ContextStore>,
 60    confirm_behavior: ConfirmBehavior,
 61    matches: Vec<ThreadContextEntry>,
 62    selected_index: usize,
 63}
 64
 65impl ThreadContextPickerDelegate {
 66    pub fn new(
 67        thread_store: WeakEntity<ThreadStore>,
 68        context_picker: WeakEntity<ContextPicker>,
 69        context_store: WeakEntity<context_store::ContextStore>,
 70        confirm_behavior: ConfirmBehavior,
 71    ) -> Self {
 72        ThreadContextPickerDelegate {
 73            thread_store,
 74            context_picker,
 75            context_store,
 76            confirm_behavior,
 77            matches: Vec::new(),
 78            selected_index: 0,
 79        }
 80    }
 81}
 82
 83impl PickerDelegate for ThreadContextPickerDelegate {
 84    type ListItem = ListItem;
 85
 86    fn match_count(&self) -> usize {
 87        self.matches.len()
 88    }
 89
 90    fn selected_index(&self) -> usize {
 91        self.selected_index
 92    }
 93
 94    fn set_selected_index(
 95        &mut self,
 96        ix: usize,
 97        _window: &mut Window,
 98        _cx: &mut Context<Picker<Self>>,
 99    ) {
100        self.selected_index = ix;
101    }
102
103    fn placeholder_text(&self, _window: &mut Window, _cx: &mut App) -> Arc<str> {
104        "Search threads…".into()
105    }
106
107    fn update_matches(
108        &mut self,
109        query: String,
110        window: &mut Window,
111        cx: &mut Context<Picker<Self>>,
112    ) -> Task<()> {
113        let Some(threads) = self.thread_store.upgrade() else {
114            return Task::ready(());
115        };
116
117        let search_task = search_threads(query, threads, cx);
118        cx.spawn_in(window, async move |this, cx| {
119            let matches = search_task.await;
120            this.update(cx, |this, cx| {
121                this.delegate.matches = matches;
122                this.delegate.selected_index = 0;
123                cx.notify();
124            })
125            .ok();
126        })
127    }
128
129    fn confirm(&mut self, _secondary: bool, window: &mut Window, cx: &mut Context<Picker<Self>>) {
130        let Some(entry) = self.matches.get(self.selected_index) else {
131            return;
132        };
133
134        let Some(thread_store) = self.thread_store.upgrade() else {
135            return;
136        };
137
138        let open_thread_task = thread_store.update(cx, |this, cx| this.open_thread(&entry.id, cx));
139
140        cx.spawn_in(window, async move |this, cx| {
141            let thread = open_thread_task.await?;
142            this.update_in(cx, |this, window, cx| {
143                this.delegate
144                    .context_store
145                    .update(cx, |context_store, cx| {
146                        context_store.add_thread(thread, true, cx)
147                    })
148                    .ok();
149
150                match this.delegate.confirm_behavior {
151                    ConfirmBehavior::KeepOpen => {}
152                    ConfirmBehavior::Close => this.delegate.dismissed(window, cx),
153                }
154            })
155        })
156        .detach_and_log_err(cx);
157    }
158
159    fn dismissed(&mut self, _window: &mut Window, cx: &mut Context<Picker<Self>>) {
160        self.context_picker
161            .update(cx, |_, cx| {
162                cx.emit(DismissEvent);
163            })
164            .ok();
165    }
166
167    fn render_match(
168        &self,
169        ix: usize,
170        selected: bool,
171        _window: &mut Window,
172        cx: &mut Context<Picker<Self>>,
173    ) -> Option<Self::ListItem> {
174        let thread = &self.matches[ix];
175
176        Some(ListItem::new(ix).inset(true).toggle_state(selected).child(
177            render_thread_context_entry(thread, self.context_store.clone(), cx),
178        ))
179    }
180}
181
182pub fn render_thread_context_entry(
183    thread: &ThreadContextEntry,
184    context_store: WeakEntity<ContextStore>,
185    cx: &mut App,
186) -> Div {
187    let added = context_store.upgrade().map_or(false, |ctx_store| {
188        ctx_store.read(cx).includes_thread(&thread.id).is_some()
189    });
190
191    h_flex()
192        .gap_1p5()
193        .w_full()
194        .justify_between()
195        .child(
196            h_flex()
197                .gap_1p5()
198                .max_w_72()
199                .child(
200                    Icon::new(IconName::MessageBubbles)
201                        .size(IconSize::XSmall)
202                        .color(Color::Muted),
203                )
204                .child(Label::new(thread.summary.clone()).truncate()),
205        )
206        .when(added, |el| {
207            el.child(
208                h_flex()
209                    .gap_1()
210                    .child(
211                        Icon::new(IconName::Check)
212                            .size(IconSize::Small)
213                            .color(Color::Success),
214                    )
215                    .child(Label::new("Added").size(LabelSize::Small)),
216            )
217        })
218}
219
220pub(crate) fn search_threads(
221    query: String,
222    thread_store: Entity<ThreadStore>,
223    cx: &mut App,
224) -> Task<Vec<ThreadContextEntry>> {
225    let threads = thread_store.update(cx, |this, _cx| {
226        this.threads()
227            .into_iter()
228            .map(|thread| ThreadContextEntry {
229                id: thread.id,
230                summary: thread.summary,
231            })
232            .collect::<Vec<_>>()
233    });
234
235    let executor = cx.background_executor().clone();
236    cx.background_spawn(async move {
237        if query.is_empty() {
238            threads
239        } else {
240            let candidates = threads
241                .iter()
242                .enumerate()
243                .map(|(id, thread)| StringMatchCandidate::new(id, &thread.summary))
244                .collect::<Vec<_>>();
245            let matches = fuzzy::match_strings(
246                &candidates,
247                &query,
248                false,
249                100,
250                &Default::default(),
251                executor,
252            )
253            .await;
254
255            matches
256                .into_iter()
257                .map(|mat| threads[mat.candidate_id].clone())
258                .collect()
259        }
260    })
261}