modal_view.rs

 1use gpui::{ModelHandle, ViewContext};
 2use workspace::Workspace;
 3
 4use crate::{
 5    get_working_directory, model::Terminal, DeployModal, Event, TerminalContent, TerminalView,
 6};
 7
 8#[derive(Debug)]
 9struct StoredTerminal(ModelHandle<Terminal>);
10
11pub fn deploy_modal(workspace: &mut Workspace, _: &DeployModal, cx: &mut ViewContext<Workspace>) {
12    // Pull the terminal connection out of the global if it has been stored
13    let possible_terminal =
14        cx.update_default_global::<Option<StoredTerminal>, _, _>(|possible_connection, _| {
15            possible_connection.take()
16        });
17
18    if let Some(StoredTerminal(stored_terminal)) = possible_terminal {
19        workspace.toggle_modal(cx, |_, cx| {
20            // Create a view from the stored connection if the terminal modal is not already shown
21            cx.add_view(|cx| TerminalView::from_terminal(stored_terminal.clone(), true, cx))
22        });
23        // Toggle Modal will dismiss the terminal modal if it is currently shown, so we must
24        // store the terminal back in the global
25        cx.set_global::<Option<StoredTerminal>>(Some(StoredTerminal(stored_terminal.clone())));
26    } else {
27        // No connection was stored, create a new terminal
28        if let Some(closed_terminal_handle) = workspace.toggle_modal(cx, |workspace, cx| {
29            // No terminal modal visible, construct a new one.
30            let working_directory = get_working_directory(workspace, cx);
31
32            let this = cx.add_view(|cx| TerminalView::new(working_directory, true, cx));
33
34            if let TerminalContent::Connected(connected) = &this.read(cx).content {
35                let terminal_handle = connected.read(cx).handle();
36                cx.subscribe(&terminal_handle, on_event).detach();
37                // Set the global immediately if terminal construction was successful,
38                // in case the user opens the command palette
39                cx.set_global::<Option<StoredTerminal>>(Some(StoredTerminal(
40                    terminal_handle.clone(),
41                )));
42            }
43
44            this
45        }) {
46            // Terminal modal was dismissed. Store terminal if the terminal view is connected
47            if let TerminalContent::Connected(connected) = &closed_terminal_handle.read(cx).content
48            {
49                let terminal_handle = connected.read(cx).handle();
50                // Set the global immediately if terminal construction was successful,
51                // in case the user opens the command palette
52                cx.set_global::<Option<StoredTerminal>>(Some(StoredTerminal(
53                    terminal_handle.clone(),
54                )));
55            }
56        }
57    }
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<StoredTerminal>>(None);
69        if workspace.modal::<TerminalView>().is_some() {
70            workspace.dismiss_modal(cx)
71        }
72    }
73}