Skip to content

Commit 4c363db

Browse files
authored
Added context menu options to rearrange Favorites item (#5979)
1 parent b0eca61 commit 4c363db

File tree

3 files changed

+168
-41
lines changed

3 files changed

+168
-41
lines changed

Files/DataModels/SidebarPinnedModel.cs

Lines changed: 31 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ public void RemoveItem(string item)
159159
}
160160

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

175-
if (oldIndex >= 0 && newIndex >= 0)
175+
if (oldIndex >= 1 && newIndex >= 1 && newIndex <= FavoriteItems.Count())
176176
{
177-
favoriteSection.ChildItems.RemoveAt(oldIndex);
178-
favoriteSection.ChildItems.Insert(newIndex, locationItem);
177+
// A backup of the items, because the swapping of items requires removing and inserting them in the correct position
178+
var sidebarItemsBackup = new List<string>(FavoriteItems);
179+
180+
try
181+
{
182+
FavoriteItems.RemoveAt(oldIndex - 1);
183+
FavoriteItems.Insert(newIndex - 1, locationItem.Path);
184+
favoriteSection.ChildItems.RemoveAt(oldIndex);
185+
favoriteSection.ChildItems.Insert(newIndex, locationItem);
186+
Save();
187+
}
188+
catch (Exception ex) when (
189+
ex is ArgumentException // Pinned item was invalid
190+
|| ex is FileNotFoundException // Pinned item was deleted
191+
|| ex is System.Runtime.InteropServices.COMException // Pinned item's drive was ejected
192+
|| (uint)ex.HResult == 0x8007000F // The system cannot find the drive specified
193+
|| (uint)ex.HResult == 0x800700A1) // The specified path is invalid (usually an mtp device was disconnected)
194+
{
195+
Debug.WriteLine($"An error occurred while moving pinned items in the Favorites sidebar section. {ex.Message}");
196+
FavoriteItems = sidebarItemsBackup;
197+
RemoveStaleSidebarItems();
198+
_ = AddAllItemsToSidebar();
199+
return false;
200+
}
201+
179202
return true;
180203
}
181204

@@ -194,43 +217,11 @@ public void SwapItems(INavigationControlItem firstLocationItem, INavigationContr
194217
return;
195218
}
196219

197-
// A backup of the items, because the swapping of items requires removing and inserting them in the correct position
198-
var sidebarItemsBackup = new List<string>(this.FavoriteItems);
199-
200-
try
201-
{
202-
var indexOfFirstItemInMainPage = IndexOfItem(firstLocationItem);
203-
var indexOfSecondItemInMainPage = IndexOfItem(secondLocationItem);
220+
var indexOfFirstItemInMainPage = IndexOfItem(firstLocationItem);
221+
var indexOfSecondItemInMainPage = IndexOfItem(secondLocationItem);
204222

205-
// Moves the items in the MainPage
206-
var result = MoveItem(firstLocationItem, indexOfFirstItemInMainPage, indexOfSecondItemInMainPage);
207-
208-
// Moves the items in this model and saves the model
209-
if (result == true)
210-
{
211-
var indexOfFirstItemInModel = this.FavoriteItems.IndexOf(firstLocationItem.Path);
212-
var indexOfSecondItemInModel = this.FavoriteItems.IndexOf(secondLocationItem.Path);
213-
if (indexOfFirstItemInModel >= 0 && indexOfSecondItemInModel >= 0)
214-
{
215-
this.FavoriteItems.RemoveAt(indexOfFirstItemInModel);
216-
this.FavoriteItems.Insert(indexOfSecondItemInModel, firstLocationItem.Path);
217-
}
218-
219-
Save();
220-
}
221-
}
222-
catch (Exception ex) when (
223-
ex is ArgumentException // Pinned item was invalid
224-
|| ex is FileNotFoundException // Pinned item was deleted
225-
|| ex is System.Runtime.InteropServices.COMException // Pinned item's drive was ejected
226-
|| (uint)ex.HResult == 0x8007000F // The system cannot find the drive specified
227-
|| (uint)ex.HResult == 0x800700A1) // The specified path is invalid (usually an mtp device was disconnected)
228-
{
229-
Debug.WriteLine($"An error occurred while swapping pinned items in the navigation sidebar. {ex.Message}");
230-
this.FavoriteItems = sidebarItemsBackup;
231-
this.RemoveStaleSidebarItems();
232-
_ = this.AddAllItemsToSidebar();
233-
}
223+
// Moves the items in the MainPage
224+
MoveItem(firstLocationItem, indexOfFirstItemInMainPage, indexOfSecondItemInMainPage);
234225
}
235226

