Skip to content

Commit 18ae2c4

Browse files
committed
add Project list custom sorting (fixes #48)
1 parent c3cc094 commit 18ae2c4

File tree

3 files changed

+82
-4
lines changed

3 files changed

+82
-4
lines changed

UnityLauncherPro/Data/Project.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
using System;
2-
using System.Diagnostics;
32
using System.Globalization;
43
using System.Windows.Data;
54

@@ -12,9 +11,9 @@ public class Project : IValueConverter
1211
public string Path { set; get; }
1312
public DateTime? Modified { set; get; }
1413
public string Arguments { set; get; }
15-
public string GITBranch { set; get; }
14+
public string GITBranch { set; get; } // TODO rename to Branch
1615
//public string TargetPlatform { set; get; }
17-
public string TargetPlatform { set; get; }
16+
public string TargetPlatform { set; get; } // TODO rename to Platform
1817
public string[] TargetPlatforms { set; get; }
1918
public bool folderExists { set; get; }
2019

UnityLauncherPro/MainWindow.xaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -520,7 +520,7 @@
520520
<Button Style="{StaticResource CustomButton}" ToolTip="Add existing project" x:Name="btnAddProjectFolder" Content="Add Project.." Height="22" Width="78" HorizontalAlignment="Right" VerticalAlignment="Top" Margin="0,4,34,0" Click="BtnAddProjectFolder_Click" BorderBrush="{x:Null}" TabIndex="10" />
521521
<Button Style="{StaticResource CustomButton}" ToolTip="Refresh list (F5)" x:Name="btnRefreshProjectList" Content="" Height="22" Width="22" HorizontalAlignment="Right" VerticalAlignment="Top" FontSize="16" Margin="0,4,3,0" Padding="1,-2,1,1" BorderBrush="{x:Null}" Click="BtnRefreshProjectList_Click" TabIndex="11"/>
522522

523-
<DataGrid x:Name="gridRecent" HorizontalContentAlignment="Left" VerticalContentAlignment="Top" SelectionMode="Single" Margin="4,30,2,42" CanUserAddRows="False" Background="{x:Null}" BorderBrush="{x:Null}" ColumnHeaderStyle="{StaticResource HeaderStyle}" Padding="0" HorizontalScrollBarVisibility="Disabled" HeadersVisibility="Column" Foreground="{DynamicResource ThemeGridForeground}" HorizontalGridLinesBrush="{DynamicResource ThemeDatagridLines}" VerticalGridLinesBrush="{DynamicResource ThemeGridVerticalGridLines}" AutoGenerateColumns="False" PreviewKeyDown="GridRecent_PreviewKeyDown" Loaded="GridRecent_Loaded" TabIndex="2" CellEditEnding="GridRecent_CellEditEnding" PreviewMouseDoubleClick="GridRecent_PreviewMouseDoubleClick" ContextMenuOpening="GridRecent_ContextMenuOpening" BeginningEdit="GridRecent_BeginningEdit" RowHeight="21" ColumnReordered="GridRecent_ColumnReordered" >
523+
<DataGrid x:Name="gridRecent" HorizontalContentAlignment="Left" VerticalContentAlignment="Top" SelectionMode="Single" Margin="4,30,2,42" CanUserAddRows="False" Background="{x:Null}" BorderBrush="{x:Null}" ColumnHeaderStyle="{StaticResource HeaderStyle}" Padding="0" HorizontalScrollBarVisibility="Disabled" HeadersVisibility="Column" Foreground="{DynamicResource ThemeGridForeground}" HorizontalGridLinesBrush="{DynamicResource ThemeDatagridLines}" VerticalGridLinesBrush="{DynamicResource ThemeGridVerticalGridLines}" AutoGenerateColumns="False" PreviewKeyDown="GridRecent_PreviewKeyDown" Loaded="GridRecent_Loaded" TabIndex="2" CellEditEnding="GridRecent_CellEditEnding" PreviewMouseDoubleClick="GridRecent_PreviewMouseDoubleClick" ContextMenuOpening="GridRecent_ContextMenuOpening" BeginningEdit="GridRecent_BeginningEdit" RowHeight="21" ColumnReordered="GridRecent_ColumnReordered" Sorting="gridRecent_Sorting" >
524524

525525
<DataGrid.CommandBindings>
526526
<CommandBinding Command="ApplicationCommands.Copy" Executed="CopyRowFolderToClipBoard" CanExecute="CanExecute_Copy"/>

UnityLauncherPro/MainWindow.xaml.cs

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2563,6 +2563,85 @@ private void menuItemCopyPathToClipboard_Click(object sender, RoutedEventArgs e)
25632563
Clipboard.SetText(path);
25642564
}
25652565

