Skip to content

Code Quality: Introduced SideBarContext #14116

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

Merged
merged 2 commits into from
Dec 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions src/Files.App/Data/Contexts/SideBar/ISideBarContext.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// Copyright (c) 2023 Files Community
// Licensed under the MIT License. See the LICENSE.

namespace Files.App.Data.Contexts
{
/// <summary>
/// Represents context for <see cref="UserControls.Sidebar.SidebarView"/>.
/// </summary>
public interface ISidebarContext
{
/// <summary>
/// Gets the last sidebar right clicked item
/// </summary>
INavigationControlItem? RightClickedItem { get; }

/// <summary>
/// Gets the value that indicates whether any item has been right clicked
/// </summary>
bool IsItemRightClicked { get; }

/// <summary>
/// Gets the value that indicates whether right clicked item is a favorite item
/// </summary>
bool IsFavoriteItem { get; }

/// <summary>
/// Gets the drive item to open if any
/// </summary>
DriveItem? OpenDriveItem { get; }
}
}
46 changes: 46 additions & 0 deletions src/Files.App/Data/Contexts/SideBar/SideBarContext.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
// Copyright (c) 2023 Files Community
// Licensed under the MIT License. See the LICENSE.

namespace Files.App.Data.Contexts
{
/// <inheritdoc cref="ISidebarContext"/>
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));
}
}
}
}
24 changes: 18 additions & 6 deletions src/Files.App/ViewModels/UserControls/SidebarViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ public SidebarDisplayMode SidebarDisplayMode
public delegate void SelectedTagChangedEventHandler(object sender, SelectedTagChangedEventArgs e);

public static event SelectedTagChangedEventHandler? SelectedTagChanged;
public static event EventHandler<INavigationControlItem?>? RightClickedItemChanged;

private readonly SectionType[] SectionOrder =
new SectionType[]
Expand Down Expand Up @@ -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;
}

Expand All @@ -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<FrameworkElement>()
.ForEach(i => i.MinWidth = Constants.UI.ContextMenuItemsMaxWidth);
secondaryElements
.OfType<FrameworkElement>()
.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)
Expand Down