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
Show file tree
Hide file tree
Changes from all 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
21 changes: 18 additions & 3 deletions src/Files.App/Data/Enums/IconOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,19 +19,34 @@ public enum IconOptions
/// </summary>
UseCurrentScale = 1,

/// <summary>
/// Scale the thumbnail to the requested size.
/// </summary>
ResizeThumbnail = 2,

/// <summary>
/// Retrieve only the file icon, even a thumbnail is available. This has the best performance.
/// </summary>
ReturnIconOnly = 2,
ReturnIconOnly = 4,

/// <summary>
/// Retrieve only the thumbnail.
/// </summary>
ReturnThumbnailOnly = 4,
ReturnThumbnailOnly = 8,

/// <summary>
/// Retrieve a thumbnail only if it is cached or embedded in the file.
/// </summary>
ReturnOnlyIfCached = 8,
ReturnOnlyIfCached = 16,

/// <summary>
/// Default. Retrieve a thumbnail to display a preview of any single item (like a file, folder, or file group).
/// </summary>
SingleItem = 32,

/// <summary>
/// Retrieve a thumbnail to display previews of files (or other items) in a list.
/// </summary>
ListView = 64,
}
}
16 changes: 7 additions & 9 deletions src/Files.App/Data/Items/DriveItem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,15 @@
using Microsoft.UI.Xaml.Controls;
using Microsoft.UI.Xaml.Media.Imaging;
using Windows.Storage;
using Windows.Storage.Streams;
using Windows.Storage.FileProperties;
using ByteSize = ByteSizeLib.ByteSize;

namespace Files.App.Data.Items
{
public sealed class DriveItem : ObservableObject, INavigationControlItem, ILocatableFolder
{
private BitmapImage icon;
public BitmapImage Icon
private BitmapImage? icon;
public BitmapImage? Icon
{
get => icon;
set
Expand All @@ -24,7 +24,7 @@ public BitmapImage Icon
}
}

public byte[] IconData { get; set; }
public byte[]? IconData { get; set; }

private string path;
public string Path
Expand Down Expand Up @@ -232,12 +232,11 @@ private void ItemDecorator_Click(object sender, RoutedEventArgs e)
DriveHelpers.EjectDeviceAsync(Path);
}

