Fix showing/hiding copilot actions when `disable_ai` setting is changed (#35506)

Michael Sloan created

Release Notes:

- N/A

Change summary

crates/copilot/src/copilot.rs | 84 +++++++++++++++++++-----------------
1 file changed, 45 insertions(+), 39 deletions(-)

Detailed changes

crates/copilot/src/copilot.rs 🔗

@@ -85,45 +85,13 @@ pub fn init(
         move |cx| Copilot::start(new_server_id, fs, node_runtime, cx)
     });
     Copilot::set_global(copilot.clone(), cx);
-    cx.observe(&copilot, |handle, cx| {
-        let copilot_action_types = [
-            TypeId::of::<Suggest>(),
-            TypeId::of::<NextSuggestion>(),
-            TypeId::of::<PreviousSuggestion>(),
-            TypeId::of::<Reinstall>(),
-        ];
-        let copilot_auth_action_types = [TypeId::of::<SignOut>()];
-        let copilot_no_auth_action_types = [TypeId::of::<SignIn>()];
-        let status = handle.read(cx).status();
-
-        let is_ai_disabled = DisableAiSettings::get_global(cx).disable_ai;
-        let filter = CommandPaletteFilter::global_mut(cx);
-
-        if is_ai_disabled {
-            filter.hide_action_types(&copilot_action_types);
-            filter.hide_action_types(&copilot_auth_action_types);
-            filter.hide_action_types(&copilot_no_auth_action_types);
-        } else {
-            match status {
-                Status::Disabled => {
-                    filter.hide_action_types(&copilot_action_types);
-                    filter.hide_action_types(&copilot_auth_action_types);
-                    filter.hide_action_types(&copilot_no_auth_action_types);
-                }
-                Status::Authorized => {
-                    filter.hide_action_types(&copilot_no_auth_action_types);
-                    filter.show_action_types(
-                        copilot_action_types
-                            .iter()
-                            .chain(&copilot_auth_action_types),
-                    );
-                }
-                _ => {
-                    filter.hide_action_types(&copilot_action_types);
-                    filter.hide_action_types(&copilot_auth_action_types);
-                    filter.show_action_types(copilot_no_auth_action_types.iter());
-                }
-            }
+    cx.observe(&copilot, |copilot, cx| {
+        copilot.update(cx, |copilot, cx| copilot.update_action_visibilities(cx));
+    })
+    .detach();
+    cx.observe_global::<SettingsStore>(|cx| {
+        if let Some(copilot) = Copilot::global(cx) {
+            copilot.update(cx, |copilot, cx| copilot.update_action_visibilities(cx));
         }
     })
     .detach();
@@ -1131,6 +1099,44 @@ impl Copilot {
             cx.notify();
         }
     }
+
+    fn update_action_visibilities(&self, cx: &mut App) {
+        let signed_in_actions = [
+            TypeId::of::<Suggest>(),
+            TypeId::of::<NextSuggestion>(),
+            TypeId::of::<PreviousSuggestion>(),
+            TypeId::of::<Reinstall>(),
+        ];
+        let auth_actions = [TypeId::of::<SignOut>()];
+        let no_auth_actions = [TypeId::of::<SignIn>()];
+        let status = self.status();
+
+        let is_ai_disabled = DisableAiSettings::get_global(cx).disable_ai;
+        let filter = CommandPaletteFilter::global_mut(cx);
+
+        if is_ai_disabled {
+            filter.hide_action_types(&signed_in_actions);
+            filter.hide_action_types(&auth_actions);
+            filter.hide_action_types(&no_auth_actions);
+        } else {
+            match status {
+                Status::Disabled => {
+                    filter.hide_action_types(&signed_in_actions);
+                    filter.hide_action_types(&auth_actions);
+                    filter.hide_action_types(&no_auth_actions);
+                }
+                Status::Authorized => {
+                    filter.hide_action_types(&no_auth_actions);
+                    filter.show_action_types(signed_in_actions.iter().chain(&auth_actions));
+                }
+                _ => {
+                    filter.hide_action_types(&signed_in_actions);
+                    filter.hide_action_types(&auth_actions);
+                    filter.show_action_types(no_auth_actions.iter());
+                }
+            }
+        }
+    }
 }
 
 fn id_for_language(language: Option<&Arc<Language>>) -> String {