-
-
Notifications
You must be signed in to change notification settings - Fork 14
/
NewCommandScriptView.swift
202 lines (182 loc) · 5.66 KB
/
NewCommandScriptView.swift
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
import SwiftUI
struct NewCommandScriptView: View {
enum Kind: String, CaseIterable, Hashable, Identifiable {
var id: String { rawValue }
case file = "File"
case source = "Source"
}
enum ScriptExtension: String, CaseIterable, Hashable, Identifiable {
var id: String { rawValue }
// TODO: Add support for `.swift`
case appleScript = "scpt"
case shellScript = "sh"
var displayName: String {
switch self {
case .shellScript:
return "Shellscript"
case .appleScript:
return "AppleScript"
}
}
var syntax: any SyntaxHighlighting {
switch self {
case .appleScript:
return AppleScriptHighlighting()
case .shellScript:
return ShellScriptHighlighting()
}
}
}
@EnvironmentObject var openPanel: OpenPanelController
@State private var kind: Kind
@State private var scriptExtension: ScriptExtension
@State private var value: String
@Binding private var payload: NewCommandPayload
@Binding private var validation: NewCommandValidation
init(_ payload: Binding<NewCommandPayload>,
kind: Kind,
value: String,
scriptExtension: ScriptExtension,
validation: Binding<NewCommandValidation>) {
_kind = .init(initialValue: kind)
_scriptExtension = .init(initialValue: scriptExtension)
_value = .init(initialValue: value)
_payload = payload
_validation = validation
}
var body: some View {
VStack(alignment: .leading) {
Label(title: { Text("Open file or folder:") }, icon: { EmptyView() })
.labelStyle(HeaderLabelStyle())
HStack {
Menu(content: {
ForEach(ScriptExtension.allCases) { scriptExtension in
Button(action: { self.scriptExtension = scriptExtension }, label: {
Text(scriptExtension.displayName)
})
}
}, label: {
Text(scriptExtension.displayName)
})
Menu(content: {
ForEach(Kind.allCases) { kind in
Button(action: { self.kind = kind }, label: {
Text(kind.rawValue)
})
}
}, label: {
switch kind {
case .file:
Text("File")
case .source:
Text("Inline")
}
})
}
.menuStyle(GradientMenuStyle(.init(nsColor: .systemGray), fixedSize: false))
.padding(.vertical, 8)
switch kind {
case .file:
NewCommandFileSelectorView($scriptExtension, path: value) { path in
value = path
validation = updateAndValidatePayload()
}
.overlay(NewCommandValidationView($validation))
case .source:
VStack {
NewCommandScriptSourceView($scriptExtension, text: value) { newString in
value = newString
validation = updateAndValidatePayload()
}
.overlay(NewCommandValidationView($validation))
}
}
}
.menuStyle(.appStyle(padding: 4))
.onChange(of: validation, perform: { newValue in
guard newValue == .needsValidation else { return }
validation = updateAndValidatePayload()
})
.onAppear {
validation = .unknown
}
}
@discardableResult
private func updateAndValidatePayload() -> NewCommandValidation {
switch kind {
case .file:
payload = .script(value: value, kind: kind, scriptExtension: scriptExtension)
if value.hasSuffix(scriptExtension.rawValue) {
return .valid
} else {
return .invalid(reason: "Wrong file extension.")
}
case .source:
payload = .script(value: value, kind: kind, scriptExtension: scriptExtension)
return .valid
}
}
}
struct NewCommandFileSelectorView: View {
@EnvironmentObject var openPanel: OpenPanelController
@State var path: String = "~/"
@Binding private var scriptExtension: NewCommandScriptView.ScriptExtension
private var onPathChange: (String) -> Void
init(_ scriptExtension: Binding<NewCommandScriptView.ScriptExtension>,
path: String,
onPathChange: @escaping (String) -> Void) {
_path = .init(initialValue: path)
self.onPathChange = onPathChange
_scriptExtension = scriptExtension
}
var body: some View {
HStack {
TextField("Path", text: $path)
.textFieldStyle(FileSystemTextFieldStyle())
.onChange(of: path) { newPath in
onPathChange(newPath)
}
Button("Browse", action: {
openPanel.perform(.selectFile(type: scriptExtension.rawValue, handler: { newPath in
self.path = newPath
onPathChange(newPath)
}))
})
.buttonStyle(.gradientStyle(config: .init(nsColor: .systemBlue, grayscaleEffect: true)))
}
.padding(4)
.background {
RoundedRectangle(cornerRadius: 4)
.stroke(Color(.white).opacity(0.2), lineWidth: 1)
}
}
}
struct NewCommandScriptSourceView: View {
@State var text: String = ""
@Binding private var kind: NewCommandScriptView.ScriptExtension
private let onChange: (String) -> Void
init(_ kind: Binding<NewCommandScriptView.ScriptExtension>,
text: String,
onChange: @escaping (String) -> Void) {
_text = .init(initialValue: text)
_kind = kind
self.onChange = onChange
}
var body: some View {
ScriptEditorView(text: $text, syntax: Binding(get: { kind.syntax }, set: { _ in }))
.onChange(of: text, perform: onChange)
}
}
struct NewCommandScriptView_Previews: PreviewProvider {
static var previews: some View {
NewCommandView(
workflowId: UUID().uuidString,
commandId: nil,
title: "New command",
selection: .script,
payload: .placeholder,
onDismiss: {},
onSave: { _, _ in })
.designTime()
}
}