contact_finder.rs

  1use client::{ContactRequestStatus, User, UserStore};
  2use gpui::{
  3    actions, elements::*, AnyViewHandle, Entity, ModelHandle, MouseState, MutableAppContext,
  4    RenderContext, Task, View, ViewContext, ViewHandle,
  5};
  6use picker::{Picker, PickerDelegate};
  7use settings::Settings;
  8use std::sync::Arc;
  9use util::TryFutureExt;
 10use workspace::Workspace;
 11
 12actions!(contact_finder, [Toggle]);
 13
 14pub fn init(cx: &mut MutableAppContext) {
 15    Picker::<ContactFinder>::init(cx);
 16    cx.add_action(ContactFinder::toggle);
 17}
 18
 19pub struct ContactFinder {
 20    picker: ViewHandle<Picker<Self>>,
 21    potential_contacts: Arc<[Arc<User>]>,
 22    user_store: ModelHandle<UserStore>,
 23    selected_index: usize,
 24}
 25
 26pub enum Event {
 27    Dismissed,
 28}
 29
 30impl Entity for ContactFinder {
 31    type Event = Event;
 32}
 33
 34impl View for ContactFinder {
 35    fn ui_name() -> &'static str {
 36        "ContactFinder"
 37    }
 38
 39    fn render(&mut self, _: &mut RenderContext<Self>) -> ElementBox {
 40        ChildView::new(self.picker.clone()).boxed()
 41    }
 42
 43    fn on_focus_in(&mut self, _: AnyViewHandle, cx: &mut ViewContext<Self>) {
 44        if cx.is_self_focused() {
 45            cx.focus(&self.picker);
 46        }
 47    }
 48}
 49
 50impl PickerDelegate for ContactFinder {
 51    fn match_count(&self) -> usize {
 52        self.potential_contacts.len()
 53    }
 54
 55    fn selected_index(&self) -> usize {
 56        self.selected_index
 57    }
 58
 59    fn set_selected_index(&mut self, ix: usize, _: &mut ViewContext<Self>) {
 60        self.selected_index = ix;
 61    }
 62
 63    fn update_matches(&mut self, query: String, cx: &mut ViewContext<Self>) -> Task<()> {
 64        let search_users = self
 65            .user_store
 66            .update(cx, |store, cx| store.fuzzy_search_users(query, cx));
 67
 68        cx.spawn(|this, mut cx| async move {
 69            async {
 70                let potential_contacts = search_users.await?;
 71                this.update(&mut cx, |this, cx| {
 72                    this.potential_contacts = potential_contacts.into();
 73                    cx.notify();
 74                });
 75                Ok(())
 76            }
 77            .log_err()
 78            .await;
 79        })
 80    }
 81
 82    fn confirm(&mut self, cx: &mut ViewContext<Self>) {
 83        if let Some(user) = self.potential_contacts.get(self.selected_index) {
 84            let user_store = self.user_store.read(cx);
 85            match user_store.contact_request_status(user) {
 86                ContactRequestStatus::None | ContactRequestStatus::RequestReceived => {
 87                    self.user_store
 88                        .update(cx, |store, cx| store.request_contact(user.id, cx))
 89                        .detach();
 90                }
 91                ContactRequestStatus::RequestSent => {
 92                    self.user_store
 93                        .update(cx, |store, cx| store.remove_contact(user.id, cx))
 94                        .detach();
 95                }
 96                _ => {}
 97            }
 98        }
 99    }
100
101    fn dismiss(&mut self, cx: &mut ViewContext<Self>) {
102        cx.emit(Event::Dismissed);
103    }
104
105    fn render_match(
106        &self,
107        ix: usize,
108        mouse_state: MouseState,
109        selected: bool,
110        cx: &gpui::AppContext,
111    ) -> ElementBox {
112        let theme = &cx.global::<Settings>().theme;
113        let user = &self.potential_contacts[ix];
114        let request_status = self.user_store.read(cx).contact_request_status(user);
115
116        let icon_path = match request_status {
117            ContactRequestStatus::None | ContactRequestStatus::RequestReceived => {
118                Some("icons/check_8.svg")
119            }
120            ContactRequestStatus::RequestSent => Some("icons/x_mark_8.svg"),
121            ContactRequestStatus::RequestAccepted => None,
122        };
123        let button_style = if self.user_store.read(cx).is_contact_request_pending(user) {
124            &theme.contact_finder.disabled_contact_button
125        } else {
126            &theme.contact_finder.contact_button
127        };
128        let style = theme.picker.item.style_for(mouse_state, selected);
129        Flex::row()
130            .with_children(user.avatar.clone().map(|avatar| {
131                Image::new(avatar)
132                    .with_style(theme.contact_finder.contact_avatar)
133                    .aligned()
134                    .left()
135                    .boxed()
136            }))
137            .with_child(
138                Label::new(user.github_login.clone(), style.label.clone())
139                    .contained()
140                    .with_style(theme.contact_finder.contact_username)
141                    .aligned()
142                    .left()
143                    .boxed(),
144            )
145            .with_children(icon_path.map(|icon_path| {
146                Svg::new(icon_path)
147                    .with_color(button_style.color)
148                    .constrained()
149                    .with_width(button_style.icon_width)
150                    .aligned()
151                    .contained()
152                    .with_style(button_style.container)
153                    .constrained()
154                    .with_width(button_style.button_width)
155                    .with_height(button_style.button_width)
156                    .aligned()
157                    .flex_float()
158                    .boxed()
159            }))
160            .contained()
161            .with_style(style.container)
162            .constrained()
163            .with_height(theme.contact_finder.row_height)
164            .boxed()
165    }
166}
167
168impl ContactFinder {
169    fn toggle(workspace: &mut Workspace, _: &Toggle, cx: &mut ViewContext<Workspace>) {
170        workspace.toggle_modal(cx, |workspace, cx| {
171            let finder = cx.add_view(|cx| Self::new(workspace.user_store().clone(), cx));
172            cx.subscribe(&finder, Self::on_event).detach();
173            finder
174        });
175    }
176
177    pub fn new(user_store: ModelHandle<UserStore>, cx: &mut ViewContext<Self>) -> Self {
178        let this = cx.weak_handle();
179        Self {
180            picker: cx.add_view(|cx| Picker::new(this, cx)),
181            potential_contacts: Arc::from([]),
182            user_store,
183            selected_index: 0,
184        }
185    }
186
187    fn on_event(
188        workspace: &mut Workspace,
189        _: ViewHandle<Self>,
190        event: &Event,
191        cx: &mut ViewContext<Workspace>,
192    ) {
193        match event {
194            Event::Dismissed => {
195                workspace.dismiss_modal(cx);
196            }
197        }
198    }
199}