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 5 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
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
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()
);
}
}
17 changes: 17 additions & 0 deletions src/Files.App/Utils/Storage/Helpers/FileThumbnailHelper.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Copyright (c) Files Community
// Licensed under the MIT License.

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

namespace Files.App.Utils.Storage
Expand All @@ -17,6 +18,22 @@ public static class FileThumbnailHelper
return await Win32Helper.StartSTATask(() => Win32Helper.GetIcon(path, (int)size, isFolder, iconOptions));
}

/// <summary>
/// Returns thumbnail for given file or folder using Storage API
/// </summary>
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>
/// Returns overlay for given file or folder
/// /// </summary>
Expand Down
49 changes: 47 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,43 @@ 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 (thumbnailMode, thumbnailOptions) = matchingStorageItem switch
{
BaseStorageFolder => (ThumbnailMode.SingleItem, ThumbnailOptions.ReturnOnlyIfCached),
BaseStorageFile when thumbnailSize < 96 => (ThumbnailMode.ListView, ThumbnailOptions.ResizeThumbnail),
_ => (ThumbnailMode.SingleItem, ThumbnailOptions.ResizeThumbnail),
};

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

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 +1165,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 +1217,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 +1291,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