Skip to content

Commit 4f666da

Browse files
committed
Remove duplicated sorting in the measure pass
1 parent 6ba9915 commit 4f666da

1 file changed

Lines changed: 11 additions & 10 deletions

File tree

components/Ribbon/src/RibbonPanel.cs

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,23 +17,25 @@ protected override Size MeasureOverride(Size availableSize)
1717
// We try to limit the layout changes if the parent scrollviewer is sending values with small changes.
1818
availableSize.Width = Math.Floor(availableSize.Width);
1919

20-
var childrenByPriority = Children.OrderBy(c => c is RibbonCollapsibleGroup collapsibleGroup ? collapsibleGroup.Priority : 0);
20+
var childrenByPriority = Children
21+
.OrderBy(c => c is RibbonCollapsibleGroup collapsibleGroup ? collapsibleGroup.Priority : 0)
22+
.ToList();
23+
2124
var desiredSize = new Size();
2225
foreach (var child in childrenByPriority)
2326
{
2427
var collapsibleGroup = child as RibbonCollapsibleGroup;
2528
var requestedWidths = collapsibleGroup?.RequestedWidths;
26-
if (requestedWidths is null || collapsibleGroup?.State == Visibility.Collapsed)
29+
if (requestedWidths is null || collapsibleGroup!.State == Visibility.Collapsed)
2730
{
2831
child.Measure(GroupAvailableSize);
2932
}
3033
else
3134
{
3235
// Get the closest match to remainingWidth or use infinite size if we do not have any match.
3336
var remainingWidth = availableSize.Width - desiredSize.Width;
34-
//var requestedWidth = requestedWidths.LastOrDefault(w => w <= remainingWidth, defaultValue: double.PositiveInfinity);
35-
var matchingWidths = requestedWidths.Where(w => w <= remainingWidth);
36-
var requestedWidth = matchingWidths.Any() ? matchingWidths.Last() : double.PositiveInfinity;
37+
var matchingWidth = requestedWidths.LastOrDefault(w => w <= remainingWidth);
38+
var requestedWidth = matchingWidth > 0 ? matchingWidth : double.PositiveInfinity;
3739
var fixedSize = new Size(requestedWidth, availableSize.Height);
3840
child.Measure(fixedSize);
3941
}
@@ -46,7 +48,7 @@ protected override Size MeasureOverride(Size availableSize)
4648
{
4749
// We need to collapse some groups.
4850
// If there is no priority order we assume that the last items are the one which should collapse first.
49-
var groups = Children.OfType<RibbonCollapsibleGroup>().Reverse().Where(g => g.State == Visibility.Visible).OrderByDescending(g => g.Priority);
51+
var groups = childrenByPriority.OfType<RibbonCollapsibleGroup>().Reverse().Where(g => g.State == Visibility.Visible);
5052
foreach (var group in groups)
5153
{
5254
group.State = Visibility.Collapsed;
@@ -76,7 +78,7 @@ protected override Size MeasureOverride(Size availableSize)
7678
else if (desiredSize.Width < availableSize.Width)
7779
{
7880
// We have more space than needed, we check if we can expand some groups
79-
var groups = Children.OfType<RibbonCollapsibleGroup>().Where(g => g.State == Visibility.Collapsed).OrderBy(g => g.Priority);
81+
var groups = childrenByPriority.OfType<RibbonCollapsibleGroup>().Where(g => g.State == Visibility.Collapsed);
8082
foreach (var group in groups)
8183
{
8284
var previousSize = group.DesiredSize;
@@ -91,9 +93,8 @@ protected override Size MeasureOverride(Size availableSize)
9193
{
9294
// Get the closest match to remainingWidth or use infinite size if we do not have any match.
9395
var remainingWidth = availableSize.Width + previousSize.Width - desiredSize.Width;
94-
//var requestedWidth = requestedWidths.LastOrDefault(w => w <= remainingWidth, defaultValue: double.PositiveInfinity);
95-
var matchingWidths = requestedWidths.Where(w => w <= remainingWidth);
96-
var requestedWidth = matchingWidths.Any() ? matchingWidths.Last() : double.PositiveInfinity;
96+
var matchingWidth = requestedWidths.LastOrDefault(w => w <= remainingWidth);
97+
var requestedWidth = matchingWidth > 0 ? matchingWidth : double.PositiveInfinity;
9798
var fixedSize = new Size(requestedWidth, availableSize.Height);
9899
group.Measure(fixedSize);
99100
}

0 commit comments

Comments
 (0)