modal.rs

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