Skip to content

Commit 2fca3f3

Browse files
committed
Code Quality: Update thumbnail helper
1 parent 4b93440 commit 2fca3f3

20 files changed

+56
-85
lines changed

src/Files.App/Data/Items/DriveItem.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -318,10 +318,9 @@ public async Task LoadThumbnailAsync()
318318
DeviceID,
319319
Constants.ShellIconSizes.Small,
320320
false,
321-
false,
322321
IconOptions.ReturnIconOnly | IconOptions.UseCurrentScale);
323322

324-
IconData ??= result.IconData;
323+
IconData ??= result;
325324
}
326325

327326
if (Root is not null)

src/Files.App/Data/Items/SidebarLibraryItem.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,10 +50,9 @@ public async Task LoadLibraryIconAsync()
5050
Path,
5151
Constants.ShellIconSizes.Small,
5252
false,
53-
false,
5453
IconOptions.ReturnIconOnly | IconOptions.UseCurrentScale);
5554

56-
var bitmapImage = await result.IconData.ToBitmapAsync();
55+
var bitmapImage = await result.ToBitmapAsync();
5756
if (bitmapImage is not null)
5857
Icon = bitmapImage;
5958
}

src/Files.App/Data/Items/WidgetDriveCardItem.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,9 @@ public async Task LoadCardThumbnailAsync()
3030
Item.Path,
3131
Constants.ShellIconSizes.Large,
3232
true,
33-
false,
3433
IconOptions.ReturnIconOnly | IconOptions.UseCurrentScale);
3534

36-
thumbnailData = result.IconData;
35+
thumbnailData = result;
3736

3837
var bitmapImage = await MainWindow.Instance.DispatcherQueue.EnqueueOrInvokeAsync(() => thumbnailData.ToBitmapAsync(), Microsoft.UI.Dispatching.DispatcherQueuePriority.Low);
3938
if (bitmapImage is not null)

src/Files.App/Data/Items/WidgetFolderCardItem.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,10 +54,9 @@ public async Task LoadCardThumbnailAsync()
5454
Path,
5555
Constants.ShellIconSizes.Large,
5656
true,
57-
false,
5857
IconOptions.ReturnIconOnly | IconOptions.UseCurrentScale);
5958

60-
_thumbnailData = result.IconData;
59+
_thumbnailData = result;
6160
if (_thumbnailData is not null)
6261
Thumbnail = await MainWindow.Instance.DispatcherQueue.EnqueueOrInvokeAsync(() => _thumbnailData.ToBitmapAsync(), Microsoft.UI.Dispatching.DispatcherQueuePriority.Low);
6362
}

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

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -921,19 +921,18 @@ private async Task LoadThumbnailAsync(ListedItem item)
921921
var returnIconOnly = UserSettingsService.FoldersSettingsService.ShowThumbnails == false || thumbnailSize < 48;
922922

