Add SVG Thumbnail Provider for Windows Explorer (#5048)

* Add SVG Thumbnail Provider

* Some cleanup

* Small settings changes

* Update PowerToys.sln

Remove Any CPU entries

* Fix project configuration issues

* Fix bad merge

* Update output path for SVG thumbnail provider

* Sync with latest
This commit is contained in:
Chris Davis
2020-07-21 16:27:12 -07:00
committed by GitHub
parent 8e5a877997
commit 40d2ef4f0a
28 changed files with 1610 additions and 687 deletions

View File

@@ -103,6 +103,8 @@
<Compile Include="cominterop\IPreviewHandler.cs" />
<Compile Include="cominterop\IPreviewHandlerFrame.cs" />
<Compile Include="cominterop\IPreviewHandlerVisuals.cs" />
<Compile Include="cominterop\IThumbnailProvider.cs" />
<Compile Include="cominterop\IViewObject.cs" />
<Compile Include="cominterop\LOGFONT.cs" />
<Compile Include="cominterop\MSG.cs" />
<Compile Include="cominterop\RECT.cs" />
@@ -125,6 +127,7 @@
<Compile Include="handlers\StreamBasedPreviewHandler.cs" />
<Compile Include="examplehandler\TestCustomHandler.cs" />
<Compile Include="Utilities\StreamWrapper.cs" />
<Compile Include="Utilities\SvgPreviewHandlerHelper.cs" />
</ItemGroup>
<ItemGroup>
<AdditionalFiles Include="..\..\..\codeAnalysis\StyleCop.json">

View File

@@ -0,0 +1,66 @@
// Copyright (c) Microsoft Corporation
// The Microsoft Corporation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Xml.Linq;
namespace Common.Utilities
{
/// <summary>
/// Helper utilities for Svg Preview Handler.
/// </summary>
public class SvgPreviewHandlerHelper
{
/// <summary>
/// Dictionary of elements in lower case that are blocked from Svg for preview pane.
/// Reference for list of Svg Elements: https://developer.mozilla.org/en-US/docs/Web/SVG/Element.
/// </summary>
private static Dictionary<string, bool> blockedElementsName = new Dictionary<string, bool>
{
{ "script", true },
{ "image", true },
{ "feimage", true },
};
/// <summary>
/// Check if any of the blocked elements present in Svg.
/// </summary>
/// <param name="svgData">Input Svg.</param>
/// <returns>Returns true in case any of the blocked element is present otherwise false.</returns>
public static bool CheckBlockedElements(string svgData)
{
bool foundBlockedElement = false;
if (string.IsNullOrWhiteSpace(svgData))
{
return foundBlockedElement;
}
// Check if any of the blocked element is present. If failed to parse or iterate over Svg return default false.
// No need to throw because all the external content and script are blocked on the Web Browser Control itself.
try
{
var doc = XDocument.Parse(svgData);
var elements = doc.Descendants().ToList();
foreach (XElement element in elements)
{
var elementName = element?.Name?.LocalName?.ToLower();
if (elementName != null && blockedElementsName.ContainsKey(elementName))
{
foundBlockedElement = true;
// No need to iterate further since we are displaying info bar with condition of atleast one occurrence of blocked element is present.
break;
}
}
}
catch (Exception)
{
}
return foundBlockedElement;
}
}
}

View File

@@ -0,0 +1,47 @@
// Copyright (c) Microsoft Corporation
// The Microsoft Corporation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using System;
using System.Runtime.InteropServices;
namespace Common.ComInterlop
{
/// <summary>
/// Specifies the alpha type of the image.
/// </summary>
public enum WTS_ALPHATYPE : int
{
/// <summary>
/// he bitmap is an unknown format. The Shell tries nonetheless to detect whether the image has an alpha channel.
/// </summary>
WTSAT_UNKNOWN = 0,
/// <summary>
/// The bitmap is an RGB image without alpha. The alpha channel is invalid and the Shell ignores it.
/// </summary>
WTSAT_RGB = 1,
/// <summary>
/// The bitmap is an ARGB image with a valid alpha channel.
/// </summary>
WTSAT_ARGB = 2,
}
/// <summary>
/// Exposes methods for thumbnail provider.
/// </summary>
[ComImport]
[Guid("e357fccd-a995-4576-b01f-234630154e96")]
[InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
public interface IThumbnailProvider
{
/// <summary>
/// Gets a thumbnail image and alpha type.
/// </summary>
/// <param name="cx">The maximum thumbnail size, in pixels.</param>
/// <param name="phbmp">When this method returns, contains a pointer to the thumbnail image handle.</param>
/// <param name="pdwAlpha">When this method returns, contains a pointer to one of the values from the WTS_ALPHATYPE enumeration.</param>
void GetThumbnail(uint cx, out IntPtr phbmp, out WTS_ALPHATYPE pdwAlpha);
}
}

View File

@@ -0,0 +1,33 @@
// Copyright (c) Microsoft Corporation
// The Microsoft Corporation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using System;
using System.Runtime.InteropServices;
namespace Common.ComInterlop
{
/// <summary>
/// Enables an object to display itself directly without passing a data object to the caller.
/// </summary>
[ComImport]
[Guid("0000010D-0000-0000-C000-000000000046")]
[InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
public interface IViewObject
{
/// <summary>
/// Draws a representation of an object onto the specified device context.
/// </summary>
/// <param name="dwAspect">Specifies the aspect to be drawn, that is, how the object is to be represented.</param>
/// <param name="lindex">Portion of the object that is of interest for the draw operation.</param>
/// <param name="pvAspect">Pointer to additional information in a DVASPECTINFO structure that enables drawing optimizations depending on the aspect specified.</param>
/// <param name="ptd">Pointer to the DVTARGETDEVICE structure that describes the device for which the object is to be rendered.</param>
/// <param name="hdcTargetDev">Information context for the target device indicated by the ptd parameter from which the object can extract device metrics and test the device's capabilities.</param>
/// <param name="hdcDraw">Device context on which to draw. For a windowless object, the hdcDraw parameter should be in MM_TEXT mapping mode with its logical coordinates matching the client coordinates of the containing window.</param>
/// <param name="lprcBounds">Pointer to a RECTL structure specifying the rectangle on hdcDraw and in which the object should be drawn.</param>
/// <param name="lprcWBounds">If hdcDraw is a metafile device context, pointer to a RECTL structure specifying the bounding rectangle in the underlying metafile.</param>
/// <param name="pfnContinue">Pointer to a callback function that the view object should call periodically during a lengthy drawing operation to determine whether the operation should continue or be canceled.</param>
/// <param name="dwContinue">Value to pass as a parameter to the function pointed to by the pfnContinue parameter.</param>
void Draw([MarshalAs(UnmanagedType.U4)] uint dwAspect, int lindex, IntPtr pvAspect, [In] IntPtr ptd, IntPtr hdcTargetDev, IntPtr hdcDraw, [MarshalAs(UnmanagedType.Struct)] ref RECT lprcBounds, [In] IntPtr lprcWBounds, IntPtr pfnContinue, [MarshalAs(UnmanagedType.U4)] uint dwContinue);
}
}