Skip to content

Commit 0804de9

Browse files
committed
Pass storage item for preview and properties
1 parent 75589dd commit 0804de9

File tree

10 files changed

+67
-24
lines changed

10 files changed

+67
-24
lines changed

src/Files.App/Data/Enums/IconOptions.cs

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,19 +19,34 @@ public enum IconOptions
1919
/// </summary>
2020
UseCurrentScale = 1,
2121

22+
/// <summary>
23+
/// Scale the thumbnail to the requested size.
24+
/// </summary>
25+
ResizeThumbnail = 2,
26+
2227
/// <summary>
2328
/// Retrieve only the file icon, even a thumbnail is available. This has the best performance.
2429
/// </summary>
25-
ReturnIconOnly = 2,
30+
ReturnIconOnly = 4,
2631

2732
/// <summary>
2833
/// Retrieve only the thumbnail.
2934
/// </summary>
30-
ReturnThumbnailOnly = 4,
35+
ReturnThumbnailOnly = 8,
3136

3237
/// <summary>
3338
/// Retrieve a thumbnail only if it is cached or embedded in the file.
3439
/// </summary>
35-
ReturnOnlyIfCached = 8,
40+
ReturnOnlyIfCached = 16,
41+
42+
/// <summary>
43+
/// Default. Retrieve a thumbnail to display a preview of any single item (like a file, folder, or file group).
44+
/// </summary>
45+
SingleItem = 32,
46+
47+
/// <summary>
48+
/// Retrieve a thumbnail to display previews of files (or other items) in a list.
49+
/// </summary>
50+
ListView = 64,
3651
}
3752
}

