From 2e9bdac77573c5184a0bb2309631ee228651b236 Mon Sep 17 00:00:00 2001
From: 0x5bfa <62196528+0x5bfa@users.noreply.github.com>
Date: Wed, 20 Dec 2023 02:04:53 +0900
Subject: [PATCH] Code Quality: Introduced SideBarContext (#14116)
---
.../Data/Contexts/SideBar/ISideBarContext.cs | 31 +++++++++++++
.../Data/Contexts/SideBar/SideBarContext.cs | 46 +++++++++++++++++++
.../UserControls/SidebarViewModel.cs | 24 +++++++---
3 files changed, 95 insertions(+), 6 deletions(-)
create mode 100644 src/Files.App/Data/Contexts/SideBar/ISideBarContext.cs
create mode 100644 src/Files.App/Data/Contexts/SideBar/SideBarContext.cs
diff --git a/src/Files.App/Data/Contexts/SideBar/ISideBarContext.cs b/src/Files.App/Data/Contexts/SideBar/ISideBarContext.cs
new file mode 100644
index 000000000000..73897d0ddd65
--- /dev/null
+++ b/src/Files.App/Data/Contexts/SideBar/ISideBarContext.cs
@@ -0,0 +1,31 @@
+// Copyright (c) 2023 Files Community
+// Licensed under the MIT License. See the LICENSE.
+
+namespace Files.App.Data.Contexts
+{
+ ///
+ /// Represents context for .
+ ///
+ public interface ISidebarContext
+ {
+ ///
+ /// Gets the last sidebar right clicked item
+ ///
+ INavigationControlItem? RightClickedItem { get; }
+
+ ///
+ /// Gets the value that indicates whether any item has been right clicked
+ ///
+ bool IsItemRightClicked { get; }
+
+ ///
+ /// Gets the value that indicates whether right clicked item is a favorite item
+ ///
+ bool IsFavoriteItem { get; }
+
+ ///
+ /// Gets the drive item to open if any
+ ///
+ DriveItem? OpenDriveItem { get; }
+ }
+}
diff --git a/src/Files.App/Data/Contexts/SideBar/SideBarContext.cs b/src/Files.App/Data/Contexts/SideBar/SideBarContext.cs
new file mode 100644
index 000000000000..3a8f2f8f03dc
--- /dev/null
+++ b/src/Files.App/Data/Contexts/SideBar/SideBarContext.cs
@@ -0,0 +1,46 @@
+// Copyright (c) 2023 Files Community
+// Licensed under the MIT License. See the LICENSE.
+
+namespace Files.App.Data.Contexts
+{
+ ///
+ internal class SidebarContext : ObservableObject, ISidebarContext
+ {
+ private readonly SidebarPinnedModel favoriteModel = App.QuickAccessManager.Model;
+
+ private int FavoriteIndex =>
+ IsItemRightClicked
+ ? favoriteModel.IndexOfItem(_RightClickedItem!)
+ : -1;
+
+ private INavigationControlItem? _RightClickedItem = null;
+ public INavigationControlItem? RightClickedItem => _RightClickedItem;
+
+ public bool IsItemRightClicked =>
+ _RightClickedItem is not null;
+
+ public bool IsFavoriteItem =>
+ IsItemRightClicked &&
+ _RightClickedItem!.Section is SectionType.Favorites &&
+ FavoriteIndex is not -1;
+
+ public DriveItem? OpenDriveItem
+ => _RightClickedItem as DriveItem;
+
+ public SidebarContext()
+ {
+ SidebarViewModel.RightClickedItemChanged += SidebarControl_RightClickedItemChanged;
+ }
+
+ public void SidebarControl_RightClickedItemChanged(object? sender, INavigationControlItem? e)
+ {
+ if (SetProperty(ref _RightClickedItem, e, nameof(RightClickedItem)))
+ {
+ OnPropertyChanged(nameof(IsItemRightClicked));
+ OnPropertyChanged(nameof(FavoriteIndex));
+ OnPropertyChanged(nameof(IsFavoriteItem));
+ OnPropertyChanged(nameof(OpenDriveItem));
+ }
+ }
+ }
+}
diff --git a/src/Files.App/ViewModels/UserControls/SidebarViewModel.cs b/src/Files.App/ViewModels/UserControls/SidebarViewModel.cs
index 8c017af8628e..162e7444f939 100644
--- a/src/Files.App/ViewModels/UserControls/SidebarViewModel.cs
+++ b/src/Files.App/ViewModels/UserControls/SidebarViewModel.cs
@@ -71,6 +71,7 @@ public SidebarDisplayMode SidebarDisplayMode
public delegate void SelectedTagChangedEventHandler(object sender, SelectedTagChangedEventArgs e);
public static event SelectedTagChangedEventHandler? SelectedTagChanged;
+ public static event EventHandler? RightClickedItemChanged;
private readonly SectionType[] SectionOrder =
new SectionType[]
@@ -663,12 +664,14 @@ public void UpdateTabControlMargin()
public async void HandleItemContextInvokedAsync(object sender, ItemContextInvokedArgs args)
{
- if (sender is not FrameworkElement sidebarItem) return;
+ if (sender is not FrameworkElement sidebarItem)
+ return;
if (args.Item is not INavigationControlItem item)
{
// We are in the pane context requested path
PaneFlyout.ShowAt(sender as FrameworkElement, args.Position);
+
return;
}
@@ -688,20 +691,29 @@ public async void HandleItemContextInvokedAsync(object sender, ItemContextInvoke
}
rightClickedItem = item;
- var itemContextMenuFlyout = new CommandBarFlyout { Placement = FlyoutPlacementMode.Full };
+ RightClickedItemChanged?.Invoke(this, item);
+
+ var itemContextMenuFlyout = new CommandBarFlyout()
+ {
+ Placement = FlyoutPlacementMode.Full
+ };
+
itemContextMenuFlyout.Opening += (sender, e) => App.LastOpenedFlyout = sender as CommandBarFlyout;
+ itemContextMenuFlyout.Closed += (sender, e) => RightClickedItemChanged?.Invoke(this, null);
var menuItems = GetLocationItemMenuItems(item, itemContextMenuFlyout);
var (_, secondaryElements) = ItemModelListToContextFlyoutHelper.GetAppBarItemsFromModel(menuItems);
- secondaryElements.OfType()
- .ForEach(i => i.MinWidth = Constants.UI.ContextMenuItemsMaxWidth);
+ secondaryElements
+ .OfType()
+ .ForEach(i => i.MinWidth = Constants.UI.ContextMenuItemsMaxWidth);
+
+ secondaryElements.ForEach(itemContextMenuFlyout.SecondaryCommands.Add);
- secondaryElements.ForEach(i => itemContextMenuFlyout.SecondaryCommands.Add(i));
if (item.MenuOptions.ShowShellItems)
itemContextMenuFlyout.Opened += ItemContextMenuFlyout_Opened;
- itemContextMenuFlyout.ShowAt(sidebarItem, new FlyoutShowOptions { Position = args.Position });
+ itemContextMenuFlyout.ShowAt(sidebarItem, new() { Position = args.Position });
}
private async void ItemContextMenuFlyout_Opened(object? sender, object e)