Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add drawsBackground parameter for WKWebView #1

Merged
merged 6 commits into from
Apr 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion PreviewExtension/Configuration.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ struct Configuration {
var loadingStrategy: LoadingStrategy
var pageURL: URL
var preferredContentSize: NSSize?
var transparentBackground: Bool
}

extension Configuration {
Expand Down Expand Up @@ -44,10 +45,13 @@ extension Configuration {
throw GenericError(message: "Invalid loadingStrategy “\(strategyStr)” in QLJS configuration")
}

let transparentBackground = (dict["transparentBackground"] as? Bool) ?? false

return Configuration(
loadingStrategy: loadingStrategy,
pageURL: pageURL,
preferredContentSize: preferredContentSize
preferredContentSize: preferredContentSize,
transparentBackground: transparentBackground
)
}
}
2 changes: 2 additions & 0 deletions PreviewExtension/Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@
<string>preview.html</string>
<key>preferredContentSize</key>
<string>{500,300}</string>
<key>transparentBackground</key>
<false/>
</dict>
</dict>
</plist>
22 changes: 20 additions & 2 deletions PreviewExtension/PreviewViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,24 @@ private func makeMouseEvent(_ type: NSEvent.EventType, at location: NSPoint, in
)
}

class TransparentWebView: WKWebView {
func enableTransparentBackground() {
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<AnyCancellable>()
Expand All @@ -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()
Expand All @@ -88,6 +102,10 @@ class PreviewViewController: NSViewController, QLPreviewingController, WKUIDeleg
fatalError("Unable to load QuickLookJS configuration: \(error.localizedDescription)")
}

if configuration.transparentBackground {
webView.enableTransparentBackground()
}

super.init(nibName: nibNameOrNil, bundle: nibBundleOrNil)
if let size = configuration.preferredContentSize {
preferredContentSize = size
Expand Down
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down