Skip to content

armgit / zed

Public Imported from GitHub
0

Code at the speed of thought – Zed is a high-performance, multiplayer code editor from the creators of Atom and Tree-sitter.

git_ui: Drag files from the git panel to the terminal (#66423)

Michal Matczukf4d40a2ccb88
diff --git a/crates/git_ui/src/git_panel.rs b/crates/git_ui/src/git_panel.rs
index 2d40e4e0b19..c206a1d8a9b 100644
--- a/crates/git_ui/src/git_panel.rs
+++ b/crates/git_ui/src/git_panel.rs
@@ -98,7 +98,7 @@ use util::paths::PathStyle;
use util::{ResultExt, TryFutureExt, markdown::MarkdownInlineCode, maybe, rel_path::RelPath};
use workspace::SERIALIZATION_THROTTLE_TIME;
use workspace::{
- Item, ModalView, Workspace,
+ DraggedSelection, Item, ModalView, SelectedEntry, Workspace,
dock::{DockPosition, Panel, PanelEvent},
notifications::{DetachAndPromptErr, NotificationId, NotifyTaskExt},
};
@@ -299,6 +299,41 @@ impl Render for StashMessageModal {
}
}
+struct DraggedGitEntryView {
+ icon: Option<SharedString>,
+ filename: SharedString,
+ entry_count: usize,
+ click_offset: Point<Pixels>,
+}
+
+impl Render for DraggedGitEntryView {
+ fn render(&mut self, _: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
+ let ui_font = ThemeSettings::get_global(cx).ui_font.clone();
+ h_flex()
+ .font(ui_font)
+ .pl(self.click_offset.x + px(12.))
+ .pt(self.click_offset.y + px(12.))
+ .child(
+ h_flex()
+ .gap_1()
+ .py_1()
+ .px_2()
+ .rounded_lg()
+ .bg(cx.theme().colors().background)
+ .map(|this| {
+ if self.entry_count > 1 {
+ this.child(Label::new(format!("{} entries", self.entry_count)))
+ } else {
+ this.when_some(self.icon.clone(), |this, icon| {
+ this.child(Icon::from_path(icon))
+ })
+ .child(Label::new(self.filename.clone()))
+ }
+ }),
+ )
+ }
+}
+
fn git_panel_context_menu(
has_tracked_changes: bool,
has_staged_tracked_changes: bool,
@@ -1831,6 +1866,42 @@ impl GitPanel {
}
}
+ /// Builds the drag payload for the status entry at `index`, reusing the
+ /// project panel's so terminals and panes accept it. A marked row drags
+ /// every marked row. Deleted files have no project entry and are skipped.
+ fn dragged_selection(
+ &self,
+ index: usize,
+ entry: &GitStatusEntry,
+ repo: &Repository,
+ cx: &App,
+ ) -> Option<DraggedSelection> {
+ let project = self.project.read(cx);
+ let selected_entry_for = |repo_path: &RepoPath| {
+ let project_path = repo.repo_path_to_project_path(repo_path, cx)?;
+ let entry_id = project.entry_for_path(&project_path, cx)?.id;
+ Some(SelectedEntry {
+ worktree_id: project_path.worktree_id,
+ entry_id,
+ })
+ };
+ let marked_selections = if self.row_is_marked(index) {
+ self.marked_entries
+ .iter()
+ .chain(&self.marked_directories)
+ .map(|key| &key.path)
+ .unique()
+ .filter_map(|repo_path| selected_entry_for(repo_path))
+ .collect()
+ } else {
+ Arc::default()
+ };
+ Some(DraggedSelection {
+ active_selection: selected_entry_for(&entry.repo_path)?,
+ marked_selections,
+ })
+ }
+
/// The entries a file operation should act on: the marked set when a
/// multi-selection exists, the selected entry otherwise. A single mark on
/// another row does not override the selection, matching the project panel.
@@ -8963,6 +9034,10 @@ impl GitPanel {
)
};
+ let dragged_selection = self.dragged_selection(ix, entry, repo, cx);
+ let dragged_icon = file_icon.clone();
+ let dragged_filename: SharedString = display_name.clone().into();
+
let folder_indicator = settings.folder_indicator;
let name_row = h_flex()
.min_w_0()
@@ -9101,6 +9176,16 @@ impl GitPanel {
}),
),
)
+ .when_some(dragged_selection, |this, dragged_selection| {
+ this.on_drag(dragged_selection, move |selection, click_offset, _, cx| {
+ cx.new(|_| DraggedGitEntryView {
+ icon: dragged_icon.clone(),
+ filename: dragged_filename.clone(),
+ entry_count: selection.items().count(),
+ click_offset,
+ })
+ })
+ })
.on_click({
cx.listener(move |this, event: &ClickEvent, window, cx| {
if event.modifiers().shift {
@@ -16939,6 +17024,41 @@ mod tests {
});
}
+ #[gpui::test]
+ async fn test_dragged_selection_carries_marked_rows(cx: &mut TestAppContext) {
+ init_test(cx);
+ let (_fs, _project, panel, mut cx) = setup_flat_marks_fixture(cx).await;
+
+ fn dragged_paths(panel: &GitPanel, path: &str, cx: &App) -> HashSet<RepoPath> {
+ let index = entry_index_for_repo_path(panel, &repo_path(path)).unwrap();
+ let entry = panel.entries[index].status_entry().unwrap();
+ let repo = panel.active_repository.as_ref().unwrap().read(cx);
+ let selection = panel.dragged_selection(index, entry, repo, cx).unwrap();
+ let project = panel.project.read(cx);
+ selection
+ .items()
+ .map(|item| project.path_for_entry(item.entry_id, cx).unwrap())
+ .map(|project_path| repo.project_path_to_repo_path(&project_path, cx).unwrap())
+ .collect()
+ }
+
+ panel.update_in(&mut cx, |panel, _window, cx| {
+ mark_file(panel, "a.txt");
+ mark_file(panel, "b.txt");
+
+ assert_eq!(
+ dragged_paths(panel, "a.txt", cx),
+ HashSet::from_iter([repo_path("a.txt"), repo_path("b.txt")]),
+ "dragging a marked row should carry every marked row",
+ );
+ assert_eq!(
+ dragged_paths(panel, "c.txt", cx),
+ HashSet::from_iter([repo_path("c.txt")]),
+ "dragging an unmarked row should carry only that row",
+ );
+ });
+ }
+
#[gpui::test]
async fn test_shift_select_next_extends_marks(cx: &mut TestAppContext) {
init_test(cx);