2022-05-10 13:24:14 +00:00
|
|
|
use client::{ContactRequestStatus, User, UserStore};
|
|
|
|
use gpui::{
|
2022-08-08 02:23:22 +00:00
|
|
|
actions, elements::*, AnyViewHandle, Entity, ModelHandle, MouseState, MutableAppContext,
|
|
|
|
RenderContext, Task, View, ViewContext, ViewHandle,
|
2022-05-10 13:24:14 +00:00
|
|
|
};
|
2022-05-10 14:43:51 +00:00
|
|
|
use picker::{Picker, PickerDelegate};
|
2022-05-10 13:24:14 +00:00
|
|
|
use settings::Settings;
|
|
|
|
use std::sync::Arc;
|
|
|
|
use util::TryFutureExt;
|
2022-05-10 14:43:51 +00:00
|
|
|
use workspace::Workspace;
|
2022-05-10 13:24:14 +00:00
|
|
|
|
2022-05-10 17:47:25 +00:00
|
|
|
use crate::render_icon_button;
|
|
|
|
|
2022-05-10 14:43:51 +00:00
|
|
|
actions!(contact_finder, [Toggle]);
|
|
|
|
|
|
|
|
pub fn init(cx: &mut MutableAppContext) {
|
|
|
|
Picker::<ContactFinder>::init(cx);
|
|
|
|
cx.add_action(ContactFinder::toggle);
|
|
|
|
}
|
2022-05-10 13:24:14 +00:00
|
|
|
|
|
|
|
pub struct ContactFinder {
|
2022-05-10 14:43:51 +00:00
|
|
|
picker: ViewHandle<Picker<Self>>,
|
2022-05-10 13:24:14 +00:00
|
|
|
potential_contacts: Arc<[Arc<User>]>,
|
|
|
|
user_store: ModelHandle<UserStore>,
|
2022-05-10 14:43:51 +00:00
|
|
|
selected_index: usize,
|
|
|
|
}
|
|
|
|
|
|
|
|
pub enum Event {
|
|
|
|
Dismissed,
|
2022-05-10 13:24:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Entity for ContactFinder {
|
2022-05-10 14:43:51 +00:00
|
|
|
type Event = Event;
|
2022-05-10 13:24:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl View for ContactFinder {
|
|
|
|
fn ui_name() -> &'static str {
|
|
|
|
"ContactFinder"
|
|
|
|
}
|
|
|
|
|
2022-05-10 14:43:51 +00:00
|
|
|
fn render(&mut self, _: &mut RenderContext<Self>) -> ElementBox {
|
|
|
|
ChildView::new(self.picker.clone()).boxed()
|
|
|
|
}
|
|
|
|
|
2022-08-08 02:23:22 +00:00
|
|
|
fn on_focus_in(&mut self, _: AnyViewHandle, cx: &mut ViewContext<Self>) {
|
2022-08-17 15:33:48 +00:00
|
|
|
if cx.is_self_focused() {
|
|
|
|
cx.focus(&self.picker);
|
|
|
|
}
|
2022-05-10 13:24:14 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-10 14:43:51 +00:00
|
|
|
impl PickerDelegate for ContactFinder {
|
|
|
|
fn match_count(&self) -> usize {
|
|
|
|
self.potential_contacts.len()
|
|
|
|
}
|
2022-05-10 13:24:14 +00:00
|
|
|
|
2022-05-10 14:43:51 +00:00
|
|
|
fn selected_index(&self) -> usize {
|
|
|
|
self.selected_index
|
|
|
|
}
|
|
|
|
|
|
|
|
fn set_selected_index(&mut self, ix: usize, _: &mut ViewContext<Self>) {
|
|
|
|
self.selected_index = ix;
|
|
|
|
}
|
|
|
|
|
|
|
|
fn update_matches(&mut self, query: String, cx: &mut ViewContext<Self>) -> 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(())
|
2022-05-10 13:24:14 +00:00
|
|
|
}
|
2022-05-10 14:43:51 +00:00
|
|
|
.log_err()
|
|
|
|
.await;
|
2022-05-10 13:24:14 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-05-10 14:43:51 +00:00
|
|
|
fn confirm(&mut self, cx: &mut ViewContext<Self>) {
|
|
|
|
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();
|
|
|
|
}
|
|
|
|
_ => {}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-05-10 13:24:14 +00:00
|
|
|
|
2022-05-10 14:43:51 +00:00
|
|
|
fn dismiss(&mut self, cx: &mut ViewContext<Self>) {
|
|
|
|
cx.emit(Event::Dismissed);
|
|
|
|
}
|
2022-05-10 13:24:14 +00:00
|
|
|
|
2022-05-10 14:43:51 +00:00
|
|
|
fn render_match(
|
|
|
|
&self,
|
|
|
|
ix: usize,
|
2022-05-27 02:00:01 +00:00
|
|
|
mouse_state: MouseState,
|
2022-05-10 14:43:51 +00:00
|
|
|
selected: bool,
|
|
|
|
cx: &gpui::AppContext,
|
|
|
|
) -> ElementBox {
|
|
|
|
let theme = &cx.global::<Settings>().theme;
|
2022-05-10 16:25:47 +00:00
|
|
|
let user = &self.potential_contacts[ix];
|
2022-08-10 21:39:24 +00:00
|
|
|
let request_status = self.user_store.read(cx).contact_request_status(user);
|
2022-05-10 16:25:47 +00:00
|
|
|
|
|
|
|
let icon_path = match request_status {
|
|
|
|
ContactRequestStatus::None | ContactRequestStatus::RequestReceived => {
|
2022-07-19 21:11:15 +00:00
|
|
|
"icons/check_8.svg"
|
2022-05-10 16:25:47 +00:00
|
|
|
}
|
|
|
|
ContactRequestStatus::RequestSent | ContactRequestStatus::RequestAccepted => {
|
2022-07-19 21:11:15 +00:00
|
|
|
"icons/x_mark_8.svg"
|
2022-05-10 16:25:47 +00:00
|
|
|
}
|
|
|
|
};
|
2022-08-10 21:39:24 +00:00
|
|
|
let button_style = if self.user_store.read(cx).is_contact_request_pending(user) {
|
2022-05-10 16:25:47 +00:00
|
|
|
&theme.contact_finder.disabled_contact_button
|
|
|
|
} else {
|
|
|
|
&theme.contact_finder.contact_button
|
2022-05-10 14:43:51 +00:00
|
|
|
};
|
|
|
|
let style = theme.picker.item.style_for(mouse_state, selected);
|
2022-05-10 13:24:14 +00:00
|
|
|
Flex::row()
|
2022-05-10 16:25:47 +00:00
|
|
|
.with_children(user.avatar.clone().map(|avatar| {
|
2022-05-10 13:24:14 +00:00
|
|
|
Image::new(avatar)
|
2022-05-10 14:43:51 +00:00
|
|
|
.with_style(theme.contact_finder.contact_avatar)
|
2022-05-10 13:24:14 +00:00
|
|
|
.aligned()
|
|
|
|
.left()
|
|
|
|
.boxed()
|
|
|
|
}))
|
2022-05-10 14:43:51 +00:00
|
|
|
.with_child(
|
2022-05-10 16:25:47 +00:00
|
|
|
Label::new(user.github_login.clone(), style.label.clone())
|
2022-05-10 14:43:51 +00:00
|
|
|
.contained()
|
|
|
|
.with_style(theme.contact_finder.contact_username)
|
|
|
|
.aligned()
|
|
|
|
.left()
|
|
|
|
.boxed(),
|
|
|
|
)
|
2022-05-10 13:24:14 +00:00
|
|
|
.with_child(
|
2022-05-10 17:47:25 +00:00
|
|
|
render_icon_button(button_style, icon_path)
|
2022-05-10 16:25:47 +00:00
|
|
|
.aligned()
|
|
|
|
.flex_float()
|
|
|
|
.boxed(),
|
2022-05-10 13:24:14 +00:00
|
|
|
)
|
2022-05-10 14:43:51 +00:00
|
|
|
.contained()
|
|
|
|
.with_style(style.container)
|
2022-05-10 13:24:14 +00:00
|
|
|
.constrained()
|
2022-05-10 14:43:51 +00:00
|
|
|
.with_height(theme.contact_finder.row_height)
|
2022-05-10 13:24:14 +00:00
|
|
|
.boxed()
|
|
|
|
}
|
2022-05-10 14:43:51 +00:00
|
|
|
}
|
2022-05-10 13:24:14 +00:00
|
|
|
|
2022-05-10 14:43:51 +00:00
|
|
|
impl ContactFinder {
|
|
|
|
fn toggle(workspace: &mut Workspace, _: &Toggle, cx: &mut ViewContext<Workspace>) {
|
2022-05-10 22:46:53 +00:00
|
|
|
workspace.toggle_modal(cx, |workspace, cx| {
|
2022-05-10 14:43:51 +00:00
|
|
|
let finder = cx.add_view(|cx| Self::new(workspace.user_store().clone(), cx));
|
|
|
|
cx.subscribe(&finder, Self::on_event).detach();
|
|
|
|
finder
|
|
|
|
});
|
|
|
|
}
|
2022-05-10 13:24:14 +00:00
|
|
|
|
2022-05-10 14:43:51 +00:00
|
|
|
pub fn new(user_store: ModelHandle<UserStore>, cx: &mut ViewContext<Self>) -> 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<Self>,
|
|
|
|
event: &Event,
|
|
|
|
cx: &mut ViewContext<Workspace>,
|
|
|
|
) {
|
|
|
|
match event {
|
|
|
|
Event::Dismissed => {
|
|
|
|
workspace.dismiss_modal(cx);
|
2022-05-10 13:24:14 +00:00
|
|
|
}
|
2022-05-10 14:43:51 +00:00
|
|
|
}
|
2022-05-10 13:24:14 +00:00
|
|
|
}
|
|
|
|
}
|