diff --git a/crates/git_ui/src/project_diff.rs b/crates/git_ui/src/project_diff.rs index 05c8e482107cd30e37c6009f49ea6428fa0804fc..af6f5a587a7d99e2e8040e875c3aa011949bedd7 100644 --- a/crates/git_ui/src/project_diff.rs +++ b/crates/git_ui/src/project_diff.rs @@ -1446,20 +1446,104 @@ impl Render for ProjectDiffToolbar { // "Send Review to Agent" button (only shown when there are review comments) .when(review_count > 0, |el| { el.child(vertical_divider()).child( - Button::new( - "send-review", - format!("Send Review to Agent ({})", review_count), - ) - .icon(IconName::ZedAssistant) - .icon_position(IconPosition::Start) - .tooltip(Tooltip::for_action_title_in( - "Send all review comments to the Agent panel", - &SendReviewToAgent, - &focus_handle, - )) - .on_click(cx.listener(|this, _, window, cx| { - this.dispatch_action(&SendReviewToAgent, window, cx) - })), + render_send_review_to_agent_button(review_count, &focus_handle).on_click( + cx.listener(|this, _, window, cx| { + this.dispatch_action(&SendReviewToAgent, window, cx) + }), + ), + ) + }) + } +} + +fn render_send_review_to_agent_button(review_count: usize, focus_handle: &FocusHandle) -> Button { + Button::new( + "send-review", + format!("Send Review to Agent ({})", review_count), + ) + .icon(IconName::ZedAssistant) + .icon_position(IconPosition::Start) + .tooltip(Tooltip::for_action_title_in( + "Send all review comments to the Agent panel", + &SendReviewToAgent, + focus_handle, + )) +} + +pub struct BranchDiffToolbar { + project_diff: Option>, +} + +impl BranchDiffToolbar { + pub fn new(_: &mut Context) -> Self { + Self { project_diff: None } + } + + fn project_diff(&self, _: &App) -> Option> { + self.project_diff.as_ref()?.upgrade() + } + + fn dispatch_action(&self, action: &dyn Action, window: &mut Window, cx: &mut Context) { + if let Some(project_diff) = self.project_diff(cx) { + project_diff.focus_handle(cx).focus(window, cx); + } + let action = action.boxed_clone(); + cx.defer(move |cx| { + cx.dispatch_action(action.as_ref()); + }) + } +} + +impl EventEmitter for BranchDiffToolbar {} + +impl ToolbarItemView for BranchDiffToolbar { + fn set_active_pane_item( + &mut self, + active_pane_item: Option<&dyn ItemHandle>, + _: &mut Window, + cx: &mut Context, + ) -> ToolbarItemLocation { + self.project_diff = active_pane_item + .and_then(|item| item.act_as::(cx)) + .filter(|item| matches!(item.read(cx).diff_base(cx), DiffBase::Merge { .. })) + .map(|entity| entity.downgrade()); + if self.project_diff.is_some() { + ToolbarItemLocation::PrimaryRight + } else { + ToolbarItemLocation::Hidden + } + } + + fn pane_focus_update( + &mut self, + _pane_focused: bool, + _window: &mut Window, + _cx: &mut Context, + ) { + } +} + +impl Render for BranchDiffToolbar { + fn render(&mut self, _: &mut Window, cx: &mut Context) -> impl IntoElement { + let Some(project_diff) = self.project_diff(cx) else { + return div(); + }; + let focus_handle = project_diff.focus_handle(cx); + let review_count = project_diff.read(cx).total_review_comment_count(); + + h_group_xl() + .my_neg_1() + .py_1() + .items_center() + .flex_wrap() + .justify_end() + .when(review_count > 0, |el| { + el.child( + render_send_review_to_agent_button(review_count, &focus_handle).on_click( + cx.listener(|this, _, window, cx| { + this.dispatch_action(&SendReviewToAgent, window, cx) + }), + ), ) }) } diff --git a/crates/zed/src/zed.rs b/crates/zed/src/zed.rs index caee8d7c1b24488d69c954bb0ad41ebe773598f5..c8ac687bbaad551ec7b9e533599163ea1731ecd3 100644 --- a/crates/zed/src/zed.rs +++ b/crates/zed/src/zed.rs @@ -32,7 +32,7 @@ use futures::future::Either; use futures::{StreamExt, channel::mpsc, select_biased}; use git_ui::commit_view::CommitViewToolbar; use git_ui::git_panel::GitPanel; -use git_ui::project_diff::ProjectDiffToolbar; +use git_ui::project_diff::{BranchDiffToolbar, ProjectDiffToolbar}; use gpui::{ Action, App, AppContext as _, AsyncWindowContext, Context, DismissEvent, Element, Entity, Focusable, KeyBinding, ParentElement, PathPromptOptions, PromptLevel, ReadGlobal, SharedString, @@ -1233,6 +1233,8 @@ fn initialize_pane( toolbar.add_item(migration_banner, window, cx); let project_diff_toolbar = cx.new(|cx| ProjectDiffToolbar::new(workspace, cx)); toolbar.add_item(project_diff_toolbar, window, cx); + let branch_diff_toolbar = cx.new(BranchDiffToolbar::new); + toolbar.add_item(branch_diff_toolbar, window, cx); let commit_view_toolbar = cx.new(|_| CommitViewToolbar::new()); toolbar.add_item(commit_view_toolbar, window, cx); let agent_diff_toolbar = cx.new(AgentDiffToolbar::new);