Move project browser toggling logic in workspace::open_paths

This commit is contained in:
Antonio Scandurra 2022-04-22 11:25:23 +02:00
parent bba65e120d
commit 9730213ed7
4 changed files with 26 additions and 27 deletions

View file

@ -43,7 +43,7 @@ pub fn new_journal_entry(app_state: Arc<AppState>, cx: &mut MutableAppContext) {
cx.spawn(|mut cx| {
async move {
let (journal_dir, entry_path) = create_entry.await?;
let (workspace, _, _) = cx
let (workspace, _) = cx
.update(|cx| workspace::open_paths(&[journal_dir], &app_state, cx))
.await;

View file

@ -31,7 +31,7 @@ pub use pane_group::*;
use postage::prelude::Stream;
use project::{fs, Fs, Project, ProjectEntryId, ProjectPath, Worktree};
use settings::Settings;
use sidebar::{Side, Sidebar, ToggleSidebarItem, ToggleSidebarItemFocus};
use sidebar::{Side, Sidebar, SidebarItemId, ToggleSidebarItem, ToggleSidebarItemFocus};
use status_bar::StatusBar;
pub use status_bar::StatusItemView;
use std::{
@ -2120,7 +2120,6 @@ pub fn open_paths(
) -> Task<(
ViewHandle<Workspace>,
Vec<Option<Result<Box<dyn ItemHandle>, Arc<anyhow::Error>>>>,
bool,
)> {
log::info!("open paths {:?}", abs_paths);
@ -2160,9 +2159,24 @@ pub fn open_paths(
let task = workspace.update(cx, |workspace, cx| {
workspace.open_paths(abs_paths.to_vec(), cx)
});
cx.spawn(|_| async move {
cx.spawn(|mut cx| async move {
let items = task.await;
(workspace, items, is_new_workspace)
let opened_dir = items.iter().any(|item| item.is_none());
// Toggle project browser when opening a new workspace that contains a directory.
if is_new_workspace && opened_dir {
workspace.update(&mut cx, |workspace, cx| {
workspace.toggle_sidebar_item(
&ToggleSidebarItem(SidebarItemId {
side: Side::Left,
item_index: 0,
}),
cx,
);
cx.focus_self();
});
}
(workspace, items)
})
}

View file

@ -22,11 +22,7 @@ use smol::process::Command;
use std::{env, fs, path::PathBuf, sync::Arc, thread, time::Duration};
use theme::{ThemeRegistry, DEFAULT_THEME_NAME};
use util::ResultExt;
use workspace::{
self,
sidebar::{Side, SidebarItemId, ToggleSidebarItem},
AppState, OpenNew, OpenPaths,
};
use workspace::{self, AppState, OpenNew, OpenPaths};
use zed::{
self, build_window_options, build_workspace,
fs::RealFs,
@ -366,12 +362,11 @@ async fn handle_cli_connection(
if let Some(request) = requests.next().await {
match request {
CliRequest::Open { paths, wait } => {
let (workspace, items, is_new_workspace) = cx
let (workspace, items) = cx
.update(|cx| workspace::open_paths(&paths, &app_state, cx))
.await;
let mut errored = false;
let mut opened_directory = false;
let mut item_release_futures = Vec::new();
cx.update(|cx| {
for (item, path) in items.into_iter().zip(&paths) {
@ -395,23 +390,11 @@ async fn handle_cli_connection(
.log_err();
errored = true;
}
None => opened_directory = true,
None => {}
}
}
});
if opened_directory && is_new_workspace {
workspace.update(&mut cx, |workspace, cx| {
workspace.toggle_sidebar_item(
&ToggleSidebarItem(SidebarItemId {
side: Side::Left,
item_index: 0,
}),
cx,
);
});
}
if wait {
let background = cx.background();
let wait = async move {

View file

@ -360,8 +360,10 @@ mod tests {
.await;
assert_eq!(cx.window_ids().len(), 1);
let workspace_1 = cx.root_view::<Workspace>(cx.window_ids()[0]).unwrap();
workspace_1.read_with(cx, |workspace, cx| {
assert_eq!(workspace.worktrees(cx).count(), 2)
workspace_1.update(cx, |workspace, cx| {
assert_eq!(workspace.worktrees(cx).count(), 2);
assert!(workspace.left_sidebar_mut().active_item().is_some());
assert!(workspace.active_pane().is_focused(cx));
});
cx.update(|cx| {