This commit is contained in:
KCaverly 2023-10-31 11:32:56 -04:00
parent 5550e80c4e
commit 68a1c7ce4c
5 changed files with 110 additions and 111 deletions

View file

@ -120,7 +120,7 @@ type FrameCallback = Box<dyn FnOnce(&mut WindowContext) + Send>;
type Handler = Box<dyn FnMut(&mut AppContext) -> bool + Send + 'static>; type Handler = Box<dyn FnMut(&mut AppContext) -> bool + Send + 'static>;
type Listener = Box<dyn FnMut(&dyn Any, &mut AppContext) -> bool + Send + 'static>; type Listener = Box<dyn FnMut(&dyn Any, &mut AppContext) -> bool + Send + 'static>;
type QuitHandler = Box<dyn FnMut(&mut AppContext) -> BoxFuture<'static, ()> + Send + 'static>; type QuitHandler = Box<dyn FnMut(&mut AppContext) -> BoxFuture<'static, ()> + Send + 'static>;
type ReleaseListener = Box<dyn FnMut(&mut dyn Any, &mut AppContext) + Send + 'static>; type ReleaseListener = Box<dyn FnOnce(&mut dyn Any, &mut AppContext) + Send + 'static>;
pub struct AppContext { pub struct AppContext {
this: Weak<Mutex<AppContext>>, this: Weak<Mutex<AppContext>>,
@ -408,7 +408,7 @@ impl AppContext {
for (entity_id, mut entity) in dropped { for (entity_id, mut entity) in dropped {
self.observers.remove(&entity_id); self.observers.remove(&entity_id);
self.event_listeners.remove(&entity_id); self.event_listeners.remove(&entity_id);
for mut release_callback in self.release_listeners.remove(&entity_id) { for release_callback in self.release_listeners.remove(&entity_id) {
release_callback(&mut entity, self); release_callback(&mut entity, self);
} }
} }
@ -697,7 +697,7 @@ impl AppContext {
pub fn observe_release<E, T>( pub fn observe_release<E, T>(
&mut self, &mut self,
handle: &E, handle: &E,
mut on_release: impl FnMut(&mut T, &mut AppContext) + Send + 'static, on_release: impl FnOnce(&mut T, &mut AppContext) + Send + 'static,
) -> Subscription ) -> Subscription
where where
E: Entity<T>, E: Entity<T>,

View file

@ -393,18 +393,18 @@ impl Bind for WindowBounds {
} }
}; };
todo!()
// statement.bind( // statement.bind(
// &region.map(|region| { // &region.map(|region| {
// ( // (
// region.min_x(), // region.origin.x,
// region.min_y(), // region.origin.y,
// region.width(), // region.size.width,
// region.height(), // region.size.height,
// ) // )
// }), // }),
// next_index, // next_index,
// ) // )
todo!()
} }
} }

View file

