Add save command

Co-Authored-By: Nathan Sobo <nathan@zed.dev>
This commit is contained in:
Max Brunsfeld
2021-04-05 16:52:33 -07:00
co-authored by Nathan Sobo
parent c39c7c3eff
commit 46f8665e41
6 changed files with 192 additions and 26 deletions
+1
View File
@@ -15,6 +15,7 @@ use std::path::PathBuf;
pub fn init(app: &mut App) {
app.add_global_action("workspace:open_paths", open_paths);
pane::init(app);
workspace_view::init(app);
}
pub struct OpenParams {
+31 -2
View File
@@ -1,12 +1,17 @@
use super::{pane, Pane, PaneGroup, SplitDirection, Workspace};
use crate::{settings::Settings, watch};
use gpui::{
color::rgbu, elements::*, AnyViewHandle, AppContext, Entity, ModelHandle, MutableAppContext,
View, ViewContext, ViewHandle,
color::rgbu, elements::*, executor::BackgroundTask, keymap::Binding, AnyViewHandle, App,
AppContext, Entity, ModelHandle, MutableAppContext, View, ViewContext, ViewHandle,
};
use log::{error, info};
use std::{collections::HashSet, path::PathBuf};
pub fn init(app: &mut App) {
app.add_action("workspace:save", WorkspaceView::save_active_item);
app.add_bindings(vec![Binding::new("cmd-s", "workspace:save", None)]);
}
pub trait ItemView: View {
fn is_activate_event(event: &Self::Event) -> bool;
fn title(&self, app: &AppContext) -> String;
@@ -17,6 +22,9 @@ pub trait ItemView: View {
{
None
}
fn save(&self, _: &mut MutableAppContext) -> Option<BackgroundTask<anyhow::Result<()>>> {
None
}
}
pub trait ItemViewHandle: Send + Sync {
@@ -27,6 +35,7 @@ pub trait ItemViewHandle: Send + Sync {
fn set_parent_pane(&self, pane: &ViewHandle<Pane>, app: &mut MutableAppContext);
fn id(&self) -> usize;
fn to_any(&self) -> AnyViewHandle;
fn save(&self, ctx: &mut MutableAppContext) -> Option<BackgroundTask<anyhow::Result<()>>>;
}
impl<T: ItemView> ItemViewHandle for ViewHandle<T> {
@@ -62,6 +71,10 @@ impl<T: ItemView> ItemViewHandle for ViewHandle<T> {
})
}
fn save(&self, ctx: &mut MutableAppContext) -> Option<BackgroundTask<anyhow::Result<()>>> {
self.update(ctx, |item, ctx| item.save(ctx.app_mut()))
}
fn id(&self) -> usize {
self.id()
}
@@ -206,6 +219,22 @@ impl WorkspaceView {
}
}
pub fn save_active_item(&mut self, _: &(), ctx: &mut ViewContext<Self>) {
self.active_pane.update(ctx, |pane, ctx| {
if let Some(item) = pane.active_item() {
if let Some(task) = item.save(ctx.app_mut()) {
ctx.spawn(task, |_, result, _| {
if let Err(e) = result {
// TODO - present this error to the user
error!("failed to save item: {:?}, ", e);
}
})
.detach();
}
}
});
}
fn workspace_updated(&mut self, _: ModelHandle<Workspace>, ctx: &mut ViewContext<Self>) {
ctx.notify();
}