Skip to content

Commit ea0f437

Browse files
committed
Code Quality: Use LoadIconWithoutOverlayAsync for Drives Widget
1 parent 4607303 commit ea0f437

File tree

6 files changed

+18
-40
lines changed

6 files changed

+18
-40
lines changed

src/Files.App/Data/Items/DriveItem.cs

Lines changed: 9 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,7 @@ public FrameworkElement? ItemDecorator
219219
ToolTipService.SetToolTip(itemDecorator, "Eject".GetLocalizedResource());
220220

221221
itemDecorator.Click += ItemDecorator_Click;
222-
222+
223223
return itemDecorator;
224224
}
225225
}
@@ -314,29 +314,22 @@ public int CompareTo(INavigationControlItem other)
314314
return result == 0 ? Text.CompareTo(other.Text) : result;
315315
}
316316

317-
public async Task LoadThumbnailAsync(bool isSidebar = false)
317+
public async Task LoadThumbnailAsync()
318318
{
319-
if (!isSidebar)
319+
if (!string.IsNullOrEmpty(DeviceID) && !string.Equals(DeviceID, "network-folder"))
320+
IconData ??= await FileThumbnailHelper.LoadIconWithoutOverlayAsync(DeviceID, Constants.DefaultIconSizes.Large, false, true);
321+
322+
if (Root is not null)
320323
{
321324
using var thumbnail = await DriveHelpers.GetThumbnailAsync(Root);
322325
IconData ??= await thumbnail.ToByteArrayAsync();
323326
}
324-
else
325-
{
326-
if (!string.IsNullOrEmpty(DeviceID) && !string.Equals(DeviceID, "network-folder"))
327-
IconData ??= await FileThumbnailHelper.LoadIconWithoutOverlayAsync(DeviceID, Constants.DefaultIconSizes.Large, false, true);
328327

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

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

338-
IconData ??= UIHelpers.GetSidebarIconResourceInfo(Constants.ImageRes.Folder).IconData;
339-
}
340333
Icon ??= await IconData.ToBitmapAsync();
341334
}
342335

src/Files.App/UserControls/Widgets/DrivesWidget.xaml.cs

Lines changed: 6 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,8 @@
11
// Copyright (c) 2023 Files Community
22
// Licensed under the MIT License. See the LICENSE.
33

4-
using CommunityToolkit.Mvvm.DependencyInjection;
5-
using CommunityToolkit.Mvvm.Input;
6-
using CommunityToolkit.WinUI;
7-
using Files.App.Data.Items;
8-
using Files.App.Utils.Shell;
94
using Files.App.ViewModels.Widgets;
5+
using Microsoft.UI.Dispatching;
106
using Microsoft.UI.Xaml;
117
using Microsoft.UI.Xaml.Controls;
128
using Microsoft.UI.Xaml.Input;
@@ -25,12 +21,13 @@ public class DriveCardItem : WidgetCardItem, IWidgetCardItem<DriveItem>, ICompar
2521
private byte[] thumbnailData;
2622

