[FileExplorer] StlThumbnailProvider (#15568)

* Adds StlThumbnailProvider

* Spell checker fixes

* Adds missing changes

* Attempts to fix alpha background issue

* Adds missing dependency references

* Upgrades .NET Core 3.1 to .NET 5

* Updates Helix Toolkit to fix .net5 compatibility

* Return null bitmap If STL model is empty
This commit is contained in:
Pedro Lamas 2022-01-25 11:51:37 +00:00 committed by GitHub
parent edc43e39ca
commit cccadec44c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
22 changed files with 530 additions and 2 deletions

View File

@ -48,6 +48,7 @@ body:
- PowerRename
- PowerToys Run
- Shortcut Guide
- STL Thumbnail
- SVG Preview
- SVG Thumbnail
- Settings

View File

@ -26,6 +26,7 @@ ignore$
\.pdf$
\.PNG$
\.png$
\.stl$
\.woff$
\.zip$
^doc/devdocs/akaLinks\.md$

View File

@ -1103,6 +1103,7 @@ LPVOID
LPW
lpwndpl
LPWSTR
LReader
LRESULT
lshift
lstrcmp
@ -1397,6 +1398,7 @@ outsettings
OVERLAPPEDWINDOW
overlaywindow
Overridable
Oversampling
OWNDC
PACL
PAINTSTRUCT
@ -1861,6 +1863,7 @@ STEPIT
stgm
STGMEDIUM
sticpl
stl
stoi
stol
stoll

View File

@ -57,6 +57,8 @@
"modules\\FileExplorerPreview\\PowerToys.PdfThumbnailProvider.comhost.dll",
"modules\\FileExplorerPreview\\PowerToys.powerpreview.dll",
"modules\\FileExplorerPreview\\PowerToys.PreviewHandlerCommon.dll",
"modules\\FileExplorerPreview\\PowerToys.StlThumbnailProvider.dll",
"modules\\FileExplorerPreview\\PowerToys.StlThumbnailProvider.comhost.dll",
"modules\\FileExplorerPreview\\PowerToys.SvgPreviewHandler.dll",
"modules\\FileExplorerPreview\\PowerToys.SvgPreviewHandler.comhost.dll",
"modules\\FileExplorerPreview\\PowerToys.SvgThumbnailProvider.dll",
@ -165,6 +167,8 @@
"NLog.dll",
"HtmlAgilityPack.dll",
"Markdig.Signed.dll",
"HelixToolkit.dll",
"HelixToolkit.Core.Wpf.dll",
"Mages.Core.dll",
"JetBrains.Annotations.dll",
"ICSharpCode.SharpZipLib.dll",

View File

@ -103,6 +103,7 @@ steps:
testAssemblyVer2: |
**\UnitTests-GcodeThumbnailProvider.dll
**\UnitTests-SvgThumbnailProvider.dll
**\UnitTests-StlThumbnailProvider.dll
**\UnitTests-PdfThumbnailProvider.dll
**\Settings.UI.UnitTests.dll
**\UnitTests-MarkdownPreviewHandler.dll

View File

@ -390,6 +390,10 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Community.PowerToys.Run.Plu
EndProject
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "MousePointerCrosshair", "src\modules\MouseUtils\MousePointerCrosshair\MousePointerCrosshair.vcxproj", "{EAE14C0E-7A6B-45DA-9080-A7D8C077BA6E}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "StlThumbnailProvider", "src\modules\previewpane\StlThumbnailProvider\StlThumbnailProvider.csproj", "{F7C8C0F1-5431-4347-89D0-8E5354F93CF2}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "UnitTests-StlThumbnailProvider", "src\modules\previewpane\UnitTests-StlThumbnailProvider\UnitTests-StlThumbnailProvider.csproj", "{F1F6B6B6-9F18-4A17-8B5C-97DF552C53DC}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|x64 = Debug|x64
@ -1053,6 +1057,18 @@ Global
{EAE14C0E-7A6B-45DA-9080-A7D8C077BA6E}.Release|x64.ActiveCfg = Release|x64
{EAE14C0E-7A6B-45DA-9080-A7D8C077BA6E}.Release|x64.Build.0 = Release|x64
{EAE14C0E-7A6B-45DA-9080-A7D8C077BA6E}.Release|x86.ActiveCfg = Release|x64
{F7C8C0F1-5431-4347-89D0-8E5354F93CF2}.Debug|x64.ActiveCfg = Debug|x64
{F7C8C0F1-5431-4347-89D0-8E5354F93CF2}.Debug|x64.Build.0 = Debug|x64
{F7C8C0F1-5431-4347-89D0-8E5354F93CF2}.Debug|x86.ActiveCfg = Debug|x64
{F7C8C0F1-5431-4347-89D0-8E5354F93CF2}.Release|x64.ActiveCfg = Release|x64
{F7C8C0F1-5431-4347-89D0-8E5354F93CF2}.Release|x64.Build.0 = Release|x64
{F7C8C0F1-5431-4347-89D0-8E5354F93CF2}.Release|x86.ActiveCfg = Release|x64
{F1F6B6B6-9F18-4A17-8B5C-97DF552C53DC}.Debug|x64.ActiveCfg = Debug|x64
{F1F6B6B6-9F18-4A17-8B5C-97DF552C53DC}.Debug|x64.Build.0 = Debug|x64
{F1F6B6B6-9F18-4A17-8B5C-97DF552C53DC}.Debug|x86.ActiveCfg = Debug|x64
{F1F6B6B6-9F18-4A17-8B5C-97DF552C53DC}.Release|x64.ActiveCfg = Release|x64
{F1F6B6B6-9F18-4A17-8B5C-97DF552C53DC}.Release|x64.Build.0 = Release|x64
{F1F6B6B6-9F18-4A17-8B5C-97DF552C53DC}.Release|x86.ActiveCfg = Release|x64
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
@ -1181,6 +1197,8 @@ Global
{48A0A19E-A0BE-4256-ACF8-CC3B80291AF9} = {60CD2D4F-C3B9-4897-9821-FCA5098B41CE}
{9F94B303-5E21-4364-9362-64426F8DB932} = {4AFC9975-2456-4C70-94A4-84073C1CED93}
{EAE14C0E-7A6B-45DA-9080-A7D8C077BA6E} = {322566EF-20DC-43A6-B9F8-616AF942579A}
{F7C8C0F1-5431-4347-89D0-8E5354F93CF2} = {2F305555-C296-497E-AC20-5FA1B237996A}
{F1F6B6B6-9F18-4A17-8B5C-97DF552C53DC} = {2F305555-C296-497E-AC20-5FA1B237996A}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {C3A2F9D1-7930-4EF4-A6FC-7EE0A99821D0}

View File

@ -877,6 +877,13 @@
<File Source="$(var.BinX64Dir)modules\FileExplorerPreview\PowerToys.GcodeThumbnailProvider.comhost.dll" />
<File Source="$(var.BinX64Dir)modules\FileExplorerPreview\PowerToys.GcodeThumbnailProvider.runtimeconfig.json" />
<File Source="$(var.BinX64Dir)modules\FileExplorerPreview\PowerToys.GcodeThumbnailProvider.deps.json" />
<!-- File to include dll for Stl Thumbnail Provider -->
<File Source="$(var.BinX64Dir)modules\FileExplorerPreview\PowerToys.StlThumbnailProvider.dll" />
<File Source="$(var.BinX64Dir)modules\FileExplorerPreview\PowerToys.StlThumbnailProvider.comhost.dll" />
<File Source="$(var.BinX64Dir)modules\FileExplorerPreview\PowerToys.StlThumbnailProvider.runtimeconfig.json" />
<File Source="$(var.BinX64Dir)modules\FileExplorerPreview\PowerToys.StlThumbnailProvider.deps.json" />
<File Source="$(var.BinX64Dir)modules\FileExplorerPreview\HelixToolkit.dll" />
<File Source="$(var.BinX64Dir)modules\FileExplorerPreview\HelixToolkit.Core.Wpf.dll" />
</Component>
</DirectoryRef>

View File

@ -106,6 +106,20 @@ inline registry::ChangeSet getGcodeThumbnailHandlerChangeSet(const std::wstring
L".gcode");
}
inline registry::ChangeSet getStlThumbnailHandlerChangeSet(const std::wstring installationDir, const bool perUser)
{
using namespace registry::shellex;
return generatePreviewHandler(PreviewHandlerType::thumbnail,
perUser,
L"{8BC8AFC2-4E7C-4695-818E-8C1FFDCEA2AF}",
get_std_product_version(),
(fs::path{ installationDir } / LR"d(modules\FileExplorerPreview\PowerToys.StlThumbnailProvider.comhost.dll)d").wstring(),
registry::DOTNET_COMPONENT_CATEGORY_CLSID,
L"Microsoft.PowerToys.ThumbnailHandler.Stl.StlThumbnailProvider",
L"Stl Thumbnail Provider",
L".stl");
}
inline std::vector<registry::ChangeSet> getAllModulesChangeSets(const std::wstring installationDir)
{
constexpr bool PER_USER = true;
@ -115,5 +129,6 @@ inline std::vector<registry::ChangeSet> getAllModulesChangeSets(const std::wstri
getGcodePreviewHandlerChangeSet(installationDir, PER_USER),
getSvgThumbnailHandlerChangeSet(installationDir, PER_USER),
getPdfThumbnailHandlerChangeSet(installationDir, PER_USER),
getGcodeThumbnailHandlerChangeSet(installationDir, PER_USER) };
getGcodeThumbnailHandlerChangeSet(installationDir, PER_USER),
getStlThumbnailHandlerChangeSet(installationDir, PER_USER) };
}

