Skip to content

Commit bd87cc5

Browse files
Added an option to reorder pinned items in the sidebar (#2116)
Co-authored-by: Yair Aichenbaum <[email protected]>
1 parent 86f8da3 commit bd87cc5

24 files changed

+574
-43
lines changed

Files/DataModels/SidebarPinnedModel.cs

Lines changed: 138 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
1-
using Files.Filesystem;
1+
using Files.Enums;
2+
using Files.Filesystem;
23
using Files.View_Models;
34
using Files.Views;
5+
using Microsoft.Toolkit.Uwp.UI;
46
using Newtonsoft.Json;
57
using System;
68
using System.Collections.Generic;
9+
using System.Collections.ObjectModel;
710
using System.Diagnostics;
811
using System.IO;
912
using System.Linq;
@@ -21,6 +24,9 @@ public class SidebarPinnedModel
2124
[JsonProperty("items")]
2225
public List<string> Items { get; set; } = new List<string>();
2326

27+
/// <summary>
28+
/// Adds the default items to the navigation page
29+
/// </summary>
2430
public void AddDefaultItems()
2531
{
2632
Items.Add(AppSettings.DesktopPath);
@@ -31,11 +37,18 @@ public void AddDefaultItems()
3137
Items.Add(AppSettings.VideosPath);
3238
}
3339

40+
/// <summary>
41+
/// Gets the items from the navigation page
42+
/// </summary>
3443
public List<string> GetItems()
3544
{
3645
return Items;
3746
}
3847

48+
/// <summary>
49+
/// Adds the item to the navigation page
50+
/// </summary>
51+
/// <param name="item">Item to remove</param>
3952
public async void AddItem(string item)
4053
{
4154
if (!Items.Contains(item))
@@ -46,6 +59,10 @@ public async void AddItem(string item)
4659
}
4760
}
4861

62+
/// <summary>
63+
/// Removes the item from the navigation page
64+
/// </summary>
65+
/// <param name="item">Item to remove</param>
4966
public void RemoveItem(string item)
5067
{
5168
if (Items.Contains(item))
@@ -56,8 +73,112 @@ public void RemoveItem(string item)
5673
}
5774
}
5875

