-
-
Notifications
You must be signed in to change notification settings - Fork 2.4k
Code Quality: Refactor MeasureOverride method to early exit for null or empty context #17483
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -30,30 +30,40 @@ public BreadcrumbBarLayout(BreadcrumbBar breadcrumb) | |
|
||
protected override Size MeasureOverride(NonVirtualizingLayoutContext context, Size availableSize) | ||
{ | ||
var accumulatedSize = new Size(0, 0); | ||
// Early exit for null or empty context | ||
if (context?.Children == null || context.Children.Count == 0) | ||
{ | ||
return new Size(0, 0); | ||
} | ||
|
||
_availableSize = availableSize; | ||
|
||
var indexAfterEllipsis = GetFirstIndexToRender(context); | ||
var maxHeight = 0; | ||
var totalWidth = 0; | ||
var children = context.Children; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Caching like this is redundant. |
||
|
||
// Cache ellipsis button reference from first item | ||
_ellipsisButton ??= children[0] as BreadcrumbBarItem; | ||
|
||
// Go through all items and measure them | ||
for (int index = 0; index < context.Children.Count; index++) | ||
for (int index = 0; index < children.Count; index++) | ||
{ | ||
if (context.Children[index] is BreadcrumbBarItem breadcrumbItem) | ||
if (children[index] is not BreadcrumbBarItem breadcrumbItem) | ||
continue; | ||
|
||
breadcrumbItem.Measure(availableSize); | ||
if (index >= indexAfterEllipsis) | ||
{ | ||
breadcrumbItem.Measure(availableSize); | ||
accumulatedSize.Width += index < indexAfterEllipsis ? 0 : breadcrumbItem.DesiredSize.Width; | ||
accumulatedSize.Height = Math.Max(accumulatedSize.Height, breadcrumbItem.DesiredSize.Height); | ||
totalWidth += breadcrumbItem.DesiredSize.Width; | ||
} | ||
maxHeight = Math.Max(maxHeight, breadcrumbItem.DesiredSize.Height); | ||
} | ||
|
||
// Get a reference to the ellipsis item | ||
if (context.Children.Count > 0) | ||
_ellipsisButton ??= context.Children[0] as BreadcrumbBarItem; | ||
|
||
// Sets the ellipsis item's visibility based on whether the items are overflowing | ||
EllipsisIsRendered = indexAfterEllipsis is not 0; | ||
// Update ellipsis visibility based on whether items are hidden | ||
EllipsisIsRendered = indexAfterEllipsis > 0; | ||
|
||
return accumulatedSize; | ||
return new Size(totalWidth, maxHeight); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why you don't use Size instead, like I did? And I feel like the code changes in the middle are unnecessary, is there a reason why you change there? |
||
} | ||
|
||
protected override Size ArrangeOverride(NonVirtualizingLayoutContext context, Size finalSize) | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.