public static async Task<DriveItem> CreateFromPropertiesAsync(StorageFolder root, string deviceId, string label, DriveType type, IRandomAccessStream imageStream = null)
public static async Task<DriveItem> CreateFromPropertiesAsync(StorageFolder root, string deviceId, string label, DriveType type, byte[]? imageData = null)
{
var item = new DriveItem();

if (imageStream is not null)
item.IconData = await imageStream.ToByteArrayAsync();
item.IconData = imageData;

item.Text = type switch
{
Expand Down Expand Up @@ -336,8 +335,7 @@ public async Task LoadThumbnailAsync()

if (Root is not null)
{
using var thumbnail = await DriveHelpers.GetThumbnailAsync(Root);
IconData ??= thumbnail is not null ? await thumbnail.ToByteArrayAsync() : null;
IconData ??= await FileThumbnailHelper.GetIconAsync(Root, 40, ThumbnailMode.SingleItem, ThumbnailOptions.UseCurrentScale);
}

if (string.Equals(DeviceID, "network-folder"))
Expand Down
4 changes: 2 additions & 2 deletions src/Files.App/Data/Items/WidgetDriveCardItem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// Licensed under the MIT License.

using Microsoft.UI.Xaml.Media.Imaging;
using Windows.Storage.FileProperties;

namespace Files.App.Data.Items
{
Expand Down Expand Up @@ -34,8 +35,7 @@ public async Task LoadCardThumbnailAsync()

if (result is null)
{
using var thumbnail = await DriveHelpers.GetThumbnailAsync(Item.Root);
result ??= await thumbnail.ToByteArrayAsync();
result ??= await FileThumbnailHelper.GetIconAsync(Item.Root, 40, ThumbnailMode.SingleItem, ThumbnailOptions.UseCurrentScale);
}

thumbnailData = result;
Expand Down
1 change: 1 addition & 0 deletions src/Files.App/Data/Models/PinnedFoldersManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ public async Task<LocationItem> CreateLocationItemFromPathAsync(string path)
{
var result = await FileThumbnailHelper.GetIconAsync(
res.Result.Path,
res.Result,
Constants.ShellIconSizes.Small,
true,
IconOptions.ReturnIconOnly | IconOptions.UseCurrentScale);
Expand Down
3 changes: 2 additions & 1 deletion src/Files.App/Services/Storage/StorageDevicesService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using Microsoft.Extensions.Logging;
using System.IO;
using Windows.Storage;
using Windows.Storage.FileProperties;

namespace Files.App.Services
{
Expand Down Expand Up @@ -42,7 +43,7 @@ public async IAsyncEnumerable<ILocatableFolder> GetDrivesAsync()
continue;
}

using var thumbnail = await DriveHelpers.GetThumbnailAsync(res.Result);
var thumbnail = await FileThumbnailHelper.GetIconAsync(res.Result, 40, ThumbnailMode.SingleItem, ThumbnailOptions.UseCurrentScale);
var type = DriveHelpers.GetDriveType(drive);
var label = DriveHelpers.GetExtendedDriveLabel(drive);
var driveItem = await DriveItem.CreateFromPropertiesAsync(res.Result, drive.Name.TrimEnd('\\'), label, type, thumbnail);
Expand Down
5 changes: 0 additions & 5 deletions src/Files.App/Utils/Storage/Helpers/DriveHelpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -163,10 +163,5 @@ public static unsafe string GetExtendedDriveLabel(SystemIO.DriveInfo drive)

}) ?? "";
}

public static async Task<StorageItemThumbnail> GetThumbnailAsync(StorageFolder folder)
=> (StorageItemThumbnail)await FilesystemTasks.Wrap(()
=> folder.GetThumbnailAsync(ThumbnailMode.SingleItem, 40, ThumbnailOptions.UseCurrentScale).AsTask()
);
}
}
51 changes: 44 additions & 7 deletions src/Files.App/Utils/Storage/Helpers/FileThumbnailHelper.cs
Original file line number Diff line number Diff line change
@@ -1,20 +1,58 @@
// Copyright (c) Files Community
// Licensed under the MIT License.

using Windows.Storage;
using Windows.Storage.FileProperties;

