Skip to content

Commit

Permalink
Merge pull request #32 from vincentneo/thumbnails
Browse files Browse the repository at this point in the history
Quick Look Thumbnails + Document Icon support
  • Loading branch information
vincentneo authored Jan 14, 2023
2 parents 631fe56 + 59da33b commit f033d4d
Show file tree
Hide file tree
Showing 30 changed files with 369 additions and 5 deletions.
6 changes: 6 additions & 0 deletions Avenue Thumbnails/Assets.xcassets/Contents.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"info" : {
"author" : "xcode",
"version" : 1
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
"images" : [
{
"filename" : "V2.png",
"idiom" : "universal",
"scale" : "1x"
},
{
"filename" : "[email protected]",
"idiom" : "universal",
"scale" : "2x"
},
{
"idiom" : "universal",
"scale" : "3x"
}
],
"info" : {
"author" : "xcode",
"version" : 1
}
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
10 changes: 10 additions & 0 deletions Avenue Thumbnails/Avenue_Thumbnails.entitlements
Original file line number Diff line number Diff line change
@@ -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>
28 changes: 28 additions & 0 deletions Avenue Thumbnails/Info.plist
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?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.topografix.gpx</string>
<string>com.apple.dt.document.gpx</string>
<string>com.glacierpeakstudios.gpx</string>
<string>com.berbie.documenttypes.gpx</string>
<string>com.topographix.gpx</string>
<string>public.gpx</string>
<string>public.xml</string>
</array>
<key>QLThumbnailMinimumDimension</key>
<integer>0</integer>
</dict>
<key>NSExtensionPointIdentifier</key>
<string>com.apple.quicklook.thumbnail</string>
<key>NSExtensionPrincipalClass</key>
<string>$(PRODUCT_MODULE_NAME).ThumbnailProvider</string>
</dict>
</dict>
</plist>
81 changes: 81 additions & 0 deletions Avenue Thumbnails/ThumbnailProvider.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
//
// ThumbnailProvider.swift
// Avenue Thumbnails
//
// Created by Vincent Neo on 13/1/23.
// Copyright © 2023 Vincent. All rights reserved.
//

import QuickLookThumbnailing
import MapKit
import CoreGPX

class ThumbnailProvider: QLThumbnailProvider {

func prepareExtent(from gpx: GPXRoot) -> GPXExtentCoordinates {
let extent = GPXExtentCoordinates()
for route in gpx.routes {
for point in route.points {
extent.extendAreaToIncludeLocation(point.coordinate)
}
}

for track in gpx.tracks {
for segment in track.segments {
for point in segment.points {
extent.extendAreaToIncludeLocation(point.coordinate)
}
}
}

for waypoint in gpx.waypoints {
extent.extendAreaToIncludeLocation(waypoint.coordinate)
}
return extent
}

override func provideThumbnail(for request: QLFileThumbnailRequest, _ handler: @escaping (QLThumbnailReply?, Error?) -> Void) {

// There are three ways to provide a thumbnail through a QLThumbnailReply. Only one of them should be used.

// First way: Draw the thumbnail into the current context, set up with UIKit's coordinate system.
guard let parser = GPXParser(withURL: request.fileURL), let gpx = parser.parsedData() else {
handler(nil, GPXError.parser.fileIsEmpty)
return
}

let extent = self.prepareExtent(from: gpx)

let options = MKMapSnapshotter.Options()
options.region = extent.region
options.size = request.maximumSize

let snapshotter = MKMapSnapshotter(options: options)
snapshotter.start { snapshot, error in
let drawer = MKSnapshotDrawer(snapshot!, gpx: gpx)
let newImage = drawer.processImage()

handler(QLThumbnailReply(contextSize: request.maximumSize, currentContextDrawing: { () -> Bool in
// Draw the thumbnail here.
newImage.draw(in: NSRect(origin: .zero, size: request.maximumSize))
// Return true if the thumbnail was successfully drawn inside this block.
return true
}), nil)
}

/*

// Second way: Draw the thumbnail into a context passed to your block, set up with Core Graphics's coordinate system.
handler(QLThumbnailReply(contextSize: request.maximumSize, drawing: { (context) -> Bool in
// Draw the thumbnail here.

// Return true if the thumbnail was successfully drawn inside this block.
return true
}), nil)

// Third way: Set an image file URL.
handler(QLThumbnailReply(imageFileURL: Bundle.main.url(forResource: "fileThumbnail", withExtension: "jpg")!), nil)

*/
}
}
Loading

0 comments on commit f033d4d

Please sign in to comment.