Skip to content

Commit

Permalink
Add preview with WKWebView
Browse files Browse the repository at this point in the history
  • Loading branch information
samuelmeuli committed Mar 3, 2020
1 parent bbe1af8 commit 1492ee6
Show file tree
Hide file tree
Showing 6 changed files with 66 additions and 33 deletions.
4 changes: 4 additions & 0 deletions Glance.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
7ECC8CF2240CB4CC000D6970 /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7ECC8CF1240CB4CC000D6970 /* AppDelegate.swift */; };
7ECC8CF4240CB4CC000D6970 /* ViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7ECC8CF3240CB4CC000D6970 /* ViewController.swift */; };
7ECC8CF6240CB4CC000D6970 /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 7ECC8CF5240CB4CC000D6970 /* Assets.xcassets */; };
7ECE2C69240CF317001711C7 /* content.html in Resources */ = {isa = PBXBuildFile; fileRef = 7ECE2C68240CF317001711C7 /* content.html */; };
/* End PBXBuildFile section */

/* Begin PBXContainerItemProxy section */
Expand Down Expand Up @@ -59,6 +60,7 @@
7ECC8CF5240CB4CC000D6970 /* Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Assets.xcassets; sourceTree = "<group>"; };
7ECC8CFA240CB4CC000D6970 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
7ECC8CFB240CB4CC000D6970 /* Glance.entitlements */ = {isa = PBXFileReference; lastKnownFileType = text.plist.entitlements; path = Glance.entitlements; sourceTree = "<group>"; };
7ECE2C68240CF317001711C7 /* content.html */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.html; name = content.html; path = static/content.html; sourceTree = "<group>"; };
/* End PBXFileReference section */

/* Begin PBXFrameworksBuildPhase section */
Expand Down Expand Up @@ -94,6 +96,7 @@
7E6EF1FF240CC802009E4199 /* PreviewViewController.swift */,
7E6EF204240CC802009E4199 /* Info.plist */,
7E6EF205240CC802009E4199 /* QLPlugin.entitlements */,
7ECE2C68240CF317001711C7 /* content.html */,
);
path = QLPlugin;
sourceTree = "<group>";
Expand Down Expand Up @@ -214,6 +217,7 @@
isa = PBXResourcesBuildPhase;
buildActionMask = 2147483647;
files = (
7ECE2C69240CF317001711C7 /* content.html in Resources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down
8 changes: 4 additions & 4 deletions Glance/Glance.entitlements
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http:https://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/>
<key>com.apple.security.app-sandbox</key>
<true/>
<key>com.apple.security.files.user-selected.read-only</key>
<true/>
</dict>
</plist>
2 changes: 1 addition & 1 deletion Glance/GlanceApp.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import Cocoa

// Source: https://stackoverflow.com/questions/40008141/nsapplicationdelegate-not-working-without-storyboard
class GlanceApp: NSApplication {
// swiftlint:disable weak_delegate
// swiftlint:disable:next weak_delegate
let strongDelegate = AppDelegate()

override init() {
Expand Down
61 changes: 37 additions & 24 deletions QLPlugin/PreviewViewController.swift
Original file line number Diff line number Diff line change
@@ -1,37 +1,50 @@
import Cocoa
import Quartz
import WebKit

class PreviewViewController: NSViewController, QLPreviewingController, WKNavigationDelegate {
var webView: WKWebView!

class PreviewViewController: NSViewController, QLPreviewingController {
override func loadView() {
view = NSView()
// Do not call `super.loadView()` (related to storybooks)

// Create NSView (overrides inherited storybook logic)
view = NSView(frame: NSRect(x: 0, y: 0, width: 200, height: 100))
}

/*
* Implement this method and set QLSupportsSearchableItems to YES in the Info.plist of the
* extension if you support CoreSpotlight.
*
* func preparePreviewOfSearchableItem(
* identifier: String,
* queryString: String?,
* completionHandler handler: @escaping (Error?) -> Void
* ) {
* // Perform any setup necessary in order to prepare the view.
*
* // Call the completion handler so Quick Look knows that the preview is fully loaded.
* // Quick Look will display a loading spinner while the completion handler is not called.
* handler(nil)
* }
*/
// swiftlint:disable:next overridden_super_call
override func viewDidLoad() {
// Do not call `super.viewDidLoad()` (related to storybooks)

func preparePreviewOfFile(at _: URL, completionHandler handler: @escaping (Error?) -> Void) {
// Add the supported content types to the QLSupportedContentTypes array in the Info.plist of
// the extension.
// Create web view for rendering file preview
webView = WKWebView(frame: view.bounds)
webView.autoresizingMask = [.height, .width]
view.addSubview(webView)
}

// Perform any setup necessary in order to prepare the view.
/// Spotlight preview handler
func preparePreviewOfSearchableItem(
identifier _: String,
queryString _: String?,
completionHandler handler: @escaping (Error?) -> Void
) {
previewFile(completionHandler: handler)
}

/// File preview handler (e.g. preview in the Finder or when pressing space on a file)
func preparePreviewOfFile(at _: URL, completionHandler handler: @escaping (Error?) -> Void) {
previewFile(completionHandler: handler)
}

// Call the completion handler so Quick Look knows that the preview is fully loaded.
// Quick Look will display a loading spinner while the completion handler is not called.
private func previewFile(completionHandler handler: @escaping (Error?) -> Void) {
if let url = Bundle.main.url(forResource: "content", withExtension: "html") {
webView.loadFileURL(url, allowingReadAccessTo: url.deletingLastPathComponent())
} else {
// TODO: Display error message if HTML cannot be loaded
print("Error loading preview HTML")
}

// Stop displaying Quick Look loading spinner
handler(nil)
}
}
12 changes: 8 additions & 4 deletions QLPlugin/QLPlugin.entitlements
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,13 @@
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http:https://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/>
<key>com.apple.security.app-sandbox</key>
<true/>
<key>com.apple.security.files.user-selected.read-only</key>
<true/>
<key>com.apple.security.network.client</key>
<true/>
<key>com.apple.security.network.server</key>
<true/>
</dict>
</plist>
12 changes: 12 additions & 0 deletions QLPlugin/static/content.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<!DOCTYPE html>

<html>
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no" />
</head>

<body>
<p>Hello world!</p>
</body>
</html>

0 comments on commit 1492ee6

Please sign in to comment.