mirror of
https://github.com/microsoft/PowerToys.git
synced 2026-04-03 17:56:44 +02:00
[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:
@@ -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);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user