22
33using System ;
44using System . Diagnostics ;
5+ using System . Drawing ;
56using System . Globalization ;
67using System . IO ;
78using System . Linq ;
@@ -81,7 +82,10 @@ public WebpagePanel()
8182 // See "Process model for WebView2 apps"
8283 UserDataFolder = Path . Combine ( SettingHelper . LocalDataPath , @"WebViewPlus_Data\\" ) ,
8384 Language = CultureInfo . CurrentUICulture . Name
84- }
85+ } ,
86+ // Match background color in theme.tsx in WebViewPlus
87+ // Note: this will not adjust if dark/light is swapped via the in-app yingyang toggle
88+ DefaultBackgroundColor = OSThemeHelper . AppsUseDarkTheme ( ) ? Color . FromArgb ( 0x21 , 0x21 , 0x21 ) : Color . FromArgb ( 0xee , 0xee , 0xee ) ,
8589 } ;
8690 _webView . NavigationStarting += NavigationStarting_CancelNavigation ;
8791 _webView . CoreWebView2InitializationCompleted += CoreWebView2InitializationCompleted ;
@@ -151,25 +155,25 @@ void sendFileData()
151155 {
152156 _sharedBuffer = WebViewEnvironment . CreateSharedBuffer ( ( ulong ) _activeFileInfo . Length ) ;
153157 using ( BinaryWriter writer = new BinaryWriter ( _sharedBuffer . OpenStream ( ) ) )
154- {
155- using ( var br = new BinaryReader ( new FileStream ( _activeFileInfo . FullName , FileMode . Open , FileAccess . Read , FileShare . ReadWrite ) ) )
156- {
157- writer . Write ( br . ReadBytes ( ( int ) br . BaseStream . Length ) ) ;
158- }
158+ {
159+ using ( var br = new BinaryReader ( new FileStream ( _activeFileInfo . FullName , FileMode . Open , FileAccess . Read , FileShare . ReadWrite ) ) )
160+ {
161+ writer . Write ( br . ReadBytes ( ( int ) br . BaseStream . Length ) ) ;
162+ }
159163 }
160164 }
161165 else
162166 {
163- _sharedBuffer = WebViewEnvironment . CreateSharedBuffer ( 1 ) ;
164-
165- var encoding = Encoding . UTF8 ;
166- if ( DetectEncoding )
167- {
168- encoding = CharsetDetector . DetectFromFile ( _activeFileInfo ) . Detected ? . Encoding ?? encoding ;
169- }
170- using ( var sr = new StreamReader ( new FileStream ( _activeFileInfo . FullName , FileMode . Open , FileAccess . Read , FileShare . ReadWrite ) , encoding ) )
171- {
172- textContent = sr . ReadToEnd ( ) ;
167+ _sharedBuffer = WebViewEnvironment . CreateSharedBuffer ( 1 ) ;
168+
169+ var encoding = Encoding . UTF8 ;
170+ if ( DetectEncoding )
171+ {
172+ encoding = CharsetDetector . DetectFromFile ( _activeFileInfo ) . Detected ? . Encoding ?? encoding ;
173+ }
174+ using ( var sr = new StreamReader ( new FileStream ( _activeFileInfo . FullName , FileMode . Open , FileAccess . Read , FileShare . ReadWrite ) , encoding ) )
175+ {
176+ textContent = sr . ReadToEnd ( ) ;
173177 }
174178 }
175179 var json = JsonConvert . SerializeObject (
@@ -193,19 +197,19 @@ void WebMessageReceived(object sender, CoreWebView2WebMessageReceivedEventArgs a
193197 switch ( msg . command )
194198 {
195199 case "AppReadyForData" :
196- _webAppReady = true ;
197- // If the user profile was previously generated (WebView2_Data), it doesn't seem to change the navigator.language setting
198- // when a webview is created with a different language in 'new WebView2' above.
199- // You'd have to delete the WebView2_Data folder first.
200- // Oh, well. Explicitly sending current language to web app, instead.
201- var json = JsonConvert . SerializeObject (
202- new InitData
203- {
204- langCode = CultureInfo . CurrentUICulture . Name ,
205- detectEncoding = SettingHelper . Get ( "DetectEncoding" , false , "QuickLook.Plugin.WebViewPlus" ) ,
206- showTrayIcon = SettingHelper . Get ( "ShowTrayIcon" , true ) ,
207- useTransparency = SettingHelper . Get ( "UseTransparency" , true )
208- }
200+ _webAppReady = true ;
201+ // If the user profile was previously generated (WebView2_Data), it doesn't seem to change the navigator.language setting
202+ // when a webview is created with a different language in 'new WebView2' above.
203+ // You'd have to delete the WebView2_Data folder first.
204+ // Oh, well. Explicitly sending current language to web app, instead.
205+ var json = JsonConvert . SerializeObject (
206+ new InitData
207+ {
208+ langCode = CultureInfo . CurrentUICulture . Name ,
209+ detectEncoding = SettingHelper . Get ( "DetectEncoding" , false , "QuickLook.Plugin.WebViewPlus" ) ,
210+ showTrayIcon = SettingHelper . Get ( "ShowTrayIcon" , true ) ,
211+ useTransparency = SettingHelper . Get ( "UseTransparency" , true )
212+ }
209213 ) ;
210214
211215 _webView . CoreWebView2 . PostWebMessageAsString ( $ "initData:{ json } ") ;
@@ -227,28 +231,28 @@ void WebMessageReceived(object sender, CoreWebView2WebMessageReceivedEventArgs a
227231 break ;
228232
229233 case "UseTransparency" :
230- SettingHelper . Set ( "UseTransparency" , msg . boolValue ) ;
234+ SettingHelper . Set ( "UseTransparency" , msg . boolValue ) ;
231235 break ;
232236
233237 case "Restart" :
234- RestartQuicklook ( ) ;
238+ RestartQuicklook ( ) ;
235239 break ;
236240
237241 default :
238242 break ;
239243 }
240244 }
241245
242- private void RestartQuicklook ( )
243- {
244- // Restart QL. Clunky way to delay startup to pass already running check.
245- ProcessStartInfo Info = new ProcessStartInfo ( ) ;
246- Info . Arguments = "/C ping 127.0.0.1 -n 2 && \" " + string . Join ( " " , Environment . GetCommandLineArgs ( ) ) + "\" " ;
247- Info . WindowStyle = ProcessWindowStyle . Hidden ;
248- Info . CreateNoWindow = true ;
249- Info . FileName = "cmd.exe" ;
250- Process . Start ( Info ) ;
251- Process . GetCurrentProcess ( ) . Kill ( ) ;
246+ private void RestartQuicklook ( )
247+ {
248+ // Restart QL. Clunky way to delay startup to pass already running check.
249+ ProcessStartInfo Info = new ProcessStartInfo ( ) ;
250+ Info . Arguments = "/C ping 127.0.0.1 -n 2 && \" " + string . Join ( " " , Environment . GetCommandLineArgs ( ) ) + "\" " ;
251+ Info . WindowStyle = ProcessWindowStyle . Hidden ;
252+ Info . CreateNoWindow = true ;
253+ Info . FileName = "cmd.exe" ;
254+ Process . Start ( Info ) ;
255+ Process . GetCurrentProcess ( ) . Kill ( ) ;
252256 }
253257
254258 private void CoreWebView2InitializationCompleted ( object sender , EventArgs e )
@@ -314,10 +318,10 @@ private void FrameNavigationStarting(object sender, CoreWebView2NavigationStarti
314318 _webView . CoreWebView2 . PostWebMessageAsString ( "frameNavigationRejected" ) ;
315319 }
316320 }
317- }
318-
319- // No longer used - Changes in webview2 caused crashes due to
320- // "WebView2 controller will be auto closed if the parent hwnd is destroyed"
321+ }
322+
323+ // No longer used - Changes in webview2 caused crashes due to
324+ // "WebView2 controller will be auto closed if the parent hwnd is destroyed"
321325 public void UnloadData ( )
322326 {
323327 _activeFileInfo = null ;
@@ -331,22 +335,22 @@ public void UnloadData()
331335
332336 public void Dispose ( )
333337 {
334- if ( firstPreview )
335- {
336- firstPreview = false ;
337- _webView . CoreWebView2 . Profile . ClearBrowsingDataAsync (
338- CoreWebView2BrowsingDataKinds . FileSystems |
339- CoreWebView2BrowsingDataKinds . IndexedDb |
340- // keep local storage
341- CoreWebView2BrowsingDataKinds . WebSql |
342- CoreWebView2BrowsingDataKinds . CacheStorage |
343- CoreWebView2BrowsingDataKinds . Cookies |
344- CoreWebView2BrowsingDataKinds . DiskCache |
345- CoreWebView2BrowsingDataKinds . DownloadHistory |
346- CoreWebView2BrowsingDataKinds . GeneralAutofill |
347- CoreWebView2BrowsingDataKinds . PasswordAutosave |
348- CoreWebView2BrowsingDataKinds . Settings
349- ) ;
338+ if ( firstPreview )
339+ {
340+ firstPreview = false ;
341+ _webView . CoreWebView2 . Profile . ClearBrowsingDataAsync (
342+ CoreWebView2BrowsingDataKinds . FileSystems |
343+ CoreWebView2BrowsingDataKinds . IndexedDb |
344+ // keep local storage
345+ CoreWebView2BrowsingDataKinds . WebSql |
346+ CoreWebView2BrowsingDataKinds . CacheStorage |
347+ CoreWebView2BrowsingDataKinds . Cookies |
348+ CoreWebView2BrowsingDataKinds . DiskCache |
349+ CoreWebView2BrowsingDataKinds . DownloadHistory |
350+ CoreWebView2BrowsingDataKinds . GeneralAutofill |
351+ CoreWebView2BrowsingDataKinds . PasswordAutosave |
352+ CoreWebView2BrowsingDataKinds . Settings
353+ ) ;
350354 }
351355
352356 _activeFileInfo = null ;
0 commit comments