View File

@ -0,0 +1,145 @@
// 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.IO;
using System.Runtime.InteropServices;
using System.Runtime.InteropServices.ComTypes;
using System.Windows;
using System.Windows.Media;
using System.Windows.Media.Media3D;
using Common.ComInterlop;
using Common.Utilities;
using HelixToolkit.Wpf;
using Bitmap = System.Drawing.Bitmap;
namespace Microsoft.PowerToys.ThumbnailHandler.Stl
{
/// <summary>
/// Stl Thumbnail Provider.
/// </summary>
[Guid("8BC8AFC2-4E7C-4695-818E-8C1FFDCEA2AF")]
[ClassInterface(ClassInterfaceType.None)]
[ComVisible(true)]
public class StlThumbnailProvider : IInitializeWithStream, IThumbnailProvider
{
/// <summary>
/// Gets the stream object to access file.
/// </summary>
public IStream Stream { get; private set; }
/// <summary>
/// The maximum dimension (width or height) thumbnail we will generate.
/// </summary>
private const uint MaxThumbnailSize = 10000;
/// <summary>
/// Loads the Stl model into a Viewport3D and renders a bitmap of it.
/// </summary>
/// <param name="stream">The Stream instance for the Stl content.</param>
/// <param name="cx">The maximum thumbnail size, in pixels.</param>
/// <returns>A thumbnail rendered from the Stl model.</returns>
public static Bitmap GetThumbnail(Stream stream, uint cx)
{
if (cx > MaxThumbnailSize || stream == null || stream.Length == 0)
{
return null;
}
Bitmap thumbnail = null;
var stlReader = new StLReader
{
DefaultMaterial = new DiffuseMaterial(new SolidColorBrush(Color.FromRgb(255, 201, 36))),
};
var model = stlReader.Read(stream);
if (model.Bounds == Rect3D.Empty)
{
return null;
}
var viewport = new System.Windows.Controls.Viewport3D();
viewport.Measure(new Size(cx, cx));
viewport.Arrange(new Rect(0, 0, cx, cx));
var modelVisual = new ModelVisual3D()
{
Transform = new RotateTransform3D(new AxisAngleRotation3D(new Vector3D(0, 0, 1), 180)),
};
viewport.Children.Add(modelVisual);
viewport.Children.Add(new DefaultLights());
var perspectiveCamera = new PerspectiveCamera
{
Position = new Point3D(1, 2, 1),
LookDirection = new Vector3D(-1, -2, -1),
UpDirection = new Vector3D(0, 0, 1),
FieldOfView = 20,
NearPlaneDistance = 0.1,
FarPlaneDistance = double.PositiveInfinity,
};
viewport.Camera = perspectiveCamera;
modelVisual.Content = model;
perspectiveCamera.ZoomExtents(viewport);
var bitmapExporter = new BitmapExporter
{
Background = new SolidColorBrush(Colors.Transparent),
OversamplingMultiplier = 1,
};
var bitmapStream = new MemoryStream();
bitmapExporter.Export(viewport, bitmapStream);
bitmapStream.Position = 0;
thumbnail = new Bitmap(bitmapStream);
return thumbnail;
}
/// <inheritdoc/>
public void Initialize(IStream pstream, uint grfMode)
{
// Ignore the grfMode always use read mode to access the file.
this.Stream = pstream;
}
/// <inheritdoc/>
public void GetThumbnail(uint cx, out IntPtr phbmp, out WTS_ALPHATYPE pdwAlpha)
{
phbmp = IntPtr.Zero;
pdwAlpha = WTS_ALPHATYPE.WTSAT_UNKNOWN;
if (cx == 0 || cx > MaxThumbnailSize)
{
return;
}
using (var stream = new ReadonlyStream(this.Stream as IStream))
{
using (var memStream = new MemoryStream())
{
stream.CopyTo(memStream);
memStream.Position = 0;
using (Bitmap thumbnail = GetThumbnail(memStream, cx))
{
if (thumbnail != null && thumbnail.Size.Width > 0 && thumbnail.Size.Height > 0)
{
phbmp = thumbnail.GetHbitmap(System.Drawing.Color.Transparent);
pdwAlpha = WTS_ALPHATYPE.WTSAT_ARGB;
}
}
}
}
}
}
}

