mirror of
https://github.com/microsoft/PowerToys.git
synced 2025-12-15 19:27:56 +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 IsPng() => Item.Extension == ".png";
|
||||||
|
|
||||||
private bool IsSvg() => Item.Extension == ".svg";
|
|
||||||
|
|
||||||
private bool IsQoi() => Item.Extension == ".qoi";
|
private bool IsQoi() => Item.Extension == ".qoi";
|
||||||
|
|
||||||
private DispatcherQueue Dispatcher { get; }
|
private DispatcherQueue Dispatcher { get; }
|
||||||
@@ -63,7 +61,7 @@ namespace Peek.FilePreviewer.Previewers
|
|||||||
private static readonly HashSet<string> _supportedFileTypes =
|
private static readonly HashSet<string> _supportedFileTypes =
|
||||||
BitmapDecoder.GetDecoderInformationEnumerator()
|
BitmapDecoder.GetDecoderInformationEnumerator()
|
||||||
.SelectMany(di => di.FileExtensions)
|
.SelectMany(di => di.FileExtensions)
|
||||||
.Union([".svg", ".qoi"])
|
.Union([".qoi"])
|
||||||
.ToHashSet(StringComparer.OrdinalIgnoreCase);
|
.ToHashSet(StringComparer.OrdinalIgnoreCase);
|
||||||
|
|
||||||
public static bool IsItemSupported(IFileSystemItem item)
|
public static bool IsItemSupported(IFileSystemItem item)
|
||||||
@@ -75,15 +73,7 @@ namespace Peek.FilePreviewer.Previewers
|
|||||||
{
|
{
|
||||||
cancellationToken.ThrowIfCancellationRequested();
|
cancellationToken.ThrowIfCancellationRequested();
|
||||||
|
|
||||||
if (IsSvg())
|
if (IsQoi())
|
||||||
{
|
|
||||||
var size = await Task.Run(Item.GetSvgSize);
|
|
||||||
if (size != null)
|
|
||||||
{
|
|
||||||
ImageSize = size.Value;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else if (IsQoi())
|
|
||||||
{
|
{
|
||||||
var size = await Task.Run(Item.GetQoiSize);
|
var size = await Task.Run(Item.GetQoiSize);
|
||||||
if (size != null)
|
if (size != null)
|
||||||
@@ -176,31 +166,16 @@ namespace Peek.FilePreviewer.Previewers
|
|||||||
{
|
{
|
||||||
cancellationToken.ThrowIfCancellationRequested();
|
cancellationToken.ThrowIfCancellationRequested();
|
||||||
|
|
||||||
using FileStream stream = ReadHelper.OpenReadOnly(Item.Path);
|
if (IsQoi())
|
||||||
|
|
||||||
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())
|
|
||||||
{
|
{
|
||||||
|
using FileStream stream = ReadHelper.OpenReadOnly(Item.Path);
|
||||||
using var bitmap = QoiImage.FromStream(stream);
|
using var bitmap = QoiImage.FromStream(stream);
|
||||||
|
|
||||||
Preview = await BitmapHelper.BitmapToImageSource(bitmap, true, cancellationToken);
|
Preview = await BitmapHelper.BitmapToImageSource(bitmap, true, cancellationToken);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
|
using FileStream stream = ReadHelper.OpenReadOnly(Item.Path);
|
||||||
Preview = new BitmapImage();
|
Preview = new BitmapImage();
|
||||||
await ((BitmapImage)Preview).SetSourceAsync(stream.AsRandomAccessStream());
|
await ((BitmapImage)Preview).SetSourceAsync(stream.AsRandomAccessStream());
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -33,6 +33,10 @@ namespace Peek.FilePreviewer.Previewers
|
|||||||
|
|
||||||
// Markdown
|
// Markdown
|
||||||
".md",
|
".md",
|
||||||
|
|
||||||
|
// SVG - using WebView2 for better compatibility with complex SVGs
|
||||||
|
// (e.g., from Adobe Illustrator, Inkscape)
|
||||||
|
".svg",
|
||||||
};
|
};
|
||||||
|
|
||||||
[ObservableProperty]
|
[ObservableProperty]
|
||||||
@@ -111,9 +115,10 @@ namespace Peek.FilePreviewer.Previewers
|
|||||||
{
|
{
|
||||||
bool isHtml = File.Extension == ".html" || File.Extension == ".htm";
|
bool isHtml = File.Extension == ".html" || File.Extension == ".htm";
|
||||||
bool isMarkdown = File.Extension == ".md";
|
bool isMarkdown = File.Extension == ".md";
|
||||||
|
bool isSvg = File.Extension == ".svg";
|
||||||
|
|
||||||
bool supportedByMonaco = MonacoHelper.SupportedMonacoFileTypes.Contains(File.Extension);
|
bool supportedByMonaco = MonacoHelper.SupportedMonacoFileTypes.Contains(File.Extension);
|
||||||
bool useMonaco = supportedByMonaco && !isHtml && !isMarkdown;
|
bool useMonaco = supportedByMonaco && !isHtml && !isMarkdown && !isSvg;
|
||||||
|
|
||||||
IsDevFilePreview = supportedByMonaco;
|
IsDevFilePreview = supportedByMonaco;
|
||||||
CustomContextMenu = useMonaco;
|
CustomContextMenu = useMonaco;
|
||||||
@@ -128,6 +133,13 @@ namespace Peek.FilePreviewer.Previewers
|
|||||||
var raw = await ReadHelper.Read(File.Path.ToString());
|
var raw = await ReadHelper.Read(File.Path.ToString());
|
||||||
Preview = new Uri(MarkdownHelper.PreviewTempFile(raw, File.Path, TempFolderPath.Path));
|
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
|
else
|
||||||
{
|
{
|
||||||
// Simple html file to preview. Shouldn't do things like enabling scripts or using a virtual mapped directory.
|
// 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