Skip to content

Commit 0a94aa8

Browse files
authored
Fix sorting of installed-packages column under NativeAOT (#5116)
1 parent 950269d commit 0a94aa8

2 files changed

Lines changed: 43 additions & 7 deletions

File tree

‎src/UniGetUI.Avalonia/Models/PackageCollections.cs‎

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
using System.Collections;
12
using System.Collections.ObjectModel;
23
using System.ComponentModel;
34
using System.Net.Http;
@@ -489,19 +490,34 @@ public void ClearSelection()
489490
public void SetSortDirection(bool ascending) => _ascending = ascending;
490491

491492
/// <summary>Returns <paramref name="items"/> in the current sort order.</summary>
492-
public IEnumerable<PackageWrapper> ApplyToList(IEnumerable<PackageWrapper> items) =>
493-
_ascending
494-
? items.OrderBy(GetSortKey, StringComparer.OrdinalIgnoreCase)
495-
: items.OrderByDescending(GetSortKey, StringComparer.OrdinalIgnoreCase);
493+
public IEnumerable<PackageWrapper> ApplyToList(IEnumerable<PackageWrapper> items)
494+
{
495+
var comparer = Comparer<PackageWrapper>.Create((a, b) => Compare(a, b, CurrentSorter));
496+
return _ascending ? items.OrderBy(w => w, comparer) : items.OrderByDescending(w => w, comparer);
497+
}
498+
499+
// Shared by the "Order by" menu (ApplyToList) and the DataGrid's native column sorting
500+
// (GetColumnComparer) so both order identically. Versions compare semantically via
501+
// CoreTools.Version; a string key can't be used there because that struct has no ToString
502+
// override, so its key would be a constant type name that never sorts.
503+
private static int Compare(PackageWrapper a, PackageWrapper b, Sorter sorter) => sorter switch
504+
{
505+
Sorter.Version => a.Package.NormalizedVersion.CompareTo(b.Package.NormalizedVersion),
506+
Sorter.NewVersion => a.Package.NormalizedNewVersion.CompareTo(b.Package.NormalizedNewVersion),
507+
_ => string.Compare(GetSortKey(a, sorter), GetSortKey(b, sorter), StringComparison.OrdinalIgnoreCase),
508+
};
496509

497-
private string GetSortKey(PackageWrapper w) => CurrentSorter switch
510+
private static string GetSortKey(PackageWrapper w, Sorter sorter) => sorter switch
498511
{
499512
Sorter.Checked => w.IsChecked ? "0" : "1",
500513
Sorter.Name => w.Package.Name,
501514
Sorter.Id => w.Package.Id,
502-
Sorter.Version => w.Package.NormalizedVersion.ToString() ?? string.Empty,
503-
Sorter.NewVersion => w.Package.NormalizedNewVersion.ToString() ?? string.Empty,
504515
Sorter.Source => w.Package.Source.AsString_DisplayName,
505516
_ => w.Package.Name,
506517
};
518+
519+
// Comparer for the DataGrid's native column sorting. Reflection-free (unlike SortMemberPath),
520+
// so it keeps working under full-trim NativeAOT release builds — see issue #5103.
521+
public static IComparer GetColumnComparer(Sorter sorter) =>
522+
Comparer<PackageWrapper>.Create((a, b) => Compare(a, b, sorter));
507523
}

‎src/UniGetUI.Avalonia/Views/SoftwarePages/AbstractPackagesPage.axaml.cs‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,26 @@ or nameof(PackagesPageViewModel.SortAscending))
8484
// Double-click a list row → show details
8585
PackageList.DoubleTapped += (_, _) => _ = ShowDetailsForPackage(SelectedItem);
8686

87+
// Native DataGrid column sorting. The default SortMemberPath path resolves the property by
88+
// reflection, which full-trim NativeAOT release builds strip away — so CanUserSort reports
89+
// false and header clicks are silently dropped (issue #5103). A strongly-typed
90+
// CustomSortComparer plus an explicit CanUserSort keeps Avalonia's built-in sort AOT-safe.
91+
foreach (var col in PackageList.Columns)
92+
{
93+
ObservablePackageCollection.Sorter? sorter = (col.Tag as string) switch
94+
{
95+
"Name" => ObservablePackageCollection.Sorter.Name,
96+
"Id" => ObservablePackageCollection.Sorter.Id,
97+
"Version" => ObservablePackageCollection.Sorter.Version,
98+
"NewVersion" => ObservablePackageCollection.Sorter.NewVersion,
99+
"Source" => ObservablePackageCollection.Sorter.Source,
100+
_ => null,
101+
};
102+
if (sorter is null) continue;
103+
col.CanUserSort = true;
104+
col.CustomSortComparer = ObservablePackageCollection.GetColumnComparer(sorter.Value);
105+
}
106+
87107
// Keyboard shortcuts on the package list. Handled on the tunnel route (and even when
88108
// already handled) because the DataGrid swallows Enter on the bubble route otherwise.
89109
PackageList.AddHandler(KeyDownEvent, PackageList_KeyDown, RoutingStrategies.Tunnel, handledEventsToo: true);

0 commit comments

Comments
 (0)