Skip to content

Conversation

@Soar360
Copy link

@Soar360 Soar360 commented Oct 31, 2025

What does the pull request do?

When UseLayoutRounding is enabled, round the measured cell sizes in UniformGrid to avoid pixel-alignment issues.

Reference file:

  • src/Avalonia.Controls/Primitives/UniformGrid.cs
image

What is the current behavior?

With UseLayoutRounding set to true, UniformGrid didn't apply layout rounding during the measure pass, causing pixel alignment artifacts and small layout offsets.

What is the updated/expected behavior with this PR?

Apply layout rounding to the calculated cell width/height during measurement when UseLayoutRounding is enabled so measurements match the final layout.

How to test:

  1. Compare a UniformGrid layout with fractional per-cell sizes and ColumnSpacing/RowSpacing with UseLayoutRounding = true and false.
  2. Verify that children.Measure receives the same available size as before (no rounding) and that final arranged cells align cleanly when UseLayoutRounding is true.

How was the solution implemented (if it's not obvious)?

  • Kept computation of childAvailableSize and passed it unmodified to child.Measure(childAvailableSize).
  • After measuring all children and computing maxWidth/maxHeight from DesiredSize, added a conditional block:
    • if (UseLayoutRounding) then if the value is finite (not Infinity/NaN) call Math.Round on maxWidth and maxHeight.
  • Compute totalWidth/totalHeight using potentially-rounded maxWidth/maxHeight as before.
  • Left ArrangeOverride in place; it still computes per-cell width/height from finalSize and applies Math.Round there when UseLayoutRounding is true.
  • Added a short code comment explaining that rounding is applied to measured max sizes to avoid changing measurement inputs to children.

Implementation notes:

  • Uses Math.Round and guards with !double.IsInfinity(...) && !double.IsNaN(...), matching existing repository style.
  • No other files modified.

Checklist

  • Added unit tests (if possible)?

Breaking changes

  • None expected. This changes internal measurement rounding behavior only when UseLayoutRounding is enabled; it avoids changing how children are measured but changes the MeasureOverride return value to be pixel-aligned when rounding is enabled.

Obsoletions / Deprecations

  • None.

Fixed issues

  • None specific. (If you have a related issue number, add: Fixes #)

Added layout rounding for child measurements in UniformGrid.
Removed comment about measuring children in UniformGrid.
@Soar360 Soar360 changed the title Patch 1 UniformGrid: Round measured cell sizes when UseLayoutRounding is enabled Oct 31, 2025
@avaloniaui-bot
Copy link

You can test this PR using the following package version. 12.0.999-cibuild0059692-alpha. (feed url: https://nuget-feed-all.avaloniaui.net/v3/index.json) [PRBUILDID]

@cla-avalonia
Copy link
Collaborator

cla-avalonia commented Oct 31, 2025

  • All contributors have signed the CLA.

@Soar360
Copy link
Author

Soar360 commented Oct 31, 2025

@cla-avalonia agree

Copy link
Member

@MrJul MrJul left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you. This absolutely needs an unit test.

// used for children.Measure(...) which could change how children measure themselves.
if (UseLayoutRounding)
{
if (!double.IsInfinity(maxWidth) && !double.IsNaN(maxWidth))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: use !MathUtilities.IsFinite() instead (I'm leaving this once, but this applies to the 4 added if conditions).

{
if (!double.IsInfinity(maxWidth) && !double.IsNaN(maxWidth))
{
maxWidth = Math.Round(maxWidth);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This doesn't seem right, we normally use LayoutHelper.RoundLayoutXxx methods with a scale to adjust for the current DPI.

}
}

// If layout rounding is enabled, round the measured per-cell unit size (maxWidth/maxHeight).
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reading the comment, I understand that it's now possible for the child control size not to match the grid, which would be unexpected (controls won't be uniform anymore).

Could you please provide a sample to compare the before and after behaviors?
Plus, a matching unit test is needed.

@avaloniaui-bot
Copy link

You can test this PR using the following package version. 12.0.999-cibuild0059714-alpha. (feed url: https://nuget-feed-all.avaloniaui.net/v3/index.json) [PRBUILDID]

@avaloniaui-bot
Copy link

You can test this PR using the following package version. 12.0.999-cibuild0059718-alpha. (feed url: https://nuget-feed-all.avaloniaui.net/v3/index.json) [PRBUILDID]

@avaloniaui-bot
Copy link

You can test this PR using the following package version. 12.0.999-cibuild0059782-alpha. (feed url: https://nuget-feed-all.avaloniaui.net/v3/index.json) [PRBUILDID]

@Soar360 Soar360 requested a review from MrJul November 5, 2025 11:23
@avaloniaui-bot
Copy link

You can test this PR using the following package version. 12.0.999-cibuild0059990-alpha. (feed url: https://nuget-feed-all.avaloniaui.net/v3/index.json) [PRBUILDID]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants