use client::{ContactRequestStatus, User, UserStore}; use gpui::{ actions, elements::*, AnyViewHandle, Entity, ModelHandle, MouseState, MutableAppContext, RenderContext, Task, View, ViewContext, ViewHandle, }; use picker::{Picker, PickerDelegate}; use settings::Settings; use std::sync::Arc; use util::TryFutureExt; use workspace::Workspace; use crate::render_icon_button; actions!(contact_finder, [Toggle]); pub fn init(cx: &mut MutableAppContext) { Picker::::init(cx); cx.add_action(ContactFinder::toggle); } pub struct ContactFinder { picker: ViewHandle>, potential_contacts: Arc<[Arc]>, user_store: ModelHandle, selected_index: usize, } pub enum Event { Dismissed, } impl Entity for ContactFinder { type Event = Event; } impl View for ContactFinder { fn ui_name() -> &'static str { "ContactFinder" } fn render(&mut self, _: &mut RenderContext) -> ElementBox { ChildView::new(self.picker.clone()).boxed() } fn on_focus_in(&mut self, _: AnyViewHandle, cx: &mut ViewContext) { if cx.is_self_focused() { cx.focus(&self.picker); } } } impl PickerDelegate for ContactFinder { fn match_count(&self) -> usize { self.potential_contacts.len() } fn selected_index(&self) -> usize { self.selected_index } fn set_selected_index(&mut self, ix: usize, _: &mut ViewContext) { self.selected_index = ix; } fn update_matches(&mut self, query: String, cx: &mut ViewContext) -> Task<()> { let search_users = self .user_store .update(cx, |store, cx| store.fuzzy_search_users(query, cx)); cx.spawn(|this, mut cx| async move { async { let potential_contacts = search_users.await?; this.update(&mut cx, |this, cx| { this.potential_contacts = potential_contacts.into(); cx.notify(); }); Ok(()) } .log_err() .await; }) } fn confirm(&mut self, cx: &mut ViewContext) { if let Some(user) = self.potential_contacts.get(self.selected_index) { let user_store = self.user_store.read(cx); match user_store.contact_request_status(user) { ContactRequestStatus::None | ContactRequestStatus::RequestReceived => { self.user_store .update(cx, |store, cx| store.request_contact(user.id, cx)) .detach(); } ContactRequestStatus::RequestSent => { self.user_store .update(cx, |store, cx| store.remove_contact(user.id, cx)) .detach(); } _ => {} } } } fn dismiss(&mut self, cx: &mut ViewContext) { cx.emit(Event::Dismissed); } fn render_match( &self, ix: usize, mouse_state: MouseState, selected: bool, cx: &gpui::AppContext, ) -> ElementBox { let theme = &cx.global::().theme; let user = &self.potential_contacts[ix]; let request_status = self.user_store.read(cx).contact_request_status(user); let icon_path = match request_status { ContactRequestStatus::None | ContactRequestStatus::RequestReceived => { "icons/check_8.svg" } ContactRequestStatus::RequestSent | ContactRequestStatus::RequestAccepted => { "icons/x_mark_8.svg" } }; let button_style = if self.user_store.read(cx).is_contact_request_pending(user) { &theme.contact_finder.disabled_contact_button } else { &theme.contact_finder.contact_button }; let style = theme.picker.item.style_for(mouse_state, selected); Flex::row() .with_children(user.avatar.clone().map(|avatar| { Image::new(avatar) .with_style(theme.contact_finder.contact_avatar) .aligned() .left() .boxed() })) .with_child( Label::new(user.github_login.clone(), style.label.clone()) .contained() .with_style(theme.contact_finder.contact_username) .aligned() .left() .boxed(), ) .with_child( render_icon_button(button_style, icon_path) .aligned() .flex_float() .boxed(), ) .contained() .with_style(style.container) .constrained() .with_height(theme.contact_finder.row_height) .boxed() } } impl ContactFinder { fn toggle(workspace: &mut Workspace, _: &Toggle, cx: &mut ViewContext) { workspace.toggle_modal(cx, |workspace, cx| { let finder = cx.add_view(|cx| Self::new(workspace.user_store().clone(), cx)); cx.subscribe(&finder, Self::on_event).detach(); finder }); } pub fn new(user_store: ModelHandle, cx: &mut ViewContext) -> Self { let this = cx.weak_handle(); Self { picker: cx.add_view(|cx| Picker::new(this, cx)), potential_contacts: Arc::from([]), user_store, selected_index: 0, } } fn on_event( workspace: &mut Workspace, _: ViewHandle, event: &Event, cx: &mut ViewContext, ) { match event { Event::Dismissed => { workspace.dismiss_modal(cx); } } } }