Skip to content

Commit

Permalink
Merge pull request #59 from deadlydog/RememberGridSortDirection
Browse files Browse the repository at this point in the history
  • Loading branch information
deadlydog authored Feb 20, 2021
2 parents 96b5945 + 8f1ed7c commit d346a44
Show file tree
Hide file tree
Showing 6 changed files with 100 additions and 9 deletions.
6 changes: 6 additions & 0 deletions Changelog.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# Changelog

## v1.10.0 - February 20, 2021

Features:

- Preserve UI grid column sorting between searches and sessions.

## v1.9.0 - December 26, 2020

Features:
Expand Down
2 changes: 1 addition & 1 deletion build/azure-pipelines.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ variables:
buildConfiguration: 'Release'
appBinariesDirectory: 'src\PathLengthCheckerGUI\bin\$(buildConfiguration)'

version.MajorMinor: '1.9' # Manually adjust the version number as needed for semantic versioning. Revision is auto-incremented.
version.MajorMinor: '1.10' # Manually adjust the version number as needed for semantic versioning. Revision is auto-incremented.
version.Revision: $[counter(variables['version.MajorMinor'], 0)]
versionNumber: '$(version.MajorMinor).$(version.Revision)'

Expand Down
18 changes: 13 additions & 5 deletions src/PathLengthCheckerGUI/MainWindow.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
xmlns:local="clr-namespace:PathLengthCheckerGUI"
xmlns:extToolkit="http://schemas.xceed.com/wpf/xaml/toolkit"
xmlns:System="clr-namespace:System;assembly=mscorlib"
xmlns:scm="clr-namespace:System.ComponentModel;assembly=WindowsBase"
xmlns:PathLengthChecker="clr-namespace:PathLengthChecker;assembly=PathLengthChecker"
Style="{StaticResource ResourceKey=DefaultWindowStyle}"
MinWidth="770" MinHeight="400"
Expand All @@ -12,15 +13,22 @@
Left="{local:ApplicationSettingsBinding Path=WindowLeftPosition}"
Top="{local:ApplicationSettingsBinding Path=WindowTopPosition}"
WindowState="{local:ApplicationSettingsBinding Path=WindowState}"
FocusManager.FocusedElement="{Binding ElementName=txtRootDirectory}">
FocusManager.FocusedElement="{Binding ElementName=txtRootDirectory}"
Loaded="Window_Loaded"
Closed="Window_Closed">
<Window.Resources>
<ObjectDataProvider MethodName="GetValues" ObjectType="{x:Type System:Enum}" x:Key="FileSystemTypesEnumValues" IsAsynchronous="True">
<ObjectDataProvider.MethodParameters>
<x:Type TypeName="PathLengthChecker:FileSystemTypes" />
</ObjectDataProvider.MethodParameters>
</ObjectDataProvider>

<CollectionViewSource x:Key="PathsCollectionViewSource" Source="{Binding Paths}" />
<CollectionViewSource x:Key="PathsCollectionViewSource" Source="{Binding Paths}">
<CollectionViewSource.SortDescriptions>
<scm:SortDescription PropertyName="Length" />
<scm:SortDescription PropertyName="Path" />
</CollectionViewSource.SortDescriptions>
</CollectionViewSource>
</Window.Resources>

<Grid Margin="5">
Expand Down Expand Up @@ -154,10 +162,10 @@
</Button>
</Grid>

<DataGrid Grid.Row="10" AutoGenerateColumns="False" Name="dgPaths" IsReadOnly="True" IsTabStop="False" Grid.ColumnSpan="4" LoadingRow="dgPaths_LoadingRow" ItemsSource="{Binding Source={StaticResource PathsCollectionViewSource}}" SelectedItem="{Binding Path=SelectedPath, Mode=TwoWay}" FontFamily="Courier New">
<DataGrid Grid.Row="10" AutoGenerateColumns="False" Name="dgPaths" IsReadOnly="True" IsTabStop="False" Grid.ColumnSpan="4" LoadingRow="dgPaths_LoadingRow" ItemsSource="{Binding Source={StaticResource PathsCollectionViewSource}}" SelectedItem="{Binding Path=SelectedPath, Mode=TwoWay}" FontFamily="Courier New" CanUserSortColumns="True">
<DataGrid.Columns>
<DataGridTextColumn Header="Length" IsReadOnly="True" Binding="{Binding Path=Length}" />
<DataGridTextColumn Header="Path" IsReadOnly="True" Binding="{Binding Path=Path}" />
<DataGridTextColumn Header="Length" IsReadOnly="True" Binding="{Binding Path=Length}" SortMemberPath="Length" />
<DataGridTextColumn Header="Path" IsReadOnly="True" Binding="{Binding Path=Path}" SortMemberPath="Path" />
</DataGrid.Columns>

