Skip to content

Commit

Permalink
Code Quality: Introduced SideBarContext (files-community#14116)
Browse files Browse the repository at this point in the history
  • Loading branch information
0x5bfa committed Dec 19, 2023
1 parent e3f093b commit 2e9bdac
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 6 deletions.
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 @@ await foreach (var taggedItem in fileTagsService.GetItemsForTagAsync(tagItem.Fil
}

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

0 comments on commit 2e9bdac

Please sign in to comment.