View File

@ -0,0 +1,65 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<Platforms>x64</Platforms>
<UseWindowsForms>true</UseWindowsForms>
<ProjectGuid>{F7C8C0F1-5431-4347-89D0-8E5354F93CF2}</ProjectGuid>
<RootNamespace>Microsoft.PowerToys.ThumbnailHandler.Stl</RootNamespace>
<AssemblyName>PowerToys.StlThumbnailProvider</AssemblyName>
<AssemblyTitle>PowerToys.StlThumbnailProvider</AssemblyTitle>
<AssemblyDescription>PowerToys StlPreviewHandler</AssemblyDescription>
<AssemblyCompany>Microsoft Corporation</AssemblyCompany>
<AssemblyCopyright>Copyright (C) 2020 Microsoft Corporation</AssemblyCopyright>
<AssemblyProduct>PowerToys</AssemblyProduct>
<TargetFramework>net5.0-windows</TargetFramework>
<EnableComHosting>true</EnableComHosting>
<AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
<Company>Microsoft Corporation</Company>
<Product>PowerToys</Product>
<NeutralLanguage>en-US</NeutralLanguage>
<Description>PowerToys StlPreviewHandler</Description>
<Copyright>Copyright (C) 2020 Microsoft Corporation</Copyright>
<OutputPath>$(SolutionDir)$(Platform)\$(Configuration)\modules\FileExplorerPreview\</OutputPath>
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
<AppendTargetFrameworkToOutputPath>false</AppendTargetFrameworkToOutputPath>
<AppendRuntimeIdentifierToOutputPath>false</AppendRuntimeIdentifierToOutputPath>
<UseWPF>true</UseWPF>
<CopyLocalLockFileAssemblies>true</CopyLocalLockFileAssemblies>
</PropertyGroup>
<Import Project="..\..\..\Version.props" />
<ItemGroup>
<PackageReference Include="HelixToolkit" Version="2.20.1" />
<PackageReference Include="HelixToolkit.Core.Wpf" Version="2.20.1" />
<PackageReference Include="Microsoft.CodeAnalysis.FxCopAnalyzers" Version="3.3.0">
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="Microsoft.CodeAnalysis.VersionCheckAnalyzer" Version="3.3.0">
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="Microsoft.CodeQuality.Analyzers" Version="3.3.0">
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="Microsoft.NetCore.Analyzers" Version="3.3.0">
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="Microsoft.NetFramework.Analyzers" Version="3.3.0">
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="StyleCop.Analyzers" Version="1.1.118">
<PrivateAssets>all</PrivateAssets>
</PackageReference>
</ItemGroup>
<ItemGroup>
<Compile Include="..\..\..\codeAnalysis\GlobalSuppressions.cs" Link="GlobalSuppressions.cs" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\..\common\ManagedTelemetry\Telemetry\ManagedTelemetry.csproj" />
<ProjectReference Include="..\Common\PreviewHandlerCommon.csproj" />
</ItemGroup>
<ItemGroup>
<AdditionalFiles Include="..\..\..\codeAnalysis\StyleCop.json">
<Link>StyleCop.json</Link>
</AdditionalFiles>
</ItemGroup>
</Project>