236227
/// <summary>

Files/Strings/en-US/Resources.resw

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2848,4 +2848,16 @@ We use App Center to keep track of app usage, find bugs, and fix crashes. All in
28482848
<data name="OngoingTasks" xml:space="preserve">
28492849
<value>Ongoing Tasks</value>
28502850
</data>
2851-
</root>
2851+
<data name="SideBarFavoritesMoveOneDown" xml:space="preserve">
2852+
<value>Move one down</value>
2853+
</data>
2854+
<data name="SideBarFavoritesMoveOneUp" xml:space="preserve">
2855+
<value>Move one up</value>
2856+
</data>
2857+
<data name="SideBarFavoritesMoveToBottom" xml:space="preserve">
2858+
<value>Move to bottom</value>
2859+
</data>
2860+
<data name="SideBarFavoritesMoveToTop" xml:space="preserve">
2861+
<value>Move to top</value>
2862+
</data>
2863+
</root>

Files/UserControls/SidebarControl.xaml.cs

Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,10 @@ public bool CanOpenInNewPane
117117
}
118118
}
119119

120+
public bool ShowMoveItemUp { get; set; }
121+
122+
public bool ShowMoveItemDown { get; set; }
123+
120124
public bool ShowUnpinItem { get; set; }
121125

122126
public bool ShowHideSection { get; set; }
@@ -186,6 +190,90 @@ public void UnpinItem_Click(object sender, RoutedEventArgs e)
186190
}
187191
}
188192

