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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
|
import SwiftUI
import UniformTypeIdentifiers
struct FileView: View {
@Environment(Hotline.self) private var model: Hotline
@State var expanded = false
@State var loading = false
var file: FileInfo
let depth: Int
var body: some View {
HStack {
if file.isFolder {
Button {
if file.isFolder {
file.expanded.toggle()
}
} label: {
Text(Image(systemName: file.expanded ? "chevron.down" : "chevron.right"))
.bold()
.font(.system(size: 10))
.opacity(0.5)
}
.buttonStyle(.plain)
.frame(width: 10)
.padding(.leading, 4)
}
else {
HStack {
}.frame(width: 10)
.padding(.leading, 4)
}
HStack(alignment: .center) {
if file.isUnavailable {
Image(systemName: "questionmark.app.fill").opacity(0.5)
}
else if file.isFolder {
Image(systemName: "folder.fill")
}
else {
FileIconView(filename: file.name)
.frame(width: 16, height: 16)
}
}
.frame(width: 15)
Text(file.name).lineLimit(1).truncationMode(.tail).opacity(file.isUnavailable ? 0.5 : 1.0)
if file.isFolder && loading {
ProgressView().controlSize(.small).padding([.leading, .trailing], 1)
}
Spacer()
if !file.isUnavailable {
if file.isFolder {
Text("^[\(file.fileSize) file](inflect: true)")
.foregroundStyle(.secondary)
.lineLimit(1)
}
else {
Text(formattedFileSize(file.fileSize)).foregroundStyle(.secondary).lineLimit(1)
}
}
}
.frame(maxWidth: .infinity, maxHeight: .infinity)
.padding(.leading, CGFloat(depth * (12 + 10)))
.onChange(of: file.expanded) {
loading = false
if file.isFolder && file.fileSize > 0 {
if file.expanded {
Task {
loading = true
let _ = await model.getFileList(path: file.path)
loading = false
}
}
}
}
if file.expanded {
ForEach(file.children!, id: \.self) { childFile in
FileView(file: childFile, depth: self.depth + 1).tag(file.id)
// .environment(self.selectedFile)
// .frame(height: 34)
}
}
}
static let byteFormatter = ByteCountFormatter()
private func formattedFileSize(_ fileSize: UInt) -> String {
// let bcf = ByteCountFormatter()
FileView.byteFormatter.allowedUnits = [.useAll]
FileView.byteFormatter.countStyle = .file
return FileView.byteFormatter.string(fromByteCount: Int64(fileSize))
}
}
struct FilesView: View {
@Environment(Hotline.self) private var model: Hotline
@Environment(\.openWindow) private var openWindow
@State private var selection: FileInfo?
@State private var fileDetails: FileDetails?
private func openPreviewWindow(_ previewInfo: PreviewFileInfo) {
switch previewInfo.previewType {
case .image:
openWindow(id: "preview-image", value: previewInfo)
case .text:
openWindow(id: "preview-text", value: previewInfo)
default:
return
}
// let _ = FilePreviewWindowController(info: previewInfo)
}
var body: some View {
NavigationStack {
List(model.files, id: \.self, selection: $selection) { file in
FileView(file: file, depth: 0).tag(file.id)
// .environment(self.fileSelection)
// .frame(height: 34)
}
.environment(\.defaultMinListRowHeight, 34)
.listStyle(.inset)
.alternatingRowBackgrounds(.enabled)
.task {
if !model.filesLoaded {
let _ = await model.getFileList()
}
}
.contextMenu(forSelectionType: FileInfo.self) { items in
// ...
} primaryAction: { items in
guard let clickedFile = items.first else {
return
}
self.selection = clickedFile
if clickedFile.isFolder {
clickedFile.expanded.toggle()
}
else {
model.downloadFile(clickedFile.name, path: clickedFile.path)
}
}
.onKeyPress(.rightArrow) {
if let s = selection, s.isFolder {
s.expanded = true
return .handled
}
return .ignored
}
.onKeyPress(.leftArrow) {
if let s = selection, s.isFolder {
s.expanded = false
return .handled
}
return .ignored
}
.onKeyPress(.space) {
if let s = selection, s.isPreviewable {
model.previewFile(s.name, path: s.path) { info in
if let info = info {
openPreviewWindow(info)
}
}
return .handled
}
return .ignored
}
.overlay {
if !model.filesLoaded {
VStack {
ProgressView()
.controlSize(.large)
}
.frame(maxWidth: .infinity)
}
}
.toolbar {
ToolbarItem(placement: .primaryAction) {
Button {
} label: {
Label("Delete File", systemImage: "trash")
}
.help("Delete")
}
ToolbarItem(placement: .primaryAction) {
Button {
if let s = selection, !s.isFolder {
model.previewFile(s.name, path: s.path) { info in
if let info = info {
openPreviewWindow(info)
}
}
}
} label: {
Label("Preview File", systemImage: "eye")
}
.help("Preview File")
.disabled(selection?.isPreviewable == false)
}
ToolbarItem(placement: .primaryAction) {
Button {
if let s = selection {
model.fileDetails(s.name, path: s.path) { info in
fileDetails = info
}
}
} label: {
Label("Get File Info", systemImage: "info.circle")
}
.help("Get File Info")
}
ToolbarItem(placement: .primaryAction) {
Button {
if let s = selection, !s.isFolder {
model.downloadFile(s.name, path: s.path)
}
} label: {
Label("Download File", systemImage: "square.and.arrow.down")
}
.help("Download")
}
}
}
.sheet(item: $fileDetails ) { item in
FileDetailsView(fd: item)
}
}
}
#Preview {
FilesView()
.environment(Hotline(trackerClient: HotlineTrackerClient(), client: HotlineClient()))
}
|