src/Files.App/Data/Models/PinnedFoldersManager.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,7 @@ public async Task<LocationItem> CreateLocationItemFromPathAsync(string path)
108108
{
109109
var result = await FileThumbnailHelper.GetIconAsync(
110110
res.Result.Path,
111+
res.Result,
111112
Constants.ShellIconSizes.Small,
112113
true,
113114
IconOptions.ReturnIconOnly | IconOptions.UseCurrentScale);

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

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,19 +8,40 @@ namespace Files.App.Utils.Storage
88
{
99
public static class FileThumbnailHelper
1010
{
11+
public static async Task<byte[]?> GetIconAsync(string? path, IStorageItem? item, uint requestedSize, bool isFolder, IconOptions iconOptions)
12+
{
13+
byte[]? result = null;
14+
if (path is not null)
15+
result ??= await GetIconAsync(path, requestedSize, isFolder, iconOptions);
16+
if (item is not null)
17+
result ??= await GetIconAsync(item, requestedSize, iconOptions);
18+
return result;
19+
}
20+
1121
/// <summary>
1222
/// Returns icon or thumbnail for given file or folder
1323
/// </summary>
14-
public static async Task<byte[]?> GetIconAsync(string path, uint requestedSize, bool isFolder, IconOptions iconOptions)
24+
public static Task<byte[]?> GetIconAsync(string path, uint requestedSize, bool isFolder, IconOptions iconOptions)
1525
{
1626
var size = iconOptions.HasFlag(IconOptions.UseCurrentScale) ? requestedSize * App.AppModel.AppWindowDPI : requestedSize;
1727

18-
return await Win32Helper.StartSTATask(() => Win32Helper.GetIcon(path, (int)size, isFolder, iconOptions));
28+
return Win32Helper.StartSTATask(() => Win32Helper.GetIcon(path, (int)size, isFolder, iconOptions));
1929
}
2030

2131
/// <summary>
2232
/// Returns thumbnail for given file or folder using Storage API
2333
/// </summary>
34+
public static Task<byte[]?> GetIconAsync(IStorageItem item, uint requestedSize, IconOptions iconOptions)
35+
{
36+
var thumbnailOptions = (iconOptions.HasFlag(IconOptions.UseCurrentScale) ? ThumbnailOptions.UseCurrentScale : 0) |
37+
(iconOptions.HasFlag(IconOptions.ReturnOnlyIfCached) ? ThumbnailOptions.ReturnOnlyIfCached : 0) |
38+
(iconOptions.HasFlag(IconOptions.ResizeThumbnail) ? ThumbnailOptions.ResizeThumbnail : 0);
39+
40+
var thumbnailMode = iconOptions.HasFlag(IconOptions.ListView) ? ThumbnailMode.ListView : ThumbnailMode.SingleItem;
41+
42+
return GetIconAsync(item, requestedSize, thumbnailMode, thumbnailOptions);
43+
}
44+
2445
public static async Task<byte[]?> GetIconAsync(IStorageItem item, uint requestedSize, ThumbnailMode thumbnailMode, ThumbnailOptions thumbnailOptions)
2546
{
2647
using StorageItemThumbnail thumbnail = item switch
@@ -40,14 +61,13 @@ public static class FileThumbnailHelper
4061
/// <param name="path"></param>
4162
/// <param name="isFolder"></param>
4263
/// <returns></returns>
43-
public static async Task<byte[]?> GetIconOverlayAsync(string path, bool isFolder)
44-
=> await Win32Helper.StartSTATask(() => Win32Helper.GetIconOverlay(path, isFolder));
64+
public static Task<byte[]?> GetIconOverlayAsync(string path, bool isFolder)
65+
=> Win32Helper.StartSTATask(() => Win32Helper.GetIconOverlay(path, isFolder));
4566

4667
[Obsolete]
47-
public static async Task<byte[]?> LoadIconFromPathAsync(string filePath, uint thumbnailSize, ThumbnailMode thumbnailMode, ThumbnailOptions thumbnailOptions, bool isFolder = false)
68+
public static Task<byte[]?> LoadIconFromPathAsync(string filePath, uint thumbnailSize, ThumbnailMode thumbnailMode, ThumbnailOptions thumbnailOptions, bool isFolder = false)
4869
{
49-
var result = await GetIconAsync(filePath, thumbnailSize, isFolder, IconOptions.None);
50-
return result;
70+
return GetIconAsync(filePath, thumbnailSize, isFolder, IconOptions.None);
5171
}
5272
}
5373
}

src/Files.App/Utils/Storage/Search/FolderSearch.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -512,6 +512,7 @@ private async Task<ListedItem> GetListedItemAsync(IStorageItem item)
512512
{
513513
var iconResult = await FileThumbnailHelper.GetIconAsync(
514514
item.Path,
515+
item,
515516
Constants.ShellIconSizes.Small,
516517
item.IsOfType(StorageItemTypes.Folder),
517518
IconOptions.ReturnIconOnly | IconOptions.UseCurrentScale);

src/Files.App/ViewModels/Properties/Items/DriveProperties.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ public async override Task GetSpecialPropertiesAsync()
5252
{
5353
var result = await FileThumbnailHelper.GetIconAsync(
5454
Drive.Path,
55+
diskRoot,
5556
Constants.ShellIconSizes.ExtraLarge,
5657
true,
5758
IconOptions.ReturnIconOnly | IconOptions.UseCurrentScale);

src/Files.App/ViewModels/Properties/Items/FileProperties.cs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -108,8 +108,13 @@ public override async Task GetSpecialPropertiesAsync()
108108
ViewModel.ItemSizeOnDisk = Win32Helper.GetFileSizeOnDisk(Item.ItemPath)?.ToLongSizeString() ??
109109
string.Empty;
110110

111+
string filePath = (Item as ShortcutItem)?.TargetPath ?? Item.ItemPath;
112+
BaseStorageFile? file = !string.IsNullOrWhiteSpace(filePath) ?
113+
await AppInstance.ShellViewModel.GetFileFromPathAsync(filePath) : null!;
114+
111115
var result = await FileThumbnailHelper.GetIconAsync(
112116
Item.ItemPath,
117+
file,
113118
Constants.ShellIconSizes.ExtraLarge,
114119
false,
115120
IconOptions.UseCurrentScale);
@@ -132,9 +137,6 @@ public override async Task GetSpecialPropertiesAsync()
132137
}
133138
}
134139

135-
string filePath = (Item as ShortcutItem)?.TargetPath ?? Item.ItemPath;
136-
BaseStorageFile file = await AppInstance.ShellViewModel.GetFileFromPathAsync(filePath);
137-
138140
// Couldn't access the file and can't load any other properties
139141
if (file is null)
140142
return;
@@ -152,7 +154,7 @@ public override async Task GetSpecialPropertiesAsync()
152154
ViewModel.UncompressedItemSizeBytes = uncompressedSize;
153155
}
154156

155-
if (file.Properties is not null)
157+
if (file?.Properties is not null)
156158
GetOtherPropertiesAsync(file.Properties);
157159
}
158160

