mirror of
https://github.com/microsoft/PowerToys.git
synced 2025-12-14 18:57:55 +01:00
[Peek] Use WebView2 for SVG preview to improve compatibility
This commit is contained in:
@@ -54,8 +54,6 @@ namespace Peek.FilePreviewer.Previewers
|
||||
|
||||
private bool IsPng() => Item.Extension == ".png";
|
||||
|
||||
private bool IsSvg() => Item.Extension == ".svg";
|
||||
|
||||
private bool IsQoi() => Item.Extension == ".qoi";
|
||||
|
||||
private DispatcherQueue Dispatcher { get; }
|
||||
@@ -63,7 +61,7 @@ namespace Peek.FilePreviewer.Previewers
|
||||
private static readonly HashSet<string> _supportedFileTypes =
|
||||
BitmapDecoder.GetDecoderInformationEnumerator()
|
||||
.SelectMany(di => di.FileExtensions)
|
||||
.Union([".svg", ".qoi"])
|
||||
.Union([".qoi"])
|
||||
.ToHashSet(StringComparer.OrdinalIgnoreCase);
|
||||
|
||||
public static bool IsItemSupported(IFileSystemItem item)
|
||||
@@ -75,15 +73,7 @@ namespace Peek.FilePreviewer.Previewers
|
||||
{
|
||||
cancellationToken.ThrowIfCancellationRequested();
|
||||
|
||||
if (IsSvg())
|
||||
{
|
||||
var size = await Task.Run(Item.GetSvgSize);
|
||||
if (size != null)
|
||||
{
|
||||
ImageSize = size.Value;
|
||||
}
|
||||
}
|
||||
else if (IsQoi())
|
||||
if (IsQoi())
|
||||
{
|
||||
var size = await Task.Run(Item.GetQoiSize);
|
||||
if (size != null)
|
||||
@@ -176,31 +166,16 @@ namespace Peek.FilePreviewer.Previewers
|
||||
{
|
||||
cancellationToken.ThrowIfCancellationRequested();
|
||||
|
||||
using FileStream stream = ReadHelper.OpenReadOnly(Item.Path);
|
||||
|
||||
if (IsSvg())
|
||||
{
|
||||
var source = new SvgImageSource();
|
||||
source.RasterizePixelHeight = ImageSize?.Height ?? 0;
|
||||
source.RasterizePixelWidth = ImageSize?.Width ?? 0;
|
||||
|
||||
var loadStatus = await source.SetSourceAsync(stream.AsRandomAccessStream());
|
||||
if (loadStatus != SvgImageSourceLoadStatus.Success)
|
||||
{
|
||||
Logger.LogError("Error loading SVG: " + loadStatus.ToString());
|
||||
throw new ImageLoadingException(nameof(source));
|
||||
}
|
||||
|
||||
Preview = source;
|
||||
}
|
||||
else if (IsQoi())
|
||||
if (IsQoi())
|
||||
{
|
||||
using FileStream stream = ReadHelper.OpenReadOnly(Item.Path);
|
||||
using var bitmap = QoiImage.FromStream(stream);
|
||||
|
||||
Preview = await BitmapHelper.BitmapToImageSource(bitmap, true, cancellationToken);
|
||||
}
|
||||
else
|
||||
{
|
||||
using FileStream stream = ReadHelper.OpenReadOnly(Item.Path);
|
||||
Preview = new BitmapImage();
|
||||
await ((BitmapImage)Preview).SetSourceAsync(stream.AsRandomAccessStream());
|
||||
}
|
||||
|
||||
@@ -33,6 +33,10 @@ namespace Peek.FilePreviewer.Previewers
|
||||
|
||||
// Markdown
|
||||
".md",
|
||||
|
||||
// SVG - using WebView2 for better compatibility with complex SVGs
|
||||
// (e.g., from Adobe Illustrator, Inkscape)
|
||||
".svg",
|
||||
};
|
||||
|
||||
[ObservableProperty]
|
||||
@@ -111,9 +115,10 @@ namespace Peek.FilePreviewer.Previewers
|
||||
{
|
||||
bool isHtml = File.Extension == ".html" || File.Extension == ".htm";
|
||||
bool isMarkdown = File.Extension == ".md";
|
||||
bool isSvg = File.Extension == ".svg";
|
||||
|
||||
bool supportedByMonaco = MonacoHelper.SupportedMonacoFileTypes.Contains(File.Extension);
|
||||
bool useMonaco = supportedByMonaco && !isHtml && !isMarkdown;
|
||||
bool useMonaco = supportedByMonaco && !isHtml && !isMarkdown && !isSvg;
|
||||
|
||||
IsDevFilePreview = supportedByMonaco;
|
||||
CustomContextMenu = useMonaco;
|
||||
@@ -128,6 +133,13 @@ namespace Peek.FilePreviewer.Previewers
|
||||
var raw = await ReadHelper.Read(File.Path.ToString());
|
||||
Preview = new Uri(MarkdownHelper.PreviewTempFile(raw, File.Path, TempFolderPath.Path));
|
||||
}
|
||||
else if (isSvg)
|
||||
{
|
||||
// SVG files are rendered directly by WebView2 for better compatibility
|
||||
// with complex SVGs from Adobe Illustrator, Inkscape, etc.
|
||||
IsDevFilePreview = false;
|
||||
Preview = new Uri(File.Path);
|
||||
}
|
||||
else
|
||||
{
|
||||
// Simple html file to preview. Shouldn't do things like enabling scripts or using a virtual mapped directory.
|
||||
|
||||
Reference in New Issue
Block a user