Skip to content

Commit 251f063

Browse files
committed
Move to FileThumbnailHelper
1 parent 75f4f00 commit 251f063

File tree

2 files changed

+32
-12
lines changed

2 files changed

+32
-12
lines changed

src/Files.App/Utils/Storage/Helpers/FileThumbnailHelper.cs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// Copyright (c) Files Community
22
// Licensed under the MIT License.
33

4+
using Windows.Storage;
45
using Windows.Storage.FileProperties;
56

67
namespace Files.App.Utils.Storage
@@ -17,6 +18,22 @@ public static class FileThumbnailHelper
1718
return await Win32Helper.StartSTATask(() => Win32Helper.GetIcon(path, (int)size, isFolder, iconOptions));
1819
}
1920

21+
/// <summary>
22+
/// Returns thumbnail for given file or folder using Storage API
23+
/// </summary>
24+
public static async Task<byte[]?> GetIconAsync(IStorageItem item, uint requestedSize, ThumbnailMode thumbnailMode, ThumbnailOptions thumbnailOptions)
25+
{
26+
using StorageItemThumbnail thumbnail = item switch
27+
{
28+
BaseStorageFile file => await FilesystemTasks.Wrap(() => file.GetThumbnailAsync(thumbnailMode, requestedSize, thumbnailOptions).AsTask()),
29+
BaseStorageFolder folder => await FilesystemTasks.Wrap(() => folder.GetThumbnailAsync(thumbnailMode, requestedSize, thumbnailOptions).AsTask()),
30+
_ => new(null!, FileSystemStatusCode.Generic)
31+
};
32+
if (thumbnail is not null && thumbnail.Size != 0 && thumbnail.OriginalHeight != 0 && thumbnail.OriginalWidth != 0)
33+
return await thumbnail.ToByteArrayAsync();
34+
return null;
35+
}
36+
2037
/// <summary>
2138
/// Returns overlay for given file or folder
2239
/// /// </summary>

src/Files.App/ViewModels/ShellViewModel.cs

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1095,25 +1095,28 @@ private async Task<bool> LoadThumbnailAsync(ListedItem item, IStorageItem matchi
10951095
var thumbnailSize = LayoutSizeKindHelper.GetIconSize(folderSettings.LayoutMode);
10961096
// SingleItem returns image thumbnails in the correct aspect ratio for the grid layouts
10971097
// ListView is used for the details and columns layout
1098-
var thumbnailMode = thumbnailSize < 96 ? ThumbnailMode.ListView : ThumbnailMode.SingleItem;
1099-
11001098
// We use ReturnOnlyIfCached because otherwise folders thumbnails have a black background, this has the downside the folder previews don't work
1101-
using StorageItemThumbnail thumbnail = matchingStorageItem switch
1099+
var (thumbnailMode, thumbnailOptions) = matchingStorageItem switch
11021100
{
1103-
BaseStorageFile file => await FilesystemTasks.Wrap(() => file.GetThumbnailAsync(thumbnailMode, thumbnailSize, ThumbnailOptions.ResizeThumbnail).AsTask()),
1104-
BaseStorageFolder folder => await FilesystemTasks.Wrap(() => folder.GetThumbnailAsync(ThumbnailMode.SingleItem, thumbnailSize, ThumbnailOptions.ReturnOnlyIfCached).AsTask()),
1105-
_ => new (null!, FileSystemStatusCode.Generic)
1101+
BaseStorageFolder => (ThumbnailMode.SingleItem, ThumbnailOptions.ReturnOnlyIfCached),
1102+
BaseStorageFile when thumbnailSize < 96 => (ThumbnailMode.ListView, ThumbnailOptions.ResizeThumbnail),
1103+
_ => (ThumbnailMode.SingleItem, ThumbnailOptions.ResizeThumbnail),
11061104
};
11071105

1108-
if (thumbnail is not null && thumbnail.Size != 0 && thumbnail.OriginalHeight != 0 && thumbnail.OriginalWidth != 0)
1106+
var result = await FileThumbnailHelper.GetIconAsync(
1107+
matchingStorageItem,
1108+
thumbnailSize,
1109+
thumbnailMode,
1110+
thumbnailOptions);
1111+
1112+
if (result is not null)
11091113
{
11101114
await dispatcherQueue.EnqueueOrInvokeAsync(async () =>
11111115
{
1112-
var img = new BitmapImage();
1113-
img.DecodePixelType = DecodePixelType.Logical;
1114-
img.DecodePixelWidth = (int)thumbnailSize;
1115-
await img.SetSourceAsync(thumbnail);
1116-
item.FileImage = img;
1116+
// Assign FileImage property
1117+
var image = await result.ToBitmapAsync();
1118+
if (image is not null)
1119+
item.FileImage = image;
11171120
}, Microsoft.UI.Dispatching.DispatcherQueuePriority.Normal);
11181121

11191122
return true;

0 commit comments

Comments
 (0)