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

Code Quality: Improved Widget code #14458

Merged
merged 15 commits into from
Feb 8, 2024
2 changes: 1 addition & 1 deletion src/Files.App/Actions/FileSystem/FormatDriveAction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public string Description
public bool IsExecutable =>
context.HasItem &&
!context.HasSelection &&
(drivesViewModel.Drives.Cast<DriveItem>().FirstOrDefault(x =>
(drivesViewModel.Drives.Cast<SideBarDriveItem>().FirstOrDefault(x =>
string.Equals(x.Path, context.Folder?.ItemPath))?.MenuOptions.ShowFormatDrive ?? false);

public FormatDriveAction()
Expand Down
11 changes: 5 additions & 6 deletions src/Files.App/Data/Contexts/HomePage/HomePageContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,14 @@
// Licensed under the MIT License. See the LICENSE.

using Files.App.UserControls.Widgets;
using Files.App.ViewModels.Widgets;
using Microsoft.UI.Xaml.Controls;
using System.Collections.Immutable;

namespace Files.App.Data.Contexts
{
internal class HomePageContext : ObservableObject, IHomePageContext
{
private static readonly IImmutableList<FileTagsItemViewModel> emptyTaggedItems = Enumerable.Empty<FileTagsItemViewModel>().ToImmutableList();
private static readonly IImmutableList<WidgetFileTagCardItem> emptyTaggedItems = Enumerable.Empty<WidgetFileTagCardItem>().ToImmutableList();

public bool IsAnyItemRightClicked => rightClickedItem is not null;

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

private IReadOnlyList<FileTagsItemViewModel> selectedTaggedItems = emptyTaggedItems;
public IReadOnlyList<FileTagsItemViewModel> SelectedTaggedItems
private IReadOnlyList<WidgetFileTagCardItem> selectedTaggedItems = emptyTaggedItems;
public IReadOnlyList<WidgetFileTagCardItem> SelectedTaggedItems
{
get => selectedTaggedItems;
set => selectedTaggedItems = value ?? emptyTaggedItems;
}

public HomePageContext()
{
HomePageWidget.RightClickedItemChanged += HomePageWidget_RightClickedItemChanged;
BaseWidgetViewModel.RightClickedItemChanged += HomePageWidget_RightClickedItemChanged;
FileTagsWidget.SelectedTaggedItemsChanged += FileTagsWidget_SelectedTaggedItemsChanged;
}

private void FileTagsWidget_SelectedTaggedItemsChanged(object? sender, IEnumerable<FileTagsItemViewModel> e)
private void FileTagsWidget_SelectedTaggedItemsChanged(object? sender, IEnumerable<WidgetFileTagCardItem> e)
{
SetProperty(ref selectedTaggedItems, e.ToList());
}
Expand Down
4 changes: 1 addition & 3 deletions src/Files.App/Data/Contexts/HomePage/IHomePageContext.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
// Copyright (c) 2023 Files Community
// Licensed under the MIT License. See the LICENSE.

using Files.App.UserControls.Widgets;
using Files.App.ViewModels.Widgets;
using Microsoft.UI.Xaml.Controls;

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

/// <summary>
/// Tells whether any item has been right clicked
Expand Down
2 changes: 1 addition & 1 deletion src/Files.App/Data/Contexts/SideBar/ISideBarContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,6 @@ public interface ISidebarContext
/// <summary>
/// Gets the drive item to open if any
/// </summary>
DriveItem? OpenDriveItem { get; }
SideBarDriveItem? OpenDriveItem { get; }
}
}
4 changes: 2 additions & 2 deletions src/Files.App/Data/Contexts/SideBar/SideBarContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ internal class SidebarContext : ObservableObject, ISidebarContext
_RightClickedItem!.Section is SectionType.Favorites &&
FavoriteIndex is not -1;

public DriveItem? OpenDriveItem
=> _RightClickedItem as DriveItem;
public SideBarDriveItem? OpenDriveItem
=> _RightClickedItem as SideBarDriveItem;

public SidebarContext()
{
Expand Down
5 changes: 2 additions & 3 deletions src/Files.App/Data/Contexts/Tags/TagsContext.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
// Copyright (c) 2023 Files Community
// Licensed under the MIT License. See the LICENSE.

using Files.App.ViewModels.Widgets;
using System.Collections.Immutable;

namespace Files.App.Data.Contexts
{
sealed class TagsContext : ITagsContext
sealed class TagsContext : ITagsContext
{
private static readonly IReadOnlyList<(string path, bool isFolder)> _emptyTaggedItemsList
= Enumerable.Empty<(string path, bool isFolder)>().ToImmutableList();
Expand All @@ -29,7 +28,7 @@ sealed class TagsContext : ITagsContext

public TagsContext()
{
FileTagsContainerViewModel.SelectedTagChanged += SelectedTagsChanged;
WidgetFileTagsContainerItem.SelectedTagChanged += SelectedTagsChanged;
SidebarViewModel.SelectedTagChanged += SelectedTagsChanged;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,8 @@
// Licensed under the MIT License. See the LICENSE.

using Microsoft.UI.Xaml.Media.Imaging;
using System.Threading.Tasks;

namespace Files.App.UserControls.Widgets
namespace Files.App.Data.Contracts
{
public interface IWidgetCardItem<T>
{
Expand All @@ -16,4 +15,4 @@ public interface IWidgetCardItem<T>

Task LoadCardThumbnailAsync();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@

using Microsoft.UI.Xaml.Controls;

namespace Files.App.ViewModels.Widgets
namespace Files.App.Data.Contracts
{
public interface IWidgetItem : IDisposable
public interface IWidgetViewModel : IDisposable
{
string WidgetName { get; }

Expand Down
38 changes: 38 additions & 0 deletions src/Files.App/Data/EventArguments/QuickAccessCardEventArgs.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
// Copyright (c) 2023 Files Community
// Licensed under the MIT License. See the LICENSE.

namespace Files.App.Data.EventArguments
{
public class QuickAccessCardEventArgs : EventArgs
{
public SideBarLocationItem? Item { get; set; }
}

public class QuickAccessCardInvokedEventArgs : EventArgs
{
public string? Path { get; set; }
}

public class ModifyQuickAccessEventArgs : EventArgs
{
public string[]? Paths { get; set; }
public ShellFileItem[]? Items { get; set; }
public bool Add;
public bool Pin = true;
public bool Reset = false;
public bool Reorder = false;

public ModifyQuickAccessEventArgs(string[] paths, bool add)
{
Paths = paths;
Add = add;
}

public ModifyQuickAccessEventArgs(ShellFileItem[] items, bool add)
{
Paths = items.Select(x => x.FilePath).ToArray();
Items = items;
Add = add;
}
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
// Copyright (c) 2023 Files Community
// Licensed under the MIT License. See the LICENSE.

using Files.App.UserControls.Widgets;
using Microsoft.UI.Xaml.Controls;

namespace Files.App.Data.EventArguments
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ public static ObservableCollection<NavigationViewItemButtonStyleItem> Initialize
if (!compatibilityItemEnabled)
PropertiesNavigationViewItems.Remove(compatibilityItem);
}
else if (item is DriveItem)
else if (item is SideBarDriveItem)
{
PropertiesNavigationViewItems.Remove(hashesItem);
PropertiesNavigationViewItems.Remove(shortcutItem);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

namespace Files.App.Data.Items
{
public class DriveItem : ObservableObject, INavigationControlItem, ILocatableFolder
public class SideBarDriveItem : ObservableObject, INavigationControlItem, ILocatableFolder
{
private BitmapImage icon;
public BitmapImage Icon
Expand Down Expand Up @@ -230,9 +230,9 @@ private async void ItemDecorator_Click(object sender, RoutedEventArgs e)
await UIHelpers.ShowDeviceEjectResultAsync(Type, result);
}

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

if (imageStream is not null)
item.IconData = await imageStream.ToByteArrayAsync();
Expand Down Expand Up @@ -310,7 +310,7 @@ public async Task UpdatePropertiesAsync()

public int CompareTo(INavigationControlItem other)
{
var result = Type.CompareTo((other as DriveItem)?.Type ?? Type);
var result = Type.CompareTo((other as SideBarDriveItem)?.Type ?? Type);
return result == 0 ? Text.CompareTo(other.Text) : result;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

namespace Files.App.Data.Items
{
public class FileTagItem : ObservableObject, INavigationControlItem
public class SideBarFileTagItem : ObservableObject, INavigationControlItem
{
public string Text { get; set; }

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

namespace Files.App.Data.Items
{
public class LocationItem : ObservableObject, INavigationControlItem
public class SideBarLocationItem : ObservableObject, INavigationControlItem
{
public BitmapImage icon;
public BitmapImage Icon
Expand Down Expand Up @@ -115,13 +115,13 @@ public FrameworkElement? ItemDecorator
public int CompareTo(INavigationControlItem other)
=> Text.CompareTo(other.Text);

public static T Create<T>() where T : LocationItem, new()
public static T Create<T>() where T : SideBarLocationItem, new()
{
return new T();
}
}

public class RecycleBinLocationItem : LocationItem
public class RecycleBinLocationItem : SideBarLocationItem
{
public void RefreshSpaceUsed(object sender, FileSystemEventArgs e)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

namespace Files.App.Data.Items
{
public class WslDistroItem : ObservableObject, INavigationControlItem
public class SideBarWSLItem : ObservableObject, INavigationControlItem
{
public string Text { get; set; }

Expand Down
1 change: 0 additions & 1 deletion src/Files.App/Data/Items/TagsListItem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@

namespace Files.App.Data.Items
{

public class TagsListItem
{
public bool IsTag
Expand Down
15 changes: 15 additions & 0 deletions src/Files.App/Data/Items/WidgetCardItem.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// Copyright (c) 2023 Files Community
// Licensed under the MIT License. See the LICENSE.

namespace Files.App.Data.Items
{
/// <summary>
/// Represents base item for widget card item.
/// </summary>
public abstract class WidgetCardItem : ObservableObject
{
public virtual string? Path { get; set; }

public virtual object? Item { get; set; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,28 +3,39 @@

using Microsoft.UI.Xaml.Controls;

namespace Files.App.ViewModels.Widgets
namespace Files.App.Data.Items
{
public class WidgetsListControlItemViewModel : ObservableObject, IDisposable
/// <summary>
/// Represents an item of Files widget container.
/// </summary>
public class WidgetContainerItem : ObservableObject, IDisposable
{
private readonly Action<bool> _expanderValueChangedCallback;
// Fields

private readonly Action<bool> _expanderValueChangedCallback;
private readonly Func<bool> _expanderValueRequestedCallback;

// Properties

public IWidgetViewModel WidgetItemModel
=> WidgetControl as IWidgetViewModel;

public string WidgetAutomationProperties
=> WidgetItemModel.AutomationProperties;

public bool ShowMenuFlyout
=> WidgetItemModel.ShowMenuFlyout;

public MenuFlyoutItem MenuFlyoutItem
=> WidgetItemModel.MenuFlyoutItem;

private object _WidgetControl;
public object WidgetControl
{
get => _WidgetControl;
set => SetProperty(ref _WidgetControl, value);
}

public WidgetsListControlItemViewModel(object widgetControl, Action<bool> expanderValueChangedCallback, Func<bool> expanderValueRequestedCallback)
{
WidgetControl = widgetControl;
_expanderValueChangedCallback = expanderValueChangedCallback;
_expanderValueRequestedCallback = expanderValueRequestedCallback;
}

public bool IsExpanded
{
get => _expanderValueRequestedCallback?.Invoke() ?? true;
Expand All @@ -35,26 +46,18 @@ public bool IsExpanded
}
}

public IWidgetItemModel WidgetItemModel
{
get => WidgetControl as IWidgetItemModel;
}
// Constructor

public string WidgetAutomationProperties
public WidgetContainerItem(object widgetControl, Action<bool> expanderValueChangedCallback, Func<bool> expanderValueRequestedCallback)
{
get => WidgetItemModel.AutomationProperties;
}
_expanderValueChangedCallback = expanderValueChangedCallback;
_expanderValueRequestedCallback = expanderValueRequestedCallback;

public bool ShowMenuFlyout
{
get => WidgetItemModel.ShowMenuFlyout;
}

public MenuFlyoutItem MenuFlyoutItem
{
get => WidgetItemModel.MenuFlyoutItem;
WidgetControl = widgetControl;
}

// Disposer

public void Dispose()
{
(WidgetControl as IDisposable)?.Dispose();
Expand Down
Loading