<!-- Add context menu to rows -->
Expand Down
67 changes: 65 additions & 2 deletions src/PathLengthCheckerGUI/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,33 @@ public MainWindow()

SetWindowTitle();
}
private void Window_Loaded(object sender, RoutedEventArgs e)
{
LoadColumnSortDescriptionsFromSettings();
}

private void Window_Closed(object sender, EventArgs e)
{
SaveColumnSortDescriptionsToSettings();
}

private void SetWindowTitle()
{
this.Title = "Path Length Checker v" + Assembly.GetEntryAssembly().GetName().Version.ToString(3) + " - Written by Daniel Schroeder";
}

private void LoadColumnSortDescriptionsFromSettings()
{
var previousGridColumnSortDescriptions = Properties.Settings.Default.ResultsGridColumnSortDescriptionCollection ?? SortDescriptionCollection.Empty;
SetGridColumnSortDescriptions(previousGridColumnSortDescriptions);
}

private void SaveColumnSortDescriptionsToSettings()
{
var gridColumnSortDescriptions = GetCurrentGridColumnSortDescriptions();
Properties.Settings.Default.ResultsGridColumnSortDescriptionCollection = gridColumnSortDescriptions;
}

public ObservableCollection<PathInfo> Paths
{
get => _paths;
Expand Down Expand Up @@ -132,7 +153,7 @@ private async void btnGetPathLengths_Click(object sender, RoutedEventArgs e)
btnCancelGetPathLengths.Visibility = Visibility.Visible;

// Clear any previous paths out.
Paths = new ObservableCollection<PathInfo>();
Paths.Clear();
txtNumberOfPaths.Text = string.Empty;
txtMinAndMaxPathLengths.Text = string.Empty;

Expand Down Expand Up @@ -205,11 +226,53 @@ private async Task BuildSearchOptionsAndGetPaths(string rootDirectory, string ro
};

// Get the paths in a background task so we don't lock the UI.
Paths = await Task.Run(() =>
var newPaths = await Task.Run(() =>
{
var paths = PathLengthChecker.PathLengthChecker.GetPathsWithLengths(searchOptions, cancellationToken);
return new ObservableCollection<PathInfo>(paths.ToList());
}, cancellationToken);

// Assigning Paths to a new ObservableCollection wipes out the column sorting in the CollectionViewSource.
// Ideally we would just use Paths.Add() to repopulate the list, which would preserve the sorting, but it takes forever when there's a lot of items.
// So instead we backup the CollectionViewSource sorting before assigning Paths to a new ObservableCollection, and then restore it after.
var previousColumnSortDescriptions = GetCurrentGridColumnSortDescriptions().ToList();

Paths = newPaths;

// Restore the previous column sort directions on the GUI DataGrid.
SetGridColumnSortDescriptions(previousColumnSortDescriptions);
}

private SortDescriptionCollection GetCurrentGridColumnSortDescriptions()
{
var collectionView = CollectionViewSource.GetDefaultView(dgPaths.ItemsSource);
return collectionView.SortDescriptions;
}

private void SetGridColumnSortDescriptions(SortDescriptionCollection sortDescriptions)
{
SetGridColumnSortDescriptions(sortDescriptions.ToList());
}

private void SetGridColumnSortDescriptions(IEnumerable<SortDescription> sortDescriptions)
{
var collectionView = CollectionViewSource.GetDefaultView(dgPaths.ItemsSource);
collectionView.SortDescriptions.Clear();
sortDescriptions.ToList().ForEach(collectionView.SortDescriptions.Add);

// We need to manually update the sort direction of each column on the grid to show it's sorting glyph.
foreach (var column in dgPaths.Columns)
{
var columnsSortDescription = sortDescriptions.FirstOrDefault(c => string.Equals(c.PropertyName, column.SortMemberPath));
if (columnsSortDescription.PropertyName != null)
{
column.SortDirection = columnsSortDescription.Direction;
}
else
{
column.SortDirection = null;
}
}
}

private void DisplayResultsMetadata()
Expand Down
13 changes: 12 additions & 1 deletion src/PathLengthCheckerGUI/Properties/Settings.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions src/PathLengthCheckerGUI/Properties/Settings.settings
Original file line number Diff line number Diff line change
Expand Up @@ -41,5 +41,8 @@
<Setting Name="SearchOption_RootDirectoryReplacementText" Type="System.String" Scope="User">
<Value Profile="(Default)" />
</Setting>
<Setting Name="ResultsGridColumnSortDescriptionCollection" Type="System.ComponentModel.SortDescriptionCollection" Scope="User">
<Value Profile="(Default)" />
</Setting>
</Settings>
</SettingsFile>

0 comments on commit d346a44

Please sign in to comment.