1use gpui::{ModelHandle, ViewContext};
2use workspace::Workspace;
3
4use crate::{get_wd_for_workspace, DeployModal, Event, Terminal, TerminalConnection};
5
6#[derive(Debug)]
7struct StoredConnection(ModelHandle<TerminalConnection>);
8
9pub fn deploy_modal(workspace: &mut Workspace, _: &DeployModal, cx: &mut ViewContext<Workspace>) {
10 // Pull the terminal connection out of the global if it has been stored
11 let possible_connection =
12 cx.update_default_global::<Option<StoredConnection>, _, _>(|possible_connection, _| {
13 possible_connection.take()
14 });
15
16 if let Some(StoredConnection(stored_connection)) = possible_connection {
17 // Create a view from the stored connection
18 workspace.toggle_modal(cx, |_, cx| {
19 cx.add_view(|cx| Terminal::from_connection(stored_connection, true, cx))
20 });
21 } else {
22 // No connection was stored, create a new terminal
23 if let Some(closed_terminal_handle) = workspace.toggle_modal(cx, |workspace, cx| {
24 let wd = get_wd_for_workspace(workspace, cx);
25 let this = cx.add_view(|cx| Terminal::new(wd, true, cx));
26 let connection_handle = this.read(cx).connection.clone();
27 cx.subscribe(&connection_handle, on_event).detach();
28 //Set the global immediately, in case the user opens the command palette
29 cx.set_global::<Option<StoredConnection>>(Some(StoredConnection(
30 connection_handle.clone(),
31 )));
32 this
33 }) {
34 let connection = closed_terminal_handle.read(cx).connection.clone();
35 cx.set_global(Some(StoredConnection(connection)));
36 }
37 }
38
39 //The problem is that the terminal modal is never re-stored.
40}
41
42pub fn on_event(
43 workspace: &mut Workspace,
44 _: ModelHandle<TerminalConnection>,
45 event: &Event,
46 cx: &mut ViewContext<Workspace>,
47) {
48 // Dismiss the modal if the terminal quit
49 if let Event::CloseTerminal = event {
50 cx.set_global::<Option<StoredConnection>>(None);
51 if workspace
52 .modal()
53 .cloned()
54 .and_then(|modal| modal.downcast::<Terminal>())
55 .is_some()
56 {
57 workspace.dismiss_modal(cx)
58 }
59 }
60}