External file drag and drop (#3933)

Deals with https://github.com/zed-industries/community/issues/1317
Deals with https://github.com/zed-industries/community/issues/486

Reworks pane drag and drop code to support 
* dropping external files into main pane (supports splits same as tabs
and project entries drop) — this will open the file dropped
* dropping external files, tabs and project entries drop into the
terminal — this will add file abs path into the terminal

Release Notes:

- Added a way to drag and drop external files into Zed main & terminal
panes; support tabs and project entries drop into terminal pane
This commit is contained in:
Kirill Bulatov 2024-01-07 13:37:49 +02:00 committed by GitHub
commit 419b4d029c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
10 changed files with 233 additions and 92 deletions

View file

@ -431,6 +431,13 @@ pub enum Event {
WorkspaceCreated(WeakView<Workspace>),
}
pub enum OpenVisible {
All,
None,
OnlyFiles,
OnlyDirectories,
}
pub struct Workspace {
weak_self: WeakView<Self>,
workspace_actions: Vec<Box<dyn Fn(Div, &mut ViewContext<Self>) -> Div>>,
@ -1315,7 +1322,8 @@ impl Workspace {
pub fn open_paths(
&mut self,
mut abs_paths: Vec<PathBuf>,
visible: bool,
visible: OpenVisible,
pane: Option<WeakView<Pane>>,
cx: &mut ViewContext<Self>,
) -> Task<Vec<Option<Result<Box<dyn ItemHandle>, anyhow::Error>>>> {
log::info!("open paths {abs_paths:?}");
@ -1326,31 +1334,56 @@ impl Workspace {
abs_paths.sort_unstable();
cx.spawn(move |this, mut cx| async move {
let mut tasks = Vec::with_capacity(abs_paths.len());
for abs_path in &abs_paths {
let project_path = match this
.update(&mut cx, |this, cx| {
Workspace::project_path_for_path(
this.project.clone(),
abs_path,
visible,
cx,
)
})
.log_err()
{
Some(project_path) => project_path.await.log_err(),
let visible = match visible {
OpenVisible::All => Some(true),
OpenVisible::None => Some(false),
OpenVisible::OnlyFiles => match fs.metadata(abs_path).await.log_err() {
Some(Some(metadata)) => Some(!metadata.is_dir),
Some(None) => {
log::error!("No metadata for file {abs_path:?}");
None
}
None => None,
},
OpenVisible::OnlyDirectories => match fs.metadata(abs_path).await.log_err() {
Some(Some(metadata)) => Some(metadata.is_dir),
Some(None) => {
log::error!("No metadata for file {abs_path:?}");
None
}
None => None,
},
};
let project_path = match visible {
Some(visible) => match this
.update(&mut cx, |this, cx| {
Workspace::project_path_for_path(
this.project.clone(),
abs_path,
visible,
cx,
)
})
.log_err()
{
Some(project_path) => project_path.await.log_err(),
None => None,
},
None => None,
};
let this = this.clone();
let abs_path = abs_path.clone();
let fs = fs.clone();
let pane = pane.clone();
let task = cx.spawn(move |mut cx| async move {
let (worktree, project_path) = project_path?;
if fs.is_file(&abs_path).await {
Some(
this.update(&mut cx, |this, cx| {
this.open_path(project_path, None, true, cx)
this.open_path(project_path, pane, true, cx)
})
.log_err()?
.await,
@ -1396,7 +1429,9 @@ impl Workspace {
cx.spawn(|this, mut cx| async move {
if let Some(paths) = paths.await.log_err().flatten() {
let results = this
.update(&mut cx, |this, cx| this.open_paths(paths, true, cx))?
.update(&mut cx, |this, cx| {
this.open_paths(paths, OpenVisible::All, None, cx)
})?
.await;
for result in results.into_iter().flatten() {
result.log_err();
@ -1782,7 +1817,16 @@ impl Workspace {
cx.spawn(|workspace, mut cx| async move {
let open_paths_task_result = workspace
.update(&mut cx, |workspace, cx| {
workspace.open_paths(vec![abs_path.clone()], visible, cx)
workspace.open_paths(
vec![abs_path.clone()],
if visible {
OpenVisible::All
} else {
OpenVisible::None
},
None,
cx,
)
})
.with_context(|| format!("open abs path {abs_path:?} task spawn"))?
.await;
@ -4081,7 +4125,7 @@ pub fn open_paths(
existing.clone(),
existing
.update(&mut cx, |workspace, cx| {
workspace.open_paths(abs_paths, true, cx)
workspace.open_paths(abs_paths, OpenVisible::All, None, cx)
})?
.await,
))
@ -4129,7 +4173,7 @@ pub fn create_and_open_local_file(
let mut items = workspace
.update(&mut cx, |workspace, cx| {
workspace.with_local_workspace(cx, |workspace, cx| {
workspace.open_paths(vec![path.to_path_buf()], false, cx)
workspace.open_paths(vec![path.to_path_buf()], OpenVisible::None, None, cx)
})
})?
.await?