mirror of
https://github.com/microsoft/PowerToys.git
synced 2026-04-03 09:46:54 +02:00
[Peek]Clear thumbnails (#26440)
This commit is contained in:
@@ -11,6 +11,7 @@ using CommunityToolkit.Mvvm.ComponentModel;
|
||||
using Microsoft.UI.Dispatching;
|
||||
using Microsoft.UI.Xaml.Media;
|
||||
using Microsoft.UI.Xaml.Media.Imaging;
|
||||
using Peek.Common;
|
||||
using Peek.Common.Extensions;
|
||||
using Peek.Common.Helpers;
|
||||
using Peek.Common.Models;
|
||||
@@ -59,6 +60,10 @@ namespace Peek.FilePreviewer.Previewers
|
||||
|
||||
private bool IsFullImageLoaded => FullQualityImageTask?.Status == TaskStatus.RanToCompletion;
|
||||
|
||||
private IntPtr lowQualityThumbnail;
|
||||
|
||||
private IntPtr highQualityThumbnail;
|
||||
|
||||
public static bool IsFileTypeSupported(string fileExt)
|
||||
{
|
||||
return _supportedFileTypes.Contains(fileExt);
|
||||
@@ -66,6 +71,7 @@ namespace Peek.FilePreviewer.Previewers
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
Clear();
|
||||
GC.SuppressFinalize(this);
|
||||
}
|
||||
|
||||
@@ -94,6 +100,7 @@ namespace Peek.FilePreviewer.Previewers
|
||||
|
||||
public async Task LoadPreviewAsync(CancellationToken cancellationToken)
|
||||
{
|
||||
Clear();
|
||||
State = PreviewState.Loading;
|
||||
|
||||
LowQualityThumbnailTask = LoadLowQualityThumbnailAsync(cancellationToken);
|
||||
@@ -157,11 +164,11 @@ namespace Peek.FilePreviewer.Previewers
|
||||
{
|
||||
cancellationToken.ThrowIfCancellationRequested();
|
||||
|
||||
var hr = ThumbnailHelper.GetThumbnail(Item.Path, out IntPtr hbitmap, ThumbnailHelper.LowQualityThumbnailSize);
|
||||
var hr = ThumbnailHelper.GetThumbnail(Item.Path, out lowQualityThumbnail, ThumbnailHelper.LowQualityThumbnailSize);
|
||||
if (hr != HResult.Ok)
|
||||
{
|
||||
Logger.LogError("Error loading low quality thumbnail - hresult: " + hr);
|
||||
throw new ImageLoadingException(nameof(hbitmap));
|
||||
throw new ImageLoadingException(nameof(lowQualityThumbnail));
|
||||
}
|
||||
|
||||
cancellationToken.ThrowIfCancellationRequested();
|
||||
@@ -169,9 +176,9 @@ namespace Peek.FilePreviewer.Previewers
|
||||
await Dispatcher.RunOnUiThread(async () =>
|
||||
{
|
||||
cancellationToken.ThrowIfCancellationRequested();
|
||||
var thumbnailBitmap = await BitmapHelper.GetBitmapFromHBitmapAsync(hbitmap, IsPng(Item), cancellationToken);
|
||||
if (!IsFullImageLoaded && !IsHighQualityThumbnailLoaded)
|
||||
{
|
||||
var thumbnailBitmap = await BitmapHelper.GetBitmapFromHBitmapAsync(lowQualityThumbnail, IsPng(Item), cancellationToken);
|
||||
Preview = thumbnailBitmap;
|
||||
}
|
||||
});
|
||||
@@ -184,11 +191,11 @@ namespace Peek.FilePreviewer.Previewers
|
||||
{
|
||||
cancellationToken.ThrowIfCancellationRequested();
|
||||
|
||||
var hr = ThumbnailHelper.GetThumbnail(Item.Path, out IntPtr hbitmap, ThumbnailHelper.HighQualityThumbnailSize);
|
||||
var hr = ThumbnailHelper.GetThumbnail(Item.Path, out highQualityThumbnail, ThumbnailHelper.HighQualityThumbnailSize);
|
||||
if (hr != HResult.Ok)
|
||||
{
|
||||
Logger.LogError("Error loading high quality thumbnail - hresult: " + hr);
|
||||
throw new ImageLoadingException(nameof(hbitmap));
|
||||
throw new ImageLoadingException(nameof(highQualityThumbnail));
|
||||
}
|
||||
|
||||
cancellationToken.ThrowIfCancellationRequested();
|
||||
@@ -196,9 +203,9 @@ namespace Peek.FilePreviewer.Previewers
|
||||
await Dispatcher.RunOnUiThread(async () =>
|
||||
{
|
||||
cancellationToken.ThrowIfCancellationRequested();
|
||||
var thumbnailBitmap = await BitmapHelper.GetBitmapFromHBitmapAsync(hbitmap, IsPng(Item), cancellationToken);
|
||||
if (!IsFullImageLoaded)
|
||||
{
|
||||
var thumbnailBitmap = await BitmapHelper.GetBitmapFromHBitmapAsync(highQualityThumbnail, IsPng(Item), cancellationToken);
|
||||
Preview = thumbnailBitmap;
|
||||
}
|
||||
});
|
||||
@@ -211,12 +218,12 @@ namespace Peek.FilePreviewer.Previewers
|
||||
{
|
||||
cancellationToken.ThrowIfCancellationRequested();
|
||||
|
||||
using FileStream stream = ReadHelper.OpenReadOnly(Item.Path);
|
||||
|
||||
await Dispatcher.RunOnUiThread(async () =>
|
||||
{
|
||||
cancellationToken.ThrowIfCancellationRequested();
|
||||
|
||||
using FileStream stream = ReadHelper.OpenReadOnly(Item.Path);
|
||||
|
||||
if (IsSvg(Item))
|
||||
{
|
||||
var source = new SvgImageSource();
|
||||
@@ -261,6 +268,21 @@ namespace Peek.FilePreviewer.Previewers
|
||||
return item.Extension == ".svg";
|
||||
}
|
||||
|
||||
private void Clear()
|
||||
{
|
||||
Preview = null;
|
||||
|
||||
if (lowQualityThumbnail != IntPtr.Zero)
|
||||
{
|
||||
NativeMethods.DeleteObject(lowQualityThumbnail);
|
||||
}
|
||||
|
||||
if (highQualityThumbnail != IntPtr.Zero)
|
||||
{
|
||||
NativeMethods.DeleteObject(highQualityThumbnail);
|
||||
}
|
||||
}
|
||||
|
||||
private static readonly HashSet<string> _supportedFileTypes = new HashSet<string>
|
||||
{
|
||||
// Image types
|
||||
|
||||
Reference in New Issue
Block a user