forked from CodeEditApp/CodeEdit
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathTasksCommands.swift
More file actions
114 lines (97 loc) · 3.64 KB
/
TasksCommands.swift
File metadata and controls
114 lines (97 loc) · 3.64 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
//
// TasksCommands.swift
// CodeEdit
//
// Created by Khan Winter on 7/8/25.
//
import SwiftUI
import Combine
struct TasksCommands: Commands {
@UpdatingWindowController var windowController: CodeEditWindowController?
var taskManager: TaskManager? {
windowController?.workspace?.taskManager
}
@State private var activeTaskStatus: CETaskStatus = .notRunning
@State private var taskManagerListener: AnyCancellable?
@State private var statusListener: AnyCancellable?
var body: some Commands {
CommandMenu("Tasks") {
let selectedTaskName: String = if let selectedTask = taskManager?.selectedTask {
"\"" + selectedTask.name + "\""
} else {
"(No Selected Task)"
}
Button("Run \(selectedTaskName)", systemImage: "play.fill") {
taskManager?.executeActiveTask()
showOutput()
}
.keyboardShortcut("R")
.disabled(taskManager?.selectedTaskID == nil)
Button("Stop \(selectedTaskName)", systemImage: "stop.fill") {
taskManager?.terminateActiveTask()
}
.keyboardShortcut(".")
.onChange(of: windowController) { _ in
taskManagerListener = taskManager?.objectWillChange.sink {
updateStatusListener()
}
}
.disabled(activeTaskStatus != .running)
Button("Show \(selectedTaskName) Output") {
showOutput()
}
// Disable when there's no output yet
.disabled(taskManager?.activeTasks[taskManager?.selectedTaskID ?? UUID()] == nil)
Divider()
Menu {
if let taskManager {
ForEach(taskManager.availableTasks) { task in
Button(task.name) {
taskManager.selectedTaskID = task.id
}
}
}
if taskManager?.availableTasks.isEmpty ?? true {
Button("Create Tasks") {
openSettings()
}
}
} label: {
Text("Choose Task...")
}
.disabled(taskManager?.availableTasks.isEmpty == true)
Button("Manage Tasks...") {
openSettings()
}
.disabled(windowController == nil)
}
}
/// Update the ``statusListener`` to listen to a potentially new active task.
private func updateStatusListener() {
statusListener?.cancel()
guard let taskManager else { return }
activeTaskStatus = taskManager.activeTasks[taskManager.selectedTaskID ?? UUID()]?.status ?? .notRunning
guard let id = taskManager.selectedTaskID else { return }
statusListener = taskManager.activeTasks[id]?.$status.sink { newValue in
activeTaskStatus = newValue
}
}
private func showOutput() {
guard let utilityAreaModel = windowController?.workspace?.utilityAreaModel else {
return
}
if utilityAreaModel.isCollapsed {
// Open the utility area
utilityAreaModel.isCollapsed.toggle()
}
utilityAreaModel.selectedTab = .debugConsole // Switch to the correct tab
taskManager?.taskShowingOutput = taskManager?.selectedTaskID // Switch to the selected task
}
private func openSettings() {
NSApp.sendAction(
#selector(CodeEditWindowController.openWorkspaceSettings(_:)),
to: windowController,
from: nil
)
}
}