Refactor terminal connection into a model which can be copied between terminal views

Refactor terminal modal code to use TerminalConnection model handle so we aren't storing TerminalViews in the globals
Adjust INSTANCE_BUFFER_SIZE in renderer to handle pathological terminal renders

Co-authored-by: mikayla.c.maki@gmail.com
This commit is contained in:
Keith Simmons 2022-07-08 16:10:09 -07:00
parent a82e56918e
commit 8d34fe7e94
9 changed files with 377 additions and 259 deletions

View file

@ -1,31 +1,48 @@
use gpui::{ViewContext, ViewHandle};
use gpui::{ModelHandle, ViewContext, ViewHandle};
use workspace::Workspace;
use crate::{get_working_directory, DeployModal, Event, Terminal};
use crate::{get_working_directory, DeployModal, Event, Terminal, TerminalConnection};
pub fn deploy_modal(workspace: &mut Workspace, _: &DeployModal, cx: &mut ViewContext<Workspace>) {
if let Some(stored_terminal) = cx.default_global::<Option<ViewHandle<Terminal>>>().clone() {
workspace.toggle_modal(cx, |_, _| stored_terminal);
} else {
let project = workspace.project().read(cx);
let abs_path = project
.active_entry()
.and_then(|entry_id| project.worktree_for_entry(entry_id, cx))
.and_then(|worktree_handle| worktree_handle.read(cx).as_local())
.and_then(get_working_directory);
// Pull the terminal connection out of the global if it has been stored
let possible_connection = cx
.update_default_global::<Option<ModelHandle<TerminalConnection>>, _, _>(
|possible_connection, _| possible_connection.take(),
);
let displaced_modal = workspace.toggle_modal(cx, |_, cx| {
let this = cx.add_view(|cx| Terminal::new(cx, abs_path, true));
cx.subscribe(&this, on_event).detach();
this
if let Some(stored_connection) = possible_connection {
println!("Found stored connection");
// Create a view from the stored connection
workspace.toggle_modal(cx, |_, cx| {
cx.add_view(|cx| Terminal::from_connection(stored_connection, true, cx))
});
cx.set_global(displaced_modal);
} else {
println!("No global connection :(");
// No connection was stored, create a new terminal
if let Some(closed_terminal_handle) = workspace.toggle_modal(cx, |workspace, cx| {
println!("Creating new terminal connection");
let project = workspace.project().read(cx);
let abs_path = project
.active_entry()
.and_then(|entry_id| project.worktree_for_entry(entry_id, cx))
.and_then(|worktree_handle| worktree_handle.read(cx).as_local())
.and_then(get_working_directory);
let this = cx.add_view(|cx| Terminal::new(abs_path, true, cx));
let connection_handle = this.read(cx).connection.clone();
cx.subscribe(&connection_handle, on_event).detach();
this
}) {
println!("Pulled connection from modal and stored in global");
let connection = closed_terminal_handle.read(cx).connection.clone();
cx.set_global(Some(connection));
}
}
}
pub fn on_event(
workspace: &mut Workspace,
_: ViewHandle<Terminal>,
_: ModelHandle<TerminalConnection>,
event: &Event,
cx: &mut ViewContext<Workspace>,
) {