193+
public void MoveItemToTop_Click(object sender, RoutedEventArgs e)
194+
{
195+
if (RightClickedItem.Section == SectionType.Favorites)
196+
{
197+
bool isSelectedSidebarItem = false;
198+
199+
if (SelectedSidebarItem == RightClickedItem)
200+
{
201+
isSelectedSidebarItem = true;
202+
}
203+
204+
int oldIndex = App.SidebarPinnedController.Model.IndexOfItem(RightClickedItem);
205+
App.SidebarPinnedController.Model.MoveItem(RightClickedItem, oldIndex, 1);
206+
207+
if (isSelectedSidebarItem)
208+
{
209+
SetValue(SelectedSidebarItemProperty, RightClickedItem);
210+
}
211+
}
212+
}
213+
214+
public void MoveItemUp_Click(object sender, RoutedEventArgs e)
215+
{
216+
if (RightClickedItem.Section == SectionType.Favorites)
217+
{
218+
bool isSelectedSidebarItem = false;
219+
220+
if (SelectedSidebarItem == RightClickedItem)
221+
{
222+
isSelectedSidebarItem = true;
223+
}
224+
225+
int oldIndex = App.SidebarPinnedController.Model.IndexOfItem(RightClickedItem);
226+
App.SidebarPinnedController.Model.MoveItem(RightClickedItem, oldIndex, oldIndex - 1);
227+
228+
if (isSelectedSidebarItem)
229+
{
230+
SetValue(SelectedSidebarItemProperty, RightClickedItem);
231+
}
232+
}
233+
}
234+
235+
public void MoveItemDown_Click(object sender, RoutedEventArgs e)
236+
{
237+
if (RightClickedItem.Section == SectionType.Favorites)
238+
{
239+
bool isSelectedSidebarItem = false;
240+
241+
if (SelectedSidebarItem == RightClickedItem)
242+
{
243+
isSelectedSidebarItem = true;
244+
}
245+
246+
int oldIndex = App.SidebarPinnedController.Model.IndexOfItem(RightClickedItem);
247+
App.SidebarPinnedController.Model.MoveItem(RightClickedItem, oldIndex, oldIndex + 1);
248+
249+
if (isSelectedSidebarItem)
250+
{
251+
SetValue(SelectedSidebarItemProperty, RightClickedItem);
252+
}
253+
}
254+
}
255+
256+
public void MoveItemToBottom_Click(object sender, RoutedEventArgs e)
257+
{
258+
if (RightClickedItem.Section == SectionType.Favorites)
259+
{
260+
bool isSelectedSidebarItem = false;
261+
262+
if (SelectedSidebarItem == RightClickedItem)
263+
{
264+
isSelectedSidebarItem = true;
265+
}
266+
267+
int oldIndex = App.SidebarPinnedController.Model.IndexOfItem(RightClickedItem);
268+
App.SidebarPinnedController.Model.MoveItem(RightClickedItem, oldIndex, App.SidebarPinnedController.Model.FavoriteItems.Count());
269+
270+
if (isSelectedSidebarItem)
271+
{
272+
SetValue(SelectedSidebarItemProperty, RightClickedItem);
273+
}
274+
}
275+
}
276+
189277
public static GridLength GetSidebarCompactSize()
190278
{
191279
if (App.Current.Resources.TryGetValue("NavigationViewCompactPaneLength", out object paneLength))
@@ -262,6 +350,8 @@ private void NavigationViewLocationItem_RightTapped(object sender, RightTappedRo
262350
ShowProperties = true;
263351
IsLibrariesHeader = false;
264352
ShowUnpinItem = ((library || favorite) && !item.IsDefaultLocation);
353+
ShowMoveItemUp = ShowUnpinItem && App.SidebarPinnedController.Model.IndexOfItem(item) > 1;
354+
ShowMoveItemDown = ShowUnpinItem && App.SidebarPinnedController.Model.IndexOfItem(item) < App.SidebarPinnedController.Model.FavoriteItems.Count();
265355
ShowHideSection = false;
266356
ShowEjectDevice = false;
267357

@@ -295,6 +385,8 @@ private void NavigationViewLocationItem_RightTapped(object sender, RightTappedRo
295385
ShowProperties = false;
296386
IsLibrariesHeader = librariesHeader;
297387
ShowUnpinItem = false;
388+
ShowMoveItemUp = false;
389+
ShowMoveItemDown = false;
298390
ShowHideSection = true;
299391
ShowEjectDevice = false;
300392
ShowEmptyRecycleBin = false;
@@ -319,6 +411,8 @@ private void NavigationViewDriveItem_RightTapped(object sender, RightTappedRoute
319411
IsLibrariesHeader = false;
320412
ShowEjectDevice = item.IsRemovable;
321413
ShowUnpinItem = false;
414+
ShowMoveItemUp = false;
415+
ShowMoveItemDown = false;
322416
ShowEmptyRecycleBin = false;
323417
ShowProperties = true;
324418
ShowHideSection = false;
@@ -344,6 +438,8 @@ private void NavigationViewWSLItem_RightTapped(object sender, RightTappedRoutedE
344438
IsLibrariesHeader = false;
345439
ShowEjectDevice = false;
346440
ShowUnpinItem = false;
441+
ShowMoveItemUp = false;
442+
ShowMoveItemDown = false;
347443
ShowEmptyRecycleBin = false;
348444
ShowProperties = false;
349445
ShowHideSection = false;
@@ -930,6 +1026,34 @@ public List<ContextMenuFlyoutItemViewModel> GetLocationItemMenuItems()
9301026
ShowItem = IsLocationItem
9311027
},
9321028
new ContextMenuFlyoutItemViewModel()
1029+
{
1030+
Text = "SideBarFavoritesMoveToTop".GetLocalized(),
1031+
Glyph = "\uE11C",
1032+
Command = new RelayCommand(() => MoveItemToTop_Click(null, null)),
1033+
ShowItem = ShowMoveItemUp
1034+
},
1035+
new ContextMenuFlyoutItemViewModel()
1036+
{
1037+
Text = "SideBarFavoritesMoveOneUp".GetLocalized(),
1038+
Glyph = "\uE70E",
1039+
Command = new RelayCommand(() => MoveItemUp_Click(null, null)),
1040+
ShowItem = ShowMoveItemUp
1041+
},
1042+
new ContextMenuFlyoutItemViewModel()
1043+
{
1044+
Text = "SideBarFavoritesMoveOneDown".GetLocalized(),
1045+
Glyph = "\uE70D",
1046+
Command = new RelayCommand(() => MoveItemDown_Click(null, null)),
1047+
ShowItem = ShowMoveItemDown
1048+
},
1049+
new ContextMenuFlyoutItemViewModel()
1050+
{
1051+
Text = "SideBarFavoritesMoveToBottom".GetLocalized(),
1052+
Glyph = "\uE118",
1053+
Command = new RelayCommand(() => MoveItemToBottom_Click(null, null)),
1054+
ShowItem = ShowMoveItemDown
1055+
},
1056+
new ContextMenuFlyoutItemViewModel()
9331057
{
9341058
Text = "SideBarUnpinFromFavorites/Text".GetLocalized(),
9351059
Glyph = "\uE77A",

0 commit comments

Comments
 (0)