mirror of
https://github.com/microsoft/PowerToys.git
synced 2025-12-16 11:48:06 +01:00
[File Explorer Add-ons] Add viewBox attribute to svgs that don't have one (#18434)
This alows svg previewer to show full svg, not just a part of it
This commit is contained in:
@@ -84,6 +84,7 @@ namespace Microsoft.PowerToys.PreviewHandler.Svg.Utilities
|
||||
string widthR = string.Empty;
|
||||
string heightR = string.Empty;
|
||||
string oldStyle = string.Empty;
|
||||
bool hasViewBox = false;
|
||||
|
||||
// Get width and height of element and remove it afterwards because it will be added inside style attribute
|
||||
for (int i = 0; i < attributes.Count(); i++)
|
||||
@@ -106,6 +107,10 @@ namespace Microsoft.PowerToys.PreviewHandler.Svg.Utilities
|
||||
attributes.ElementAt(i).Remove();
|
||||
i--;
|
||||
}
|
||||
else if (attributes.ElementAt(i).Name == "viewBox")
|
||||
{
|
||||
hasViewBox = true;
|
||||
}
|
||||
}
|
||||
|
||||
svgData.ReplaceAttributes(attributes);
|
||||
@@ -122,6 +127,14 @@ namespace Microsoft.PowerToys.PreviewHandler.Svg.Utilities
|
||||
scaling += $" _height:expression(this.scrollHeight > {heightR} ? \" {height}\" : \"auto\"); _width:expression(this.scrollWidth > {widthR} ? \"{width}\" : \"auto\");";
|
||||
|
||||
svgData.Add(new XAttribute("style", scaling + centering + oldStyle));
|
||||
|
||||
if (!hasViewBox)
|
||||
{
|
||||
// Fixes https://github.com/microsoft/PowerToys/issues/18107
|
||||
string viewBox = $"0 0 {widthR} {heightR}";
|
||||
svgData.Add(new XAttribute("viewBox", viewBox));
|
||||
}
|
||||
|
||||
return svgData.ToString();
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user