View File

@ -0,0 +1,111 @@
// 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.Drawing;
using System.IO;
using System.Runtime.InteropServices;
using System.Runtime.InteropServices.ComTypes;
using Common.ComInterlop;
using Microsoft.PowerToys.STATestExtension;
using Microsoft.PowerToys.ThumbnailHandler.Stl;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Moq;
namespace StlThumbnailProviderUnitTests
{
[STATestClass]
public class StlThumbnailProviderTests
{
[TestMethod]
public void GetThumbnailValidStreamStl()
{
// Act
var file = File.ReadAllBytes("HelperFiles/sample.stl");
StlThumbnailProvider provider = new StlThumbnailProvider();
provider.Initialize(GetMockStream(file), 0);
provider.GetThumbnail(256, out IntPtr bitmap, out WTS_ALPHATYPE alphaType);
Assert.IsTrue(bitmap != IntPtr.Zero);
Assert.IsTrue(alphaType == WTS_ALPHATYPE.WTSAT_ARGB);
}
[TestMethod]
public void GetThumbnailInValidSizeStl()
{
// Act
var file = File.ReadAllBytes("HelperFiles/sample.stl");
StlThumbnailProvider provider = new StlThumbnailProvider();
provider.Initialize(GetMockStream(file), 0);
provider.GetThumbnail(0, out IntPtr bitmap, out WTS_ALPHATYPE alphaType);
Assert.IsTrue(bitmap == IntPtr.Zero);
Assert.IsTrue(alphaType == WTS_ALPHATYPE.WTSAT_UNKNOWN);
}
[TestMethod]
public void GetThumbnailToBigStl()
{
// Act
var file = File.ReadAllBytes("HelperFiles/sample.stl");
StlThumbnailProvider provider = new StlThumbnailProvider();
provider.Initialize(GetMockStream(file), 0);
provider.GetThumbnail(10001, out IntPtr bitmap, out WTS_ALPHATYPE alphaType);
Assert.IsTrue(bitmap == IntPtr.Zero);
Assert.IsTrue(alphaType == WTS_ALPHATYPE.WTSAT_UNKNOWN);
}
[TestMethod]
public void CheckNoStlEmptyStreamShouldReturnNullBitmap()
{
using (var stream = new MemoryStream())
{
Bitmap thumbnail = StlThumbnailProvider.GetThumbnail(stream, 256);
Assert.IsTrue(thumbnail == null);
}
}
[TestMethod]
public void CheckNoStlNullStreamShouldReturnNullBitmap()
{
Bitmap thumbnail = StlThumbnailProvider.GetThumbnail(null, 256);
Assert.IsTrue(thumbnail == null);
}
private static IStream GetMockStream(byte[] sourceArray)
{
var streamMock = new Mock<IStream>();
int bytesRead = 0;
streamMock
.Setup(x => x.Read(It.IsAny<byte[]>(), It.IsAny<int>(), It.IsAny<IntPtr>()))
.Callback<byte[], int, IntPtr>((buffer, countToRead, bytesReadPtr) =>
{
int actualCountToRead = Math.Min(sourceArray.Length - bytesRead, countToRead);
if (actualCountToRead > 0)
{
Array.Copy(sourceArray, bytesRead, buffer, 0, actualCountToRead);
Marshal.WriteInt32(bytesReadPtr, actualCountToRead);
bytesRead += actualCountToRead;
}
else
{
Marshal.WriteInt32(bytesReadPtr, 0);
}
});
return streamMock.Object;
}
}
}

