Fix unstage/stage in project diff not working when git panel isn't open (#26575)

Anthony Eid and Conrad Irwin created

Closes #ISSUE

Release Notes:

- Fix Bug where unstage/stage all in project diff wouldn't work while
git panel was closed

Co-authored-by: Conrad Irwin <conrad.irwin@gmail.com>

Change summary

crates/git_ui/src/project_diff.rs | 35 +++++++++++++++++++-------------
1 file changed, 21 insertions(+), 14 deletions(-)

Detailed changes

crates/git_ui/src/project_diff.rs 🔗

@@ -815,23 +815,30 @@ impl ProjectDiffToolbar {
             cx.dispatch_action(action.as_ref());
         })
     }
-    fn dispatch_panel_action(
-        &self,
-        action: &dyn Action,
-        window: &mut Window,
-        cx: &mut Context<Self>,
-    ) {
+
+    fn stage_all(&mut self, window: &mut Window, cx: &mut Context<Self>) {
         self.workspace
-            .read_with(cx, |workspace, cx| {
+            .update(cx, |workspace, cx| {
                 if let Some(panel) = workspace.panel::<GitPanel>(cx) {
-                    panel.focus_handle(cx).focus(window)
+                    panel.update(cx, |panel, cx| {
+                        panel.stage_all(&Default::default(), window, cx);
+                    });
                 }
             })
             .ok();
-        let action = action.boxed_clone();
-        cx.defer(move |cx| {
-            cx.dispatch_action(action.as_ref());
-        })
+    }
+
+    fn unstage_all(&mut self, window: &mut Window, cx: &mut Context<Self>) {
+        self.workspace
+            .update(cx, |workspace, cx| {
+                let Some(panel) = workspace.panel::<GitPanel>(cx) else {
+                    return;
+                };
+                panel.update(cx, |panel, cx| {
+                    panel.unstage_all(&Default::default(), window, cx);
+                });
+            })
+            .ok();
     }
 }
 
@@ -985,7 +992,7 @@ impl Render for ProjectDiffToolbar {
                                         &focus_handle,
                                     ))
                                     .on_click(cx.listener(|this, _, window, cx| {
-                                        this.dispatch_panel_action(&UnstageAll, window, cx)
+                                        this.unstage_all(window, cx)
                                     })),
                             )
                         },
@@ -1005,7 +1012,7 @@ impl Render for ProjectDiffToolbar {
                                             &focus_handle,
                                         ))
                                         .on_click(cx.listener(|this, _, window, cx| {
-                                            this.dispatch_panel_action(&StageAll, window, cx)
+                                            this.stage_all(window, cx)
                                         })),
                                 ),
                             )