[Peek]Always use default white background for HTML files(#28557)

* [Peek] A check for color of body element in html and change it based on theme is added.

* [Peek] WebView2 solution is added instead of javascript injection

* [Peek] Detailed comments are added.
This commit is contained in:
gokcekantarci
2023-09-20 20:30:32 +03:00
committed by GitHub
parent 0a1759028d
commit 5ddd8d72dd

View File

@@ -24,6 +24,8 @@ namespace Peek.FilePreviewer.Controls
/// </summary>
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);
}