diff --git a/src/modules/peek/Peek.FilePreviewer/Controls/BrowserControl.xaml.cs b/src/modules/peek/Peek.FilePreviewer/Controls/BrowserControl.xaml.cs index 39f94af55e..1fbe78a166 100644 --- a/src/modules/peek/Peek.FilePreviewer/Controls/BrowserControl.xaml.cs +++ b/src/modules/peek/Peek.FilePreviewer/Controls/BrowserControl.xaml.cs @@ -24,6 +24,8 @@ namespace Peek.FilePreviewer.Controls /// private Uri? _navigatedUri; + private Color originalBackgroundColor; + public delegate void NavigationCompletedHandler(WebView2? sender, CoreWebView2NavigationCompletedEventArgs? args); public delegate void DOMContentLoadedHandler(CoreWebView2? sender, CoreWebView2DOMContentLoadedEventArgs? args); @@ -120,7 +122,11 @@ namespace Peek.FilePreviewer.Controls { await PreviewBrowser.EnsureCoreWebView2Async(); - // transparent background when loading the page + // Storing the original background color so it can be reset later for specific file types like HTML. + originalBackgroundColor = PreviewBrowser.DefaultBackgroundColor; + + // Setting the background color to transparent when initially loading the WebView2 component. + // This ensures that non-HTML files are displayed with a transparent background. PreviewBrowser.DefaultBackgroundColor = Color.FromArgb(0, 0, 0, 0); PreviewBrowser.CoreWebView2.Settings.AreDefaultScriptDialogsEnabled = false; @@ -150,6 +156,15 @@ namespace Peek.FilePreviewer.Controls private void CoreWebView2_DOMContentLoaded(CoreWebView2 sender, CoreWebView2DOMContentLoadedEventArgs args) { + // If the file being previewed is HTML or HTM, reset the background color to its original state. + // This is done to ensure that HTML and HTM files are displayed as intended, with their own background settings. + if (Source?.ToString().EndsWith(".html", StringComparison.OrdinalIgnoreCase) == true || + Source?.ToString().EndsWith(".htm", StringComparison.OrdinalIgnoreCase) == true) + { + // Reset to default behavior for HTML files + PreviewBrowser.DefaultBackgroundColor = originalBackgroundColor; + } + DOMContentLoaded?.Invoke(sender, args); }