76+
/// <summary>
77+
/// Moves the location item in the navigation sidebar from the old position to the new position
78+
/// </summary>
79+
/// <param name="locationItem">Location item to move</param>
80+
/// <param name="oldIndex">The old position index of the location item</param>
81+
/// <param name="newIndex">The new position index of the location item</param>
82+
/// <returns>True if the move was successful</returns>
83+
public bool MoveItem(INavigationControlItem locationItem, int oldIndex, int newIndex)
84+
{
85+
if (locationItem == null)
86+
{
87+
return false;
88+
}
89+
90+
if (oldIndex >= 0 && newIndex >=0)
91+
{
92+
MainPage.sideBarItems.RemoveAt(oldIndex);
93+
MainPage.sideBarItems.Insert(newIndex, locationItem);
94+
return true;
95+
}
96+
97+
return false;
98+
}
99+
100+
/// <summary>
101+
/// Swaps two location items in the navigation sidebar
102+
/// </summary>
103+
/// <param name="firstLocationItem">The first location item</param>
104+
/// <param name="secondLocationItem">The second location item</param>
105+
public void SwapItems(INavigationControlItem firstLocationItem, INavigationControlItem secondLocationItem)
106+
{
107+
if (firstLocationItem == null || secondLocationItem == null)
108+
{
109+
return;
110+
}
111+
112+
// A backup of the items, because the swapping of items requires removing and inserting them in the corrent position
113+
var sidebarItemsBackup = new List<string>(this.Items);
114+
115+
try
116+
{
117+
var indexOfFirstItemInMainPage = IndexOfItem(firstLocationItem);
118+
var indexOfSecondItemInMainPage = IndexOfItem(secondLocationItem);
119+
120+
// Moves the items in the MainPage
121+
var result = MoveItem(firstLocationItem, indexOfFirstItemInMainPage, indexOfSecondItemInMainPage);
122+
123+
// Moves the items in this model and saves the model
124+
if(result == true)
125+
{
126+
var indexOfFirstItemInModel = this.Items.IndexOf(firstLocationItem.Path);
127+
var indexOfSecondItemInModel = this.Items.IndexOf(secondLocationItem.Path);
128+
if (indexOfFirstItemInModel >= 0 && indexOfSecondItemInModel >= 0)
129+
{
130+
this.Items.RemoveAt(indexOfFirstItemInModel);
131+
this.Items.Insert(indexOfSecondItemInModel, firstLocationItem.Path);
132+
}
133+
134+
Save();
135+
}
136+
}
137+
catch (Exception ex) when (
138+
ex is ArgumentException // Pinned item was invalid
139+
|| ex is FileNotFoundException // Pinned item was deleted
140+
|| ex is System.Runtime.InteropServices.COMException // Pinned item's drive was ejected
141+
|| (uint)ex.HResult == 0x8007000F // The system cannot find the drive specified
142+
|| (uint)ex.HResult == 0x800700A1) // The specified path is invalid (usually an mtp device was disconnected)
143+
{
144+
Debug.WriteLine("An error occured while swapping pinned items in the navigation sidebar. " + ex.Message);
145+
this.Items = sidebarItemsBackup;
146+
this.RemoveStaleSidebarItems();
147+
this.AddAllItemsToSidebar();
148+
}
149+
}
150+
151+
/// <summary>
152+
/// Returns the index of the location item in the navigation sidebar
153+
/// </summary>
154+
/// <param name="locationItem">The location item</param>
155+
/// <returns>Index of the item</returns>
156+
public int IndexOfItem(INavigationControlItem locationItem)
157+
{
158+
return MainPage.sideBarItems.IndexOf(locationItem);
159+
}
160+
161+
/// <summary>
162+
/// Returns the index of the location item in the collection containing Navigation control items
163+
/// </summary>
164+
/// <param name="locationItem">The location item</param>
165+
/// <param name="collection">The collection in which to find the location item</param>
166+
/// <returns>Index of the item</returns>
167+
public int IndexOfItem(INavigationControlItem locationItem, List<INavigationControlItem> collection)
168+
{
169+
return collection.IndexOf(locationItem);
170+
}
171+
172+
/// <summary>
173+
/// Saves the model
174+
/// </summary>
59175
public void Save() => App.SidebarPinnedController.SaveModel();
60176

177+
/// <summary>
178+
/// Adds the item do the navigation sidebar
179+
/// </summary>
180+
/// <param name="path">The path which to save</param>
181+
/// <returns>Task</returns>
61182
public async Task AddItemToSidebar(string path)
62183
{
63184
try
@@ -74,7 +195,11 @@ public async Task AddItemToSidebar(string path)
74195
IsDefaultLocation = false,
75196
Text = folder.DisplayName
76197
};
77-
MainPage.sideBarItems.Insert(insertIndex, locationItem);
198+
199+
if (!MainPage.sideBarItems.Contains(locationItem))
200+
{
201+
MainPage.sideBarItems.Insert(insertIndex, locationItem);
202+
}
78203
}
79204
catch (UnauthorizedAccessException ex)
80205
{
@@ -92,6 +217,9 @@ ex is ArgumentException // Pinned item was invalid
92217
}
93218
}
94219

220+
/// <summary>
221+
/// Adds all items to the navigation sidebar
222+
/// </summary>
95223
public async void AddAllItemsToSidebar()
96224
{
97225
for (int i = 0; i < Items.Count(); i++)
@@ -101,6 +229,9 @@ public async void AddAllItemsToSidebar()
101229
}
102230
}
103231

