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
|
//
// FilePreviewQuickLookView.swift
// Hotline
//
// QuickLook-based file preview window for all supported file types
//
import SwiftUI
import UniformTypeIdentifiers
struct FilePreviewQuickLookView: View {
enum FilePreviewFocus: Hashable {
case window
}
@Environment(\.controlActiveState) private var controlActiveState
@Environment(\.colorScheme) private var colorScheme
@Environment(\.dismiss) var dismiss
@Binding var info: PreviewFileInfo?
@State var preview: FilePreviewState? = nil
@FocusState private var focusField: FilePreviewFocus?
var body: some View {
Group {
if preview?.state != .loaded {
VStack(alignment: .center, spacing: 0) {
Spacer()
ProgressView(value: max(0.0, min(1.0, preview?.progress ?? 0.0)))
.focusable(false)
.progressViewStyle(.circular)
.controlSize(.extraLarge)
.frame(maxWidth: 300, alignment: .center)
.padding(.bottom, 48)
Spacer()
}
.background(Color(nsColor: .textBackgroundColor))
.frame(minWidth: 350, maxWidth: .infinity, minHeight: 150, maxHeight: .infinity)
.padding()
}
else {
if let fileURL = preview?.fileURL {
QuickLookPreviewView(fileURL: fileURL)
.frame(minWidth: 400, maxWidth: .infinity, minHeight: 400, maxHeight: .infinity)
}
else {
VStack(alignment: .center, spacing: 0) {
Spacer()
Image(systemName: "eye.trianglebadge.exclamationmark")
.resizable()
.scaledToFit()
.frame(maxWidth: .infinity)
.frame(height: 48)
.padding(.bottom)
Group {
Text("This file type is not previewable")
.bold()
Text("Try downloading and opening this file in another application.")
.foregroundStyle(Color.secondary)
}
.font(.system(size: 14.0))
.frame(maxWidth: 300)
.multilineTextAlignment(.center)
Spacer()
Spacer()
}
.frame(minWidth: 350, maxWidth: .infinity, minHeight: 150, maxHeight: .infinity)
.padding()
}
}
}
.focusable()
.focusEffectDisabled()
.background(Color(nsColor: .textBackgroundColor))
.focused($focusField, equals: .window)
.navigationTitle(info?.name ?? "File Preview")
.background(
WindowConfigurator { window in
if let fileURL = preview?.fileURL {
window.representedURL = fileURL
window.standardWindowButton(.documentIconButton)?.isHidden = false
}
}
)
.toolbar {
if let _ = preview?.fileURL {
if let info = info {
ToolbarItem(placement: .primaryAction) {
Button {
if let fileURL = preview?.fileURL,
let data = try? Data(contentsOf: fileURL) {
let _ = data.saveAsFileToDownloads(filename: info.name)
}
} label: {
Label("Download File...", systemImage: "arrow.down")
}
.help("Download File")
}
}
}
}
.task {
if let info = info {
preview = FilePreviewState(info: info)
preview?.download()
}
}
.onAppear {
if info == nil {
Task {
dismiss()
}
return
}
focusField = .window
}
.onDisappear {
preview?.cancel()
dismiss()
}
.onChange(of: preview?.state) {
if preview?.state == .failed {
dismiss()
}
}
.preferredColorScheme(.dark)
}
}
|