Skip to content
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

Added context menu options to rearrange Favorites item #5979

Merged
merged 12 commits into from
Aug 31, 2021
71 changes: 31 additions & 40 deletions Files/DataModels/SidebarPinnedModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ public void RemoveItem(string item)
}

/// <summary>
/// Moves the location item in the navigation sidebar from the old position to the new position
/// Moves the location item in the Favorites sidebar section from the old position to the new position
/// </summary>
/// <param name="locationItem">Location item to move</param>
/// <param name="oldIndex">The old position index of the location item</param>
Expand All @@ -172,10 +172,33 @@ public bool MoveItem(INavigationControlItem locationItem, int oldIndex, int newI
return false;
}

if (oldIndex >= 0 && newIndex >= 0)
if (oldIndex >= 1 && newIndex >= 1 && newIndex <= FavoriteItems.Count())
{
favoriteSection.ChildItems.RemoveAt(oldIndex);
favoriteSection.ChildItems.Insert(newIndex, locationItem);
// A backup of the items, because the swapping of items requires removing and inserting them in the correct position
var sidebarItemsBackup = new List<string>(FavoriteItems);

try
{
FavoriteItems.RemoveAt(oldIndex - 1);
FavoriteItems.Insert(newIndex - 1, locationItem.Path);
favoriteSection.ChildItems.RemoveAt(oldIndex);
favoriteSection.ChildItems.Insert(newIndex, locationItem);
Save();
}
catch (Exception ex) when (
ex is ArgumentException // Pinned item was invalid
|| ex is FileNotFoundException // Pinned item was deleted
|| ex is System.Runtime.InteropServices.COMException // Pinned item's drive was ejected
|| (uint)ex.HResult == 0x8007000F // The system cannot find the drive specified
|| (uint)ex.HResult == 0x800700A1) // The specified path is invalid (usually an mtp device was disconnected)
{
Debug.WriteLine($"An error occurred while moving pinned items in the Favorites sidebar section. {ex.Message}");
FavoriteItems = sidebarItemsBackup;
RemoveStaleSidebarItems();
_ = AddAllItemsToSidebar();
return false;
}

return true;
}

Expand All @@ -194,43 +217,11 @@ public void SwapItems(INavigationControlItem firstLocationItem, INavigationContr
return;
}

// A backup of the items, because the swapping of items requires removing and inserting them in the correct position
var sidebarItemsBackup = new List<string>(this.FavoriteItems);

try
{
var indexOfFirstItemInMainPage = IndexOfItem(firstLocationItem);
var indexOfSecondItemInMainPage = IndexOfItem(secondLocationItem);
var indexOfFirstItemInMainPage = IndexOfItem(firstLocationItem);
var indexOfSecondItemInMainPage = IndexOfItem(secondLocationItem);

// Moves the items in the MainPage
var result = MoveItem(firstLocationItem, indexOfFirstItemInMainPage, indexOfSecondItemInMainPage);

// Moves the items in this model and saves the model
if (result == true)
{
var indexOfFirstItemInModel = this.FavoriteItems.IndexOf(firstLocationItem.Path);
var indexOfSecondItemInModel = this.FavoriteItems.IndexOf(secondLocationItem.Path);
if (indexOfFirstItemInModel >= 0 && indexOfSecondItemInModel >= 0)
{
this.FavoriteItems.RemoveAt(indexOfFirstItemInModel);
this.FavoriteItems.Insert(indexOfSecondItemInModel, firstLocationItem.Path);
}

Save();
}
}
catch (Exception ex) when (
ex is ArgumentException // Pinned item was invalid
|| ex is FileNotFoundException // Pinned item was deleted
|| ex is System.Runtime.InteropServices.COMException // Pinned item's drive was ejected
|| (uint)ex.HResult == 0x8007000F // The system cannot find the drive specified
|| (uint)ex.HResult == 0x800700A1) // The specified path is invalid (usually an mtp device was disconnected)
{
Debug.WriteLine($"An error occurred while swapping pinned items in the navigation sidebar. {ex.Message}");
this.FavoriteItems = sidebarItemsBackup;
this.RemoveStaleSidebarItems();
_ = this.AddAllItemsToSidebar();
}
// Moves the items in the MainPage
MoveItem(firstLocationItem, indexOfFirstItemInMainPage, indexOfSecondItemInMainPage);
}