2723
public new DriveItem Item { get; private set; }
28-
public bool HasThumbnail => thumbnail is not null && thumbnailData is not null;
24+
2925
public BitmapImage Thumbnail
3026
{
3127
get => thumbnail;
3228
set => SetProperty(ref thumbnail, value);
3329
}
30+
3431
public DriveCardItem(DriveItem item)
3532
{
3633
Item = item;
@@ -39,20 +36,11 @@ public DriveCardItem(DriveItem item)
3936

4037
public async Task LoadCardThumbnailAsync()
4138
{
42-
// Try load thumbnail using ListView mode
43-
if (thumbnailData is null || thumbnailData.Length == 0)
44-
thumbnailData = await FileThumbnailHelper.LoadIconFromPathAsync(Item.Path, Convert.ToUInt32(Constants.DefaultIconSizes.Jumbo), Windows.Storage.FileProperties.ThumbnailMode.SingleItem, Windows.Storage.FileProperties.ThumbnailOptions.ResizeThumbnail);
45-
46-
// Thumbnail is still null, use DriveItem icon (loaded using SingleItem mode)
47-
if (thumbnailData is null || thumbnailData.Length == 0)
48-
{
49-
await Item.LoadThumbnailAsync();
50-
thumbnailData = Item.IconData;
51-
}
39+
thumbnailData = await FileThumbnailHelper.LoadIconWithoutOverlayAsync(Item.Path, Constants.DefaultIconSizes.Jumbo, true, true);
5240

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

5846
public int CompareTo(DriveCardItem? other) => Item.Path.CompareTo(other?.Item?.Path);
@@ -135,7 +123,7 @@ public DrivesWidget()
135123
OpenPropertiesCommand = new RelayCommand<DriveCardItem>(OpenProperties);
136124
PinToFavoritesCommand = new AsyncRelayCommand<WidgetCardItem>(PinToFavoritesAsync);
137125
UnpinFromFavoritesCommand = new AsyncRelayCommand<WidgetCardItem>(UnpinFromFavoritesAsync);
138-
MapNetworkDriveCommand = new AsyncRelayCommand(DoNetworkMapDriveAsync);
126+
MapNetworkDriveCommand = new AsyncRelayCommand(DoNetworkMapDriveAsync);
139127
DisconnectNetworkDriveCommand = new RelayCommand<DriveCardItem>(DisconnectNetworkDrive);
140128
}
141129

src/Files.App/UserControls/Widgets/IWidgetCardItem.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,6 @@ public interface IWidgetCardItem<T>
1010
{
1111
T Item { get; }
1212

13-
bool HasThumbnail { get; }
14-
1513
BitmapImage Thumbnail { get; }
1614

1715
Task LoadCardThumbnailAsync();

src/Files.App/UserControls/Widgets/QuickAccessWidget.xaml.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,6 @@ public class FolderCardItem : WidgetCardItem, IWidgetCardItem<LocationItem>
5454

5555
public string AutomationProperties { get; set; }
5656
public bool HasPath => !string.IsNullOrEmpty(Path);
57-
public bool HasThumbnail => thumbnail is not null && thumbnailData is not null;
5857
public BitmapImage Thumbnail
5958
{
6059
get => thumbnail;

src/Files.App/Utils/Shell/Win32API.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -258,7 +258,7 @@ public static (byte[]? icon, byte[]? overlay) GetFileIconAndOverlay(string path,
258258
var flags = Shell32.SIIGBF.SIIGBF_BIGGERSIZEOK;
259259

260260
if (getIconOnly)
261-
flags |= Shell32.SIIGBF.SIIGBF_ICONONLY;
261+
flags = Shell32.SIIGBF.SIIGBF_ICONONLY;
262262

263263
var hres = fctry.GetImage(new SIZE(thumbnailSize, thumbnailSize), flags, out var hbitmap);
264264
if (hres == HRESULT.S_OK)

src/Files.App/ViewModels/UserControls/SidebarViewModel.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -374,7 +374,7 @@ await lib.CheckDefaultSaveFolderAccess() &&
374374
if (!section.ChildItems.Any(x => x.Path == drive.Path))
375375
{
376376
section.ChildItems.Insert(index < 0 ? section.ChildItems.Count : Math.Min(index, section.ChildItems.Count), drive);
377-
await drive.LoadThumbnailAsync(true);
377+
await drive.LoadThumbnailAsync();
378378
}
379379
}
380380
else
@@ -388,7 +388,7 @@ await lib.CheckDefaultSaveFolderAccess() &&
388388
int position = paths.IndexOf(drivePath);
389389

390390
section.ChildItems.Insert(position, drive);
391-
await drive.LoadThumbnailAsync(true);
391+
await drive.LoadThumbnailAsync();
392392
}
393393
}
394394
}

0 commit comments

Comments
 (0)