Skip to content

Commit 4f4cd89

Browse files
yaira2hishitetsu
andauthored
Code Quality: Remove thumbnail cache to simplify logic and improve performance (#14871)
Co-authored-by: hishitetsu <66369541+hishitetsu@users.noreply.github.com>
1 parent 6090141 commit 4f4cd89

File tree

2 files changed

+38
-150
lines changed

2 files changed

+38
-150
lines changed

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

Lines changed: 37 additions & 130 deletions
Original file line numberDiff line numberDiff line change
@@ -900,29 +900,6 @@ public void UpdateGroupOptions()
900900
FilesAndFolders.GetExtendedGroupHeaderInfo = groupInfoSelector.Item2;
901901
}
902902

903-
public Dictionary<string, BitmapImage> DefaultIcons = new();
904-
905-
private uint currentDefaultIconSize = 0;
906-
907-
public async Task GetDefaultItemIconsAsync(uint size)
908-
{
909-
if (currentDefaultIconSize == size)
910-
return;
911-
912-
DefaultIcons.Clear();
913-
914-
// TODO: Add more than just the folder icon
915-
using StorageItemThumbnail icon = await FilesystemTasks.Wrap(() => StorageItemIconHelpers.GetIconForItemType(size, IconPersistenceOptions.Persist));
916-
if (icon is not null)
917-
{
918-
var img = new BitmapImage();
919-
await img.SetSourceAsync(icon);
920-
DefaultIcons.Add(string.Empty, img);
921-
}
922-
923-
currentDefaultIconSize = size;
924-
}
925-
926903
private bool isLoadingItems = false;
927904
public bool IsLoadingItems
928905
{
@@ -937,117 +914,40 @@ private async Task<BitmapImage> GetShieldIcon()
937914
return shieldIcon;
938915
}
939916

