thread_context_picker.rs

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