1use gpui::{ModelHandle, ViewContext};
2use workspace::Workspace;
3
4use crate::{get_wd_for_workspace, DeployModal, Event, TerminalConnection, TerminalView};
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| TerminalView::from_connection(stored_connection.clone(), true, cx))
20 });
21 cx.set_global::<Option<StoredConnection>>(Some(StoredConnection(
22 stored_connection.clone(),
23 )));
24 } else {
25 // No connection was stored, create a new terminal
26 if let Some(closed_terminal_handle) = workspace.toggle_modal(cx, |workspace, cx| {
27 let wd = get_wd_for_workspace(workspace, cx);
28
29 let this = cx.add_view(|cx| TerminalView::new(wd, true, cx));
30
31 let connection_handle = this.read(cx).connection.clone();
32 cx.subscribe(&connection_handle, on_event).detach();
33 //Set the global immediately, in case the user opens the command palette
34 cx.set_global::<Option<StoredConnection>>(Some(StoredConnection(
35 connection_handle.clone(),
36 )));
37 this
38 }) {
39 let connection = closed_terminal_handle.read(cx).connection.clone();
40 cx.set_global(Some(StoredConnection(connection)));
41 }
42 }
43
44 //The problem is that the terminal modal is never re-stored.
45}
46
47pub fn on_event(
48 workspace: &mut Workspace,
49 _: ModelHandle<TerminalConnection>,
50 event: &Event,
51 cx: &mut ViewContext<Workspace>,
52) {
53 // Dismiss the modal if the terminal quit
54 if let Event::CloseTerminal = event {
55 cx.set_global::<Option<StoredConnection>>(None);
56 if workspace
57 .modal()
58 .cloned()
59 .and_then(|modal| modal.downcast::<TerminalView>())
60 .is_some()
61 {
62 workspace.dismiss_modal(cx)
63 }
64 }
65}