232+
/// <summary>
233+
/// Removes stale items in the navigation sidebar
234+
/// </summary>
104235
public void RemoveStaleSidebarItems()
105236
{
106237
// Remove unpinned items from sidebar
@@ -117,6 +248,11 @@ public void RemoveStaleSidebarItems()
117248
}
118249
}
119250

251+
/// <summary>
252+
/// Gets the icon for the items in the navigation sidebar
253+
/// </summary>
254+
/// <param name="path">The path in the sidebar</param>
255+
/// <returns>The icon code</returns>
120256
public string GetItemIcon(string path)
121257
{
122258
string iconCode;

Files/MultilingualResources/Files.de-DE.xlf

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1389,6 +1389,14 @@
13891389
<source>Open items with a single click</source>
13901390
<target state="new">Open items with a single click</target>
13911391
</trans-unit>
1392+
<trans-unit id="PinToSidebarByDraggingCaptionText" translate="yes" xml:space="preserve">
1393+
<source>Move here</source>
1394+
<target state="new">Move here</target>
1395+
</trans-unit>
1396+
<trans-unit id="SettingsExperimentalSortPinnedFiItemsByDragging.Header" translate="yes" xml:space="preserve">
1397+
<source>Enable reordering items on the sidebar</source>
1398+
<target state="new">Enable reordering items on the sidebar</target>
1399+
</trans-unit>
13921400
</group>
13931401
</body>
13941402
</file>

Files/MultilingualResources/Files.es-ES.xlf

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1386,6 +1386,14 @@
13861386
<source>Open items with a single click</source>
13871387
<target state="new">Open items with a single click</target>
13881388
</trans-unit>
1389+
<trans-unit id="PinToSidebarByDraggingCaptionText" translate="yes" xml:space="preserve">
1390+
<source>Move here</source>
1391+
<target state="new">Move here</target>
1392+
</trans-unit>
1393+
<trans-unit id="SettingsExperimentalSortPinnedFiItemsByDragging.Header" translate="yes" xml:space="preserve">
1394+
<source>Enable reordering items on the sidebar</source>
1395+
<target state="new">Enable reordering items on the sidebar</target>
1396+
</trans-unit>
13891397
</group>
13901398
</body>
13911399
</file>

Files/MultilingualResources/Files.fr-FR.xlf

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1387,6 +1387,14 @@
13871387
<source>Open items with a single click</source>
13881388
<target state="new">Open items with a single click</target>
13891389
</trans-unit>
1390+
<trans-unit id="PinToSidebarByDraggingCaptionText" translate="yes" xml:space="preserve">
1391+
<source>Move here</source>
1392+
<target state="new">Move here</target>
1393+
</trans-unit>
1394+
<trans-unit id="SettingsExperimentalSortPinnedFiItemsByDragging.Header" translate="yes" xml:space="preserve">
1395+
<source>Enable reordering items on the sidebar</source>
1396+
<target state="new">Enable reordering items on the sidebar</target>
1397+
</trans-unit>
13901398
</group>
13911399
</body>
13921400
</file>

Files/MultilingualResources/Files.he-IL.xlf

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1386,6 +1386,14 @@
13861386
<source>Open items with a single click</source>
13871387
<target state="new">Open items with a single click</target>
13881388
</trans-unit>
1389+
<trans-unit id="PinToSidebarByDraggingCaptionText" translate="yes" xml:space="preserve">
1390+
<source>Move here</source>
1391+
<target state="new">Move here</target>
1392+
</trans-unit>
1393+
<trans-unit id="SettingsExperimentalSortPinnedFiItemsByDragging.Header" translate="yes" xml:space="preserve">
1394+
<source>Enable reordering items on the sidebar</source>
1395+
<target state="new">Enable reordering items on the sidebar</target>
1396+
</trans-unit>
13891397
</group>
13901398
</body>
13911399
</file>

Files/MultilingualResources/Files.hi-IN.xlf

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1398,6 +1398,14 @@
13981398
<source>Open items with a single click</source>
13991399
<target state="new">Open items with a single click</target>
14001400
</trans-unit>
1401+
<trans-unit id="PinToSidebarByDraggingCaptionText" translate="yes" xml:space="preserve">
1402+
<source>Move here</source>
1403+
<target state="new">Move here</target>
1404+
</trans-unit>
1405+
<trans-unit id="SettingsExperimentalSortPinnedFiItemsByDragging.Header" translate="yes" xml:space="preserve">
1406+
<source>Enable reordering items on the sidebar</source>
1407+
<target state="new">Enable reordering items on the sidebar</target>
1408+
</trans-unit>
14011409
</group>
14021410
</body>
14031411
</file>

Files/MultilingualResources/Files.hu-HU.xlf

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1386,6 +1386,14 @@
13861386
<source>Open items with a single click</source>
13871387
<target state="new">Open items with a single click</target>
13881388
</trans-unit>
1389+
<trans-unit id="PinToSidebarByDraggingCaptionText" translate="yes" xml:space="preserve">
1390+
<source>Move here</source>
1391+
<target state="new">Move here</target>
1392+
</trans-unit>
1393+
<trans-unit id="SettingsExperimentalSortPinnedFiItemsByDragging.Header" translate="yes" xml:space="preserve">
1394+
<source>Enable reordering items on the sidebar</source>
1395+
<target state="new">Enable reordering items on the sidebar</target>
1396+
</trans-unit>
13891397
</group>
13901398
</body>
13911399
</file>

Files/MultilingualResources/Files.it-IT.xlf

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1387,6 +1387,14 @@
13871387
<source>Open items with a single click</source>
13881388
<target state="new">Open items with a single click</target>
13891389
</trans-unit>
1390+
<trans-unit id="PinToSidebarByDraggingCaptionText" translate="yes" xml:space="preserve">
1391+
<source>Move here</source>
1392+
<target state="new">Move here</target>
1393+
</trans-unit>
1394+
<trans-unit id="SettingsExperimentalSortPinnedFiItemsByDragging.Header" translate="yes" xml:space="preserve">
1395+
<source>Enable reordering items on the sidebar</source>
1396+
<target state="new">Enable reordering items on the sidebar</target>
1397+
</trans-unit>
13901398
</group>
13911399
</body>
13921400
</file>

Files/MultilingualResources/Files.ja-JP.xlf

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1386,6 +1386,14 @@
13861386
<source>Open items with a single click</source>
13871387
<target state="new">Open items with a single click</target>
13881388
</trans-unit>
1389+
<trans-unit id="PinToSidebarByDraggingCaptionText" translate="yes" xml:space="preserve">
1390+
<source>Move here</source>
1391+
<target state="new">Move here</target>
1392+
</trans-unit>
1393+
<trans-unit id="SettingsExperimentalSortPinnedFiItemsByDragging.Header" translate="yes" xml:space="preserve">
1394+
<source>Enable reordering items on the sidebar</source>
1395+
<target state="new">Enable reordering items on the sidebar</target>
1396+
</trans-unit>
13891397
</group>
13901398
</body>
13911399
</file>

Files/MultilingualResources/Files.nl-NL.xlf

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1400,6 +1400,14 @@
14001400
<source>Open items with a single click</source>
14011401
<target state="new">Open items with a single click</target>
14021402
</trans-unit>
1403+
<trans-unit id="PinToSidebarByDraggingCaptionText" translate="yes" xml:space="preserve">
1404+
<source>Move here</source>
1405+
<target state="new">Move here</target>
1406+
</trans-unit>
1407+
<trans-unit id="SettingsExperimentalSortPinnedFiItemsByDragging.Header" translate="yes" xml:space="preserve">
1408+
<source>Enable reordering items on the sidebar</source>
1409+
<target state="new">Enable reordering items on the sidebar</target>
1410+
</trans-unit>
14031411
</group>
14041412
</body>
14051413
</file>

0 commit comments

Comments
 (0)