Show contacts popover when clicking on menu bar extra

Antonio Scandurra created

Change summary

crates/collab_ui/src/active_call_popover.rs  | 40 ----------------------
crates/collab_ui/src/collab_titlebar_item.rs |  3 +
crates/collab_ui/src/collab_ui.rs            |  3 -
crates/collab_ui/src/contact_list.rs         | 14 +++---
crates/collab_ui/src/contacts_popover.rs     | 37 +++++++++++++++----
crates/collab_ui/src/menu_bar_extra.rs       | 31 +++++++++-------
6 files changed, 56 insertions(+), 72 deletions(-)

Detailed changes

crates/collab_ui/src/active_call_popover.rs 🔗

@@ -1,40 +0,0 @@
-use gpui::{color::Color, elements::*, Entity, RenderContext, View, ViewContext};
-
-pub enum Event {
-    Deactivated,
-}
-
-pub struct ActiveCallPopover {
-    _subscription: gpui::Subscription,
-}
-
-impl Entity for ActiveCallPopover {
-    type Event = Event;
-}
-
-impl View for ActiveCallPopover {
-    fn ui_name() -> &'static str {
-        "ActiveCallPopover"
-    }
-
-    fn render(&mut self, _: &mut RenderContext<Self>) -> ElementBox {
-        Empty::new()
-            .contained()
-            .with_background_color(Color::red())
-            .boxed()
-    }
-}
-
-impl ActiveCallPopover {
-    pub fn new(cx: &mut ViewContext<Self>) -> Self {
-        Self {
-            _subscription: cx.observe_window_activation(Self::window_activation_changed),
-        }
-    }
-
-    fn window_activation_changed(&mut self, is_active: bool, cx: &mut ViewContext<Self>) {
-        if !is_active {
-            cx.emit(Event::Deactivated);
-        }
-    }
-}

crates/collab_ui/src/collab_titlebar_item.rs 🔗

@@ -163,7 +163,8 @@ impl CollabTitlebarItem {
                 if let Some(workspace) = self.workspace.upgrade(cx) {
                     let project = workspace.read(cx).project().clone();
                     let user_store = workspace.read(cx).user_store().clone();
-                    let view = cx.add_view(|cx| ContactsPopover::new(project, user_store, cx));
+                    let view = cx
+                        .add_view(|cx| ContactsPopover::new(false, Some(project), user_store, cx));
                     cx.focus(&view);
                     cx.subscribe(&view, |this, _, event, cx| {
                         match event {

crates/collab_ui/src/collab_ui.rs 🔗

@@ -1,4 +1,3 @@
-mod active_call_popover;
 mod collab_titlebar_item;
 mod contact_finder;
 mod contact_list;
@@ -23,7 +22,7 @@ pub fn init(app_state: Arc<AppState>, cx: &mut MutableAppContext) {
     contact_finder::init(cx);
     contacts_popover::init(cx);
     incoming_call_notification::init(cx);
-    menu_bar_extra::init(cx);
+    menu_bar_extra::init(app_state.user_store.clone(), cx);
     project_shared_notification::init(cx);
 
     cx.add_global_action(move |action: &JoinProject, cx| {

crates/collab_ui/src/contact_list.rs 🔗

@@ -114,7 +114,7 @@ pub struct ContactList {
     entries: Vec<ContactEntry>,
     match_candidates: Vec<StringMatchCandidate>,
     list_state: ListState,
-    project: ModelHandle<Project>,
+    project: Option<ModelHandle<Project>>,
     user_store: ModelHandle<UserStore>,
     filter_editor: ViewHandle<Editor>,
     collapsed_sections: Vec<Section>,
@@ -124,7 +124,7 @@ pub struct ContactList {
 
 impl ContactList {
     pub fn new(
-        project: ModelHandle<Project>,
+        project: Option<ModelHandle<Project>>,
         user_store: ModelHandle<UserStore>,
         cx: &mut ViewContext<Self>,
     ) -> Self {
@@ -195,7 +195,7 @@ impl ContactList {
                 ),
                 ContactEntry::Contact(contact) => Self::render_contact(
                     contact,
-                    &this.project,
+                    this.project.as_ref(),
                     &theme.contact_list,
                     is_selected,
                     cx,
@@ -292,7 +292,7 @@ impl ContactList {
                             self.call(
                                 &Call {
                                     recipient_user_id: contact.user.id,
-                                    initial_project: Some(self.project.clone()),
+                                    initial_project: self.project.clone(),
                                 },
                                 cx,
                             );
@@ -664,7 +664,7 @@ impl ContactList {
 
     fn render_contact(
         contact: &Contact,
-        project: &ModelHandle<Project>,
+        project: Option<&ModelHandle<Project>>,
         theme: &theme::ContactList,
         is_selected: bool,
         cx: &mut RenderContext<Self>,
@@ -672,7 +672,7 @@ impl ContactList {
         let online = contact.online;
         let busy = contact.busy;
         let user_id = contact.user.id;
-        let initial_project = project.clone();
+        let initial_project = project.cloned();
         let mut element =
             MouseEventHandler::<Contact>::new(contact.user.id as usize, cx, |_, _| {
                 Flex::row()
@@ -726,7 +726,7 @@ impl ContactList {
                 if online && !busy {
                     cx.dispatch_action(Call {
                         recipient_user_id: user_id,
-                        initial_project: Some(initial_project.clone()),
+                        initial_project: initial_project.clone(),
                     });
                 }
             });

crates/collab_ui/src/contacts_popover.rs 🔗

@@ -23,30 +23,41 @@ enum Child {
 }
 
 pub struct ContactsPopover {
+    is_popup: bool,
     child: Child,
-    project: ModelHandle<Project>,
+    project: Option<ModelHandle<Project>>,
     user_store: ModelHandle<UserStore>,
     _subscription: Option<gpui::Subscription>,
+    _window_subscription: gpui::Subscription,
 }
 
 impl ContactsPopover {
     pub fn new(
-        project: ModelHandle<Project>,
+        is_popup: bool,
+        project: Option<ModelHandle<Project>>,
         user_store: ModelHandle<UserStore>,
         cx: &mut ViewContext<Self>,
     ) -> Self {
         let mut this = Self {
+            is_popup,
             child: Child::ContactList(
                 cx.add_view(|cx| ContactList::new(project.clone(), user_store.clone(), cx)),
             ),
             project,
             user_store,
             _subscription: None,
+            _window_subscription: cx.observe_window_activation(Self::window_activation_changed),
         };
         this.show_contact_list(cx);
         this
     }
 
+    fn window_activation_changed(&mut self, active: bool, cx: &mut ViewContext<Self>) {
+        if !active {
+            cx.emit(Event::Dismissed);
+        }
+    }
+
     fn toggle_contact_finder(&mut self, _: &ToggleContactFinder, cx: &mut ViewContext<Self>) {
         match &self.child {
             Child::ContactList(_) => self.show_contact_finder(cx),
@@ -92,13 +103,21 @@ impl View for ContactsPopover {
             Child::ContactFinder(child) => ChildView::new(child),
         };
 
-        child
-            .contained()
-            .with_style(theme.contacts_popover.container)
-            .constrained()
-            .with_width(theme.contacts_popover.width)
-            .with_height(theme.contacts_popover.height)
-            .boxed()
+        let mut container_style = theme.contacts_popover.container;
+        if self.is_popup {
+            container_style.shadow = Default::default();
+            container_style.border = Default::default();
+            container_style.corner_radius = Default::default();
+            child.contained().with_style(container_style).boxed()
+        } else {
+            child
+                .contained()
+                .with_style(container_style)
+                .constrained()
+                .with_width(theme.contacts_popover.width)
+                .with_height(theme.contacts_popover.height)
+                .boxed()
+        }
     }
 
     fn on_focus_in(&mut self, _: gpui::AnyViewHandle, cx: &mut ViewContext<Self>) {

crates/collab_ui/src/menu_bar_extra.rs 🔗

@@ -1,17 +1,18 @@
-use crate::active_call_popover::{self, ActiveCallPopover};
+use crate::contacts_popover::{self, ContactsPopover};
 use call::ActiveCall;
+use client::UserStore;
 use gpui::{
     actions,
     color::Color,
     elements::*,
     geometry::{rect::RectF, vector::vec2f},
-    Appearance, Entity, MouseButton, MutableAppContext, RenderContext, View, ViewContext,
-    ViewHandle, WindowKind,
+    Appearance, Entity, ModelHandle, MouseButton, MutableAppContext, RenderContext, View,
+    ViewContext, ViewHandle, WindowKind,
 };
 
 actions!(menu_bar_extra, [ToggleActiveCallPopover]);
 
-pub fn init(cx: &mut MutableAppContext) {
+pub fn init(user_store: ModelHandle<UserStore>, cx: &mut MutableAppContext) {
     cx.add_action(MenuBarExtra::toggle_active_call_popover);
 
     let mut status_bar_item_id = None;
@@ -24,7 +25,7 @@ pub fn init(cx: &mut MutableAppContext) {
             }
 
             if has_room {
-                let (id, _) = cx.add_status_bar_item(|_| MenuBarExtra::new());
+                let (id, _) = cx.add_status_bar_item(|_| MenuBarExtra::new(user_store.clone()));
                 status_bar_item_id = Some(id);
             }
         }
@@ -33,7 +34,8 @@ pub fn init(cx: &mut MutableAppContext) {
 }
 
 struct MenuBarExtra {
-    popover: Option<ViewHandle<ActiveCallPopover>>,
+    popover: Option<ViewHandle<ContactsPopover>>,
+    user_store: ModelHandle<UserStore>,
 }
 
 impl Entity for MenuBarExtra {
@@ -70,8 +72,11 @@ impl View for MenuBarExtra {
 }
 
 impl MenuBarExtra {
-    fn new() -> Self {
-        Self { popover: None }
+    fn new(user_store: ModelHandle<UserStore>) -> Self {
+        Self {
+            popover: None,
+            user_store,
+        }
     }
 
     fn toggle_active_call_popover(
@@ -85,7 +90,7 @@ impl MenuBarExtra {
             }
             None => {
                 let window_bounds = cx.window_bounds();
-                let size = vec2f(360., 460.);
+                let size = vec2f(300., 350.);
                 let origin = window_bounds.lower_left()
                     + vec2f(window_bounds.width() / 2. - size.x() / 2., 0.);
                 let (_, popover) = cx.add_window(
@@ -96,7 +101,7 @@ impl MenuBarExtra {
                         kind: WindowKind::PopUp,
                         is_movable: false,
                     },
-                    |cx| ActiveCallPopover::new(cx),
+                    |cx| ContactsPopover::new(true, None, self.user_store.clone(), cx),
                 );
                 cx.subscribe(&popover, Self::on_popover_event).detach();
                 self.popover = Some(popover);
@@ -106,12 +111,12 @@ impl MenuBarExtra {
 
     fn on_popover_event(
         &mut self,
-        popover: ViewHandle<ActiveCallPopover>,
-        event: &active_call_popover::Event,
+        popover: ViewHandle<ContactsPopover>,
+        event: &contacts_popover::Event,
         cx: &mut ViewContext<Self>,
     ) {
         match event {
-            active_call_popover::Event::Deactivated => {
+            contacts_popover::Event::Dismissed => {
                 self.popover.take();
                 cx.remove_window(popover.window_id());
             }