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