src/Files.App/ViewModels/Properties/Items/FolderProperties.cs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -79,8 +79,13 @@ public async override Task GetSpecialPropertiesAsync()
7979
ViewModel.CanCompressContent = Win32Helper.CanCompressContent(Item.ItemPath);
8080
ViewModel.IsContentCompressed = Win32Helper.HasFileAttribute(Item.ItemPath, System.IO.FileAttributes.Compressed);
8181

82+
string folderPath = (Item as ShortcutItem)?.TargetPath ?? Item.ItemPath;
83+
BaseStorageFolder? storageFolder = !string.IsNullOrWhiteSpace(folderPath) ?
84+
await AppInstance.ShellViewModel.GetFolderFromPathAsync(folderPath) : null!;
85+
8286
var result = await FileThumbnailHelper.GetIconAsync(
8387
Item.ItemPath,
88+
storageFolder,
8489
Constants.ShellIconSizes.ExtraLarge,
8590
true,
8691
IconOptions.UseCurrentScale);
@@ -111,9 +116,6 @@ public async override Task GetSpecialPropertiesAsync()
111116
}
112117
}
113118

114-
string folderPath = (Item as ShortcutItem)?.TargetPath ?? Item.ItemPath;
115-
BaseStorageFolder storageFolder = await AppInstance.ShellViewModel.GetFolderFromPathAsync(folderPath);
116-
117119
if (storageFolder is not null)
118120
{
119121
ViewModel.ItemCreatedTimestampReal = storageFolder.DateCreated;

src/Files.App/ViewModels/ShellViewModel.cs

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1096,18 +1096,17 @@ private async Task<bool> LoadThumbnailAsync(ListedItem item, IStorageItem matchi
10961096
// SingleItem returns image thumbnails in the correct aspect ratio for the grid layouts
10971097
// ListView is used for the details and columns layout
10981098
// We use ReturnOnlyIfCached because otherwise folders thumbnails have a black background, this has the downside the folder previews don't work
1099-
var (thumbnailMode, thumbnailOptions) = matchingStorageItem switch
1099+
var iconOptions = matchingStorageItem switch
11001100
{
1101-
BaseStorageFolder => (ThumbnailMode.SingleItem, ThumbnailOptions.ReturnOnlyIfCached),
1102-
BaseStorageFile when thumbnailSize < 96 => (ThumbnailMode.ListView, ThumbnailOptions.ResizeThumbnail),
1103-
_ => (ThumbnailMode.SingleItem, ThumbnailOptions.ResizeThumbnail),
1101+
BaseStorageFolder => IconOptions.SingleItem | IconOptions.ReturnOnlyIfCached,
1102+
BaseStorageFile when thumbnailSize < 96 => IconOptions.ListView | IconOptions.ResizeThumbnail,
1103+
_ => IconOptions.SingleItem | IconOptions.ResizeThumbnail,
11041104
};
11051105

11061106
var result = await FileThumbnailHelper.GetIconAsync(
11071107
matchingStorageItem,
1108-
thumbnailSize,
1109-
thumbnailMode,
1110-
thumbnailOptions);
1108+
thumbnailSize,
1109+
iconOptions);
11111110

11121111
if (result is not null)
11131112
{

src/Files.App/ViewModels/UserControls/Previews/BasePreviewModel.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ public async virtual Task<List<FileProperty>> LoadPreviewAndDetailsAsync()
8686
{
8787
var result = await FileThumbnailHelper.GetIconAsync(
8888
Item.ItemPath,
89+
Item.ItemFile,
8990
Constants.ShellIconSizes.Jumbo,
9091
false,
9192
IconOptions.None);

src/Files.App/ViewModels/UserControls/Previews/FolderPreviewViewModel.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ private async Task LoadPreviewAndDetailsAsync()
2929

3030
var result = await FileThumbnailHelper.GetIconAsync(
3131
Item.ItemPath,
32+
Folder,
3233
Constants.ShellIconSizes.Jumbo,
3334
true,
3435
IconOptions.None);

0 commit comments

Comments
 (0)