2222
2323import Cocoa
2424import WebKit
25- import SourceCodeSyntaxHighlightXPCService
25+ import Syntax_Highlight_XPC_Service
2626
2727class ViewController : NSViewController {
2828 var webView : WKWebView ?
@@ -40,12 +40,12 @@ class ViewController: NSViewController {
4040 }
4141
4242 func load( url: URL ) {
43- self . webView? . loadHTMLString ( " " , baseURL: nil )
4443 self . webView? . isHidden = true
44+ self . webView? . loadHTMLString ( " " , baseURL: nil )
4545
46- self . textView? . string = " "
4746 self . textScrollView? . isHidden = true
48-
47+ self . textView? . string = " "
48+
4949 self . representedObject = url
5050 }
5151
@@ -59,8 +59,8 @@ class ViewController: NSViewController {
5959 }
6060 }
6161
62- func initializeView( forMode mode: String ) {
63- if mode == SCSHFormat . rtf. rawValue {
62+ func initializeView( forMode mode: SCSHFormat ) {
63+ if mode == . rtf {
6464 if self . textScrollView == nil {
6565 self . textScrollView = NSScrollView ( frame: self . view. bounds)
6666 self . textScrollView!. autoresizingMask = [ . height, . width]
@@ -116,9 +116,10 @@ class ViewController: NSViewController {
116116
117117 self . webView = WKWebView ( frame: self . view. bounds, configuration: configuration)
118118 self . webView!. autoresizingMask = [ . height, . width]
119+ self . webView? . isHidden = true
119120
120121 self . view. addSubview ( self . webView!)
121- self . webView? . addObserver ( self , forKeyPath : #keyPath ( WKWebView . estimatedProgress ) , options : . new , context : nil )
122+ self . webView? . navigationDelegate = self
122123 }
123124
124125 if let tsv = textScrollView {
@@ -142,11 +143,11 @@ class ViewController: NSViewController {
142143 textScrollView? . isHidden = true
143144
144145 service? . colorize ( url: documentUrl, overrideSettings: [ SCSHSettings . Key. embedCustomStyle: false ] ) { ( response, settings, error) in
145- let format = settings [ SCSHSettings . Key. format] as? String ?? SCSHFormat . html. rawValue
146+ let format = SCSHFormat ( rawValue : settings [ SCSHSettings . Key. format] as? String ?? " html " ) ?? . html
146147 DispatchQueue . main. async {
147148 self . initializeView ( forMode: format)
148149
149- if format == SCSHFormat . rtf. rawValue {
150+ if format == . rtf {
150151 let text : NSAttributedString
151152 if let e = error {
152153 text = NSAttributedString ( string: String ( data: response, encoding: . utf8) ?? e. localizedDescription)
@@ -170,18 +171,21 @@ class ViewController: NSViewController {
170171 let html : String = response. decodeToString ( ) . trimmingCharacters ( in: CharacterSet . newlines)
171172
172173 self . webView? . loadHTMLString ( html, baseURL: nil )
173- self . view. window? . makeFirstResponder ( self . webView!)
174174 }
175175 }
176176 }
177177 }
178+ }
179+
180+ extension ViewController : WKNavigationDelegate {
181+ func webView( _ webView: WKWebView , didFinish navigation: WKNavigation ! ) {
182+ // Show the quicklook preview only after the complete rendering (preventing a flickering glitch).
183+ webView. isHidden = false
184+ self . view. window? . makeFirstResponder ( webView)
185+ }
178186
179- override func observeValue( forKeyPath keyPath: String ? , of object: Any ? , change: [ NSKeyValueChangeKey : Any ] ? , context: UnsafeMutableRawPointer ? ) {
180- if keyPath == " estimatedProgress " , let w = self . webView, w. estimatedProgress == 1 {
181- // Show the webview only when loading is complete.
182- self . progressIndicatorView. stopAnimation ( self )
183- self . webView? . isHidden = false
184- }
187+ func webView( _ webView: WKWebView , didFail navigation: WKNavigation ! , withError error: Error ) {
188+ webView. isHidden = false
185189 }
186190}
187191
0 commit comments