Return item handles when opening items

This will support interacting with the opened item. Although I think I should probably return the ItemView rather than the Item. Next commit.
This commit is contained in:
Nathan Sobo 2021-12-18 08:26:57 -07:00
parent ca0d7e5e1f
commit 9e8ef31452
2 changed files with 29 additions and 37 deletions

View file

@ -9,22 +9,11 @@ use workspace::AppState;
action!(NewJournalEntry); action!(NewJournalEntry);
pub fn init(app_state: Arc<AppState>, cx: &mut MutableAppContext) { pub fn init(app_state: Arc<AppState>, cx: &mut MutableAppContext) {
log::info!("JOURNAL INIT");
cx.add_bindings(vec![Binding::new("ctrl-alt-cmd-j", NewJournalEntry, None)]); cx.add_bindings(vec![Binding::new("ctrl-alt-cmd-j", NewJournalEntry, None)]);
cx.add_global_action(move |_: &NewJournalEntry, cx| new_journal_entry(app_state.clone(), cx));
let mut counter = 0;
cx.add_global_action(move |_: &NewJournalEntry, cx| {
log::info!("NEW JOURNAL ENTRY ACTION");
counter += 1;
if counter == 2 {
log::info!("called twice?");
}
new_journal_entry(app_state.clone(), cx)
});
} }
pub fn new_journal_entry(app_state: Arc<AppState>, cx: &mut MutableAppContext) { pub fn new_journal_entry(app_state: Arc<AppState>, cx: &mut MutableAppContext) {
log::info!("NEW JOURNAL ENTRY");
let paths = cx.background().spawn(async move { let paths = cx.background().spawn(async move {
let now = Local::now(); let now = Local::now();
let home_dir = dirs::home_dir().ok_or_else(|| anyhow!("can't determine home directory"))?; let home_dir = dirs::home_dir().ok_or_else(|| anyhow!("can't determine home directory"))?;
@ -50,13 +39,16 @@ pub fn new_journal_entry(app_state: Arc<AppState>, cx: &mut MutableAppContext) {
.update(|cx| workspace::open_paths(&[journal_dir], &app_state, cx)) .update(|cx| workspace::open_paths(&[journal_dir], &app_state, cx))
.await; .await;
workspace let opened = workspace
.update(&mut cx, |workspace, cx| { .update(&mut cx, |workspace, cx| {
workspace.open_paths(&[entry_path], cx) workspace.open_paths(&[entry_path], cx)
}) })
.await; .await;
dbg!(workspace); if let Some(Some(Ok(item))) = opened.first() {
log::info!("opened an item!");
}
Ok(()) Ok(())
} }
.log_err() .log_err()

View file

@ -494,7 +494,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 ItemHandle>, Arc<anyhow::Error>>>>> {
let entries = abs_paths let entries = abs_paths
.iter() .iter()
.cloned() .cloned()
@ -510,26 +514,26 @@ impl Workspace {
cx.spawn(|this, mut cx| { cx.spawn(|this, mut cx| {
let fs = fs.clone(); let fs = fs.clone();
async move { async move {
let project_path = project_path.await?; let project_path = project_path.await.ok()?;
if fs.is_file(&abs_path).await { if fs.is_file(&abs_path).await {
if let Some(entry) = if let Some(entry) =
this.update(&mut cx, |this, cx| this.open_entry(project_path, cx)) 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 { cx.foreground().spawn(async move {
let mut items = Vec::new();
for task in tasks { for task in tasks {
if let Err(error) = task.await { items.push(task.await);
log::error!("error opening paths {}", error);
}
} }
items
}) })
} }
@ -621,7 +625,7 @@ impl Workspace {
&mut self, &mut self,
project_path: ProjectPath, project_path: ProjectPath,
cx: &mut ViewContext<Self>, cx: &mut ViewContext<Self>,
) -> Option<Task<()>> { ) -> Option<Task<Result<Box<dyn ItemHandle>, Arc<anyhow::Error>>>> {
let pane = self.active_pane().clone(); let pane = self.active_pane().clone();
if self.activate_or_open_existing_entry(project_path.clone(), &pane, cx) { if self.activate_or_open_existing_entry(project_path.clone(), &pane, cx) {
return None; return None;
@ -676,21 +680,17 @@ impl Workspace {
this.update(&mut cx, |this, cx| { this.update(&mut cx, |this, cx| {
this.loading_items.remove(&project_path); this.loading_items.remove(&project_path);
if let Some(pane) = pane.upgrade(&cx) { let pane = pane
match load_result { .upgrade(&cx)
Ok(item) => { .ok_or_else(|| anyhow!("could not upgrade pane reference"))?;
// By the time loading finishes, the entry could have been already added let item = load_result?;
// to the pane. If it was, we activate it, otherwise we'll store the // By the time loading finishes, the entry could have been already added
// item and add a new view for it. // to the pane. If it was, we activate it, otherwise we'll store the
if !this.activate_or_open_existing_entry(project_path, &pane, cx) { // item and add a new view for it.
this.add_item(item, cx); if !this.activate_or_open_existing_entry(project_path, &pane, cx) {
} this.add_item(item.boxed_clone(), cx);
}
Err(error) => {
log::error!("error opening item: {}", error);
}
}
} }
Ok(item)
}) })
})) }))
} }