Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: Fixed thumbails not loading in MTP devices #16782

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 44 additions & 2 deletions src/Files.App/ViewModels/ShellViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -973,7 +973,7 @@ private async Task<BitmapImage> GetShieldIcon()
return shieldIcon;
}

private async Task LoadThumbnailAsync(ListedItem item, CancellationToken cancellationToken)
private async Task<bool> LoadThumbnailAsync(ListedItem item, CancellationToken cancellationToken)
{
var loadNonCachedThumbnail = false;
var thumbnailSize = LayoutSizeKindHelper.GetIconSize(folderSettings.LayoutMode);
Expand Down Expand Up @@ -1086,6 +1086,40 @@ await dispatcherQueue.EnqueueOrInvokeAsync(async () =>
}
}, cancellationToken);
}

return result is not null;
}

private async Task<bool> LoadThumbnailAsync(ListedItem item, IStorageItem matchingStorageItem, CancellationToken cancellationToken)
yaira2 marked this conversation as resolved.
Show resolved Hide resolved
{
var thumbnailSize = LayoutSizeKindHelper.GetIconSize(folderSettings.LayoutMode);
// SingleItem returns image thumbnails in the correct aspect ratio for the grid layouts
// ListView is used for the details and columns layout
var thumbnailMode = thumbnailSize < 96 ? ThumbnailMode.ListView : ThumbnailMode.SingleItem;

// We use ReturnOnlyIfCached because otherwise folders thumbnails have a black background, this has the downside the folder previews don't work
using StorageItemThumbnail thumbnail = matchingStorageItem switch
{
BaseStorageFile file => await FilesystemTasks.Wrap(() => file.GetThumbnailAsync(thumbnailMode, thumbnailSize, ThumbnailOptions.ResizeThumbnail).AsTask()),
BaseStorageFolder folder => await FilesystemTasks.Wrap(() => folder.GetThumbnailAsync(ThumbnailMode.SingleItem, thumbnailSize, ThumbnailOptions.ReturnOnlyIfCached).AsTask()),
_ => new (null!, FileSystemStatusCode.Generic)
};

if (thumbnail is not null && thumbnail.Size != 0 && thumbnail.OriginalHeight != 0 && thumbnail.OriginalWidth != 0)
{
await dispatcherQueue.EnqueueOrInvokeAsync(async () =>
{
var img = new BitmapImage();
img.DecodePixelType = DecodePixelType.Logical;
img.DecodePixelWidth = (int)thumbnailSize;
await img.SetSourceAsync(thumbnail);
item.FileImage = img;
}, Microsoft.UI.Dispatching.DispatcherQueuePriority.Normal);

return true;
}

return false;
}

private static void SetFileTag(ListedItem item)
Expand Down Expand Up @@ -1128,7 +1162,7 @@ public async Task LoadExtendedItemPropertiesAsync(ListedItem item)
}

cts.Token.ThrowIfCancellationRequested();
await LoadThumbnailAsync(item, cts.Token);
var wasThumbnailLoaded = await LoadThumbnailAsync(item, cts.Token);

cts.Token.ThrowIfCancellationRequested();
if (item.IsLibrary || item.PrimaryItemAttribute == StorageItemTypes.File || item.IsArchive)
Expand Down Expand Up @@ -1180,6 +1214,10 @@ await dispatcherQueue.EnqueueOrInvokeAsync(() =>
},
Microsoft.UI.Dispatching.DispatcherQueuePriority.Low);

// For MTP devices load thumbnail using Storage API (#15084)
if (!wasThumbnailLoaded)
await LoadThumbnailAsync(item, matchingStorageFile, cts.Token);

SetFileTag(item);
wasSyncStatusLoaded = true;
}
Expand Down Expand Up @@ -1250,6 +1288,10 @@ await dispatcherQueue.EnqueueOrInvokeAsync(() =>
},
Microsoft.UI.Dispatching.DispatcherQueuePriority.Low);

// For MTP devices load thumbnail using Storage API (#15084)
if (!wasThumbnailLoaded)
await LoadThumbnailAsync(item, matchingStorageFolder, cts.Token);

SetFileTag(item);
wasSyncStatusLoaded = true;
}
Expand Down