1use project::TaskSourceKind;
2use remote::ConnectionState;
3use task::{NewCenterTask, ResolvedTask, TaskContext, TaskTemplate};
4use ui::ViewContext;
5use zed_actions::TaskSpawnTarget;
6
7use crate::Workspace;
8
9pub fn schedule_task(
10 workspace: &mut Workspace,
11 task_source_kind: TaskSourceKind,
12 task_to_resolve: &TaskTemplate,
13 task_cx: &TaskContext,
14 task_target: zed_actions::TaskSpawnTarget,
15 omit_history: bool,
16 cx: &mut ViewContext<'_, Workspace>,
17) {
18 match workspace.project.read(cx).ssh_connection_state(cx) {
19 None | Some(ConnectionState::Connected) => {}
20 Some(
21 ConnectionState::Connecting
22 | ConnectionState::Disconnected
23 | ConnectionState::HeartbeatMissed
24 | ConnectionState::Reconnecting,
25 ) => {
26 log::warn!("Cannot schedule tasks when disconnected from a remote host");
27 return;
28 }
29 }
30
31 if let Some(spawn_in_terminal) =
32 task_to_resolve.resolve_task(&task_source_kind.to_id_base(), task_target, task_cx)
33 {
34 schedule_resolved_task(
35 workspace,
36 task_source_kind,
37 spawn_in_terminal,
38 omit_history,
39 cx,
40 );
41 }
42}
43
44pub fn schedule_resolved_task(
45 workspace: &mut Workspace,
46 task_source_kind: TaskSourceKind,
47 mut resolved_task: ResolvedTask,
48 omit_history: bool,
49 cx: &mut ViewContext<'_, Workspace>,
50) {
51 let target = resolved_task.target;
52 if let Some(spawn_in_terminal) = resolved_task.resolved.take() {
53 if !omit_history {
54 resolved_task.resolved = Some(spawn_in_terminal.clone());
55 workspace.project().update(cx, |project, cx| {
56 if let Some(task_inventory) =
57 project.task_store().read(cx).task_inventory().cloned()
58 {
59 task_inventory.update(cx, |inventory, _| {
60 inventory.task_scheduled(task_source_kind, resolved_task);
61 })
62 }
63 });
64 }
65
66 match target {
67 TaskSpawnTarget::Center => {
68 cx.dispatch_action(Box::new(NewCenterTask {
69 action: spawn_in_terminal,
70 }));
71 }
72 TaskSpawnTarget::Dock => {
73 cx.emit(crate::Event::SpawnTask {
74 action: Box::new(spawn_in_terminal),
75 });
76 }
77 }
78 }
79}