923923
// Get thumbnail
924-
var icon = await FileThumbnailHelper.GetIconAsync(
924+
var result = await FileThumbnailHelper.GetIconAsync(
925925
item.ItemPath,
926926
thumbnailSize,
927927
item.IsFolder,
928-
false,
929928
returnIconOnly ? IconOptions.ReturnIconOnly : IconOptions.None);
930929

931-
if (icon.IconData is not null)
930+
if (result is not null)
932931
{
933932
await dispatcherQueue.EnqueueOrInvokeAsync(async () =>
934933
{
935934
// Assign FileImage property
936-
var image = await icon.IconData.ToBitmapAsync();
935+
var image = await result.ToBitmapAsync();
937936
if (image is not null)
938937
item.FileImage = image;
939938
}, Microsoft.UI.Dispatching.DispatcherQueuePriority.Low);
@@ -1215,15 +1214,14 @@ await SafetyExtensions.IgnoreExceptions(() =>
12151214
ImageSource? groupImage = null;
12161215
if (item.PrimaryItemAttribute != StorageItemTypes.Folder || item.IsArchive)
12171216
{
1218-
var headerIconInfo = await FileThumbnailHelper.GetIconAsync(
1217+
var result = await FileThumbnailHelper.GetIconAsync(
12191218
item.ItemPath,
12201219
Constants.ShellIconSizes.Large,
12211220
false,
1222-
false,
12231221
IconOptions.ReturnIconOnly | IconOptions.UseCurrentScale);
12241222

1225-
if (headerIconInfo.IconData is not null && !item.IsShortcut)
1226-
groupImage = await dispatcherQueue.EnqueueOrInvokeAsync(() => headerIconInfo.IconData.ToBitmapAsync(), Microsoft.UI.Dispatching.DispatcherQueuePriority.Low);
1223+
if (result is not null && !item.IsShortcut)
1224+
groupImage = await dispatcherQueue.EnqueueOrInvokeAsync(() => result.ToBitmapAsync(), Microsoft.UI.Dispatching.DispatcherQueuePriority.Low);
12271225

12281226
// The groupImage is null if loading icon from fulltrust process failed
12291227
if (!item.IsShortcut && !item.IsHiddenItem && !FtpHelpers.IsFtpPath(item.ItemPath) && groupImage is null)

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -104,10 +104,9 @@ public async Task<LocationItem> CreateLocationItemFromPathAsync(string path)
104104
res.Result.Path,
105105
Constants.ShellIconSizes.Small,
106106
true,
107-
false,
108107
IconOptions.ReturnIconOnly | IconOptions.UseCurrentScale);
109108

110-
locationItem.IconData = result.IconData;
109+
locationItem.IconData = result;
111110

112111
var bitmapImage = await MainWindow.Instance.DispatcherQueue.EnqueueOrInvokeAsync(() => locationItem.IconData.ToBitmapAsync(), Microsoft.UI.Dispatching.DispatcherQueuePriority.Low);
113112
if (bitmapImage is not null)

src/Files.App/Helpers/Navigation/NavigationHelpers.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -192,11 +192,10 @@ private static async Task UpdateTabInfoAsync(TabBarItem tabItem, object navigati
192192
currentPath,
193193
Constants.ShellIconSizes.Small,
194194
true,
195-
false,
196195
IconOptions.ReturnIconOnly | IconOptions.UseCurrentScale);
197196

198-
if (result.IconData is not null)
199-
iconSource.ImageSource = await result.IconData.ToBitmapAsync();
197+
if (result is not null)
198+
iconSource.ImageSource = await result.ToBitmapAsync();
200199
}
201200

202201
return (tabLocationHeader, iconSource, toolTipText);

src/Files.App/Utils/Cloud/CloudDrivesManager.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,10 +71,9 @@ public static async Task UpdateDrivesAsync()
7171
provider.SyncFolder,
7272
Constants.ShellIconSizes.Small,
7373
false,
74-
false,
7574
IconOptions.ReturnIconOnly | IconOptions.UseCurrentScale);
7675

77-
iconData = result.IconData;
76+
iconData = result;
7877
}
7978

8079
if (iconData is not null)

src/Files.App/Utils/RecentItem/RecentItem.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,10 +68,9 @@ public async Task LoadRecentItemIconAsync()
6868
RecentPath,
6969
Constants.ShellIconSizes.Small,
7070
false,
71-
false,
7271
IconOptions.UseCurrentScale);
7372

74-
var bitmapImage = await result.IconData.ToBitmapAsync();
73+
var bitmapImage = await result.ToBitmapAsync();
7574
if (bitmapImage is not null)
7675
FileImg = bitmapImage;
7776
}

src/Files.App/Utils/Shell/Win32API.cs

Lines changed: 15 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -276,26 +276,20 @@ public static string ExtractStringFromDLL(string file, int number)
276276
private static readonly object _iconLock = new object();
277277

