assistant: Fix toggling slash command menu from toolbar menu (#16459)

Piotr Osiewicz created

Release Notes:

- N/A

Change summary

crates/assistant/src/assistant.rs            |  2 
crates/assistant/src/assistant_panel.rs      |  3 +
crates/assistant/src/slash_command_picker.rs | 27 +++++++--------------
3 files changed, 12 insertions(+), 20 deletions(-)

Detailed changes

crates/assistant/src/assistant.rs 🔗

@@ -9,7 +9,7 @@ mod model_selector;
 mod prompt_library;
 mod prompts;
 mod slash_command;
-mod slash_command_picker;
+pub(crate) mod slash_command_picker;
 pub mod slash_command_settings;
 mod streaming_diff;
 mod terminal_inline_assistant;

crates/assistant/src/assistant_panel.rs 🔗

@@ -1718,7 +1718,8 @@ pub struct ContextEditor {
     assistant_panel: WeakView<AssistantPanel>,
     error_message: Option<SharedString>,
     show_accept_terms: bool,
-    slash_menu_handle: PopoverMenuHandle<ContextMenu>,
+    pub(crate) slash_menu_handle:
+        PopoverMenuHandle<Picker<slash_command_picker::SlashCommandDelegate>>,
 }
 
 const DEFAULT_TAB_TITLE: &str = "New Context";

crates/assistant/src/slash_command_picker.rs 🔗

@@ -9,17 +9,15 @@ use ui::ListItemSpacing;
 use gpui::SharedString;
 use gpui::Task;
 use picker::{Picker, PickerDelegate};
-use ui::{prelude::*, ListItem, PopoverMenu, PopoverMenuHandle, PopoverTrigger};
+use ui::{prelude::*, ListItem, PopoverMenu, PopoverTrigger};
 
 use crate::assistant_panel::ContextEditor;
 
 #[derive(IntoElement)]
-pub struct SlashCommandSelector<T: PopoverTrigger> {
-    handle: Option<PopoverMenuHandle<Picker<SlashCommandDelegate>>>,
+pub(super) struct SlashCommandSelector<T: PopoverTrigger> {
     registry: Arc<SlashCommandRegistry>,
     active_context_editor: WeakView<ContextEditor>,
     trigger: T,
-    info_text: Option<SharedString>,
 }
 
 #[derive(Clone)]
@@ -28,7 +26,7 @@ struct SlashCommandInfo {
     description: SharedString,
 }
 
-pub struct SlashCommandDelegate {
+pub(crate) struct SlashCommandDelegate {
     all_commands: Vec<SlashCommandInfo>,
     filtered_commands: Vec<SlashCommandInfo>,
     active_context_editor: WeakView<ContextEditor>,
@@ -36,29 +34,17 @@ pub struct SlashCommandDelegate {
 }
 
 impl<T: PopoverTrigger> SlashCommandSelector<T> {
-    pub fn new(
+    pub(crate) fn new(
         registry: Arc<SlashCommandRegistry>,
         active_context_editor: WeakView<ContextEditor>,
         trigger: T,
     ) -> Self {
         SlashCommandSelector {
-            handle: None,
             registry,
             active_context_editor,
             trigger,
-            info_text: None,
         }
     }
-
-    pub fn with_handle(mut self, handle: PopoverMenuHandle<Picker<SlashCommandDelegate>>) -> Self {
-        self.handle = Some(handle);
-        self
-    }
-
-    pub fn with_info_text(mut self, text: impl Into<SharedString>) -> Self {
-        self.info_text = Some(text.into());
-        self
-    }
 }
 
 impl PickerDelegate for SlashCommandDelegate {
@@ -188,6 +174,10 @@ impl<T: PopoverTrigger> RenderOnce for SlashCommandSelector<T> {
             picker
         });
 
+        let handle = self
+            .active_context_editor
+            .update(cx, |this, _| this.slash_menu_handle.clone())
+            .ok();
         PopoverMenu::new("model-switcher")
             .menu(move |_cx| Some(picker_view.clone()))
             .trigger(self.trigger)
@@ -197,5 +187,6 @@ impl<T: PopoverTrigger> RenderOnce for SlashCommandSelector<T> {
                 x: px(0.0),
                 y: px(-16.0),
             })
+            .when_some(handle, |this, handle| this.with_handle(handle))
     }
 }