@@ -68,7 +68,7 @@ class StaticTextView: NSTextView {
6868 }
6969}
7070
71- class PreviewViewController : NSViewController , QLPreviewingController {
71+ class PreviewViewController : NSViewController , QLPreviewingController , WKNavigationDelegate {
7272 @IBOutlet weak var draggingView : MyDraggingView !
7373 @IBOutlet weak var trailingDragginViewConstraint : NSLayoutConstraint !
7474 @IBOutlet weak var bottomDragginViewConstraint : NSLayoutConstraint !
@@ -102,6 +102,8 @@ class PreviewViewController: NSViewController, QLPreviewingController {
102102 }
103103 */
104104
105+ var handler : ( ( Error ? ) -> Void ) ? = nil
106+
105107 func preparePreviewOfFile( at url: URL , completionHandler handler: @escaping ( Error ? ) -> Void ) {
106108 // Add the supported content types to the QLSupportedContentTypes array in the Info.plist of the extension.
107109
@@ -168,6 +170,8 @@ class PreviewViewController: NSViewController, QLPreviewingController {
168170
169171 let text = NSAttributedString ( rtf: response, documentAttributes: nil ) ?? NSAttributedString ( string: " Unable to convert data to rtf. " )
170172 textView. textStorage? . setAttributedString ( text)
173+
174+ handler ( nil )
171175 } else {
172176 var lossy = false
173177 let html = response. decodeToString ( lossy: & lossy) . trimmingCharacters ( in: CharacterSet . newlines)
@@ -185,15 +189,30 @@ class PreviewViewController: NSViewController, QLPreviewingController {
185189 configuration. allowsAirPlayForMediaPlayback = false
186190
187191 let webView = WKWebView ( frame: self . view. bounds, configuration: configuration)
192+ webView. navigationDelegate = self
188193 webView. autoresizingMask = [ . height, . width]
189194
190195 self . view. addSubview ( webView, positioned: NSWindow . OrderingMode. below, relativeTo: self . draggingView)
191196
192197 webView. loadHTMLString ( html, baseURL: nil )
198+ self . handler = handler
193199 }
194-
195- handler ( nil )
196200 }
197201 }
198202 }
203+
204+ func webView( _ webView: WKWebView , didFinish navigation: WKNavigation ! ) {
205+ if let handler = self . handler {
206+ // Show the quicklook preview only after the complete rendering (preventing a flickering glitch).
207+ handler ( nil )
208+ self . handler = nil
209+ }
210+ }
211+
212+ func webView( _ webView: WKWebView , didFail navigation: WKNavigation ! , withError error: Error ) {
213+ if let handler = self . handler {
214+ handler ( error)
215+ self . handler = nil
216+ }
217+ }
199218}
0 commit comments