Skip to content

Commit

Permalink
Add move items
Browse files Browse the repository at this point in the history
  • Loading branch information
hecksmosis committed Feb 1, 2023
1 parent 86b76d5 commit 10e9bb9
Show file tree
Hide file tree
Showing 3 changed files with 144 additions and 3 deletions.
57 changes: 57 additions & 0 deletions src/Files.App/DataModels/SidebarPinnedModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,63 @@ public async Task UpdateItemsWithExplorer()
}
}

/// <summary>
/// Moves the location item in the Favorites sidebar section from the old position to the new position
/// </summary>
/// <param name="oldIndex">The old position index of the location item</param>
/// <param name="newIndex">The new position index of the location item</param>
/// <returns>True if the move was successful</returns>

public bool MoveItem(INavigationControlItem locationItem, int oldIndex, int newIndex)
{
if (locationItem is null || newIndex > FavoriteItems.Count)
return false;

// 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);
FavoriteItems.Insert(newIndex, locationItem.Path);
lock (favoriteList)
{
favoriteList.RemoveAt(oldIndex);
favoriteList.Insert(newIndex, locationItem);
}
var e = new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Move, locationItem, newIndex, oldIndex);
controller?.DataChanged?.Invoke(SectionType.Favorites, e);
_ = QuickAccessService.Save(sidebarItemsBackup.ToArray());
return true;
}
catch (Exception ex)
{
Debug.WriteLine($"An error occurred while moving pinned items in the Favorites sidebar section. {ex.Message}");
FavoriteItems = sidebarItemsBackup;
RemoveStaleSidebarItems();
_ = AddAllItemsToSidebar();
return false;
}
}

/// <summary>
/// Swaps two location items in the navigation sidebar
/// </summary>
/// <param name="firstLocationItem">The first location item</param>
/// <param name="secondLocationItem">The second location item</param>
public void SwapItems(INavigationControlItem firstLocationItem, INavigationControlItem secondLocationItem)
{
if (firstLocationItem is null || secondLocationItem is null)
{
return;
}

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

// Moves the items in the MainPage
MoveItem(firstLocationItem, indexOfFirstItemInMainPage, indexOfSecondItemInMainPage);
}

/// <summary>
/// Returns the index of the location item in the navigation sidebar
Expand Down
11 changes: 8 additions & 3 deletions src/Files.App/ServicesImplementation/QuickAccessService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,14 @@ public static async Task<List<string>> GetPinnedFoldersAsync()
public static async Task PinToSidebar(string folderPath, bool loadExplorerItems = true)
=> await PinToSidebar(new[] { folderPath }, loadExplorerItems);

public static async Task PinToSidebar(string[] folderPaths, bool loadExplorerItems = true)
public static async Task PinToSidebar(string[] folderPaths, bool loadExplorerItems = true, bool oneByOne = false)
{
await ContextMenu.InvokeVerb("pintohome", folderPaths);
if (oneByOne)
foreach (string path in folderPaths)
await ContextMenu.InvokeVerb("pintohome", new[] { path });
else
await ContextMenu.InvokeVerb("pintohome", folderPaths);

if (loadExplorerItems)
await Controller.LoadAsync();
}
Expand Down Expand Up @@ -55,7 +60,7 @@ public static async Task Save(string[] toRemove)
{
// Saves pinned items by unpinning the previous items from explorer and then pinning the current items back
await UnpinFromSidebar(toRemove, false);
await PinToSidebar(Controller.Model.FavoriteItems.ToArray(), false);
await PinToSidebar(Controller.Model.FavoriteItems.ToArray(), false, true);
}
}
}
79 changes: 79 additions & 0 deletions src/Files.App/UserControls/SidebarControl.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
using Windows.ApplicationModel.DataTransfer.DragDrop;
using Windows.System;
using Windows.UI.Core;
using static System.Net.Mime.MediaTypeNames;
using DispatcherQueueTimer = Microsoft.UI.Dispatching.DispatcherQueueTimer;

namespace Files.App.UserControls
Expand Down Expand Up @@ -116,6 +117,14 @@ public ICommand EmptyRecycleBinCommand

private ICommand UnpinItemCommand { get; }

private ICommand MoveItemToTopCommand { get; }

private ICommand MoveItemUpCommand { get; }

private ICommand MoveItemDownCommand { get; }

