Skip to content

Commit

Permalink
Sort Navigation-Pane items alphabetically
Browse files Browse the repository at this point in the history
The Navigation Pane items are now sorted
alphabetically based on their Display Names.

Fixes microsoft#31089, microsoft#23763
  • Loading branch information
sanidhyas3s committed Jan 30, 2024
1 parent 172db3a commit 306e7aa
Showing 1 changed file with 29 additions and 0 deletions.
29 changes: 29 additions & 0 deletions src/settings-ui/Settings.UI/SettingsXAML/Views/ShellPage.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

using System;
using System.Collections.Generic;
using System.Linq;
using ManagedCommon;
using Microsoft.PowerToys.Settings.UI.Helpers;
using Microsoft.PowerToys.Settings.UI.Services;
Expand Down Expand Up @@ -132,6 +133,7 @@ public ShellPage()
DataContext = ViewModel;
ShellHandler = this;
ViewModel.Initialize(shellFrame, navigationView, KeyboardAccelerators);
SortNavigationPaneItems();

// NL moved navigation to general page to the moment when the window is first activated (to not make flyout window disappear)
// shellFrame.Navigate(typeof(GeneralPage));
Expand Down Expand Up @@ -274,6 +276,33 @@ private void OobeButton_Click(object sender, RoutedEventArgs e)
OpenOobeWindowCallback();
}

/// <summary>
/// Sort the Navigation-Pane items alphabetically based on their display names
/// </summary>
private void SortNavigationPaneItems()
{
// We want to sort the items after the separator
int separatorIndex = navigationView.MenuItems.IndexOf(
navigationView.MenuItems.OfType<NavigationViewItemSeparator>().FirstOrDefault());

var itemsToSort = navigationView.MenuItems
.OfType<NavigationViewItem>()
.Skip(separatorIndex)
.OrderBy(item => item.Content.ToString())
.ToList();

// Arrange the items in the sorted order
for (int i = navigationView.MenuItems.Count - 1; i > separatorIndex; i--)
{
navigationView.MenuItems.RemoveAt(i);
}

foreach (var sortedItem in itemsToSort)
{
navigationView.MenuItems.Add(sortedItem);
}
}

private bool navigationViewInitialStateProcessed; // avoid announcing initial state of the navigation pane.

private void NavigationView_PaneOpened(Microsoft.UI.Xaml.Controls.NavigationView sender, object args)
Expand Down

0 comments on commit 306e7aa

Please sign in to comment.