940-
private async Task LoadItemThumbnailAsync(ListedItem item)
917+
private async Task LoadThumbnailAsync(ListedItem item)
941918
{
919+
// Cancel if thumbnails aren't enabled
942920
var thumbnailSize = folderSettings.GetRoundedIconSize();
921+
var returnIconOnly = UserSettingsService.FoldersSettingsService.ShowThumbnails == false || thumbnailSize < 48;
943922

944-
if (item.IsLibrary || item.PrimaryItemAttribute == StorageItemTypes.File || item.IsArchive)
945-
{
946-
var getIconOnly = UserSettingsService.FoldersSettingsService.ShowThumbnails == false || thumbnailSize < 48;
947-
var getThumbnailOnly = !item.IsExecutable && !getIconOnly;
948-
var iconInfo = await FileThumbnailHelper.GetIconAsync(
923+
// Get thumbnail
924+
var icon = await FileThumbnailHelper.GetIconAsync(
949925
item.ItemPath,
950926
thumbnailSize,
927+
item.IsFolder,
951928
false,
952-
getThumbnailOnly,
953-
getIconOnly ? IconOptions.ReturnIconOnly : IconOptions.None);
954-
955-
if (!iconInfo.isIconCached)
956-
{
957-
// Assign a placeholder icon while trying to get a cached thumbnail
958-
if (iconInfo.IconData is not null)
959-
{
960-
await dispatcherQueue.EnqueueOrInvokeAsync(async () =>
961-
{
962-
item.FileImage = await iconInfo.IconData.ToBitmapAsync();
963-
}, Microsoft.UI.Dispatching.DispatcherQueuePriority.Low);
964-
}
965-
966-
// Loop until cached thumbnail is loaded or timeout is reached
967-
var cancellationTokenSource = new CancellationTokenSource(3000);
968-
while (!iconInfo.isIconCached)
969-
{
970-
iconInfo = await FileThumbnailHelper.GetIconAsync(
971-
item.ItemPath,
972-
thumbnailSize,
973-
false,
974-
getThumbnailOnly,
975-
getIconOnly ? IconOptions.ReturnIconOnly : IconOptions.None);
976-
977-
if (cancellationTokenSource.Token.IsCancellationRequested)
978-
break;
979-
980-
await Task.Delay(500);
981-
}
982-
}
929+
returnIconOnly ? IconOptions.ReturnIconOnly : IconOptions.None);
983930

984-
if (iconInfo.IconData is not null)
985-
{
986-
await dispatcherQueue.EnqueueOrInvokeAsync(async () =>
987-
{
988-
// Assign the thumbnail/icon to the listed item
989-
item.FileImage = await iconInfo.IconData.ToBitmapAsync();
990-
991-
// Add the file icon to the DefaultIcons list
992-
if
993-
(
994-
!DefaultIcons.ContainsKey(item.FileExtension.ToLowerInvariant()) &&
995-
!string.IsNullOrEmpty(item.FileExtension) &&
996-
!item.IsShortcut &&
997-
!item.IsExecutable
998-
)
999-
{
1000-
var fileIcon = await FileThumbnailHelper.GetIconAsync(
1001-
item.ItemPath,
1002-
thumbnailSize,
1003-
false,
1004-
false,
1005-
IconOptions.ReturnIconOnly);
1006-
1007-
var bitmapImage = await fileIcon.IconData.ToBitmapAsync();
1008-
DefaultIcons.TryAdd(item.FileExtension.ToLowerInvariant(), bitmapImage);
1009-
}
1010-
1011-
}, Microsoft.UI.Dispatching.DispatcherQueuePriority.Low);
1012-
}
1013-
1014-
var iconOverlay = await FileThumbnailHelper.GetIconOverlayAsync(item.ItemPath, false);
1015-
if (iconOverlay is not null)
1016-
{
1017-
// Assign the icon overlay to the listed item
1018-
await dispatcherQueue.EnqueueOrInvokeAsync(async () =>
1019-
{
1020-
item.IconOverlay = await iconOverlay.ToBitmapAsync();
1021-
item.ShieldIcon = await GetShieldIcon();
1022-
}, Microsoft.UI.Dispatching.DispatcherQueuePriority.Low);
1023-
}
1024-
}
1025-
else
931+
if (icon.IconData is not null)
1026932
{
1027-
var getIconOnly = UserSettingsService.FoldersSettingsService.ShowThumbnails == false || thumbnailSize < 48;
1028-
var iconInfo = await FileThumbnailHelper.GetIconAsync(
1029-
item.ItemPath,
1030-
thumbnailSize,
1031-
true,
1032-
false, getIconOnly ? IconOptions.ReturnIconOnly : IconOptions.None);
1033-
1034-
if (iconInfo.IconData is not null)
933+
await dispatcherQueue.EnqueueOrInvokeAsync(async () =>
1035934
{
1036-
await dispatcherQueue.EnqueueOrInvokeAsync(async () =>
1037-
{
1038-
item.FileImage = await iconInfo.IconData.ToBitmapAsync();
1039-
}, Microsoft.UI.Dispatching.DispatcherQueuePriority.Low);
1040-
}
935+
// Assign FileImage property
936+
var image = await icon.IconData.ToBitmapAsync();
937+
if (image is not null)
938+
item.FileImage = image;
939+
}, Microsoft.UI.Dispatching.DispatcherQueuePriority.Low);
940+
}
1041941

1042-
var iconOverlay = await FileThumbnailHelper.GetIconOverlayAsync(item.ItemPath, true);
1043-
if (iconOverlay is not null)
942+
// Get icon overlay
943+
var iconOverlay = await FileThumbnailHelper.GetIconOverlayAsync(item.ItemPath, true);
944+
if (iconOverlay is not null)
945+
{
946+
await dispatcherQueue.EnqueueOrInvokeAsync(async () =>
1044947
{
1045-
await dispatcherQueue.EnqueueOrInvokeAsync(async () =>
1046-
{
1047-
item.IconOverlay = await iconOverlay.ToBitmapAsync();
1048-
item.ShieldIcon = await GetShieldIcon();
1049-
}, Microsoft.UI.Dispatching.DispatcherQueuePriority.Low);
1050-
}
948+
item.IconOverlay = await iconOverlay.ToBitmapAsync();
949+
item.ShieldIcon = await GetShieldIcon();
950+
}, Microsoft.UI.Dispatching.DispatcherQueuePriority.Low);
1051951
}
1052952
}
1053953

@@ -1092,7 +992,7 @@ await Task.Run(async () =>
1092992
}
1093993

1094994
cts.Token.ThrowIfCancellationRequested();
1095-
_ = LoadItemThumbnailAsync(item);
995+
_ = LoadThumbnailAsync(item);
1096996

