modal.rs

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