@@ -20,6 +20,8 @@ let kMinimumPeriod = 60 // Don't create snapshots spaced more closely than this
20
20
let kWindowWidthThreshhold : CGFloat = 600 // Finder Column view max width = 560
21
21
let kWindowHeightThreshhold : CGFloat = 160 // Get Info height = 128, QuickLook minimum window height = 180
22
22
23
+ enum PreviewType { case snapshot, webView, contactSheet }
24
+
23
25
// Window title helper
24
26
func displayname( title: String , size: CGSize , duration: Int , channels: Int ) -> String {
25
27
var channelstring : String
@@ -85,9 +87,6 @@ class PreviewViewController: NSViewController, QLPreviewingController, NSCollect
85
87
snapshot. layer!. backgroundColor = . black // CoreMedia previewer does this in Finder's Column & Gallery views
86
88
sidebarCollection. backgroundColors = [ . clear]
87
89
sidebarCollection. register ( NSNib ( nibNamed: " SidebarItem " , bundle: nil ) , forItemWithIdentifier: SidebarItem . identifier)
88
- webView. isHidden = true
89
- webView. underPageBackgroundColor = NSColor . clear
90
- webView. enclosingScrollView? . backgroundColor = NSColor . clear
91
90
}
92
91
93
92
func collectionView( _ collectionView: NSCollectionView , numberOfItemsInSection section: Int ) -> Int {
@@ -116,6 +115,20 @@ class PreviewViewController: NSViewController, QLPreviewingController, NSCollect
116
115
}
117
116
#endif
118
117
118
+ // Hide Views other than the one we want to show
119
+ func setupPreview( _ previewType: PreviewType ) {
120
+ switch previewType {
121
+ case . snapshot:
122
+ sidebar. removeFromSuperview ( )
123
+ webView. removeFromSuperview ( )
124
+ case . webView:
125
+ sidebar. removeFromSuperview ( )
126
+ snapshot. removeFromSuperview ( )
127
+ case . contactSheet:
128
+ webView. removeFromSuperview ( )
129
+ }
130
+ }
131
+
119
132
func preparePreviewOfSearchableItem( identifier: String , queryString: String ? ) async throws {
120
133
// Implement this method and set QLSupportsSearchableItems to YES in the Info.plist of the extension if you support CoreSpotlight.
121
134
#if DEBUG
@@ -156,19 +169,33 @@ class PreviewViewController: NSViewController, QLPreviewingController, NSCollect
156
169
snapshotSize = snapshotter. previewSize
157
170
let videoCodec = snapshotter. videoCodec
158
171
159
- // if extension of the file is webm, use WebView to load it
160
- if url. pathExtension == " webm " && ( videoCodec == " vp8 " || videoCodec == " vp9 " ) {
161
- preferredContentSize = snapshotSize
162
- webView. isHidden = false
163
- webView. loadFileURL ( url, allowingReadAccessTo: url)
164
- return
165
- }
166
172
// Should we prepare a full-sized (QLPreviewViewStyle.normal) preview for e.g. Finder's QuickLook
167
173
// or a single image (QLPreviewViewStyle.compact) for e.g. Finder's Column view and Get Info panel.
168
174
// Don't know how to get hold of QLPreviewViewStyle from here, so use window size to decide
169
175
if view. frame. width < kWindowWidthThreshhold || view. frame. height < kWindowHeightThreshhold {
170
176
// QLPreviewViewStyle.compact
171
- if let coverart = snapshotter. newCoverArt ( with: view. frame. width < kWindowHeightThreshhold ? . thumbnail : . default) {
177
+
178
+ // use WebView to load WEBM files
179
+ if url. pathExtension. caseInsensitiveCompare ( " webm " ) == . orderedSame && ( videoCodec == " vp8 " || videoCodec == " vp9 " ) {
180
+ // Fit to width
181
+ let size = NSSize ( width: view. frame. width, height: view. frame. width * snapshotSize. height / snapshotSize. width)
182
+ setupPreview ( . webView)
183
+ webView. loadFileURL ( url, allowingReadAccessTo: url)
184
+ webView. loadHTMLString (
185
+ """
186
+ <html>
187
+ <meta name= " viewport " content= " width= \( size. width) , height= \( size. height) " />
188
+ <body style= " background-color:black;margin:0;padding:0; " >
189
+ <video controls width= " width= \( size. width) " height= " \( size. height) " >
190
+ <source src= " \( url. lastPathComponent) " type= " video/webm " />
191
+ </body>
192
+ </html>
193
+ """ , baseURL: url. deletingLastPathComponent ( ) )
194
+ preferredContentSize = size
195
+ return
196
+ } else if let coverart = snapshotter. newCoverArt (
197
+ with: view. frame. width < kWindowHeightThreshhold ? . thumbnail : . default)
198
+ {
172
199
snapshotSize = CGSize ( width: coverart. width, height: coverart. height)
173
200
snapshot. image = NSImage ( cgImage: coverart, size: . zero)
174
201
} else {
@@ -206,7 +233,7 @@ class PreviewViewController: NSViewController, QLPreviewingController, NSCollect
206
233
userInfo: [ NSLocalizedFailureReasonErrorKey: " Can't supply anything " ] )
207
234
}
208
235
}
209
- sidebar . removeFromSuperview ( ) // Don't want sidebar
236
+ setupPreview ( . snapshot )
210
237
snapshot. frame = NSRect ( origin: CGPointZero, size: view. frame. size)
211
238
// Fit to width
212
239
preferredContentSize = NSSize (
@@ -223,6 +250,24 @@ class PreviewViewController: NSViewController, QLPreviewingController, NSCollect
223
250
title: snapshotter. title ?? url. lastPathComponent, size: snapshotter. displaySize, duration: snapshotter. duration,
224
251
channels: Int ( snapshotter. channels) )
225
252
253
+ // use WebView to load WEBM files
254
+ if url. pathExtension. caseInsensitiveCompare ( " webm " ) == . orderedSame && ( videoCodec == " vp8 " || videoCodec == " vp9 " ) {
255
+ setupPreview ( . webView)
256
+ webView. loadFileURL ( url, allowingReadAccessTo: url)
257
+ webView. loadHTMLString (
258
+ """
259
+ <html>
260
+ <meta name= " viewport " content= " width= \( snapshotSize. width) , height= \( snapshotSize. height) " />
261
+ <body style= " background-color:black;margin:0;padding:0; " >
262
+ <video controls autoplay width= " width= \( snapshotSize. width) " height= " \( snapshotSize. height) " >
263
+ <source src= " \( url. lastPathComponent) " type= " video/webm " />
264
+ </body>
265
+ </html>
266
+ """ , baseURL: url. deletingLastPathComponent ( ) )
267
+ preferredContentSize = NSSize ( width: snapshotSize. width, height: snapshotSize. height)
268
+ return
269
+ }
270
+
226
271
var imageCount = 0
227
272
228
273
if snapshotter. pictures > 0 {
@@ -278,11 +323,12 @@ class PreviewViewController: NSViewController, QLPreviewingController, NSCollect
278
323
domain: " uk.org.marginal.qlvideo " , code: - 1 , userInfo: [ NSLocalizedFailureReasonErrorKey: " Can't supply anything " ]
279
324
)
280
325
} else if images. count == 1 {
326
+ setupPreview ( . snapshot)
281
327
snapshot. image = images [ 0 ]
282
- sidebar. removeFromSuperview ( ) // no need for sidebar
283
328
snapshot. frame = NSRect ( origin: CGPointZero, size: view. frame. size)
284
329
preferredContentSize = snapshotSize
285
330
} else {
331
+ setupPreview ( . contactSheet)
286
332
snapshot. image = images [ 0 ]
287
333
if snapshotSize. width / snapshotSize. height > 4.0 / 3.0 {
288
334
( sidebarCollection. collectionViewLayout as! NSCollectionViewFlowLayout ) . itemSize = CGSize (
0 commit comments