From 9730213ed72e034557f28fc96d26e695db5f150f Mon Sep 17 00:00:00 2001 From: Antonio Scandurra Date: Fri, 22 Apr 2022 11:25:23 +0200 Subject: [PATCH] Move project browser toggling logic in `workspace::open_paths` --- crates/journal/src/journal.rs | 2 +- crates/workspace/src/workspace.rs | 22 ++++++++++++++++++---- crates/zed/src/main.rs | 23 +++-------------------- crates/zed/src/zed.rs | 6 ++++-- 4 files changed, 26 insertions(+), 27 deletions(-) diff --git a/crates/journal/src/journal.rs b/crates/journal/src/journal.rs index 6e579d87a7..19c044e65f 100644 --- a/crates/journal/src/journal.rs +++ b/crates/journal/src/journal.rs @@ -43,7 +43,7 @@ pub fn new_journal_entry(app_state: Arc, 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; diff --git a/crates/workspace/src/workspace.rs b/crates/workspace/src/workspace.rs index 1151fd42c7..efb9527533 100644 --- a/crates/workspace/src/workspace.rs +++ b/crates/workspace/src/workspace.rs @@ -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, Vec, Arc>>>, - 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) }) } diff --git a/crates/zed/src/main.rs b/crates/zed/src/main.rs index 184864879f..d4f06f074e 100644 --- a/crates/zed/src/main.rs +++ b/crates/zed/src/main.rs @@ -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 { diff --git a/crates/zed/src/zed.rs b/crates/zed/src/zed.rs index a07dedfa97..71ce6f065a 100644 --- a/crates/zed/src/zed.rs +++ b/crates/zed/src/zed.rs @@ -360,8 +360,10 @@ mod tests { .await; assert_eq!(cx.window_ids().len(), 1); let workspace_1 = cx.root_view::(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| {