2566+
private void gridRecent_Sorting(object sender, DataGridSortingEventArgs e)
2567+
{
2568+
SortHandler(sender, e);
2569+
}
2570+
2571+
// https://stackoverflow.com/a/2130557/5452781
2572+
void SortHandler(object sender, DataGridSortingEventArgs e)
2573+
{
2574+
DataGridColumn column = e.Column;
2575+
2576+
//Console.WriteLine("Sorted by " + column.Header);
2577+
2578+
IComparer comparer = null;
2579+
2580+
// prevent the built-in sort from sorting
2581+
e.Handled = true;
2582+
2583+
ListSortDirection direction = (column.SortDirection != ListSortDirection.Ascending) ? ListSortDirection.Ascending : ListSortDirection.Descending;
2584+
2585+
//set the sort order on the column
2586+
column.SortDirection = direction;
2587+
2588+
//use a ListCollectionView to do the sort.
2589+
ListCollectionView lcv = (ListCollectionView)CollectionViewSource.GetDefaultView(gridRecent.ItemsSource);
2590+
2591+
comparer = new CustomProjectSort(direction, column.Header.ToString());
2592+
2593+
//apply the sort
2594+
lcv.CustomSort = comparer;
2595+
}
2596+
2597+
public class CustomProjectSort : IComparer
2598+
{
2599+
private ListSortDirection direction;
2600+
private string sortBy;
2601+
2602+
public CustomProjectSort(ListSortDirection direction, string sortBy)
2603+
{
2604+
this.direction = direction;
2605+
this.sortBy = sortBy;
2606+
}
2607+
2608+
// TODO cleanup this
2609+
public int Compare(Object a, Object b)
2610+
{
2611+
switch (sortBy)
2612+
{
2613+
case "Project":
2614+
return direction == ListSortDirection.Ascending ? ((Project)a).Title.CompareTo(((Project)b).Title) : ((Project)b).Title.CompareTo(((Project)a).Title);
2615+
case "Version":
2616+
return direction == ListSortDirection.Ascending ? Tools.VersionAsInt(((Project)a).Version).CompareTo(Tools.VersionAsInt(((Project)b).Version)) : Tools.VersionAsInt(((Project)b).Version).CompareTo(Tools.VersionAsInt(((Project)a).Version));
2617+
case "Path":
2618+
return direction == ListSortDirection.Ascending ? ((Project)a).Path.CompareTo(((Project)b).Path) : ((Project)b).Path.CompareTo(((Project)a).Path);
2619+
case "Modified":
2620+
return direction == ListSortDirection.Ascending ? ((DateTime)((Project)a).Modified).CompareTo(((Project)b).Modified) : ((DateTime)((Project)b).Modified).CompareTo(((Project)a).Modified);
2621+
case "Arguments":
2622+
// handle null values
2623+
if (((Project)a).Arguments == null && ((Project)b).Arguments == null) return 0;
2624+
if (((Project)a).Arguments == null) return direction == ListSortDirection.Ascending ? -1 : 1;
2625+
if (((Project)b).Arguments == null) return direction == ListSortDirection.Ascending ? 1 : -1;
2626+
return direction == ListSortDirection.Ascending ? ((Project)a).Arguments.CompareTo(((Project)b).Arguments) : ((Project)b).Arguments.CompareTo(((Project)a).Arguments);
2627+
case "Branch":
2628+
// handle null values
2629+
if (((Project)a).GITBranch == null && ((Project)b).GITBranch == null) return 0;
2630+
if (((Project)a).GITBranch == null) return direction == ListSortDirection.Ascending ? -1 : 1;
2631+
if (((Project)b).GITBranch == null) return direction == ListSortDirection.Ascending ? 1 : -1;
2632+
return direction == ListSortDirection.Ascending ? ((Project)a).GITBranch.CompareTo(((Project)b).GITBranch) : ((Project)b).GITBranch.CompareTo(((Project)a).GITBranch);
2633+
case "Platform":
2634+
// handle null values
2635+
if (((Project)a).TargetPlatform == null && ((Project)b).TargetPlatform == null) return 0;
2636+
if (((Project)a).TargetPlatform == null) return direction == ListSortDirection.Ascending ? -1 : 1;
2637+
if (((Project)b).TargetPlatform == null) return direction == ListSortDirection.Ascending ? 1 : -1;
2638+
return direction == ListSortDirection.Ascending ? ((Project)a).TargetPlatform.CompareTo(((Project)b).TargetPlatform) : ((Project)b).TargetPlatform.CompareTo(((Project)a).TargetPlatform);
2639+
default:
2640+
return 0;
2641+
}
2642+
}
2643+
}
2644+
25662645
//private void BtnBrowseTemplateUnityPackagesFolder_Click(object sender, RoutedEventArgs e)
25672646
//{
25682647
// var folder = Tools.BrowseForOutputFolder("Select unitypackage Templates folder");

0 commit comments

Comments
 (0)