forked from files-community/Files
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Code Quality: Introduced SideBarContext (files-community#14116)
- Loading branch information
Showing
3 changed files
with
95 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters