1use gpui::{ViewContext, ViewHandle};
2use workspace::Workspace;
3
4use crate::{get_working_directory, DeployModal, Event, Terminal};
5
6pub fn deploy_modal(workspace: &mut Workspace, _: &DeployModal, cx: &mut ViewContext<Workspace>) {
7 if let Some(stored_terminal) = cx.default_global::<Option<ViewHandle<Terminal>>>().clone() {
8 workspace.toggle_modal(cx, |_, _| stored_terminal);
9 } else {
10 let project = workspace.project().read(cx);
11 let abs_path = project
12 .active_entry()
13 .and_then(|entry_id| project.worktree_for_entry(entry_id, cx))
14 .and_then(|worktree_handle| worktree_handle.read(cx).as_local())
15 .and_then(get_working_directory);
16
17 let displaced_modal = workspace.toggle_modal(cx, |_, cx| {
18 let this = cx.add_view(|cx| Terminal::new(cx, abs_path, true));
19 cx.subscribe(&this, on_event).detach();
20 this
21 });
22 cx.set_global(displaced_modal);
23 }
24}
25
26pub fn on_event(
27 workspace: &mut Workspace,
28 _: ViewHandle<Terminal>,
29 event: &Event,
30 cx: &mut ViewContext<Workspace>,
31) {
32 // Dismiss the modal if the terminal quit
33 if let Event::CloseTerminal = event {
34 cx.set_global::<Option<ViewHandle<Terminal>>>(None);
35 if workspace
36 .modal()
37 .cloned()
38 .and_then(|modal| modal.downcast::<Terminal>())
39 .is_some()
40 {
41 workspace.dismiss_modal(cx)
42 }
43 }
44}