278278
/// <summary>
279-
/// Returns an icon when a thumbnail isn't available or if getIconOnly is true.
280-
/// <br/>
281-
/// Returns an icon overlay when getOverlay is true.
282-
/// <br/>
283-
/// Returns a boolean indicating if the icon/thumbnail is cached.
279+
/// Returns an icon if returnIconOnly is true, otherwise a thumbnail will be returned if available.
284280
/// </summary>
285281
/// <param name="path"></param>
286-
/// <param name="thumbnailSize"></param>
282+
/// <param name="size"></param>
287283
/// <param name="isFolder"></param>
288-
/// <param name="getIconOnly"></param>
284+
/// <param name="returnIconOnly"></param>
289285
/// <returns></returns>
290-
public static (byte[]? icon, bool isIconCached) GetIcon(
286+
public static byte[]? GetIcon(
291287
string path,
292-
int thumbnailSize,
288+
int size,
293289
bool isFolder,
294-
bool getThumbnailOnly,
295-
bool getIconOnly)
290+
bool returnIconOnly)
296291
{
297292
byte[]? iconData = null;
298-
bool isIconCached = false;
299293

300294
try
301295
{
@@ -307,26 +301,22 @@ public static (byte[]? icon, bool isIconCached) GetIcon(
307301
{
308302
var flags = Shell32.SIIGBF.SIIGBF_BIGGERSIZEOK;
309303

310-
if (getIconOnly)
304+
if (returnIconOnly)
311305
flags |= Shell32.SIIGBF.SIIGBF_ICONONLY;
312-
else if (getThumbnailOnly)
313-
flags |= Shell32.SIIGBF.SIIGBF_THUMBNAILONLY;
314306

315-
var hres = shellFactory.GetImage(new SIZE(thumbnailSize, thumbnailSize), flags, out var hbitmap);
307+
var hres = shellFactory.GetImage(new SIZE(size, size), flags, out var hbitmap);
316308
if (hres == HRESULT.S_OK)
317309
{
318310
using var image = GetBitmapFromHBitmap(hbitmap);
319311
if (image is not null)
320312
iconData = (byte[]?)new ImageConverter().ConvertTo(image, typeof(byte[]));
321-
322-
isIconCached = true;
323313
}
324314

325315
Marshal.ReleaseComObject(shellFactory);
326316
}
327317

328318
if (iconData is not null)
329-
return (iconData, isIconCached);
319+
return iconData;
330320
else
331321
{
332322
var shfi = new Shell32.SHFILEINFO();
@@ -337,11 +327,11 @@ public static (byte[]? icon, bool isIconCached) GetIcon(
337327

338328
var ret = Shell32.SHGetFileInfo(path, isFolder ? FileAttributes.Directory : 0, ref shfi, Shell32.SHFILEINFO.Size, flags);
339329
if (ret == IntPtr.Zero)
340-
return (iconData, isIconCached);
330+
return iconData;
341331

342332
User32.DestroyIcon(shfi.hIcon);
343333

344-
var imageListSize = thumbnailSize switch
334+
var imageListSize = size switch
345335
{
346336
<= 16 => Shell32.SHIL.SHIL_SMALL,
347337
<= 32 => Shell32.SHIL.SHIL_LARGE,
@@ -352,7 +342,7 @@ public static (byte[]? icon, bool isIconCached) GetIcon(
352342
lock (_iconLock)
353343
{
354344
if (!Shell32.SHGetImageList(imageListSize, typeof(ComCtl32.IImageList).GUID, out var imageListOut).Succeeded)
355-
return (iconData, isIconCached);
345+
return iconData;
356346

357347
var imageList = (ComCtl32.IImageList)imageListOut;
358348

@@ -375,14 +365,14 @@ public static (byte[]? icon, bool isIconCached) GetIcon(
375365
else if (isFolder)
376366
{
377367
// Could not icon, load generic icon
378-
var icons = ExtractSelectedIconsFromDLL(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.System), "imageres.dll"), new[] { 2 }, thumbnailSize);
368+
var icons = ExtractSelectedIconsFromDLL(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.System), "imageres.dll"), new[] { 2 }, size);
379369
var generic = icons.SingleOrDefault(x => x.Index == 2);
380370
iconData = generic?.IconData;
381371
}
382372
else
383373
{
384374
// Could not icon, load generic icon
385-
var icons = ExtractSelectedIconsFromDLL(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.System), "shell32.dll"), new[] { 1 }, thumbnailSize);
375+
var icons = ExtractSelectedIconsFromDLL(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.System), "shell32.dll"), new[] { 1 }, size);
386376
var generic = icons.SingleOrDefault(x => x.Index == 1);
387377
iconData = generic?.IconData;
388378
}
@@ -391,7 +381,7 @@ public static (byte[]? icon, bool isIconCached) GetIcon(
391381
Marshal.ReleaseComObject(imageList);
392382
}
393383

394-
return (iconData, isIconCached);
384+
return iconData;
395385
}
396386
}
397387
finally

0 commit comments

Comments
 (0)