modal.rs

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