namespace Files.App.Utils.Storage
{
public static class FileThumbnailHelper
{
public static async Task<byte[]?> GetIconAsync(string? path, IStorageItem? item, uint requestedSize, bool isFolder, IconOptions iconOptions)
{
byte[]? result = null;
if (path is not null)
result ??= await GetIconAsync(path, requestedSize, isFolder, iconOptions);
if (item is not null)
result ??= await GetIconAsync(item, requestedSize, iconOptions);
return result;
}

/// <summary>
/// Returns icon or thumbnail for given file or folder
/// </summary>
public static async Task<byte[]?> GetIconAsync(string path, uint requestedSize, bool isFolder, IconOptions iconOptions)
public static Task<byte[]?> GetIconAsync(string path, uint requestedSize, bool isFolder, IconOptions iconOptions)
{
var size = iconOptions.HasFlag(IconOptions.UseCurrentScale) ? requestedSize * App.AppModel.AppWindowDPI : requestedSize;

return await Win32Helper.StartSTATask(() => Win32Helper.GetIcon(path, (int)size, isFolder, iconOptions));
return Win32Helper.StartSTATask(() => Win32Helper.GetIcon(path, (int)size, isFolder, iconOptions));
}

/// <summary>
/// Returns thumbnail for given file or folder using Storage API
/// </summary>
public static Task<byte[]?> GetIconAsync(IStorageItem item, uint requestedSize, IconOptions iconOptions)
{
var thumbnailOptions = (iconOptions.HasFlag(IconOptions.UseCurrentScale) ? ThumbnailOptions.UseCurrentScale : 0) |
(iconOptions.HasFlag(IconOptions.ReturnOnlyIfCached) ? ThumbnailOptions.ReturnOnlyIfCached : 0) |
(iconOptions.HasFlag(IconOptions.ResizeThumbnail) ? ThumbnailOptions.ResizeThumbnail : 0);

var thumbnailMode = iconOptions.HasFlag(IconOptions.ListView) ? ThumbnailMode.ListView : ThumbnailMode.SingleItem;

return GetIconAsync(item, requestedSize, thumbnailMode, thumbnailOptions);
}

public static async Task<byte[]?> GetIconAsync(IStorageItem item, uint requestedSize, ThumbnailMode thumbnailMode, ThumbnailOptions thumbnailOptions)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is thumbnail mode always single?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Both ListView and SingleItem are used

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we can reduce complexity by just using SingleItem.

{
using StorageItemThumbnail thumbnail = item switch
{
BaseStorageFile file => await FilesystemTasks.Wrap(() => file.GetThumbnailAsync(thumbnailMode, requestedSize, thumbnailOptions).AsTask()),
BaseStorageFolder folder => await FilesystemTasks.Wrap(() => folder.GetThumbnailAsync(thumbnailMode, requestedSize, thumbnailOptions).AsTask()),
_ => new(null!, FileSystemStatusCode.Generic)
};
if (thumbnail is not null && thumbnail.Size != 0 && thumbnail.OriginalHeight != 0 && thumbnail.OriginalWidth != 0)
return await thumbnail.ToByteArrayAsync();
return null;
}

/// <summary>
Expand All @@ -23,14 +61,13 @@ public static class FileThumbnailHelper
/// <param name="path"></param>
/// <param name="isFolder"></param>
/// <returns></returns>
public static async Task<byte[]?> GetIconOverlayAsync(string path, bool isFolder)
=> await Win32Helper.StartSTATask(() => Win32Helper.GetIconOverlay(path, isFolder));
public static Task<byte[]?> GetIconOverlayAsync(string path, bool isFolder)
=> Win32Helper.StartSTATask(() => Win32Helper.GetIconOverlay(path, isFolder));

[Obsolete]
public static async Task<byte[]?> LoadIconFromPathAsync(string filePath, uint thumbnailSize, ThumbnailMode thumbnailMode, ThumbnailOptions thumbnailOptions, bool isFolder = false)
public static Task<byte[]?> LoadIconFromPathAsync(string filePath, uint thumbnailSize, ThumbnailMode thumbnailMode, ThumbnailOptions thumbnailOptions, bool isFolder = false)
{
var result = await GetIconAsync(filePath, thumbnailSize, isFolder, IconOptions.None);
return result;
return GetIconAsync(filePath, thumbnailSize, isFolder, IconOptions.None);
}
}
}
1 change: 1 addition & 0 deletions src/Files.App/Utils/Storage/Search/FolderSearch.cs
Original file line number Diff line number Diff line change
Expand Up @@ -512,6 +512,7 @@ private async Task<ListedItem> GetListedItemAsync(IStorageItem item)
{
var iconResult = await FileThumbnailHelper.GetIconAsync(
item.Path,
item,
Constants.ShellIconSizes.Small,
item.IsOfType(StorageItemTypes.Folder),
IconOptions.ReturnIconOnly | IconOptions.UseCurrentScale);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ public async override Task GetSpecialPropertiesAsync()
{
var result = await FileThumbnailHelper.GetIconAsync(
Drive.Path,
diskRoot,
Constants.ShellIconSizes.ExtraLarge,
true,
IconOptions.ReturnIconOnly | IconOptions.UseCurrentScale);
Expand Down
10 changes: 6 additions & 4 deletions src/Files.App/ViewModels/Properties/Items/FileProperties.cs
Original file line number Diff line number Diff line change
Expand Up @@ -108,8 +108,13 @@ public override async Task GetSpecialPropertiesAsync()
ViewModel.ItemSizeOnDisk = Win32Helper.GetFileSizeOnDisk(Item.ItemPath)?.ToLongSizeString() ??
string.Empty;

string filePath = (Item as ShortcutItem)?.TargetPath ?? Item.ItemPath;
BaseStorageFile? file = !string.IsNullOrWhiteSpace(filePath) ?
await AppInstance.ShellViewModel.GetFileFromPathAsync(filePath) : null!;

var result = await FileThumbnailHelper.GetIconAsync(
Item.ItemPath,
file,
Constants.ShellIconSizes.ExtraLarge,
false,
IconOptions.UseCurrentScale);
Expand All @@ -132,9 +137,6 @@ public override async Task GetSpecialPropertiesAsync()
}
}

