modal.rs

 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.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            let this = cx.add_view(|cx| Terminal::new(wd, true, cx));
29            let connection_handle = this.read(cx).connection.clone();
30            cx.subscribe(&connection_handle, on_event).detach();
31            //Set the global immediately, in case the user opens the command palette
32            cx.set_global::<Option<StoredConnection>>(Some(StoredConnection(
33                connection_handle.clone(),
34            )));
35            this
36        }) {
37            let connection = closed_terminal_handle.read(cx).connection.clone();
38            cx.set_global(Some(StoredConnection(connection)));
39        }
40    }
41
42    //The problem is that the terminal modal is never re-stored.
43}
44
45pub fn on_event(
46    workspace: &mut Workspace,
47    _: ModelHandle<TerminalConnection>,
48    event: &Event,
49    cx: &mut ViewContext<Workspace>,
50) {
51    // Dismiss the modal if the terminal quit
52    if let Event::CloseTerminal = event {
53        cx.set_global::<Option<StoredConnection>>(None);
54        if workspace
55            .modal()
56            .cloned()
57            .and_then(|modal| modal.downcast::<Terminal>())
58            .is_some()
59        {
60            workspace.dismiss_modal(cx)
61        }
62    }
63}