Skip to content

Code Quality: Use LoadIconWithoutOverlayAsync for Drives Widget #14524

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

Merged
merged 2 commits into from
Jan 23, 2024
Merged
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
25 changes: 9 additions & 16 deletions src/Files.App/Data/Items/DriveItem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ public FrameworkElement? ItemDecorator
ToolTipService.SetToolTip(itemDecorator, "Eject".GetLocalizedResource());

itemDecorator.Click += ItemDecorator_Click;

return itemDecorator;
}
}
Expand Down Expand Up @@ -314,29 +314,22 @@ public int CompareTo(INavigationControlItem other)
return result == 0 ? Text.CompareTo(other.Text) : result;
}

public async Task LoadThumbnailAsync(bool isSidebar = false)
public async Task LoadThumbnailAsync()
{
if (!isSidebar)
if (!string.IsNullOrEmpty(DeviceID) && !string.Equals(DeviceID, "network-folder"))
IconData ??= await FileThumbnailHelper.LoadIconWithoutOverlayAsync(DeviceID, Constants.DefaultIconSizes.Large, false, true);

if (Root is not null)
{
using var thumbnail = await DriveHelpers.GetThumbnailAsync(Root);
IconData ??= await thumbnail.ToByteArrayAsync();
}
else
{
if (!string.IsNullOrEmpty(DeviceID) && !string.Equals(DeviceID, "network-folder"))
IconData ??= await FileThumbnailHelper.LoadIconWithoutOverlayAsync(DeviceID, Constants.DefaultIconSizes.Large, false, true);

if (Root is not null)
{
using var thumbnail = await DriveHelpers.GetThumbnailAsync(Root);
IconData ??= await thumbnail.ToByteArrayAsync();
}
if (string.Equals(DeviceID, "network-folder"))
IconData ??= UIHelpers.GetSidebarIconResourceInfo(Constants.ImageRes.NetworkDrives).IconData;

if (string.Equals(DeviceID, "network-folder"))
IconData ??= UIHelpers.GetSidebarIconResourceInfo(Constants.ImageRes.NetworkDrives).IconData;
IconData ??= UIHelpers.GetSidebarIconResourceInfo(Constants.ImageRes.Folder).IconData;

IconData ??= UIHelpers.GetSidebarIconResourceInfo(Constants.ImageRes.Folder).IconData;
}
Icon ??= await IconData.ToBitmapAsync();
}

Expand Down
24 changes: 6 additions & 18 deletions src/Files.App/UserControls/Widgets/DrivesWidget.xaml.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,8 @@
// Copyright (c) 2023 Files Community
// Licensed under the MIT License. See the LICENSE.

using CommunityToolkit.Mvvm.DependencyInjection;
using CommunityToolkit.Mvvm.Input;
using CommunityToolkit.WinUI;
using Files.App.Data.Items;
using Files.App.Utils.Shell;
using Files.App.ViewModels.Widgets;
using Microsoft.UI.Dispatching;
using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Controls;
using Microsoft.UI.Xaml.Input;
Expand All @@ -25,12 +21,13 @@ public class DriveCardItem : WidgetCardItem, IWidgetCardItem<DriveItem>, ICompar
private byte[] thumbnailData;

public new DriveItem Item { get; private set; }
public bool HasThumbnail => thumbnail is not null && thumbnailData is not null;

public BitmapImage Thumbnail
{
get => thumbnail;
set => SetProperty(ref thumbnail, value);
}

public DriveCardItem(DriveItem item)
{
Item = item;
Expand All @@ -39,20 +36,11 @@ public DriveCardItem(DriveItem item)

public async Task LoadCardThumbnailAsync()
{
// Try load thumbnail using ListView mode
if (thumbnailData is null || thumbnailData.Length == 0)
thumbnailData = await FileThumbnailHelper.LoadIconFromPathAsync(Item.Path, Convert.ToUInt32(Constants.DefaultIconSizes.Jumbo), Windows.Storage.FileProperties.ThumbnailMode.SingleItem, Windows.Storage.FileProperties.ThumbnailOptions.ResizeThumbnail);

// Thumbnail is still null, use DriveItem icon (loaded using SingleItem mode)
if (thumbnailData is null || thumbnailData.Length == 0)
{
await Item.LoadThumbnailAsync();
thumbnailData = Item.IconData;
}
thumbnailData = await FileThumbnailHelper.LoadIconWithoutOverlayAsync(Item.Path, Constants.DefaultIconSizes.Jumbo, true, true);

// Thumbnail data is valid, set the item icon
if (thumbnailData is not null && thumbnailData.Length > 0)
Thumbnail = await MainWindow.Instance.DispatcherQueue.EnqueueOrInvokeAsync(() => thumbnailData.ToBitmapAsync(Constants.DefaultIconSizes.Jumbo));
Thumbnail = await MainWindow.Instance.DispatcherQueue.EnqueueOrInvokeAsync(() => thumbnailData.ToBitmapAsync(), Microsoft.UI.Dispatching.DispatcherQueuePriority.Low);
}

public int CompareTo(DriveCardItem? other) => Item.Path.CompareTo(other?.Item?.Path);
Expand Down Expand Up @@ -135,7 +123,7 @@ public DrivesWidget()
OpenPropertiesCommand = new RelayCommand<DriveCardItem>(OpenProperties);
PinToFavoritesCommand = new AsyncRelayCommand<WidgetCardItem>(PinToFavoritesAsync);
UnpinFromFavoritesCommand = new AsyncRelayCommand<WidgetCardItem>(UnpinFromFavoritesAsync);
MapNetworkDriveCommand = new AsyncRelayCommand(DoNetworkMapDriveAsync);
MapNetworkDriveCommand = new AsyncRelayCommand(DoNetworkMapDriveAsync);
DisconnectNetworkDriveCommand = new RelayCommand<DriveCardItem>(DisconnectNetworkDrive);
}

Expand Down
2 changes: 0 additions & 2 deletions src/Files.App/UserControls/Widgets/IWidgetCardItem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@ public interface IWidgetCardItem<T>
{
T Item { get; }

bool HasThumbnail { get; }

BitmapImage Thumbnail { get; }

Task LoadCardThumbnailAsync();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,6 @@ public class FolderCardItem : WidgetCardItem, IWidgetCardItem<LocationItem>

public string AutomationProperties { get; set; }
public bool HasPath => !string.IsNullOrEmpty(Path);
public bool HasThumbnail => thumbnail is not null && thumbnailData is not null;
public BitmapImage Thumbnail
{
get => thumbnail;
Expand Down
4 changes: 2 additions & 2 deletions src/Files.App/ViewModels/UserControls/SidebarViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -374,7 +374,7 @@ await lib.CheckDefaultSaveFolderAccess() &&
if (!section.ChildItems.Any(x => x.Path == drive.Path))
{
section.ChildItems.Insert(index < 0 ? section.ChildItems.Count : Math.Min(index, section.ChildItems.Count), drive);
await drive.LoadThumbnailAsync(true);
await drive.LoadThumbnailAsync();
}
}
else
Expand All @@ -388,7 +388,7 @@ await lib.CheckDefaultSaveFolderAccess() &&
int position = paths.IndexOf(drivePath);

section.ChildItems.Insert(position, drive);
await drive.LoadThumbnailAsync(true);
await drive.LoadThumbnailAsync();
}
}
}
Expand Down