View File

@ -0,0 +1,78 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<Platforms>x64</Platforms>
<AssemblyTitle>UnitTests-StlThumbnailProvider</AssemblyTitle>
<AssemblyDescription>PowerToys UnitTests-StlThumbnailProvider</AssemblyDescription>
<AssemblyCompany>Microsoft Corporation</AssemblyCompany>
<AssemblyCopyright>Copyright (C) 2020 Microsoft Corporation</AssemblyCopyright>
<AssemblyProduct>PowerToys</AssemblyProduct>
<AssemblyTitle>UnitTests-StlThumbnailProvider</AssemblyTitle>
<Company>Microsoft Corporation</Company>
<Product>PowerToys</Product>
<NeutralLanguage>en-US</NeutralLanguage>
<Description>PowerToys UnitTests-StlThumbnailProvider</Description>
<Copyright>Copyright (C) 2021 Microsoft Corporation</Copyright>
</PropertyGroup>
<PropertyGroup>
<ProjectGuid>{F1F6B6B6-9F18-4A17-8B5C-97DF552C53DC}</ProjectGuid>
<RootNamespace>StlThumbnailProviderUnitTests</RootNamespace>
<TargetFramework>net5.0-windows10.0.18362.0</TargetFramework>
<ProjectTypeGuids>{3AC096D0-A1C2-E12C-1390-A8335801FDAB};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}</ProjectTypeGuids>
<VSToolsPath Condition="'$(VSToolsPath)' == ''">$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)</VSToolsPath>
<ReferencePath>$(ProgramFiles)\Common Files\microsoft shared\VSTT\$(VisualStudioVersion)\UITestExtensionPackages</ReferencePath>
<IsCodedUITest>False</IsCodedUITest>
<TestProjectType>UnitTest</TestProjectType>
</PropertyGroup>
<Import Project="..\..\..\Version.props" />
<ItemGroup>
<None Remove="HelperFiles\sample.stl" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="Castle.Core" Version="4.4.1" />
<PackageReference Include="Microsoft.CodeAnalysis.FxCopAnalyzers" Version="3.3.0">
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="Microsoft.CodeAnalysis.VersionCheckAnalyzer" Version="3.3.0">
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="Microsoft.CodeQuality.Analyzers" Version="3.3.0">
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="Microsoft.NetCore.Analyzers" Version="3.3.0">
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="Microsoft.NetFramework.Analyzers" Version="3.3.0">
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="Moq" Version="4.16.1" />
<PackageReference Include="MSTest.TestAdapter" Version="2.2.3" />
<PackageReference Include="MSTest.TestFramework" Version="2.2.3" />
<PackageReference Include="StyleCop.Analyzers" Version="1.1.118">
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="System.Runtime.CompilerServices.Unsafe" Version="5.0.0" />
<PackageReference Include="System.Threading.Tasks.Extensions" Version="4.5.4" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.10.0" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Common\PreviewHandlerCommon.csproj" />
<ProjectReference Include="..\StlThumbnailProvider\StlThumbnailProvider.csproj" />
</ItemGroup>
<ItemGroup>
<Compile Include="..\..\..\codeAnalysis\GlobalSuppressions.cs" Link="GlobalSuppressions.cs" />
<Compile Include="..\STATestClassAttribute.cs" Link="STATestClassAttribute.cs" />
<Compile Include="..\STATestMethodAttribute.cs" Link="STATestMethodAttribute.cs" />
<AdditionalFiles Include="..\..\..\codeAnalysis\StyleCop.json">
<Link>StyleCop.json</Link>
</AdditionalFiles>
</ItemGroup>
<ItemGroup>
<Content Include="HelperFiles\sample.stl">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>
</ItemGroup>
</Project>