/// <summary>
Expand Down
14 changes: 13 additions & 1 deletion Files/Strings/en-US/Resources.resw
Original file line number Diff line number Diff line change
Expand Up @@ -2848,4 +2848,16 @@ We use App Center to keep track of app usage, find bugs, and fix crashes. All in
<data name="OngoingTasks" xml:space="preserve">
<value>Ongoing Tasks</value>
</data>
</root>
<data name="SideBarFavoritesMoveOneDown" xml:space="preserve">
<value>Move one down</value>
</data>
<data name="SideBarFavoritesMoveOneUp" xml:space="preserve">
<value>Move one up</value>
</data>
<data name="SideBarFavoritesMoveToBottom" xml:space="preserve">
<value>Move to bottom</value>
</data>
<data name="SideBarFavoritesMoveToTop" xml:space="preserve">
<value>Move to top</value>
</data>
</root>
124 changes: 124 additions & 0 deletions Files/UserControls/SidebarControl.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,10 @@ public bool CanOpenInNewPane
}
}

public bool ShowMoveItemUp { get; set; }

public bool ShowMoveItemDown { get; set; }

public bool ShowUnpinItem { get; set; }

public bool ShowHideSection { get; set; }
Expand Down Expand Up @@ -186,6 +190,90 @@ public void UnpinItem_Click(object sender, RoutedEventArgs e)
}
}

public void MoveItemToTop_Click(object sender, RoutedEventArgs e)
{
if (RightClickedItem.Section == SectionType.Favorites)
{
bool isSelectedSidebarItem = false;

if (SelectedSidebarItem == RightClickedItem)
{
isSelectedSidebarItem = true;
}

int oldIndex = App.SidebarPinnedController.Model.IndexOfItem(RightClickedItem);
App.SidebarPinnedController.Model.MoveItem(RightClickedItem, oldIndex, 1);

if (isSelectedSidebarItem)
{
SetValue(SelectedSidebarItemProperty, RightClickedItem);
}
}
}

public void MoveItemUp_Click(object sender, RoutedEventArgs e)
{
if (RightClickedItem.Section == SectionType.Favorites)
{
bool isSelectedSidebarItem = false;

if (SelectedSidebarItem == RightClickedItem)
{
isSelectedSidebarItem = true;
}

int oldIndex = App.SidebarPinnedController.Model.IndexOfItem(RightClickedItem);
App.SidebarPinnedController.Model.MoveItem(RightClickedItem, oldIndex, oldIndex - 1);

if (isSelectedSidebarItem)
{
SetValue(SelectedSidebarItemProperty, RightClickedItem);
}
}
}

public void MoveItemDown_Click(object sender, RoutedEventArgs e)
{
if (RightClickedItem.Section == SectionType.Favorites)
{
bool isSelectedSidebarItem = false;

if (SelectedSidebarItem == RightClickedItem)
{
isSelectedSidebarItem = true;
}

int oldIndex = App.SidebarPinnedController.Model.IndexOfItem(RightClickedItem);
App.SidebarPinnedController.Model.MoveItem(RightClickedItem, oldIndex, oldIndex + 1);

if (isSelectedSidebarItem)
{
SetValue(SelectedSidebarItemProperty, RightClickedItem);
}
}
}

