Merge pull request #2259 from zed-industries/preserve-contacts-editor-contents

Julia created

Preserve contacts popover editor contents when switching to search mode

Change summary

crates/collab_ui/src/contact_finder.rs   | 14 ++++++++++++--
crates/collab_ui/src/contact_list.rs     | 10 ++++++++++
crates/collab_ui/src/contacts_popover.rs | 22 ++++++++++++++--------
crates/picker/src/picker.rs              |  5 +++++
4 files changed, 41 insertions(+), 10 deletions(-)

Detailed changes

crates/collab_ui/src/contact_finder.rs 🔗

@@ -1,7 +1,7 @@
 use client::{ContactRequestStatus, User, UserStore};
 use gpui::{
-    elements::*, AnyViewHandle, Entity, ModelHandle, MouseState, MutableAppContext, RenderContext,
-    Task, View, ViewContext, ViewHandle,
+    elements::*, AnyViewHandle, AppContext, Entity, ModelHandle, MouseState, MutableAppContext,
+    RenderContext, Task, View, ViewContext, ViewHandle,
 };
 use picker::{Picker, PickerDelegate};
 use settings::Settings;
@@ -178,4 +178,14 @@ impl ContactFinder {
             selected_index: 0,
         }
     }
+
+    pub fn editor_text(&self, cx: &AppContext) -> String {
+        self.picker.read(cx).query(cx)
+    }
+
+    pub fn with_editor_text(self, editor_text: String, cx: &mut ViewContext<Self>) -> Self {
+        self.picker
+            .update(cx, |picker, cx| picker.set_query(editor_text, cx));
+        self
+    }
 }

crates/collab_ui/src/contact_list.rs 🔗

@@ -294,6 +294,16 @@ impl ContactList {
         this
     }
 
+    pub fn editor_text(&self, cx: &AppContext) -> String {
+        self.filter_editor.read(cx).text(cx)
+    }
+
+    pub fn with_editor_text(self, editor_text: String, cx: &mut ViewContext<Self>) -> Self {
+        self.filter_editor
+            .update(cx, |picker, cx| picker.set_text(editor_text, cx));
+        self
+    }
+
     fn remove_contact(&mut self, request: &RemoveContact, cx: &mut ViewContext<Self>) {
         let user_id = request.0;
         let user_store = self.user_store.clone();

crates/collab_ui/src/contacts_popover.rs 🔗

@@ -43,19 +43,23 @@ impl ContactsPopover {
             user_store,
             _subscription: None,
         };
-        this.show_contact_list(cx);
+        this.show_contact_list(String::new(), cx);
         this
     }
 
     fn toggle_contact_finder(&mut self, _: &ToggleContactFinder, cx: &mut ViewContext<Self>) {
         match &self.child {
-            Child::ContactList(_) => self.show_contact_finder(cx),
-            Child::ContactFinder(_) => self.show_contact_list(cx),
+            Child::ContactList(list) => self.show_contact_finder(list.read(cx).editor_text(cx), cx),
+            Child::ContactFinder(finder) => {
+                self.show_contact_list(finder.read(cx).editor_text(cx), cx)
+            }
         }
     }
 
-    fn show_contact_finder(&mut self, cx: &mut ViewContext<ContactsPopover>) {
-        let child = cx.add_view(|cx| ContactFinder::new(self.user_store.clone(), cx));
+    fn show_contact_finder(&mut self, editor_text: String, cx: &mut ViewContext<ContactsPopover>) {
+        let child = cx.add_view(|cx| {
+            ContactFinder::new(self.user_store.clone(), cx).with_editor_text(editor_text, cx)
+        });
         cx.focus(&child);
         self._subscription = Some(cx.subscribe(&child, |_, _, event, cx| match event {
             crate::contact_finder::Event::Dismissed => cx.emit(Event::Dismissed),
@@ -64,9 +68,11 @@ impl ContactsPopover {
         cx.notify();
     }
 
-    fn show_contact_list(&mut self, cx: &mut ViewContext<ContactsPopover>) {
-        let child =
-            cx.add_view(|cx| ContactList::new(self.project.clone(), self.user_store.clone(), cx));
+    fn show_contact_list(&mut self, editor_text: String, cx: &mut ViewContext<ContactsPopover>) {
+        let child = cx.add_view(|cx| {
+            ContactList::new(self.project.clone(), self.user_store.clone(), cx)
+                .with_editor_text(editor_text, cx)
+        });
         cx.focus(&child);
         self._subscription = Some(cx.subscribe(&child, |_, _, event, cx| match event {
             crate::contact_list::Event::Dismissed => cx.emit(Event::Dismissed),

crates/picker/src/picker.rs 🔗

@@ -205,6 +205,11 @@ impl<D: PickerDelegate> Picker<D> {
         self.query_editor.read(cx).text(cx)
     }
 
+    pub fn set_query(&self, query: impl Into<Arc<str>>, cx: &mut ViewContext<Self>) {
+        self.query_editor
+            .update(cx, |editor, cx| editor.set_text(query, cx));
+    }
+
     fn on_query_editor_event(
         &mut self,
         _: ViewHandle<Editor>,