@@ -973,7 +973,7 @@ private async Task<BitmapImage> GetShieldIcon()
973973 return shieldIcon ;
974974 }
975975
976- private async Task LoadThumbnailAsync ( ListedItem item , CancellationToken cancellationToken )
976+ private async Task < bool > LoadThumbnailAsync ( ListedItem item , CancellationToken cancellationToken )
977977 {
978978 var loadNonCachedThumbnail = false ;
979979 var thumbnailSize = LayoutSizeKindHelper . GetIconSize ( folderSettings . LayoutMode ) ;
@@ -1086,6 +1086,40 @@ await dispatcherQueue.EnqueueOrInvokeAsync(async () =>
10861086 }
10871087 } , cancellationToken ) ;
10881088 }
1089+
1090+ return result is not null ;
1091+ }
1092+
1093+ private async Task < bool > LoadThumbnailAsync ( ListedItem item , IStorageItem matchingStorageItem , CancellationToken cancellationToken )
1094+ {
1095+ var thumbnailSize = LayoutSizeKindHelper . GetIconSize ( folderSettings . LayoutMode ) ;
1096+ // SingleItem returns image thumbnails in the correct aspect ratio for the grid layouts
1097+ // ListView is used for the details and columns layout
1098+ var thumbnailMode = thumbnailSize < 96 ? ThumbnailMode . ListView : ThumbnailMode . SingleItem ;
1099+
1100+ // 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
1102+ {
1103+ BaseStorageFile file => await FilesystemTasks . Wrap ( ( ) => file . GetThumbnailAsync ( thumbnailMode , thumbnailSize , ThumbnailOptions . ResizeThumbnail ) . AsTask ( ) ) ,
1104+ BaseStorageFolder folder => await FilesystemTasks . Wrap ( ( ) => folder . GetThumbnailAsync ( thumbnailMode , thumbnailSize , ThumbnailOptions . ReturnOnlyIfCached ) . AsTask ( ) ) ,
1105+ _ => new ( null ! , FileSystemStatusCode . Generic )
1106+ } ;
1107+
1108+ if ( Thumbnail is not null && Thumbnail . Size != 0 && Thumbnail . OriginalHeight != 0 && Thumbnail . OriginalWidth != 0 )
1109+ {
1110+ await dispatcherQueue . EnqueueOrInvokeAsync ( async ( ) =>
1111+ {
1112+ var img = new BitmapImage ( ) ;
1113+ img . DecodePixelType = DecodePixelType . Logical ;
1114+ img . DecodePixelWidth = ( int ) thumbnailSize ;
1115+ await img . SetSourceAsync ( Thumbnail ) ;
1116+ item . FileImage = img ;
1117+ } , Microsoft . UI . Dispatching . DispatcherQueuePriority . Normal ) ;
1118+
1119+ return true ;
1120+ }
1121+
1122+ return false ;
10891123 }
10901124
10911125 private static void SetFileTag ( ListedItem item )
@@ -1128,7 +1162,7 @@ public async Task LoadExtendedItemPropertiesAsync(ListedItem item)
11281162 }
11291163
11301164 cts . Token . ThrowIfCancellationRequested ( ) ;
1131- await LoadThumbnailAsync ( item , cts . Token ) ;
1165+ var wasThumbnailLoaded = await LoadThumbnailAsync ( item , cts . Token ) ;
11321166
11331167 cts . Token . ThrowIfCancellationRequested ( ) ;
11341168 if ( item . IsLibrary || item . PrimaryItemAttribute == StorageItemTypes . File || item . IsArchive )
@@ -1180,6 +1214,9 @@ await dispatcherQueue.EnqueueOrInvokeAsync(() =>
11801214 } ,
11811215 Microsoft . UI . Dispatching . DispatcherQueuePriority . Low ) ;
11821216
1217+ // For MTP devices load thumbnail using Storage API (#15084)
1218+ wasThumbnailLoaded |= await LoadThumbnailAsync ( item , matchingStorageFile , cts . Token ) ;
1219+
11831220 SetFileTag ( item ) ;
11841221 wasSyncStatusLoaded = true ;
11851222 }
@@ -1250,6 +1287,9 @@ await dispatcherQueue.EnqueueOrInvokeAsync(() =>
12501287 } ,
12511288 Microsoft . UI . Dispatching . DispatcherQueuePriority . Low ) ;
12521289
1290+ // For MTP devices load thumbnail using Storage API (#15084)
1291+ wasThumbnailLoaded |= await LoadThumbnailAsync ( item , matchingStorageFolder , cts . Token ) ;
1292+
12531293 SetFileTag ( item ) ;
12541294 wasSyncStatusLoaded = true ;
12551295 }
0 commit comments