Skip to content

Commit 2e9bdac

Browse files
authored
Code Quality: Introduced SideBarContext (#14116)
1 parent e3f093b commit 2e9bdac

File tree

3 files changed

+95
-6
lines changed

3 files changed

+95
-6
lines changed
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// Copyright (c) 2023 Files Community
2+
// Licensed under the MIT License. See the LICENSE.
3+
4+
namespace Files.App.Data.Contexts
5+
{
6+
/// <summary>
7+
/// Represents context for <see cref="UserControls.Sidebar.SidebarView"/>.
8+
/// </summary>
9+
public interface ISidebarContext
10+
{
11+
/// <summary>
12+
/// Gets the last sidebar right clicked item
13+
/// </summary>
14+
INavigationControlItem? RightClickedItem { get; }
15+
16+
/// <summary>
17+
/// Gets the value that indicates whether any item has been right clicked
18+
/// </summary>
19+
bool IsItemRightClicked { get; }
20+
21+
/// <summary>
22+
/// Gets the value that indicates whether right clicked item is a favorite item
23+
/// </summary>
24+
bool IsFavoriteItem { get; }
25+
26+
/// <summary>
27+
/// Gets the drive item to open if any
28+
/// </summary>
29+
DriveItem? OpenDriveItem { get; }
30+
}
31+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
// Copyright (c) 2023 Files Community
2+
// Licensed under the MIT License. See the LICENSE.
3+
4+
namespace Files.App.Data.Contexts
5+
{
6+
/// <inheritdoc cref="ISidebarContext"/>
7+
internal class SidebarContext : ObservableObject, ISidebarContext
8+
{
9+
private readonly SidebarPinnedModel favoriteModel = App.QuickAccessManager.Model;
10+
11+
private int FavoriteIndex =>
12+
IsItemRightClicked
13+
? favoriteModel.IndexOfItem(_RightClickedItem!)
14+
: -1;
15+
16+
private INavigationControlItem? _RightClickedItem = null;
17+
public INavigationControlItem? RightClickedItem => _RightClickedItem;
18+
19+
public bool IsItemRightClicked =>
20+
_RightClickedItem is not null;
21+
22+
public bool IsFavoriteItem =>
23+
IsItemRightClicked &&
24+
_RightClickedItem!.Section is SectionType.Favorites &&
25+
FavoriteIndex is not -1;
26+
27+
public DriveItem? OpenDriveItem
28+
=> _RightClickedItem as DriveItem;
29+
30+
public SidebarContext()
31+
{
32+
SidebarViewModel.RightClickedItemChanged += SidebarControl_RightClickedItemChanged;
33+
}
34+
35+
public void SidebarControl_RightClickedItemChanged(object? sender, INavigationControlItem? e)
36+
{
37+
if (SetProperty(ref _RightClickedItem, e, nameof(RightClickedItem)))
38+
{
39+
OnPropertyChanged(nameof(IsItemRightClicked));
40+
OnPropertyChanged(nameof(FavoriteIndex));
41+
OnPropertyChanged(nameof(IsFavoriteItem));
42+
OnPropertyChanged(nameof(OpenDriveItem));
43+
}
44+
}
45+
}
46+
}

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

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ public SidebarDisplayMode SidebarDisplayMode
7171
public delegate void SelectedTagChangedEventHandler(object sender, SelectedTagChangedEventArgs e);
7272

7373
public static event SelectedTagChangedEventHandler? SelectedTagChanged;
74+
public static event EventHandler<INavigationControlItem?>? RightClickedItemChanged;
7475

7576
private readonly SectionType[] SectionOrder =
7677
new SectionType[]
@@ -663,12 +664,14 @@ public void UpdateTabControlMargin()
663664

664665
public async void HandleItemContextInvokedAsync(object sender, ItemContextInvokedArgs args)
665666
{
666-
if (sender is not FrameworkElement sidebarItem) return;
667+
if (sender is not FrameworkElement sidebarItem)
668+
return;
667669

668670
if (args.Item is not INavigationControlItem item)
669671
{
670672
// We are in the pane context requested path
671673
PaneFlyout.ShowAt(sender as FrameworkElement, args.Position);
674+
672675
return;
673676
}
674677

@@ -688,20 +691,29 @@ public async void HandleItemContextInvokedAsync(object sender, ItemContextInvoke
688691
}
689692

690693
rightClickedItem = item;
691-
var itemContextMenuFlyout = new CommandBarFlyout { Placement = FlyoutPlacementMode.Full };
694+
RightClickedItemChanged?.Invoke(this, item);
695+
696+
var itemContextMenuFlyout = new CommandBarFlyout()
697+
{
698+
Placement = FlyoutPlacementMode.Full
699+
};
700+
692701
itemContextMenuFlyout.Opening += (sender, e) => App.LastOpenedFlyout = sender as CommandBarFlyout;
702+
itemContextMenuFlyout.Closed += (sender, e) => RightClickedItemChanged?.Invoke(this, null);
693703

694704
var menuItems = GetLocationItemMenuItems(item, itemContextMenuFlyout);
695705
var (_, secondaryElements) = ItemModelListToContextFlyoutHelper.GetAppBarItemsFromModel(menuItems);
696706

697-
secondaryElements.OfType<FrameworkElement>()
698-
.ForEach(i => i.MinWidth = Constants.UI.ContextMenuItemsMaxWidth);
707+
secondaryElements
708+
.OfType<FrameworkElement>()
709+
.ForEach(i => i.MinWidth = Constants.UI.ContextMenuItemsMaxWidth);
710+
711+
secondaryElements.ForEach(itemContextMenuFlyout.SecondaryCommands.Add);
699712

700-
secondaryElements.ForEach(i => itemContextMenuFlyout.SecondaryCommands.Add(i));
701713
if (item.MenuOptions.ShowShellItems)
702714
itemContextMenuFlyout.Opened += ItemContextMenuFlyout_Opened;
703715

704-
itemContextMenuFlyout.ShowAt(sidebarItem, new FlyoutShowOptions { Position = args.Position });
716+
itemContextMenuFlyout.ShowAt(sidebarItem, new() { Position = args.Position });
705717
}
706718

707719
private async void ItemContextMenuFlyout_Opened(object? sender, object e)

0 commit comments

Comments
 (0)