Skip to content

Commit

Permalink
Fixed a glitch when showing the preview (the webview is initially sho…
Browse files Browse the repository at this point in the history
…wn smaller than the QL window).
  • Loading branch information
sbarex committed Feb 9, 2021
1 parent cd7627b commit 4451ff7
Show file tree
Hide file tree
Showing 4 changed files with 94 additions and 68 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
# Changelog
=======

### 1.0b22
Bugfix:
- Fixed a glitch when showing the preview (the webview is initially shown smaller than the QL window).

### 1.0b21
New features:
- Settings to handle autoupdate.
Expand Down
146 changes: 83 additions & 63 deletions QLExtension/PreviewViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ class MyWebView: WebView {
}

class PreviewViewController: NSViewController, QLPreviewingController {
var webView: MyWKWebView!

private let log = {
return OSLog(subsystem: Bundle.main.bundleIdentifier!, category: "quicklook.qlmarkdown-extension")
}()
Expand Down Expand Up @@ -63,6 +65,77 @@ class PreviewViewController: NSViewController, QLPreviewingController {
print("Received error:", error)
} as? ExternalLauncherProtocol
}

let settings = Settings.shared

let previewRect: CGRect
if #available(macOS 11, *) {
previewRect = self.view.bounds
} else {
previewRect = self.view.bounds.insetBy(dx: 2, dy: 2)
}

/*
if #available(macOS 11, *) {
// On Big Sur there are some bugs with the current WKWebView:
// - WKWebView crash on launch because ignore the com.apple.security.network.client entitlement (workaround setting the com.apple.security.temporary-exception.mach-lookup.global-name exception for com.apple.nsurlsessiond
// - WKWebView cannot scroll when QL preview window is in fullscreen.
// Old WebView API works.
let webView = MyWebView(frame: previewRect)
webView.autoresizingMask = [.height, .width]
webView.preferences.isJavaScriptEnabled = false
webView.preferences.allowsAirPlayForMediaPlayback = false
webView.preferences.arePlugInsEnabled = false

self.view.addSubview(webView)

webView.mainFrame.loadHTMLString(html, baseURL: nil)
webView.frameLoadDelegate = self
webView.drawsBackground = false // Best solution is use the same color of the body
} else {
*/
let preferences = WKPreferences()
preferences.javaScriptEnabled = settings.unsafeHTMLOption && settings.inlineImageExtension

// Create a configuration for the preferences
let configuration = WKWebViewConfiguration()
//configuration.preferences = preferences
configuration.allowsAirPlayForMediaPlayback = false

// Handler to replace raw <image> src with the embedded data.
configuration.userContentController.add(self, name: "imageExtensionHandler")

self.webView = MyWKWebView(frame: previewRect, configuration: configuration)
self.webView.autoresizingMask = [.height, .width]

self.webView.wantsLayer = true
if #available(macOS 11, *) {
self.webView.layer?.borderWidth = 0
} else {
// Draw a border around the web view
self.webView.layer?.borderColor = NSColor.gridColor.cgColor
self.webView.layer?.borderWidth = 1
}

self.webView.navigationDelegate = self
// webView.uiDelegate = self

self.view.addSubview(self.webView)

/*
self.webView.translatesAutoresizingMaskIntoConstraints = false
var padding: CGFloat = 2
if #available(macOS 11, *) {
padding = 0
}

NSLayoutConstraint(item: self.webView!, attribute: .leading, relatedBy: .equal, toItem: view, attribute: .leading, multiplier: 1.0, constant: padding).isActive = true
NSLayoutConstraint(item: self.webView!, attribute: .trailing, relatedBy: .equal, toItem: view, attribute: .trailing, multiplier: 1.0, constant: padding).isActive = true
NSLayoutConstraint(item: self.webView!, attribute: .top, relatedBy: .equal, toItem: view, attribute: .top, multiplier: 1.0, constant: padding).isActive = true
NSLayoutConstraint(item: self.webView!, attribute: .bottom, relatedBy: .equal, toItem: view, attribute: .bottom, multiplier: 1.0, constant: padding).isActive = true
*/

