Adopt DensityValue in Grid to Enable Precise Pixel-Aware Layout#30020
Adopt DensityValue in Grid to Enable Precise Pixel-Aware Layout#30020Copilot wants to merge 46 commits into
Conversation
|
/azp run |
|
Azure Pipelines successfully started running 3 pipeline(s). |
PureWeen
left a comment
There was a problem hiding this comment.
Make sure to always revert any changes that you make to json files that are in the Templates/src directory.
Please update your copilot instructions to help you with this next time.
We never want to commit any of the json files that get modified while copilot is working on a PR
I've reverted all the JSON files in the |
There was a problem hiding this comment.
Remove this extention Method
use the "GetWindow" extention method against the IGridLayout that's inside GridLayoutManager
That will give you an IWindow
You can retrieve the RequestDisplayDensity from that
If the window is null just use 1 for the DisplayDensity
Updated to use |
PureWeen
left a comment
There was a problem hiding this comment.
Remove all changes made to WindowExtention.cs
Removed all changes to WindowExtensions.cs as requested. Commit f6adcd9 reverts the file to its original state. |
mattleibow
left a comment
There was a problem hiding this comment.
Can you add some unit tests to GridLayoutManagerTests that show how this works. Also, use some of the examples form the PR description.
…on pixel precision Co-authored-by: PureWeen <5375137+PureWeen@users.noreply.github.com>
…ate copilot instructions Co-authored-by: PureWeen <5375137+PureWeen@users.noreply.github.com>
Co-authored-by: PureWeen <5375137+PureWeen@users.noreply.github.com>
- Modified DensityValue to store pixels as primary value (RawPx) with Dp as computed property - Updated Definition.Size to use DensityValue instead of double to maintain pixel precision - Modified TryResolveStarsWithDensityAwareness to assign DensityValue directly from pixels without precision loss - Updated all Definition usages throughout GridLayoutManager to use .Dp when dp values needed - Added density parameter to static methods to enable DensityValue creation - Eliminates precision loss from pixelAllocations[i] / density conversion Co-authored-by: PureWeen <5375137+PureWeen@users.noreply.github.com>
- Finalized GetDisplayDensity implementation to handle cases where GetHostedWindow extension method is not available - Maintained IViewWithWindow interface for testing scenarios with proper density injection - All calculations in GridLayoutManager now use pixels as source of truth to eliminate precision loss - TryResolveStarsWithDensityAwareness assigns DensityValue directly from pixel allocations without conversion Co-authored-by: PureWeen <5375137+PureWeen@users.noreply.github.com>
…x star resolution control flow Co-authored-by: PureWeen <5375137+PureWeen@users.noreply.github.com>
…l arrangement Co-authored-by: PureWeen <5375137+PureWeen@users.noreply.github.com>
…for pixel-precise calculations Co-authored-by: PureWeen <5375137+PureWeen@users.noreply.github.com>
…enting future commits Co-authored-by: PureWeen <5375137+PureWeen@users.noreply.github.com>
… is 1.0 Co-authored-by: PureWeen <5375137+PureWeen@users.noreply.github.com>
…l distribution, and update test expectations Co-authored-by: PureWeen <5375137+PureWeen@users.noreply.github.com>
… JSON files Co-authored-by: PureWeen <5375137+PureWeen@users.noreply.github.com>
…test infrastructure Co-authored-by: PureWeen <5375137+PureWeen@users.noreply.github.com>
…test by preserving fractional precision when density=1.0 Co-authored-by: PureWeen <5375137+PureWeen@users.noreply.github.com>
…ck - remove TryExpandStarDefinitionsWithDensityAwareness and fix ResolveStars for loop placement Co-authored-by: PureWeen <5375137+PureWeen@users.noreply.github.com>
…xpansion Co-authored-by: PureWeen <5375137+PureWeen@users.noreply.github.com>
… minimal view sizes, Math.Floor for pixel calculations, and dynamic expected value calculation Co-authored-by: PureWeen <5375137+PureWeen@users.noreply.github.com>
…e type replacement Co-authored-by: PureWeen <5375137+PureWeen@users.noreply.github.com>
…with proper backward compatibility Co-authored-by: PureWeen <5375137+PureWeen@users.noreply.github.com>
Co-authored-by: PureWeen <5375137+PureWeen@users.noreply.github.com>
…Completion Checklist Co-authored-by: PureWeen <5375137+PureWeen@users.noreply.github.com>
Co-authored-by: PureWeen <5375137+PureWeen@users.noreply.github.com>
…ty=1.0 Co-authored-by: PureWeen <5375137+PureWeen@users.noreply.github.com>
Co-authored-by: PureWeen <5375137+PureWeen@users.noreply.github.com>
…fect distribution Co-authored-by: PureWeen <5375137+PureWeen@users.noreply.github.com>
Issues Fixed
Fixes #28117
This PR implements the DensityValue proposal to improve Grid layout precision across density-independent units (dp) and ensure pixel-aligned rendering. It addresses layout inconsistencies caused by fractional pixel results, especially in high-DPI environments where evenly dividing space can lead to rounding errors.
Problem
In high-DPI environments, dividing space equally often results in fractional pixels that don't map cleanly to integers:
This causes:
Solution
1. DensityValue Struct
Introduces an internal
DensityValuestruct that tracks both dp and pixel values:2. Enhanced Grid Layout
Modifies
GridLayoutManager.ResolveStars()to use density-aware distribution when available, falling back to the original algorithm when density information is unavailable.3. Pixel-Perfect Distribution
The
DistributePixelsmethod implements Android's approach of accumulating rounding errors and assigning remainder pixels to the final elements:Validated Scenarios
All scenarios from the original issue are now handled correctly:
[256, 257, 257]pixels[290, 290, 290]pixels[196, 197, 197, 197]pixels[201, 201, 201, 202]pixelsBenefits
Implementation Details
DensityValueis internal, no public API changesFixes #30017.
💬 Share your feedback on Copilot coding agent for the chance to win a $200 gift card! Click here to start the survey.