Skip to content

Commit d501d1d

Browse files
authored
Code Quality: Improved Widget code (#14458)
1 parent c73e127 commit d501d1d

39 files changed

+429
-584
lines changed

src/Files.App/Data/Contexts/HomePage/HomePageContext.cs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,14 @@
22
// Licensed under the MIT License. See the LICENSE.
33

44
using Files.App.UserControls.Widgets;
5-
using Files.App.ViewModels.Widgets;
65
using Microsoft.UI.Xaml.Controls;
76
using System.Collections.Immutable;
87

98
namespace Files.App.Data.Contexts
109
{
1110
internal class HomePageContext : ObservableObject, IHomePageContext
1211
{
13-
private static readonly IImmutableList<FileTagsItemViewModel> emptyTaggedItems = Enumerable.Empty<FileTagsItemViewModel>().ToImmutableList();
12+
private static readonly IImmutableList<WidgetFileTagCardItem> emptyTaggedItems = Enumerable.Empty<WidgetFileTagCardItem>().ToImmutableList();
1413

1514
public bool IsAnyItemRightClicked => rightClickedItem is not null;
1615

@@ -20,20 +19,20 @@ internal class HomePageContext : ObservableObject, IHomePageContext
2019
private CommandBarFlyout? itemContextFlyoutMenu = null;
2120
public CommandBarFlyout? ItemContextFlyoutMenu => itemContextFlyoutMenu;
2221

23-
private IReadOnlyList<FileTagsItemViewModel> selectedTaggedItems = emptyTaggedItems;
24-
public IReadOnlyList<FileTagsItemViewModel> SelectedTaggedItems
22+
private IReadOnlyList<WidgetFileTagCardItem> selectedTaggedItems = emptyTaggedItems;
23+
public IReadOnlyList<WidgetFileTagCardItem> SelectedTaggedItems
2524
{
2625
get => selectedTaggedItems;
2726
set => selectedTaggedItems = value ?? emptyTaggedItems;
2827
}
2928

3029
public HomePageContext()
3130
{
32-
HomePageWidget.RightClickedItemChanged += HomePageWidget_RightClickedItemChanged;
31+
BaseWidgetViewModel.RightClickedItemChanged += HomePageWidget_RightClickedItemChanged;
3332
FileTagsWidget.SelectedTaggedItemsChanged += FileTagsWidget_SelectedTaggedItemsChanged;
3433
}
3534

36-
private void FileTagsWidget_SelectedTaggedItemsChanged(object? sender, IEnumerable<FileTagsItemViewModel> e)
35+
private void FileTagsWidget_SelectedTaggedItemsChanged(object? sender, IEnumerable<WidgetFileTagCardItem> e)
3736
{
3837
SetProperty(ref selectedTaggedItems, e.ToList());
3938
}

src/Files.App/Data/Contexts/HomePage/IHomePageContext.cs

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

4-
using Files.App.UserControls.Widgets;
5-
using Files.App.ViewModels.Widgets;
64
using Microsoft.UI.Xaml.Controls;
75

86
namespace Files.App.Data.Contexts
@@ -22,7 +20,7 @@ internal interface IHomePageContext
2220
/// <summary>
2321
/// An list containing all the selected tagged items
2422
/// </summary>
25-
IReadOnlyList<FileTagsItemViewModel> SelectedTaggedItems { get; }
23+
IReadOnlyList<WidgetFileTagCardItem> SelectedTaggedItems { get; }
2624

2725
/// <summary>
2826
/// Tells whether any item has been right clicked

src/Files.App/Data/Contexts/Tags/TagsContext.cs

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

4-
using Files.App.ViewModels.Widgets;
54
using System.Collections.Immutable;
65

76
namespace Files.App.Data.Contexts
87
{
9-
sealed class TagsContext : ITagsContext
8+
sealed class TagsContext : ITagsContext
109
{
1110
private static readonly IReadOnlyList<(string path, bool isFolder)> _emptyTaggedItemsList
1211
= Enumerable.Empty<(string path, bool isFolder)>().ToImmutableList();
@@ -29,7 +28,7 @@ sealed class TagsContext : ITagsContext
2928

3029
public TagsContext()
3130
{
32-
FileTagsContainerViewModel.SelectedTagChanged += SelectedTagsChanged;
31+
WidgetFileTagsContainerItem.SelectedTagChanged += SelectedTagsChanged;
3332
SidebarViewModel.SelectedTagChanged += SelectedTagsChanged;
3433
}
3534

src/Files.App/Data/Items/INavigationControlItem.cs renamed to src/Files.App/Data/Contracts/INavigationControlItem.cs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,9 @@
22
// Licensed under the MIT License. See the LICENSE.
33

44
using Files.App.UserControls.Sidebar;
5-
using Microsoft.UI.Xaml.Controls;
65

7-
namespace Files.App.Data.Items
6+
namespace Files.App.Data.Contracts
87
{
9-
108
public interface INavigationControlItem : IComparable<INavigationControlItem>, INotifyPropertyChanged, ISidebarItemModel
119
{
1210
public new string Text { get; }

src/Files.App/UserControls/Widgets/IWidgetCardItem.cs renamed to src/Files.App/Data/Contracts/IWidgetCardItem.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,8 @@
22
// Licensed under the MIT License. See the LICENSE.
33

44
using Microsoft.UI.Xaml.Media.Imaging;
5-
using System.Threading.Tasks;
65

7-
namespace Files.App.UserControls.Widgets
6+
namespace Files.App.Data.Contracts
87
{
98
public interface IWidgetCardItem<T>
109
{
@@ -14,4 +13,4 @@ public interface IWidgetCardItem<T>
1413

1514
Task LoadCardThumbnailAsync();
1615
}
17-
}
16+
}

src/Files.App/ViewModels/Widgets/IWidgetItem.cs renamed to src/Files.App/Data/Contracts/IWidgetViewModel.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@
33

44
using Microsoft.UI.Xaml.Controls;
55

6-
namespace Files.App.ViewModels.Widgets
6+
namespace Files.App.Data.Contracts
77
{
8-
public interface IWidgetItem : IDisposable
8+
public interface IWidgetViewModel : IDisposable
99
{
1010
string WidgetName { get; }
1111

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
// Copyright (c) 2023 Files Community
2+
// Licensed under the MIT License. See the LICENSE.
3+
4+
namespace Files.App.Data.EventArguments
5+
{
6+
public class QuickAccessCardEventArgs : EventArgs
7+
{
8+
public LocationItem? Item { get; set; }
9+
}
10+
11+
public class QuickAccessCardInvokedEventArgs : EventArgs
12+
{
13+
public string? Path { get; set; }
14+
}
15+
16+
public class ModifyQuickAccessEventArgs : EventArgs
17+
{
18+
public string[]? Paths { get; set; }
19+
public ShellFileItem[]? Items { get; set; }
20+
public bool Add;
21+
public bool Pin = true;
22+
public bool Reset = false;
23+
public bool Reorder = false;
24+
25+
public ModifyQuickAccessEventArgs(string[] paths, bool add)
26+
{
27+
Paths = paths;
28+
Add = add;
29+
}
30+
31+
public ModifyQuickAccessEventArgs(ShellFileItem[] items, bool add)
32+
{
33+
Paths = items.Select(x => x.FilePath).ToArray();
34+
Items = items;
35+
Add = add;
36+
}
37+
}
38+
}

src/Files.App/Data/EventArguments/WidgetsRightClickedItemChangedEventArgs.cs

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

4-
using Files.App.UserControls.Widgets;
54
using Microsoft.UI.Xaml.Controls;
65

76
namespace Files.App.Data.EventArguments

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,4 +155,4 @@ public RecycleBinLocationItem()
155155
RecycleBinManager.Default.RecycleBinItemDeleted += RefreshSpaceUsed;
156156
}
157157
}
158-
}
158+
}

src/Files.App/Utils/Library/LibraryLocationItem.cs renamed to src/Files.App/Data/Items/SidebarLibraryItem.cs

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

4-
namespace Files.App.Utils.Library
4+
namespace Files.App.Data.Items
55
{
66
public class LibraryLocationItem : LocationItem
77
{
@@ -57,4 +57,4 @@ public async Task LoadLibraryIconAsync()
5757
public override bool Equals(object obj)
5858
=> obj is LibraryLocationItem other && GetType() == obj.GetType() && string.Equals(Path, other.Path, System.StringComparison.OrdinalIgnoreCase);
5959
}
60-
}
60+
}

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55

66
namespace Files.App.Data.Items
77
{
8-
98
public class TagsListItem
109
{
1110
public bool IsTag
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// Copyright (c) 2023 Files Community
2+
// Licensed under the MIT License. See the LICENSE.
3+
4+
namespace Files.App.Data.Items
5+
{
6+
/// <summary>
7+
/// Represents base item for widget card item.
8+
/// </summary>
9+
public abstract class WidgetCardItem : ObservableObject
10+
{
11+
public virtual string? Path { get; set; }
12+
13+
public virtual object? Item { get; set; }
14+
}
15+
}
Lines changed: 28 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -3,28 +3,39 @@
33

44
using Microsoft.UI.Xaml.Controls;
55

6-
namespace Files.App.ViewModels.Widgets
6+
namespace Files.App.Data.Items
77
{
8-
public class WidgetsListControlItemViewModel : ObservableObject, IDisposable
8+
/// <summary>
9+
/// Represents an item of Files widget container.
10+
/// </summary>
11+
public class WidgetContainerItem : ObservableObject, IDisposable
912
{
10-
private readonly Action<bool> _expanderValueChangedCallback;
13+
// Fields
1114

15+
private readonly Action<bool> _expanderValueChangedCallback;
1216
private readonly Func<bool> _expanderValueRequestedCallback;
1317

18+
// Properties
19+
20+
public IWidgetViewModel WidgetItemModel
21+
=> WidgetControl as IWidgetViewModel;
22+
23+
public string WidgetAutomationProperties
24+
=> WidgetItemModel.AutomationProperties;
25+
26+
public bool ShowMenuFlyout
27+
=> WidgetItemModel.ShowMenuFlyout;
28+
29+
public MenuFlyoutItem MenuFlyoutItem
30+
=> WidgetItemModel.MenuFlyoutItem;
31+
1432
private object _WidgetControl;
1533
public object WidgetControl
1634
{
1735
get => _WidgetControl;
1836
set => SetProperty(ref _WidgetControl, value);
1937
}
2038

21-
public WidgetsListControlItemViewModel(object widgetControl, Action<bool> expanderValueChangedCallback, Func<bool> expanderValueRequestedCallback)
22-
{
23-
WidgetControl = widgetControl;
24-
_expanderValueChangedCallback = expanderValueChangedCallback;
25-
_expanderValueRequestedCallback = expanderValueRequestedCallback;
26-
}
27-
2839
public bool IsExpanded
2940
{
3041
get => _expanderValueRequestedCallback?.Invoke() ?? true;
@@ -35,26 +46,18 @@ public bool IsExpanded
3546
}
3647
}
3748

38-
public IWidgetItemModel WidgetItemModel
39-
{
40-
get => WidgetControl as IWidgetItemModel;
41-
}
49+
// Constructor
4250

43-
public string WidgetAutomationProperties
51+
public WidgetContainerItem(object widgetControl, Action<bool> expanderValueChangedCallback, Func<bool> expanderValueRequestedCallback)
4452
{
45-
get => WidgetItemModel.AutomationProperties;
46-
}
53+
_expanderValueChangedCallback = expanderValueChangedCallback;
54+
_expanderValueRequestedCallback = expanderValueRequestedCallback;
4755

48-
public bool ShowMenuFlyout
49-
{
50-
get => WidgetItemModel.ShowMenuFlyout;
51-
}
52-
53-
public MenuFlyoutItem MenuFlyoutItem
54-
{
55-
get => WidgetItemModel.MenuFlyoutItem;
56+
WidgetControl = widgetControl;
5657
}
5758

59+
// Disposer
60+
5861
public void Dispose()
5962
{
6063
(WidgetControl as IDisposable)?.Dispose();
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
// Copyright (c) 2023 Files Community
2+
// Licensed under the MIT License. See the LICENSE.
3+
4+
using Microsoft.UI.Xaml.Media.Imaging;
5+
6+
namespace Files.App.Data.Items
7+
{
8+
public class WidgetDriveCardItem : WidgetCardItem, IWidgetCardItem<DriveItem>, IComparable<WidgetDriveCardItem>
9+
{
10+
private byte[] thumbnailData;
11+
12+
public new DriveItem Item { get; private set; }
13+
14+
private BitmapImage thumbnail;
15+
public BitmapImage Thumbnail
16+
{
17+
get => thumbnail;
18+
set => SetProperty(ref thumbnail, value);
19+
}
20+
21+
public WidgetDriveCardItem(DriveItem item)
22+
{
23+
Item = item;
24+
Path = item.Path;
25+
}
26+
27+
public async Task LoadCardThumbnailAsync()
28+
{
29+
thumbnailData = await FileThumbnailHelper.LoadIconWithoutOverlayAsync(Item.Path, Constants.DefaultIconSizes.Jumbo, true, true);
30+
31+
// Thumbnail data is valid, set the item icon
32+
if (thumbnailData is not null && thumbnailData.Length > 0)
33+
Thumbnail = await MainWindow.Instance.DispatcherQueue.EnqueueOrInvokeAsync(() => thumbnailData.ToBitmapAsync(), Microsoft.UI.Dispatching.DispatcherQueuePriority.Low);
34+
}
35+
36+
public int CompareTo(WidgetDriveCardItem? other)
37+
=> Item.Path.CompareTo(other?.Item?.Path);
38+
}
39+
}

0 commit comments

Comments
 (0)