private ICommand MoveItemToBottomCommand { get; }

private ICommand OpenInNewTabCommand { get; }

private ICommand OpenInNewWindowCommand { get; }
Expand All @@ -140,6 +149,10 @@ public SidebarControl()
HideSectionCommand = new RelayCommand(HideSection);
UnpinItemCommand = new RelayCommand(UnpinItem);
PinItemCommand = new RelayCommand(PinItem);
MoveItemToTopCommand = new RelayCommand(MoveItemToTop);
MoveItemUpCommand = new RelayCommand(MoveItemUp);
MoveItemDownCommand = new RelayCommand(MoveItemDown);
MoveItemToBottomCommand = new RelayCommand(MoveItemToBottom);
OpenInNewTabCommand = new RelayCommand(OpenInNewTab);
OpenInNewWindowCommand = new RelayCommand(OpenInNewWindow);
OpenInNewPaneCommand = new RelayCommand(OpenInNewPane);
Expand Down Expand Up @@ -241,6 +254,34 @@ private List<ContextMenuFlyoutItemViewModel> GetLocationItemMenuItems(INavigatio
ShowItem = options.IsLocationItem
},
new ContextMenuFlyoutItemViewModel()
{
Text = "SideBarFavoritesMoveToTop".GetLocalizedResource(),
Glyph = "\uE11C",
Command = MoveItemToTopCommand,
ShowItem = showMoveItemUp
},
new ContextMenuFlyoutItemViewModel()
{
Text = "SideBarFavoritesMoveOneUp".GetLocalizedResource(),
Glyph = "\uE70E",
Command = MoveItemUpCommand,
ShowItem = showMoveItemUp
},
new ContextMenuFlyoutItemViewModel()
{
Text = "SideBarFavoritesMoveOneDown".GetLocalizedResource(),
Glyph = "\uE70D",
Command = MoveItemDownCommand,
ShowItem = showMoveItemDown
},
new ContextMenuFlyoutItemViewModel()
{
Text = "SideBarFavoritesMoveToBottom".GetLocalizedResource(),
Glyph = "\uE118",
Command = MoveItemToBottomCommand,
ShowItem = showMoveItemDown
},
new ContextMenuFlyoutItemViewModel()
{
Text = "BaseLayoutItemContextFlyoutPinToFavorites/Text".GetLocalizedResource(),
Glyph = "\uE840",
Expand Down Expand Up @@ -352,6 +393,40 @@ private void UnpinItem()
_ = QuickAccessService.UnpinFromSidebar(rightClickedItem.Path);
}

private void MoveItemToTop()
{
MoveItemToNewIndex(0);
}

private void MoveItemUp()
{
MoveItemToNewIndex(App.SidebarPinnedController.Model.IndexOfItem(rightClickedItem) - 1);
}

private void MoveItemDown()
{
MoveItemToNewIndex(App.SidebarPinnedController.Model.IndexOfItem(rightClickedItem) + 1);
}

private void MoveItemToBottom()
{
MoveItemToNewIndex(App.SidebarPinnedController.Model.FavoriteItems.Count - 1);
}

private void MoveItemToNewIndex(int newIndex)
{
if (rightClickedItem.Section != SectionType.Favorites)
return;

var isSelectedSidebarItem = SelectedSidebarItem == rightClickedItem;

var oldIndex = App.SidebarPinnedController.Model.IndexOfItem(rightClickedItem);
App.SidebarPinnedController.Model.MoveItem(rightClickedItem, oldIndex, newIndex);

if (isSelectedSidebarItem)
SetValue(SelectedSidebarItemProperty, rightClickedItem);
}

private void OpenProperties(CommandBarFlyout menu)
{
EventHandler<object> flyoutClosed = null!;
Expand Down Expand Up @@ -673,6 +748,10 @@ private async void NavigationViewLocationItem_Drop(object sender, DragEventArgs
isDropOnProcess = false;
deferral.Complete();
}
else if ((e.DataView.Properties["sourceLocationItem"] as NavigationViewItem)?.DataContext is LocationItem sourceLocationItem)
{
SidebarPinnedModel.SwapItems(sourceLocationItem, locationItem);
}

await Task.Yield();
lockFlag = false;
Expand Down

0 comments on commit 10e9bb9

Please sign in to comment.