View File

@ -39,6 +39,9 @@ const CLSID CLSID_GcodePreviewHandler = { 0xec52dea8, 0x7c9f, 0x4130, { 0xa7, 0x
// BFEE99B4-B74D-4348-BCA5-E757029647FF
const GUID CLSID_GcodeThumbnailProvider = { 0xbfee99b4, 0xb74d, 0x4348, { 0xbc, 0xa5, 0xe7, 0x57, 0x02, 0x96, 0x47, 0xff } };
// 8BC8AFC2-4E7C-4695-818E-8C1FFDCEA2AF
const GUID CLSID_StlThumbnailProvider = { 0x8bc8afc2, 0x4e7c, 0x4695, { 0x81, 0x8e, 0x8c, 0x1f, 0xfd, 0xce, 0xa2, 0xaf } };
// Pairs of NativeClsid vs ManagedClsid used for preview handlers.
const std::vector<std::pair<CLSID, CLSID>> NativeToManagedClsid({

View File

@ -186,4 +186,7 @@
<data name="Prevpane_Gcode_Settings_Displayname" xml:space="preserve">
<value>G-code Previewer</value>
</data>
<data name="Stl_Thumbnail_Provider_Settings_Description" xml:space="preserve">
<value>Stl Thumbnail Provider</value>
</data>
</root>

View File

@ -54,6 +54,10 @@ PowerPreviewModule::PowerPreviewModule() :
.settingDescription = GET_RESOURCE_STRING(IDS_GCODE_THUMBNAIL_PROVIDER_SETTINGS_DESCRIPTION),
.registryChanges = getGcodeThumbnailHandlerChangeSet(installationDir, installPerUser) });
m_fileExplorerModules.push_back({ .settingName = L"stl-thumbnail-toggle-setting",
.settingDescription = GET_RESOURCE_STRING(IDS_STL_THUMBNAIL_PROVIDER_SETTINGS_DESCRIPTION),
.registryChanges = getStlThumbnailHandlerChangeSet(installationDir, installPerUser) });
try
{
PowerToysSettings::PowerToyValues settings =

View File

@ -131,6 +131,23 @@ namespace Microsoft.PowerToys.Settings.UI.Library
}
}
private bool enableStlThumbnail = true;
[JsonPropertyName("stl-thumbnail-toggle-setting")]
[JsonConverter(typeof(BoolPropertyJsonConverter))]
public bool EnableStlThumbnail
{
get => enableStlThumbnail;
set
{
if (value != enableStlThumbnail)
{
LogTelemetryEvent(value);
enableStlThumbnail = value;
}
}
}
public PowerPreviewProperties()
{
}

View File

@ -53,6 +53,7 @@ namespace Microsoft.PowerToys.Settings.UI.Library.ViewModels
_gcodeRenderIsEnabled = Settings.Properties.EnableGcodePreview;
_pdfThumbnailIsEnabled = Settings.Properties.EnablePdfThumbnail;
_gcodeThumbnailIsEnabled = Settings.Properties.EnableGcodeThumbnail;
_stlThumbnailIsEnabled = Settings.Properties.EnableStlThumbnail;
}
private bool _svgRenderIsEnabled;
@ -62,6 +63,7 @@ namespace Microsoft.PowerToys.Settings.UI.Library.ViewModels
private bool _svgThumbnailIsEnabled;
private bool _pdfThumbnailIsEnabled;
private bool _gcodeThumbnailIsEnabled;
private bool _stlThumbnailIsEnabled;
public bool SVGRenderIsEnabled
{
@ -189,6 +191,24 @@ namespace Microsoft.PowerToys.Settings.UI.Library.ViewModels
}
}
public bool STLThumbnailIsEnabled
{
get
{
return _stlThumbnailIsEnabled;
}
set
{
if (value != _stlThumbnailIsEnabled)
{
_stlThumbnailIsEnabled = value;
Settings.Properties.EnableStlThumbnail = value;
RaisePropertyChanged();
}
}
}
public string GetSettingsSubPath()
{
return _settingsConfigFileFolder + "\\" + ModuleName;

View File

@ -63,6 +63,7 @@ namespace ViewModelTests
Assert.AreEqual(originalSettings.Properties.EnableSvgThumbnail, viewModel.SVGThumbnailIsEnabled);
Assert.AreEqual(originalSettings.Properties.EnablePdfThumbnail, viewModel.PDFThumbnailIsEnabled);
Assert.AreEqual(originalSettings.Properties.EnableGcodeThumbnail, viewModel.GCODEThumbnailIsEnabled);
Assert.AreEqual(originalSettings.Properties.EnableStlThumbnail, viewModel.STLThumbnailIsEnabled);
// Verify that the stub file was used
var expectedCallCount = 2; // once via the view model, and once by the test (GetSettings<T>)
@ -141,6 +142,24 @@ namespace ViewModelTests
viewModel.GCODEThumbnailIsEnabled = true;
}
[TestMethod]
public void STLThumbnailIsEnabledShouldPrevHandlerWhenSuccessful()
{
// Assert
Func<string, int> sendMockIPCConfigMSG = msg =>
{
SndModuleSettings<SndPowerPreviewSettings> snd = JsonSerializer.Deserialize<SndModuleSettings<SndPowerPreviewSettings>>(msg);
Assert.IsTrue(snd.PowertoysSetting.FileExplorerPreviewSettings.Properties.EnableStlThumbnail);
return 0;
};
// arrange
PowerPreviewViewModel viewModel = new PowerPreviewViewModel(SettingsRepository<PowerPreviewSettings>.GetInstance(mockPowerPreviewSettingsUtils.Object), SettingsRepository<GeneralSettings>.GetInstance(mockGeneralSettingsUtils.Object), sendMockIPCConfigMSG, PowerPreviewSettings.ModuleName);
// act
viewModel.STLThumbnailIsEnabled = true;
}
[TestMethod]
public void MDRenderIsEnabledShouldPrevHandlerWhenSuccessful()
{

View File

@ -632,6 +632,10 @@
<value>Enable SVG (.svg) thumbnails</value>
<comment>Do you want this feature on / off</comment>
</data>
<data name="FileExplorerPreview_ToggleSwitch_STL_Thumbnail.Header" xml:space="preserve">
<value>Enable STL (.stl) thumbnails</value>
<comment>Do you want this feature on / off</comment>
</data>
<data name="FileExplorerPreview_ToggleSwitch_PDF_Thumbnail.Header" xml:space="preserve">
<value>Enable PDF (.pdf) thumbnails</value>
<comment>Do you want this feature on / off</comment>

View File

@ -77,6 +77,13 @@
x:Uid="ToggleSwitch"/>
</controls:Setting.ActionContent>
</controls:Setting>
<controls:Setting x:Uid="FileExplorerPreview_ToggleSwitch_STL_Thumbnail" Icon="&#xF158;">
<controls:Setting.ActionContent>
<ToggleSwitch IsOn="{x:Bind Mode=TwoWay, Path=ViewModel.STLThumbnailIsEnabled}"
x:Uid="ToggleSwitch"/>
</controls:Setting.ActionContent>
</controls:Setting>
</controls:SettingsGroup>
</StackPanel>

View File

@ -19,6 +19,7 @@ namespace
{ HKEY_CLASSES_ROOT, L"CLSID\\{ec52dea8-7c9f-4130-a77b-1737d0418507}" },
{ HKEY_CLASSES_ROOT, L"CLSID\\{BCC13D15-9720-4CC4-8371-EA74A274741E}" },
{ HKEY_CLASSES_ROOT, L"CLSID\\{BFEE99B4-B74D-4348-BCA5-E757029647FF}" },
{ HKEY_CLASSES_ROOT, L"CLSID\\{8BC8AFC2-4E7C-4695-818E-8C1FFDCEA2AF}" },
{ HKEY_CLASSES_ROOT, L"CLSID\\{51B4D7E5-7568-4234-B4BB-47FB3C016A69}\\InprocServer32" },
{ HKEY_CLASSES_ROOT, L"CLSID\\{0440049F-D1DC-4E46-B27B-98393D79486B}" },
{ HKEY_CLASSES_ROOT, L"AllFileSystemObjects\\ShellEx\\ContextMenuHandlers\\PowerRenameExt" },
@ -29,7 +30,8 @@ namespace
{ HKEY_CLASSES_ROOT, L".pdf\\shellex\\{8895b1c6-b41f-4c1c-a562-0d564250836f}" },
{ HKEY_CLASSES_ROOT, L".pdf\\shellex\\{E357FCCD-A995-4576-B01F-234630154E96}" },
{ HKEY_CLASSES_ROOT, L".gcode\\shellex\\{8895b1c6-b41f-4c1c-a562-0d564250836f}" },
{ HKEY_CLASSES_ROOT, L".gcode\\shellex\\{E357FCCD-A995-4576-B01F-234630154E96}" }
{ HKEY_CLASSES_ROOT, L".gcode\\shellex\\{E357FCCD-A995-4576-B01F-234630154E96}" },
{ HKEY_CLASSES_ROOT, L".stl\\shellex\\{E357FCCD-A995-4576-B01F-234630154E96}" }
};
vector<tuple<HKEY, wstring, wstring>> registryValues = {