Merge branch 'main' into share-project
Co-Authored-By: Nathan Sobo <nathan@zed.dev>
This commit is contained in:
commit
466a377e1d
16 changed files with 423 additions and 306 deletions
|
@ -4,7 +4,7 @@ pub mod settings;
|
|||
pub mod sidebar;
|
||||
mod status_bar;
|
||||
|
||||
use anyhow::Result;
|
||||
use anyhow::{anyhow, Result};
|
||||
use client::{Authenticate, ChannelList, Client, User, UserStore};
|
||||
use gpui::{
|
||||
action,
|
||||
|
@ -13,16 +13,18 @@ use gpui::{
|
|||
geometry::{vector::vec2f, PathBuilder},
|
||||
json::{self, to_string_pretty, ToJson},
|
||||
keymap::Binding,
|
||||
platform::CursorStyle,
|
||||
platform::{CursorStyle, WindowOptions},
|
||||
AnyViewHandle, AppContext, ClipboardItem, Entity, ModelContext, ModelHandle, MutableAppContext,
|
||||
PromptLevel, RenderContext, Task, View, ViewContext, ViewHandle, WeakModelHandle,
|
||||
PathPromptOptions, PromptLevel, RenderContext, Task, View, ViewContext, ViewHandle,
|
||||
WeakModelHandle,
|
||||
};
|
||||
use language::LanguageRegistry;
|
||||
use log::error;
|
||||
pub use pane::*;
|
||||
pub use pane_group::*;
|
||||
use parking_lot::Mutex;
|
||||
use postage::{prelude::Stream, watch};
|
||||
use project::{Fs, Project, ProjectPath, Worktree};
|
||||
use project::{fs, Fs, Project, ProjectPath, Worktree};
|
||||
pub use settings::Settings;
|
||||
use sidebar::{Side, Sidebar, SidebarItemId, ToggleSidebarItem, ToggleSidebarItemFocus};
|
||||
use status_bar::StatusBar;
|
||||
|
@ -32,13 +34,23 @@ use std::{
|
|||
path::{Path, PathBuf},
|
||||
sync::Arc,
|
||||
};
|
||||
use theme::Theme;
|
||||
use theme::{Theme, ThemeRegistry};
|
||||
|
||||
action!(OpenNew, WorkspaceParams);
|
||||
action!(Open, Arc<AppState>);
|
||||
action!(OpenNew, Arc<AppState>);
|
||||
action!(OpenPaths, OpenParams);
|
||||
action!(Save);
|
||||
action!(DebugElements);
|
||||
|
||||
pub fn init(cx: &mut MutableAppContext) {
|
||||
cx.add_global_action(open);
|
||||
cx.add_global_action(move |action: &OpenPaths, cx: &mut MutableAppContext| {
|
||||
open_paths(&action.0.paths, &action.0.app_state, cx).detach()
|
||||
});
|
||||
cx.add_global_action(move |action: &OpenNew, cx: &mut MutableAppContext| {
|
||||
open_new(&action.0, cx)
|
||||
});
|
||||
|
||||
cx.add_action(Workspace::save_active_item);
|
||||
cx.add_action(Workspace::debug_elements);
|
||||
cx.add_action(Workspace::toggle_sidebar_item);
|
||||
|
@ -66,6 +78,27 @@ pub fn init(cx: &mut MutableAppContext) {
|
|||
pane::init(cx);
|
||||
}
|
||||
|
||||
pub struct AppState {
|
||||
pub settings_tx: Arc<Mutex<watch::Sender<Settings>>>,
|
||||
pub settings: watch::Receiver<Settings>,
|
||||
pub languages: Arc<LanguageRegistry>,
|
||||
pub themes: Arc<ThemeRegistry>,
|
||||
pub client: Arc<client::Client>,
|
||||
pub user_store: ModelHandle<client::UserStore>,
|
||||
pub fs: Arc<dyn fs::Fs>,
|
||||
pub channel_list: ModelHandle<client::ChannelList>,
|
||||
pub entry_openers: Arc<[Box<dyn EntryOpener>]>,
|
||||
pub build_window_options: &'static dyn Fn() -> WindowOptions<'static>,
|
||||
pub build_workspace:
|
||||
&'static dyn Fn(&WorkspaceParams, &mut ViewContext<Workspace>) -> Workspace,
|
||||
}
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct OpenParams {
|
||||
pub paths: Vec<PathBuf>,
|
||||
pub app_state: Arc<AppState>,
|
||||
}
|
||||
|
||||
pub trait EntryOpener {
|
||||
fn open(
|
||||
&self,
|
||||
|
@ -468,7 +501,11 @@ impl Workspace {
|
|||
}
|
||||
}
|
||||
|
||||
pub fn open_paths(&mut self, abs_paths: &[PathBuf], cx: &mut ViewContext<Self>) -> Task<()> {
|
||||
pub fn open_paths(
|
||||
&mut self,
|
||||
abs_paths: &[PathBuf],
|
||||
cx: &mut ViewContext<Self>,
|
||||
) -> Task<Vec<Option<Result<Box<dyn ItemViewHandle>, Arc<anyhow::Error>>>>> {
|
||||
let entries = abs_paths
|
||||
.iter()
|
||||
.cloned()
|
||||
|
@ -484,26 +521,26 @@ impl Workspace {
|
|||
cx.spawn(|this, mut cx| {
|
||||
let fs = fs.clone();
|
||||
async move {
|
||||
let project_path = project_path.await?;
|
||||
let project_path = project_path.await.ok()?;
|
||||
if fs.is_file(&abs_path).await {
|
||||
if let Some(entry) =
|
||||
this.update(&mut cx, |this, cx| this.open_entry(project_path, cx))
|
||||
{
|
||||
entry.await;
|
||||
return Some(entry.await);
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
None
|
||||
}
|
||||
})
|
||||
})
|
||||
.collect::<Vec<Task<Result<()>>>>();
|
||||
.collect::<Vec<_>>();
|
||||
|
||||
cx.foreground().spawn(async move {
|
||||
let mut items = Vec::new();
|
||||
for task in tasks {
|
||||
if let Err(error) = task.await {
|
||||
log::error!("error opening paths {}", error);
|
||||
}
|
||||
items.push(task.await);
|
||||
}
|
||||
items
|
||||
})
|
||||
}
|
||||
|
||||
|
@ -595,10 +632,12 @@ impl Workspace {
|
|||
&mut self,
|
||||
project_path: ProjectPath,
|
||||
cx: &mut ViewContext<Self>,
|
||||
) -> Option<Task<()>> {
|
||||
) -> Option<Task<Result<Box<dyn ItemViewHandle>, Arc<anyhow::Error>>>> {
|
||||
let pane = self.active_pane().clone();
|
||||
if self.activate_or_open_existing_entry(project_path.clone(), &pane, cx) {
|
||||
return None;
|
||||
if let Some(existing_item) =
|
||||
self.activate_or_open_existing_entry(project_path.clone(), &pane, cx)
|
||||
{
|
||||
return Some(cx.foreground().spawn(async move { Ok(existing_item) }));
|
||||
}
|
||||
|
||||
let worktree = match self
|
||||
|
@ -629,20 +668,20 @@ impl Workspace {
|
|||
Some(cx.spawn(|this, mut cx| async move {
|
||||
let load_result = task.await;
|
||||
this.update(&mut cx, |this, cx| {
|
||||
if let Some(pane) = pane.upgrade(&cx) {
|
||||
match load_result {
|
||||
Ok(item) => {
|
||||
// By the time loading finishes, the entry could have been already added
|
||||
// to the pane. If it was, we activate it, otherwise we'll store the
|
||||
// item and add a new view for it.
|
||||
if !this.activate_or_open_existing_entry(project_path, &pane, cx) {
|
||||
this.add_item(item, cx);
|
||||
}
|
||||
}
|
||||
Err(error) => {
|
||||
log::error!("error opening item: {}", error);
|
||||
}
|
||||
}
|
||||
let pane = pane
|
||||
.upgrade(&cx)
|
||||
.ok_or_else(|| anyhow!("could not upgrade pane reference"))?;
|
||||
let item = load_result?;
|
||||
|
||||
// By the time loading finishes, the entry could have been already added
|
||||
// to the pane. If it was, we activate it, otherwise we'll store the
|
||||
// item and add a new view for it.
|
||||
if let Some(existing) =
|
||||
this.activate_or_open_existing_entry(project_path, &pane, cx)
|
||||
{
|
||||
Ok(existing)
|
||||
} else {
|
||||
Ok(this.add_item(item, cx))
|
||||
}
|
||||
})
|
||||
}))
|
||||
|
@ -653,11 +692,13 @@ impl Workspace {
|
|||
project_path: ProjectPath,
|
||||
pane: &ViewHandle<Pane>,
|
||||
cx: &mut ViewContext<Self>,
|
||||
) -> bool {
|
||||
) -> Option<Box<dyn ItemViewHandle>> {
|
||||
// If the pane contains a view for this file, then activate
|
||||
// that item view.
|
||||
if pane.update(cx, |pane, cx| pane.activate_entry(project_path.clone(), cx)) {
|
||||
return true;
|
||||
if let Some(existing_item_view) =
|
||||
pane.update(cx, |pane, cx| pane.activate_entry(project_path.clone(), cx))
|
||||
{
|
||||
return Some(existing_item_view);
|
||||
}
|
||||
|
||||
// Otherwise, if this file is already open somewhere in the workspace,
|
||||
|
@ -680,10 +721,10 @@ impl Workspace {
|
|||
}
|
||||
});
|
||||
if let Some(view) = view_for_existing_item {
|
||||
pane.add_item_view(view, cx.as_mut());
|
||||
true
|
||||
pane.add_item_view(view.boxed_clone(), cx.as_mut());
|
||||
Some(view)
|
||||
} else {
|
||||
false
|
||||
None
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -830,13 +871,19 @@ impl Workspace {
|
|||
pane
|
||||
}
|
||||
|
||||
pub fn add_item<T>(&mut self, item_handle: T, cx: &mut ViewContext<Self>)
|
||||
pub fn add_item<T>(
|
||||
&mut self,
|
||||
item_handle: T,
|
||||
cx: &mut ViewContext<Self>,
|
||||
) -> Box<dyn ItemViewHandle>
|
||||
where
|
||||
T: ItemHandle,
|
||||
{
|
||||
let view = item_handle.add_view(cx.window_id(), self.settings.clone(), cx);
|
||||
self.items.push(item_handle.downgrade());
|
||||
self.active_pane().add_item_view(view, cx.as_mut());
|
||||
self.active_pane()
|
||||
.add_item_view(view.boxed_clone(), cx.as_mut());
|
||||
view
|
||||
}
|
||||
|
||||
fn activate_pane(&mut self, pane: ViewHandle<Pane>, cx: &mut ViewContext<Self>) {
|
||||
|
@ -1215,3 +1262,86 @@ impl Element for AvatarRibbon {
|
|||
})
|
||||
}
|
||||
}
|
||||
|
||||
impl std::fmt::Debug for OpenParams {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
f.debug_struct("OpenParams")
|
||||
.field("paths", &self.paths)
|
||||
.finish()
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a> From<&'a AppState> for WorkspaceParams {
|
||||
fn from(state: &'a AppState) -> Self {
|
||||
Self {
|
||||
client: state.client.clone(),
|
||||
fs: state.fs.clone(),
|
||||
languages: state.languages.clone(),
|
||||
settings: state.settings.clone(),
|
||||
user_store: state.user_store.clone(),
|
||||
channel_list: state.channel_list.clone(),
|
||||
entry_openers: state.entry_openers.clone(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn open(action: &Open, cx: &mut MutableAppContext) {
|
||||
let app_state = action.0.clone();
|
||||
cx.prompt_for_paths(
|
||||
PathPromptOptions {
|
||||
files: true,
|
||||
directories: true,
|
||||
multiple: true,
|
||||
},
|
||||
move |paths, cx| {
|
||||
if let Some(paths) = paths {
|
||||
cx.dispatch_global_action(OpenPaths(OpenParams { paths, app_state }));
|
||||
}
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
pub fn open_paths(
|
||||
abs_paths: &[PathBuf],
|
||||
app_state: &Arc<AppState>,
|
||||
cx: &mut MutableAppContext,
|
||||
) -> Task<ViewHandle<Workspace>> {
|
||||
log::info!("open paths {:?}", abs_paths);
|
||||
|
||||
// Open paths in existing workspace if possible
|
||||
let mut existing = None;
|
||||
for window_id in cx.window_ids().collect::<Vec<_>>() {
|
||||
if let Some(workspace) = cx.root_view::<Workspace>(window_id) {
|
||||
if workspace.update(cx, |view, cx| {
|
||||
if view.contains_paths(abs_paths, cx.as_ref()) {
|
||||
existing = Some(workspace.clone());
|
||||
true
|
||||
} else {
|
||||
false
|
||||
}
|
||||
}) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
let workspace = existing.unwrap_or_else(|| {
|
||||
cx.add_window((app_state.build_window_options)(), |cx| {
|
||||
(app_state.build_workspace)(&WorkspaceParams::from(app_state.as_ref()), cx)
|
||||
})
|
||||
.1
|
||||
});
|
||||
|
||||
let task = workspace.update(cx, |workspace, cx| workspace.open_paths(abs_paths, cx));
|
||||
cx.spawn(|_| async move {
|
||||
task.await;
|
||||
workspace
|
||||
})
|
||||
}
|
||||
|
||||
fn open_new(app_state: &Arc<AppState>, cx: &mut MutableAppContext) {
|
||||
let (window_id, workspace) = cx.add_window((app_state.build_window_options)(), |cx| {
|
||||
(app_state.build_workspace)(&app_state.as_ref().into(), cx)
|
||||
});
|
||||
cx.dispatch_action(window_id, vec![workspace.id()], &OpenNew(app_state.clone()));
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue