Fix thumbnail provider
This commit is contained in:
parent
7f95910528
commit
1c52fc0e8e
2 changed files with 52 additions and 60 deletions
|
@ -2,9 +2,9 @@
|
||||||
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
|
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
|
||||||
<plist version="1.0">
|
<plist version="1.0">
|
||||||
<dict>
|
<dict>
|
||||||
<key>com.apple.security.app-sandbox</key>
|
<key>com.apple.security.app-sandbox</key>
|
||||||
<true/>
|
<true/>
|
||||||
<key>com.apple.security.files.user-selected.read-only</key>
|
<key>com.apple.security.files.user-selected.read-only</key>
|
||||||
<true/>
|
<true/>
|
||||||
</dict>
|
</dict>
|
||||||
</plist>
|
</plist>
|
||||||
|
|
|
@ -4,63 +4,55 @@ import Cocoa
|
||||||
|
|
||||||
class ThumbnailProvider: QLThumbnailProvider {
|
class ThumbnailProvider: QLThumbnailProvider {
|
||||||
override func provideThumbnail(for request: QLFileThumbnailRequest, _ handler: @escaping (QLThumbnailReply?, Error?) -> Void) {
|
override func provideThumbnail(for request: QLFileThumbnailRequest, _ handler: @escaping (QLThumbnailReply?, Error?) -> Void) {
|
||||||
let fc: NSFileCoordinator = NSFileCoordinator()
|
do {
|
||||||
let intent: NSFileAccessIntent = NSFileAccessIntent.readingIntent(with: request.fileURL)
|
guard let archive = Archive(data: try Data(contentsOf: request.fileURL), accessMode: Archive.AccessMode.read) else {
|
||||||
fc.coordinate(with: [intent], queue: .main) { (err) in
|
return
|
||||||
if err == nil {
|
|
||||||
do {
|
|
||||||
guard let archive = Archive(data: try Data(contentsOf: intent.url), accessMode: Archive.AccessMode.read) else {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
guard let entry = archive[ThumbnailPath] else {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
guard let thumbnailData = readData(archive: archive, entry: entry) else {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
let image = NSImage(data: thumbnailData)
|
|
||||||
|
|
||||||
let maximumSize = request.maximumSize
|
|
||||||
let imageSize = image?.size
|
|
||||||
|
|
||||||
var newImageSize = maximumSize
|
|
||||||
var contextSize = maximumSize
|
|
||||||
let aspectRatio = imageSize!.height / imageSize!.width
|
|
||||||
let proposedHeight = aspectRatio * maximumSize.width
|
|
||||||
|
|
||||||
if proposedHeight <= maximumSize.height {
|
|
||||||
newImageSize.height = proposedHeight
|
|
||||||
contextSize.height = max(proposedHeight.rounded(.down), request.minimumSize.height)
|
|
||||||
} else {
|
|
||||||
newImageSize.width = maximumSize.height / aspectRatio
|
|
||||||
contextSize.width = max(newImageSize.width.rounded(.down), request.minimumSize.width)
|
|
||||||
}
|
|
||||||
|
|
||||||
let reply: QLThumbnailReply = QLThumbnailReply.init(contextSize: contextSize) { () -> Bool in
|
|
||||||
if image != nil {
|
|
||||||
image!.draw(in: CGRect(x: contextSize.width/2 - newImageSize.width/2,
|
|
||||||
y: contextSize.height/2 - newImageSize.height/2,
|
|
||||||
width: newImageSize.width,
|
|
||||||
height: newImageSize.height));
|
|
||||||
|
|
||||||
return true
|
|
||||||
} else {
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
handler(reply, nil)
|
|
||||||
} catch {
|
|
||||||
NSLog("Could not load file \(intent.url.lastPathComponent) to preview it")
|
|
||||||
handler(nil, nil)
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
NSLog("Could not find file \(intent.url.lastPathComponent) to preview it")
|
|
||||||
handler(nil, nil)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
guard let entry = archive[ThumbnailPath] else {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
guard let thumbnailData = readData(archive: archive, entry: entry) else {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
let image = NSImage(data: thumbnailData)
|
||||||
|
|
||||||
|
let maximumSize = request.maximumSize
|
||||||
|
let imageSize = image?.size
|
||||||
|
|
||||||
|
var newImageSize = maximumSize
|
||||||
|
var contextSize = maximumSize
|
||||||
|
let aspectRatio = imageSize!.height / imageSize!.width
|
||||||
|
let proposedHeight = aspectRatio * maximumSize.width
|
||||||
|
|
||||||
|
if proposedHeight <= maximumSize.height {
|
||||||
|
newImageSize.height = proposedHeight
|
||||||
|
contextSize.height = max(proposedHeight.rounded(.down), request.minimumSize.height)
|
||||||
|
} else {
|
||||||
|
newImageSize.width = maximumSize.height / aspectRatio
|
||||||
|
contextSize.width = max(newImageSize.width.rounded(.down), request.minimumSize.width)
|
||||||
|
}
|
||||||
|
|
||||||
|
let reply: QLThumbnailReply = QLThumbnailReply.init(contextSize: contextSize) { () -> Bool in
|
||||||
|
if image != nil {
|
||||||
|
image!.draw(in: CGRect(x: contextSize.width/2 - newImageSize.width/2,
|
||||||
|
y: contextSize.height/2 - newImageSize.height/2,
|
||||||
|
width: newImageSize.width,
|
||||||
|
height: newImageSize.height));
|
||||||
|
|
||||||
|
return true
|
||||||
|
} else {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
handler(reply, nil)
|
||||||
|
} catch {
|
||||||
|
NSLog("Could not access file \(request.fileURL.lastPathComponent) to preview it")
|
||||||
|
handler(nil, nil)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Reference in a new issue