/* } */
}

internal func getBundleContents(forResource: String, ofType: String) -> String?
Expand All @@ -86,12 +159,6 @@ class PreviewViewController: NSViewController, QLPreviewingController {
}
*/

/*
@IBAction func copy(_ sender: Any) {
NSPasteboard.general.setString("Ciao2", forType: .string)
}
*/

func preparePreviewOfFile(at url: URL, completionHandler handler: @escaping (Error?) -> Void) {

// Add the supported content types to the QLSupportedContentTypes array in the Info.plist of the extension.
Expand Down Expand Up @@ -135,65 +202,14 @@ class PreviewViewController: NSViewController, QLPreviewingController {
extrajs = ""
}
let html = settings.getCompleteHTML(title: url.lastPathComponent, body: text, footer: extrajs)

let previewRect: CGRect
if #available(macOS 11, *) {
previewRect = self.view.bounds
} else {
previewRect = self.view.bounds.insetBy(dx: 2, dy: 2)
}

/*
if #available(macOS 11, *) {
// On Big Sur there are some bugs with the current WKWebView:
// - WKWebView crash on launch because ignore the com.apple.security.network.client entitlement (workaround setting the com.apple.security.temporary-exception.mach-lookup.global-name exception for com.apple.nsurlsessiond
// - WKWebView cannot scroll when QL preview window is in fullscreen.
// Old WebView API works.
let webView = MyWebView(frame: previewRect)
webView.autoresizingMask = [.height, .width]
webView.preferences.isJavaScriptEnabled = false
webView.preferences.allowsAirPlayForMediaPlayback = false
webView.preferences.arePlugInsEnabled = false

self.view.addSubview(webView)

webView.mainFrame.loadHTMLString(html, baseURL: nil)
webView.frameLoadDelegate = self
webView.drawsBackground = false // Best solution is use the same color of the body
self.webView.mainFrame.loadHTMLString(html, baseURL: nil)
} else {
*/
let preferences = WKPreferences()
preferences.javaScriptEnabled = settings.unsafeHTMLOption && settings.inlineImageExtension

// Create a configuration for the preferences
let configuration = WKWebViewConfiguration()
//configuration.preferences = preferences
configuration.allowsAirPlayForMediaPlayback = false

if settings.unsafeHTMLOption && settings.inlineImageExtension {
// Handler to replace raw <image> src with the embedded data.
configuration.userContentController.add(self, name: "imageExtensionHandler")
}

let webView = MyWKWebView(frame: previewRect, configuration: configuration)
webView.autoresizingMask = [.height, .width]

webView.wantsLayer = true
if #available(macOS 11, *) {
webView.layer?.borderWidth = 0
} else {
// Draw a border around the web view
webView.layer?.borderColor = NSColor.gridColor.cgColor
webView.layer?.borderWidth = 1
}

webView.navigationDelegate = self
// webView.uiDelegate = self

self.view.addSubview(webView)