@ -1675,7 +1675,7 @@ impl<'a, 'w, V: 'static> ViewContext<'a, 'w, V> {
pub fn on_release( pub fn on_release(
&mut self, &mut self,
mut on_release: impl FnMut(&mut V, &mut WindowContext) + Send + 'static, on_release: impl FnOnce(&mut V, &mut WindowContext) + Send + 'static,
) -> Subscription { ) -> Subscription {
let window_handle = self.window.handle; let window_handle = self.window.handle;
self.app.release_listeners.insert( self.app.release_listeners.insert(

View file

@ -255,7 +255,7 @@ pub trait ItemHandle: 'static + Send {
fn on_release( fn on_release(
&mut self, &mut self,
cx: &mut AppContext, cx: &mut AppContext,
callback: Box<dyn FnMut(&mut AppContext) + Send>, callback: Box<dyn FnOnce(&mut AppContext) + Send>,
) -> gpui2::Subscription; ) -> gpui2::Subscription;
fn to_searchable_item_handle(&self, cx: &AppContext) -> Option<Box<dyn SearchableItemHandle>>; fn to_searchable_item_handle(&self, cx: &AppContext) -> Option<Box<dyn SearchableItemHandle>>;
fn breadcrumb_location(&self, cx: &AppContext) -> ToolbarItemLocation; fn breadcrumb_location(&self, cx: &AppContext) -> ToolbarItemLocation;
@ -573,7 +573,7 @@ impl<T: Item> ItemHandle for View<T> {
fn on_release( fn on_release(
&mut self, &mut self,
cx: &mut AppContext, cx: &mut AppContext,
mut callback: Box<dyn FnMut(&mut AppContext) + Send>, callback: Box<dyn FnOnce(&mut AppContext) + Send>,
) -> gpui2::Subscription { ) -> gpui2::Subscription {
cx.observe_release(self, move |_, cx| callback(cx)) cx.observe_release(self, move |_, cx| callback(cx))
} }

View file

@ -4,9 +4,8 @@ mod only_instance;
mod open_listener; mod open_listener;
pub use assets::*; pub use assets::*;
use client2::{Client, UserStore};
use collections::HashMap; use collections::HashMap;
use gpui2::{AsyncAppContext, Model}; use gpui2::{AsyncAppContext, Point};
pub use only_instance::*; pub use only_instance::*;
pub use open_listener::*; pub use open_listener::*;
@ -21,6 +20,7 @@ use futures::{
}; };
use std::{path::Path, sync::Arc, thread, time::Duration}; use std::{path::Path, sync::Arc, thread, time::Duration};
use util::{paths::PathLikeWithPosition, ResultExt}; use util::{paths::PathLikeWithPosition, ResultExt};
use workspace2::AppState;
pub fn connect_to_cli( pub fn connect_to_cli(
server_name: &str, server_name: &str,
@ -51,11 +51,6 @@ pub fn connect_to_cli(
Ok((async_request_rx, response_tx)) Ok((async_request_rx, response_tx))
} }
pub struct AppState {
pub client: Arc<Client>,
pub user_store: Model<UserStore>,
}
pub async fn handle_cli_connection( pub async fn handle_cli_connection(
(mut requests, responses): (mpsc::Receiver<CliRequest>, IpcSender<CliResponse>), (mut requests, responses): (mpsc::Receiver<CliRequest>, IpcSender<CliResponse>),
app_state: Arc<AppState>, app_state: Arc<AppState>,
@ -96,118 +91,122 @@ pub async fn handle_cli_connection(
} }
Some(path) Some(path)
}) })
.collect() .collect::<Vec<_>>()
}; };
let mut errored = false; let mut errored = false;
match cx if let Some(open_paths_task) = cx
.update(|cx| workspace2::open_paths(&paths, &app_state, None, cx)) .update(|cx| workspace2::open_paths(&paths, &app_state, None, cx))
.await .log_err()
{ {
Ok((workspace, items)) => { match open_paths_task.await {
let mut item_release_futures = Vec::new(); Ok((workspace, items)) => {
let mut item_release_futures = Vec::new();
for (item, path) in items.into_iter().zip(&paths) { for (item, path) in items.into_iter().zip(&paths) {
match item { match item {
Some(Ok(item)) => { Some(Ok(mut item)) => {
if let Some(point) = caret_positions.remove(path) { if let Some(point) = caret_positions.remove(path) {
todo!() todo!()
// if let Some(active_editor) = item.downcast::<Editor>() { // if let Some(active_editor) = item.downcast::<Editor>() {
// active_editor // active_editor
// .downgrade() // .downgrade()
// .update(&mut cx, |editor, cx| { // .update(&mut cx, |editor, cx| {
// let snapshot = // let snapshot =
// editor.snapshot(cx).display_snapshot; // editor.snapshot(cx).display_snapshot;
// let point = snapshot // let point = snapshot
// .buffer_snapshot // .buffer_snapshot
// .clip_point(point, Bias::Left); // .clip_point(point, Bias::Left);
// editor.change_selections( // editor.change_selections(
// Some(Autoscroll::center()), // Some(Autoscroll::center()),
// cx, // cx,
// |s| s.select_ranges([point..point]), // |s| s.select_ranges([point..point]),
// ); // );
// }) // })
// .log_err(); // .log_err();
// } // }
} }
let released = oneshot::channel(); let released = oneshot::channel();
cx.update(|cx| { cx.update(move |cx| {
item.on_release( item.on_release(
cx, cx,
Box::new(move |_| { Box::new(move |_| {
let _ = released.0.send(()); let _ = released.0.send(());
}), }),
) )
.detach(); .detach();
});
item_release_futures.push(released.1);
}
Some(Err(err)) => {
responses
.send(CliResponse::Stderr {
message: format!("error opening {:?}: {}", path, err),
})
.log_err();
errored = true;
}
None => {}
}
}
if wait {
let executor = cx.executor();
let wait = async move {
if paths.is_empty() {
let (done_tx, done_rx) = oneshot::channel();
if let Some(workspace) = workspace.upgrade(&cx) {
let _subscription = cx.update(|cx| {
cx.observe_release(&workspace, move |_, _| {
let _ = done_tx.send(());
})
}); });
item_release_futures.push(released.1);
}
Some(Err(err)) => {
responses
.send(CliResponse::Stderr {
message: format!(
"error opening {:?}: {}",
path, err
),
})
.log_err();
errored = true;
}
None => {}
}
}
if wait {
let executor = cx.executor().clone();
let wait = async move {
if paths.is_empty() {
let (done_tx, done_rx) = oneshot::channel();
let _subscription =
cx.update_window_root(&workspace, move |_, cx| {
cx.on_release(|_, _| {
let _ = done_tx.send(());
})
});
drop(workspace); drop(workspace);
let _ = done_rx.await; let _ = done_rx.await;
} } else {
} else { let _ = futures::future::try_join_all(item_release_futures)
let _ = .await;
futures::future::try_join_all(item_release_futures).await; };
}; }
} .fuse();
.fuse(); futures::pin_mut!(wait);
futures::pin_mut!(wait);
loop { loop {
// Repeatedly check if CLI is still open to avoid wasting resources // Repeatedly check if CLI is still open to avoid wasting resources
// waiting for files or workspaces to close. // waiting for files or workspaces to close.
let mut timer = executor.timer(Duration::from_secs(1)).fuse(); let mut timer = executor.timer(Duration::from_secs(1)).fuse();
futures::select_biased! { futures::select_biased! {
_ = wait => break, _ = wait => break,
_ = timer => { _ = timer => {
if responses.send(CliResponse::Ping).is_err() { if responses.send(CliResponse::Ping).is_err() {
break; break;
}
} }
} }
} }
} }
} }
Err(error) => {
errored = true;
responses
.send(CliResponse::Stderr {
message: format!("error opening {:?}: {}", paths, error),
})
.log_err();
}
} }
Err(error) => {
errored = true;
responses
.send(CliResponse::Stderr {
message: format!("error opening {:?}: {}", paths, error),
})
.log_err();
}
}
responses responses
.send(CliResponse::Exit { .send(CliResponse::Exit {
status: i32::from(errored), status: i32::from(errored),
}) })
.log_err(); .log_err();
}
} }
} }
} }