Make global type more resilient, and fix modal keymap context

This commit is contained in:
Keith Simmons 2022-07-08 16:29:29 -07:00
parent 20f7fba16f
commit ed3666547b
2 changed files with 13 additions and 9 deletions

View file

@ -1,16 +1,18 @@
use gpui::{ModelHandle, ViewContext, ViewHandle}; use gpui::{ModelHandle, ViewContext};
use workspace::Workspace; use workspace::Workspace;
use crate::{get_working_directory, DeployModal, Event, Terminal, TerminalConnection}; use crate::{get_working_directory, DeployModal, Event, Terminal, TerminalConnection};
struct StoredConnection(ModelHandle<TerminalConnection>);
pub fn deploy_modal(workspace: &mut Workspace, _: &DeployModal, cx: &mut ViewContext<Workspace>) { pub fn deploy_modal(workspace: &mut Workspace, _: &DeployModal, cx: &mut ViewContext<Workspace>) {
// Pull the terminal connection out of the global if it has been stored // Pull the terminal connection out of the global if it has been stored
let possible_connection = cx let possible_connection =
.update_default_global::<Option<ModelHandle<TerminalConnection>>, _, _>( cx.update_default_global::<Option<StoredConnection>, _, _>(|possible_connection, _| {
|possible_connection, _| possible_connection.take(), possible_connection.take()
); });
if let Some(stored_connection) = possible_connection { if let Some(StoredConnection(stored_connection)) = possible_connection {
// Create a view from the stored connection // Create a view from the stored connection
workspace.toggle_modal(cx, |_, cx| { workspace.toggle_modal(cx, |_, cx| {
cx.add_view(|cx| Terminal::from_connection(stored_connection, true, cx)) cx.add_view(|cx| Terminal::from_connection(stored_connection, true, cx))
@ -31,7 +33,7 @@ pub fn deploy_modal(workspace: &mut Workspace, _: &DeployModal, cx: &mut ViewCon
this this
}) { }) {
let connection = closed_terminal_handle.read(cx).connection.clone(); let connection = closed_terminal_handle.read(cx).connection.clone();
cx.set_global(Some(connection)); cx.set_global(Some(StoredConnection(connection)));
} }
} }
} }
@ -44,7 +46,7 @@ pub fn on_event(
) { ) {
// Dismiss the modal if the terminal quit // Dismiss the modal if the terminal quit
if let Event::CloseTerminal = event { if let Event::CloseTerminal = event {
cx.set_global::<Option<ViewHandle<Terminal>>>(None); cx.set_global::<Option<StoredConnection>>(None);
if workspace if workspace
.modal() .modal()
.cloned() .cloned()

View file

@ -319,7 +319,9 @@ impl View for Terminal {
fn keymap_context(&self, _: &gpui::AppContext) -> gpui::keymap::Context { fn keymap_context(&self, _: &gpui::AppContext) -> gpui::keymap::Context {
let mut context = Self::default_keymap_context(); let mut context = Self::default_keymap_context();
context.set.insert("ModalTerminal".into()); if self.modal {
context.set.insert("ModalTerminal".into());
}
context context
} }
} }