webView.loadHTMLString(html, baseURL: url.deletingLastPathComponent())
/* } */
self.webView.isHidden = true // hide the webview until complete rendering
self.webView.loadHTMLString(html, baseURL: url.deletingLastPathComponent())
/* } */
} catch {
handler(error)
}
Expand All @@ -218,7 +234,7 @@ extension PreviewViewController: WebFrameLoadDelegate {

extension PreviewViewController: WKScriptMessageHandler {
func userContentController(_ userContentController: WKUserContentController, didReceive message: WKScriptMessage) {
guard message.name == "imageExtensionHandler" else {
guard message.name == "imageExtensionHandler", Settings.shared.unsafeHTMLOption && Settings.shared.inlineImageExtension else {
return
}
guard let dict = message.body as? [String : AnyObject], let src = dict["src"] as? String, let id = dict["id"] as? String else {
Expand Down Expand Up @@ -278,13 +294,18 @@ extension PreviewViewController: WKNavigationDelegate {

handler(nil)
self.handler = nil
// Wait to show the webview to prevent a resize glitch.
DispatchQueue.main.asyncAfter(deadline: .now() + 0.1) {
self.webView.isHidden = false
}
}
}

func webView(_ webView: WKWebView, didFail navigation: WKNavigation!, withError error: Error) {
if let handler = self.handler {
handler(error)
self.handler = nil
self.webView.isHidden = false
}
}

Expand All @@ -301,7 +322,6 @@ extension PreviewViewController: WKNavigationDelegate {
return
} else {
let r = NSWorkspace.shared.open(url)
// print(r, url.absoluteString)
if r {
decisionHandler(.cancel)
return
Expand Down
10 changes: 6 additions & 4 deletions QLMarkdown.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -1166,7 +1166,7 @@
CODE_SIGN_ENTITLEMENTS = QLExtension/QLExtension.entitlements;
CODE_SIGN_IDENTITY = "-";
CODE_SIGN_STYLE = Manual;
CURRENT_PROJECT_VERSION = 21;
CURRENT_PROJECT_VERSION = 22;
DEVELOPMENT_TEAM = "";
ENABLE_HARDENED_RUNTIME = NO;
HEADER_SEARCH_PATHS = (
Expand Down Expand Up @@ -1204,7 +1204,7 @@
CODE_SIGN_ENTITLEMENTS = QLExtension/QLExtension.entitlements;
CODE_SIGN_IDENTITY = "-";
CODE_SIGN_STYLE = Manual;
CURRENT_PROJECT_VERSION = 21;
CURRENT_PROJECT_VERSION = 22;
DEVELOPMENT_TEAM = "";
ENABLE_HARDENED_RUNTIME = NO;
HEADER_SEARCH_PATHS = (
Expand Down Expand Up @@ -1361,7 +1361,7 @@
CODE_SIGN_IDENTITY = "-";
CODE_SIGN_STYLE = Manual;
COMBINE_HIDPI_IMAGES = YES;
CURRENT_PROJECT_VERSION = 21;
CURRENT_PROJECT_VERSION = 22;
DEVELOPMENT_TEAM = "";
ENABLE_HARDENED_RUNTIME = NO;
FRAMEWORK_SEARCH_PATHS = (
Expand All @@ -1387,6 +1387,7 @@
"$(PROJECT_DIR)/re2",
);
MACOSX_DEPLOYMENT_TARGET = 10.15;
MARKETING_VERSION = 1.0;
PRODUCT_BUNDLE_IDENTIFIER = org.sbarex.QLMarkdown;
PRODUCT_NAME = "$(TARGET_NAME)";
PROVISIONING_PROFILE_SPECIFIER = "";
Expand All @@ -1407,7 +1408,7 @@
CODE_SIGN_IDENTITY = "-";
CODE_SIGN_STYLE = Manual;
COMBINE_HIDPI_IMAGES = YES;
CURRENT_PROJECT_VERSION = 21;
CURRENT_PROJECT_VERSION = 22;
DEVELOPMENT_TEAM = "";
ENABLE_HARDENED_RUNTIME = NO;
FRAMEWORK_SEARCH_PATHS = (
Expand All @@ -1433,6 +1434,7 @@
"$(PROJECT_DIR)/re2",
);
MACOSX_DEPLOYMENT_TARGET = 10.15;
MARKETING_VERSION = 1.0;
PRODUCT_BUNDLE_IDENTIFIER = org.sbarex.QLMarkdown;
PRODUCT_NAME = "$(TARGET_NAME)";
PROVISIONING_PROFILE_SPECIFIER = "";
Expand Down
2 changes: 1 addition & 1 deletion QLMarkdown/Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
<key>CFBundlePackageType</key>
<string>$(PRODUCT_BUNDLE_PACKAGE_TYPE)</string>
<key>CFBundleShortVersionString</key>
<string>1.0</string>
<string>$(MARKETING_VERSION)</string>
<key>CFBundleVersion</key>
<string>$(CURRENT_PROJECT_VERSION)</string>
<key>LSApplicationCategoryType</key>
Expand Down

0 comments on commit 4451ff7

Please sign in to comment.