Revert "Svg thumbnail failed rendering (#32936)" as it caused a regression (#38058)

Revert "Svg thumbnail failed rendering (#32936)"

This reverts commit 1d358af600.
This commit is contained in:
leileizhang
2025-03-21 01:50:20 +08:00
committed by GitHub
parent 2b4d13ccb9
commit 5d03667bcf

View File

@@ -5,7 +5,8 @@ using System.Drawing.Drawing2D;
using System.Globalization; using System.Globalization;
using System.Reflection; using System.Reflection;
using System.Runtime.CompilerServices; using System.Runtime.CompilerServices;
using System.Xml.Linq; using System.Threading;
using Common.Utilities; using Common.Utilities;
using ManagedCommon; using ManagedCommon;
using Microsoft.Web.WebView2.Core; using Microsoft.Web.WebView2.Core;
@@ -117,51 +118,10 @@ namespace Microsoft.PowerToys.ThumbnailHandler.Svg
_browser.Height = (int)cx; _browser.Height = (int)cx;
_browser.NavigationCompleted += async (object sender, CoreWebView2NavigationCompletedEventArgs args) => _browser.NavigationCompleted += async (object sender, CoreWebView2NavigationCompletedEventArgs args) =>
{ {
try
{
// Check if the SVG element is present
var a = await _browser.ExecuteScriptAsync($"document.getElementsByTagName('svg')[0].viewBox;"); var a = await _browser.ExecuteScriptAsync($"document.getElementsByTagName('svg')[0].viewBox;");
if (a != null) if (a != null)
{ {
var svgContent = SvgContents.Substring(SvgContents.IndexOf("<svg", StringComparison.OrdinalIgnoreCase), SvgContents.IndexOf("</svg>", StringComparison.OrdinalIgnoreCase) - SvgContents.IndexOf("<svg", StringComparison.OrdinalIgnoreCase) + "</svg>".Length); await _browser.ExecuteScriptAsync($"document.getElementsByTagName('svg')[0].style = 'width:100%;height:100%';");
Dictionary<string, string> styleDict = new Dictionary<string, string>();
// Try to parse the SVG content
try
{
// Attempt to parse the svgContent
var svgDocument = XDocument.Parse(svgContent);
var svgElement = svgDocument.Root;
var currentStyle = svgElement?.Attribute("style")?.Value;
// If style attribute exists, preserve existing styles
if (!string.IsNullOrEmpty(currentStyle) && currentStyle != "null")
{
styleDict = currentStyle
.Split(';', StringSplitOptions.RemoveEmptyEntries)
.Select(stylePart => stylePart.Split(':', 2, StringSplitOptions.TrimEntries))
.Where(styleKeyValue => styleKeyValue.Length == 2 && !string.IsNullOrEmpty(styleKeyValue[0]))
.ToDictionary(
styleKeyValue => styleKeyValue[0],
styleKeyValue => styleKeyValue[1]);
}
}
catch (Exception ex)
{
// Log the error if the SVG content is not valid or parsing fails
Logger.LogError($"Failed to parse SVG content: {ex.Message}");
}
// Add or replace width and height in the existing style
styleDict["width"] = "100%";
styleDict["height"] = "100%";
// Construct a single JavaScript string to set all properties
var styleScript = string.Join(";", styleDict.Select(kv => $"document.getElementsByTagName('svg')[0].style.setProperty('{kv.Key}', '{kv.Value}');"));
// Apply the new style attributes using the constructed script
await _browser.ExecuteScriptAsync(styleScript);
} }
// Hide scrollbar - fixes #18286 // Hide scrollbar - fixes #18286
@@ -182,12 +142,6 @@ namespace Microsoft.PowerToys.ThumbnailHandler.Svg
} }
thumbnailDone.Set(); thumbnailDone.Set();
}
catch (Exception ex)
{
Logger.LogError("Error during NavigationCompleted: ", ex);
thumbnailDone.Set();
}
}; };
var webView2Options = new CoreWebView2EnvironmentOptions("--block-new-web-contents"); var webView2Options = new CoreWebView2EnvironmentOptions("--block-new-web-contents");
@@ -197,8 +151,6 @@ namespace Microsoft.PowerToys.ThumbnailHandler.Svg
.ConfigureAwait(true).GetAwaiter(); .ConfigureAwait(true).GetAwaiter();
webView2EnvironmentAwaiter.OnCompleted(async () => webView2EnvironmentAwaiter.OnCompleted(async () =>
{
for (int attempt = 0; attempt < 3; attempt++)
{ {
try try
{ {
@@ -246,21 +198,11 @@ namespace Microsoft.PowerToys.ThumbnailHandler.Svg
{ {
_browser.NavigateToString(SvgContents); _browser.NavigateToString(SvgContents);
} }
break; // Exit the retry loop if initialization succeeds
} }
catch (Exception ex) catch (Exception ex)
{
Logger.LogError($"Initialization attempt {attempt} failed: {ex.Message}");
if (attempt == 2)
{ {
Logger.LogError($"Failed running webView2Environment completed for {FilePath} : ", ex); Logger.LogError($"Failed running webView2Environment completed for {FilePath} : ", ex);
thumbnailDone.Set(); thumbnailDone.Set();
return;
}
await Task.Delay(1000); // Delay before retrying
}
} }
}); });