1097997
if (item.IsLibrary || item.PrimaryItemAttribute == StorageItemTypes.File || item.IsArchive)
1098998
{
@@ -1198,6 +1098,17 @@ await dispatcherQueue.EnqueueOrInvokeAsync(() =>
11981098
SetFileTag(item);
11991099
});
12001100
}
1101+
else
1102+
{
1103+
// Try loading thumbnail for cloud files in case they weren't cached the first time
1104+
if (item.SyncStatusUI.SyncStatus != CloudDriveSyncStatus.NotSynced && item.SyncStatusUI.SyncStatus != CloudDriveSyncStatus.Unknown)
1105+
{
1106+
_ = Task.Run(async () => {
1107+
await Task.Delay(500);
1108+
await LoadThumbnailAsync(item);
1109+
});
1110+
}
1111+
}
12011112

12021113
if (loadGroupHeaderInfo)
12031114
{
@@ -1473,8 +1384,6 @@ private async Task RapidAddItemsToCollectionAsync(string? path, LibraryItem? lib
14731384
break;
14741385
}
14751386

1476-
await GetDefaultItemIconsAsync(folderSettings.GetRoundedIconSize());
1477-
14781387
if (IsLoadingCancelled)
14791388
{
14801389
IsLoadingCancelled = false;
@@ -1693,7 +1602,7 @@ await Task.Run(async () =>
16931602
filesAndFolders.AddRange(intermediateList);
16941603
await OrderFilesAndFoldersAsync();
16951604
await ApplyFilesAndFoldersChangesAsync();
1696-
}, defaultIconPairs: DefaultIcons);
1605+
});
16971606

16981607
filesAndFolders.AddRange(fileList);
16991608

@@ -1741,8 +1650,7 @@ await Task.Run(async () =>
17411650

17421651
await OrderFilesAndFoldersAsync();
17431652
await ApplyFilesAndFoldersChangesAsync();
1744-
},
1745-
defaultIconPairs: DefaultIcons);
1653+
});
17461654

17471655
filesAndFolders.AddRange(finalList);
17481656

@@ -2481,7 +2389,6 @@ public void Dispose()
24812389
fileTagsSettingsService.OnSettingImportedEvent -= FileTagsSettingsService_OnSettingUpdated;
24822390
fileTagsSettingsService.OnTagsUpdated -= FileTagsSettingsService_OnSettingUpdated;
24832391
folderSizeProvider.SizeChanged -= FolderSizeProvider_SizeChanged;
2484-
DefaultIcons.Clear();
24852392
}
24862393
}
24872394

src/Files.App/Utils/Storage/Enumerators/Win32StorageEnumerator.cs

Lines changed: 1 addition & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,7 @@ public static async Task<List<ListedItem>> ListEntries(
2626
NativeFindStorageItemHelper.WIN32_FIND_DATA findData,
2727
CancellationToken cancellationToken,
2828
int countLimit,
29-
Func<List<ListedItem>, Task> intermediateAction,
30-
Dictionary<string, BitmapImage> defaultIconPairs = null
29+
Func<List<ListedItem>, Task> intermediateAction
3130
)
3231
{
3332
var sampler = new IntervalSampler(500);
@@ -54,18 +53,6 @@ public static async Task<List<ListedItem>> ListEntries(
5453
var file = await GetFile(findData, path, isGitRepo, cancellationToken);
5554
if (file is not null)
5655
{
57-
if (defaultIconPairs is not null)
58-
{
59-
if (!string.IsNullOrEmpty(file.FileExtension))
60-
{
61-
var lowercaseExtension = file.FileExtension.ToLowerInvariant();
62-
if (defaultIconPairs.ContainsKey(lowercaseExtension))
63-
{
64-
file.FileImage = defaultIconPairs[lowercaseExtension];
65-
}
66-
}
67-
}
68-
6956
tempList.Add(file);
7057
++count;
7158

@@ -82,12 +69,6 @@ public static async Task<List<ListedItem>> ListEntries(
8269
var folder = await GetFolder(findData, path, isGitRepo, cancellationToken);
8370
if (folder is not null)
8471
{
85-
if (defaultIconPairs?.ContainsKey(string.Empty) ?? false)
86-
{
87-
// Set folder icon (found by empty extension string)
88-
folder.FileImage = defaultIconPairs[string.Empty];
89-
}
90-
9172
tempList.Add(folder);
9273
++count;
9374

0 commit comments

Comments
 (0)