string filePath = (Item as ShortcutItem)?.TargetPath ?? Item.ItemPath;
BaseStorageFile file = await AppInstance.ShellViewModel.GetFileFromPathAsync(filePath);

// Couldn't access the file and can't load any other properties
if (file is null)
return;
Expand All @@ -152,7 +154,7 @@ public override async Task GetSpecialPropertiesAsync()
ViewModel.UncompressedItemSizeBytes = uncompressedSize;
}

if (file.Properties is not null)
if (file?.Properties is not null)
GetOtherPropertiesAsync(file.Properties);
}

Expand Down
8 changes: 5 additions & 3 deletions src/Files.App/ViewModels/Properties/Items/FolderProperties.cs
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,13 @@ public async override Task GetSpecialPropertiesAsync()
ViewModel.CanCompressContent = Win32Helper.CanCompressContent(Item.ItemPath);
ViewModel.IsContentCompressed = Win32Helper.HasFileAttribute(Item.ItemPath, System.IO.FileAttributes.Compressed);

string folderPath = (Item as ShortcutItem)?.TargetPath ?? Item.ItemPath;
BaseStorageFolder? storageFolder = !string.IsNullOrWhiteSpace(folderPath) ?
await AppInstance.ShellViewModel.GetFolderFromPathAsync(folderPath) : null!;

var result = await FileThumbnailHelper.GetIconAsync(
Item.ItemPath,
storageFolder,
Constants.ShellIconSizes.ExtraLarge,
true,
IconOptions.UseCurrentScale);
Expand Down Expand Up @@ -111,9 +116,6 @@ public async override Task GetSpecialPropertiesAsync()
}
}

string folderPath = (Item as ShortcutItem)?.TargetPath ?? Item.ItemPath;
BaseStorageFolder storageFolder = await AppInstance.ShellViewModel.GetFolderFromPathAsync(folderPath);

if (storageFolder is not null)
{
ViewModel.ItemCreatedTimestampReal = storageFolder.DateCreated;
Expand Down
48 changes: 46 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,42 @@ 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
// We use ReturnOnlyIfCached because otherwise folders thumbnails have a black background, this has the downside the folder previews don't work
var iconOptions = matchingStorageItem switch
{
BaseStorageFolder => IconOptions.SingleItem | IconOptions.ReturnOnlyIfCached,
BaseStorageFile when thumbnailSize < 96 => IconOptions.ListView | IconOptions.ResizeThumbnail,
_ => IconOptions.SingleItem | IconOptions.ResizeThumbnail,
};

var result = await FileThumbnailHelper.GetIconAsync(
matchingStorageItem,
thumbnailSize,
iconOptions);

if (result is not null)
{
await dispatcherQueue.EnqueueOrInvokeAsync(async () =>
{
// Assign FileImage property
var image = await result.ToBitmapAsync();
if (image is not null)
item.FileImage = image;
}, Microsoft.UI.Dispatching.DispatcherQueuePriority.Normal);

return true;
}

return false;
}

private static void SetFileTag(ListedItem item)
Expand Down Expand Up @@ -1128,7 +1164,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 +1216,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 +1290,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
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ public async virtual Task<List<FileProperty>> LoadPreviewAndDetailsAsync()
{
var result = await FileThumbnailHelper.GetIconAsync(
Item.ItemPath,
Item.ItemFile,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we're anyways passing StorageItem, is there any reason to continue passing the path?

Constants.ShellIconSizes.Jumbo,
false,
IconOptions.None);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ private async Task LoadPreviewAndDetailsAsync()

var result = await FileThumbnailHelper.GetIconAsync(
Item.ItemPath,
Folder,
Constants.ShellIconSizes.Jumbo,
true,
IconOptions.None);
Expand Down
Loading