feat: thumbnail icon
This commit is contained in:
24
QLThumbnail/Info.plist
Normal file
24
QLThumbnail/Info.plist
Normal file
@@ -0,0 +1,24 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
|
||||
<plist version="1.0">
|
||||
<dict>
|
||||
<key>NSExtension</key>
|
||||
<dict>
|
||||
<key>NSExtensionAttributes</key>
|
||||
<dict>
|
||||
<key>QLSupportedContentTypes</key>
|
||||
<array>
|
||||
<string>com.apple.itunes.ipa</string>
|
||||
<string>com.apple.application-and-system-extension</string>
|
||||
<string>com.apple.xcode.archive</string>
|
||||
</array>
|
||||
<key>QLThumbnailMinimumDimension</key>
|
||||
<integer>16</integer>
|
||||
</dict>
|
||||
<key>NSExtensionPointIdentifier</key>
|
||||
<string>com.apple.quicklook.thumbnail</string>
|
||||
<key>NSExtensionPrincipalClass</key>
|
||||
<string>$(PRODUCT_MODULE_NAME).ThumbnailProvider</string>
|
||||
</dict>
|
||||
</dict>
|
||||
</plist>
|
||||
10
QLThumbnail/QLThumbnail.entitlements
Normal file
10
QLThumbnail/QLThumbnail.entitlements
Normal file
@@ -0,0 +1,10 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
|
||||
<plist version="1.0">
|
||||
<dict>
|
||||
<key>com.apple.security.app-sandbox</key>
|
||||
<true/>
|
||||
<key>com.apple.security.files.user-selected.read-only</key>
|
||||
<true/>
|
||||
</dict>
|
||||
</plist>
|
||||
43
QLThumbnail/ThumbnailProvider.swift
Normal file
43
QLThumbnail/ThumbnailProvider.swift
Normal file
@@ -0,0 +1,43 @@
|
||||
import QuickLookThumbnailing
|
||||
import os // OSLog
|
||||
|
||||
// show Console logs with subsystem:de.relikd.QLApps
|
||||
private let log = OSLog(subsystem: Bundle.main.bundleIdentifier!, category: "thumbnail-plugin")
|
||||
|
||||
extension QLThumbnailReply {
|
||||
/// call private method `setIconFlavor:`
|
||||
/// see https://medium.com/swlh/calling-ios-and-macos-hidden-api-in-style-1a924f244ad1
|
||||
fileprivate func setFlavor(_ flavor: Int) {
|
||||
typealias setIconFlavorMethod = @convention(c) (NSObject, Selector, NSInteger) -> Bool
|
||||
let selector = NSSelectorFromString("setIconFlavor:")
|
||||
let imp = self.method(for: selector)
|
||||
let method = unsafeBitCast(imp, to: setIconFlavorMethod.self)
|
||||
_ = method(self, selector, flavor)
|
||||
}
|
||||
}
|
||||
|
||||
class ThumbnailProvider: QLThumbnailProvider {
|
||||
|
||||
// TODO: sadly, this does not seem to work for .xarchive and .appex
|
||||
// Probably overwritten by Apple somehow
|
||||
|
||||
override func provideThumbnail(for request: QLFileThumbnailRequest, _ handler: @escaping (QLThumbnailReply?, Error?) -> Void) {
|
||||
let meta = QuickLookInfo(request.fileURL)
|
||||
let icon = AppIcon(meta)
|
||||
let plistApp = meta.readPlistApp()
|
||||
let img = icon.extractImage(from: plistApp).withRoundCorners()
|
||||
|
||||
// First way: Draw the thumbnail into the current context, set up with UIKit's coordinate system.
|
||||
let reply = QLThumbnailReply(contextSize: request.maximumSize, currentContextDrawing: { () -> Bool in
|
||||
img.draw(in: CGRect(origin: .zero, size: request.maximumSize))
|
||||
return true
|
||||
})
|
||||
// defer in case `setFlavor` fails
|
||||
defer {
|
||||
handler(reply, nil)
|
||||
}
|
||||
// 0: Plain transparent, 1: Shadow, 2: Book, 3: Movie, 4: Address, 5: Image,
|
||||
// 6: Gloss, 7: Slide, 8: Square, 9: Border, 11: Calendar, 12: Pattern
|
||||
reply.setFlavor(meta.type == .Archive ? 12 : 0) // .archive looks like "in development"
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user