public void MoveItemToBottom_Click(object sender, RoutedEventArgs e)
{
if (RightClickedItem.Section == SectionType.Favorites)
{
bool isSelectedSidebarItem = false;

if (SelectedSidebarItem == RightClickedItem)
{
isSelectedSidebarItem = true;
}

int oldIndex = App.SidebarPinnedController.Model.IndexOfItem(RightClickedItem);
App.SidebarPinnedController.Model.MoveItem(RightClickedItem, oldIndex, App.SidebarPinnedController.Model.FavoriteItems.Count());

if (isSelectedSidebarItem)
{
SetValue(SelectedSidebarItemProperty, RightClickedItem);
}
}
}

public static GridLength GetSidebarCompactSize()
{
if (App.Current.Resources.TryGetValue("NavigationViewCompactPaneLength", out object paneLength))
Expand Down Expand Up @@ -262,6 +350,8 @@ private void NavigationViewLocationItem_RightTapped(object sender, RightTappedRo
ShowProperties = true;
IsLibrariesHeader = false;
ShowUnpinItem = ((library || favorite) && !item.IsDefaultLocation);
ShowMoveItemUp = ShowUnpinItem && App.SidebarPinnedController.Model.IndexOfItem(item) > 1;
ShowMoveItemDown = ShowUnpinItem && App.SidebarPinnedController.Model.IndexOfItem(item) < App.SidebarPinnedController.Model.FavoriteItems.Count();
ShowHideSection = false;
ShowEjectDevice = false;

Expand Down Expand Up @@ -295,6 +385,8 @@ private void NavigationViewLocationItem_RightTapped(object sender, RightTappedRo
ShowProperties = false;
IsLibrariesHeader = librariesHeader;
ShowUnpinItem = false;
ShowMoveItemUp = false;
ShowMoveItemDown = false;
ShowHideSection = true;
ShowEjectDevice = false;
ShowEmptyRecycleBin = false;
Expand All @@ -319,6 +411,8 @@ private void NavigationViewDriveItem_RightTapped(object sender, RightTappedRoute
IsLibrariesHeader = false;
ShowEjectDevice = item.IsRemovable;
ShowUnpinItem = false;
ShowMoveItemUp = false;
ShowMoveItemDown = false;
ShowEmptyRecycleBin = false;
ShowProperties = true;
ShowHideSection = false;
Expand All @@ -344,6 +438,8 @@ private void NavigationViewWSLItem_RightTapped(object sender, RightTappedRoutedE
IsLibrariesHeader = false;
ShowEjectDevice = false;
ShowUnpinItem = false;
ShowMoveItemUp = false;
ShowMoveItemDown = false;
ShowEmptyRecycleBin = false;
ShowProperties = false;
ShowHideSection = false;
Expand Down Expand Up @@ -930,6 +1026,34 @@ public List<ContextMenuFlyoutItemViewModel> GetLocationItemMenuItems()
ShowItem = IsLocationItem
},
new ContextMenuFlyoutItemViewModel()
{
Text = "SideBarFavoritesMoveToTop".GetLocalized(),
Glyph = "\uE11C",
Command = new RelayCommand(() => MoveItemToTop_Click(null, null)),
ShowItem = ShowMoveItemUp
},
new ContextMenuFlyoutItemViewModel()
{
Text = "SideBarFavoritesMoveOneUp".GetLocalized(),
Glyph = "\uE70E",
Command = new RelayCommand(() => MoveItemUp_Click(null, null)),
ShowItem = ShowMoveItemUp
},
new ContextMenuFlyoutItemViewModel()
{
Text = "SideBarFavoritesMoveOneDown".GetLocalized(),
Glyph = "\uE70D",
Command = new RelayCommand(() => MoveItemDown_Click(null, null)),
ShowItem = ShowMoveItemDown
},
new ContextMenuFlyoutItemViewModel()
{
Text = "SideBarFavoritesMoveToBottom".GetLocalized(),
Glyph = "\uE118",
Command = new RelayCommand(() => MoveItemToBottom_Click(null, null)),
ShowItem = ShowMoveItemDown
},
new ContextMenuFlyoutItemViewModel()
{
Text = "SideBarUnpinFromFavorites/Text".GetLocalized(),
Glyph = "\uE77A",
Expand Down