From 17611c3d40bf5b6e87b7e0017c55f48923ac1932 Mon Sep 17 00:00:00 2001 From: Palash Bansal Date: Fri, 8 Sep 2023 23:07:35 +0530 Subject: [PATCH 1/5] Add drawsBackground parameter for WKWebView --- PreviewExtension/Configuration.swift | 6 +++++- PreviewExtension/Info.plist | 2 ++ PreviewExtension/PreviewViewController.swift | 4 ++++ 3 files changed, 11 insertions(+), 1 deletion(-) diff --git a/PreviewExtension/Configuration.swift b/PreviewExtension/Configuration.swift index e522c9c..4657d91 100644 --- a/PreviewExtension/Configuration.swift +++ b/PreviewExtension/Configuration.swift @@ -13,6 +13,7 @@ struct Configuration { var loadingStrategy: LoadingStrategy var pageURL: URL var preferredContentSize: NSSize? + var drawsBackground: Bool? } extension Configuration { @@ -43,9 +44,12 @@ extension Configuration { throw GenericError(message: "Invalid loadingStrategy “\(strategyStr)” in QLJS configuration") } + let drawsBackground = (dict["drawsBackground"] as? String).flatMap{ Bool($0) } + return Configuration( loadingStrategy: loadingStrategy, pageURL: pageURL, - preferredContentSize: preferredContentSize) + preferredContentSize: preferredContentSize, + drawsBackground: drawsBackground) } } diff --git a/PreviewExtension/Info.plist b/PreviewExtension/Info.plist index 406b251..dee3712 100644 --- a/PreviewExtension/Info.plist +++ b/PreviewExtension/Info.plist @@ -53,6 +53,8 @@ preview.html preferredContentSize {500,300} + drawsBackground + true diff --git a/PreviewExtension/PreviewViewController.swift b/PreviewExtension/PreviewViewController.swift index e7b2de5..b8a3057 100644 --- a/PreviewExtension/PreviewViewController.swift +++ b/PreviewExtension/PreviewViewController.swift @@ -80,6 +80,10 @@ class PreviewViewController: NSViewController, QLPreviewingController, WKUIDeleg fatalError("Unable to load QuickLookJS configuration: \(error.localizedDescription)") } + if let drawsBackground = configuration.drawsBackground { + webView.setValue(drawsBackground, forKey:"drawsBackground") + } + super.init(nibName: nibNameOrNil, bundle: nibBundleOrNil) if let size = configuration.preferredContentSize { preferredContentSize = size From 76aaca07b302891a37ea78866e972b3800d8fc6f Mon Sep 17 00:00:00 2001 From: Palash Bansal Date: Sat, 16 Sep 2023 00:33:03 +0530 Subject: [PATCH 2/5] Fix if let issue --- PreviewExtension/PreviewViewController.swift | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/PreviewExtension/PreviewViewController.swift b/PreviewExtension/PreviewViewController.swift index b8a3057..98b6877 100644 --- a/PreviewExtension/PreviewViewController.swift +++ b/PreviewExtension/PreviewViewController.swift @@ -80,8 +80,8 @@ class PreviewViewController: NSViewController, QLPreviewingController, WKUIDeleg fatalError("Unable to load QuickLookJS configuration: \(error.localizedDescription)") } - if let drawsBackground = configuration.drawsBackground { - webView.setValue(drawsBackground, forKey:"drawsBackground") + if configuration.drawsBackground == false { + webView.setValue(configuration.drawsBackground, forKey:"drawsBackground") } super.init(nibName: nibNameOrNil, bundle: nibBundleOrNil) From efd9e23cd24652675ff57f2bae759f1ff79a1798 Mon Sep 17 00:00:00 2001 From: Jacob Bandes-Storch Date: Mon, 15 Apr 2024 11:22:16 -0500 Subject: [PATCH 3/5] Rename config key to transparentBackground, use bool value, try to avoid crashing --- PreviewExtension/Configuration.swift | 6 +++--- PreviewExtension/Info.plist | 4 ++-- PreviewExtension/PreviewViewController.swift | 22 ++++++++++++++++---- 3 files changed, 23 insertions(+), 9 deletions(-) diff --git a/PreviewExtension/Configuration.swift b/PreviewExtension/Configuration.swift index d87059c..405a3a8 100644 --- a/PreviewExtension/Configuration.swift +++ b/PreviewExtension/Configuration.swift @@ -13,7 +13,7 @@ struct Configuration { var loadingStrategy: LoadingStrategy var pageURL: URL var preferredContentSize: NSSize? - var drawsBackground: Bool? + var transparentBackground: Bool } extension Configuration { @@ -45,13 +45,13 @@ extension Configuration { throw GenericError(message: "Invalid loadingStrategy “\(strategyStr)” in QLJS configuration") } - let drawsBackground = (dict["drawsBackground"] as? String).flatMap { Bool($0) } + let transparentBackground = (dict["transparentBackground"] as? Bool) ?? false return Configuration( loadingStrategy: loadingStrategy, pageURL: pageURL, preferredContentSize: preferredContentSize, - drawsBackground: drawsBackground + transparentBackground: transparentBackground ) } } diff --git a/PreviewExtension/Info.plist b/PreviewExtension/Info.plist index dee3712..5e641a5 100644 --- a/PreviewExtension/Info.plist +++ b/PreviewExtension/Info.plist @@ -53,8 +53,8 @@ preview.html preferredContentSize {500,300} - drawsBackground - true + transparentBackground + diff --git a/PreviewExtension/PreviewViewController.swift b/PreviewExtension/PreviewViewController.swift index 68c54ea..13e6a1d 100644 --- a/PreviewExtension/PreviewViewController.swift +++ b/PreviewExtension/PreviewViewController.swift @@ -57,10 +57,24 @@ private func makeMouseEvent(_ type: NSEvent.EventType, at location: NSPoint, in ) } +class TransparentWebView: WKWebView { + func enableTransparentBackground() { + self.setValue(false, forKey: "drawsBackground") + } + + // Try to avoid crashing if this drawsBackground hack stops working in a future macOS version + override func setValue(_ value: Any?, forUndefinedKey key: String) { + if key == "drawsBackground" { + return + } + super.setValue(value, forUndefinedKey: key) + } +} + class PreviewViewController: NSViewController, QLPreviewingController, WKUIDelegate, WKNavigationDelegate, WKScriptMessageHandlerWithReply { - let webView: WKWebView + let webView: TransparentWebView let configuration: Configuration var cancellables = Set() @@ -79,7 +93,7 @@ class PreviewViewController: NSViewController, QLPreviewingController, WKUIDeleg assert(promise != nil) loadCompletePromise = promise - webView = WKWebView(frame: .zero) + webView = TransparentWebView(frame: .zero) do { configuration = try Configuration.fromMainBundle() @@ -88,8 +102,8 @@ class PreviewViewController: NSViewController, QLPreviewingController, WKUIDeleg fatalError("Unable to load QuickLookJS configuration: \(error.localizedDescription)") } - if configuration.drawsBackground == false { - webView.setValue(configuration.drawsBackground, forKey: "drawsBackground") + if configuration.transparentBackground { + webView.enableTransparentBackground() } super.init(nibName: nibNameOrNil, bundle: nibBundleOrNil) From 317b58b8db6846b0dc4e716098af1fd7f4cef1d8 Mon Sep 17 00:00:00 2001 From: Jacob Bandes-Storch Date: Mon, 15 Apr 2024 11:24:01 -0500 Subject: [PATCH 4/5] update README --- README.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/README.md b/README.md index d0f918b..c949bd7 100644 --- a/README.md +++ b/README.md @@ -101,6 +101,8 @@ You can customize some of the preview extension's behaviors by editing the `QLJS - **`preferredContentSize`** (optional): a string specifying the size of the preview window. In the example Info.plist this is `{500,300}`. +- **`transparentBackground`** (optional): a boolean specifying whether the web view should have a transparent background. This relies on undocumented APIs, so use it with caution. + ## 🧠 Behind the scenes Under the hood, quicklookjs loads your preview page in a [WKWebView](https://developer.apple.com/documentation/webkit/wkwebview), and the `quicklook` functions are provided in a user script. From e7d5568eb3b5154a1984cc8bd2ce58c0fdc97083 Mon Sep 17 00:00:00 2001 From: Jacob Bandes-Storch Date: Mon, 15 Apr 2024 11:27:06 -0500 Subject: [PATCH 5/5] lint --- PreviewExtension/PreviewViewController.swift | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/PreviewExtension/PreviewViewController.swift b/PreviewExtension/PreviewViewController.swift index 13e6a1d..5681280 100644 --- a/PreviewExtension/PreviewViewController.swift +++ b/PreviewExtension/PreviewViewController.swift @@ -59,7 +59,7 @@ private func makeMouseEvent(_ type: NSEvent.EventType, at location: NSPoint, in class TransparentWebView: WKWebView { func enableTransparentBackground() { - self.setValue(false, forKey: "drawsBackground") + setValue(false, forKey: "drawsBackground") } // Try to avoid crashing if this drawsBackground hack stops working in a future macOS version