Skip to content

add spacing to uniform grid - issue 8406 #17993

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

Merged
merged 7 commits into from
Jan 22, 2025

Conversation

Enzx
Copy link
Contributor

@Enzx Enzx commented Jan 18, 2025

What does the pull request do?

This pull request addresses this Issue:
#8406 (Marked as Enhancement)

This pull request adds RowSpacing and ColumnSpacing properties to the UniformGrid control in Avalonia. These properties allow developers to define the spacing between rows and columns, making creating consistent and flexible layouts easier. This update addresses limitations in the current UniformGrid implementation, such as the lack of built-in support for spacing, which previously required cumbersome workarounds.

What is the current behavior?

Currently, UniformGrid does not support row or column spacing. Developers often use workarounds such as:

  • Adding fake spacing rows/columns in Grid.
  • Manually adjusting Margin values for individual children.
    These methods are error-prone, hard to maintain, and do not scale well for larger layouts.

What is the updated/expected behavior with this PR?

With this PR, developers can set the RowSpacing and ColumnSpacing properties directly on UniformGrid. The grid will automatically apply the specified spacing while maintaining uniform child sizes and alignment.

How to test the PR:

  1. Add a UniformGrid with various children.
  2. Set RowSpacing and ColumnSpacing to non-zero values.
  3. Verify that:
    • Spacing is applied correctly between rows and columns.
    • Child elements remain uniformly sized and aligned.
    • Invisible children are ignored while maintaining the proper layout.

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

  • New Properties: Added RowSpacing and ColumnSpacing as styled properties with a default value of 0.
  • MeasureOverride: Adjusts available size for children to account for spacing when calculating the desired size.
  • ArrangeOverride: Includes spacing when positioning children within the grid.
  • UpdateRowsAndColumns: Ensures spacing compatibility with dynamically calculated row/column counts.
  • Unit Tests: Added extensive tests to verify layout behavior with and without spacing.

Checklist

Breaking changes

None. The default values for RowSpacing and ColumnSpacing are 0, preserving existing behavior for layouts that do not use these properties.

Obsoletions / Deprecations

None.

Fixed issues

#8406

@avaloniaui-bot
Copy link

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

@cla-avalonia
Copy link
Collaborator

cla-avalonia commented Jan 18, 2025

  • All contributors have signed the CLA.

@Enzx
Copy link
Contributor Author

Enzx commented Jan 18, 2025

@cla-avalonia agree

@DoubleDBE
Copy link

Awesome! These properties are for certain missing on a regular Grid as well. But, yeah... 🙄

#5152
#8406

@timunie timunie added the needs-api-review The PR adds new public APIs that should be reviewed. label Jan 18, 2025
@Enzx Enzx marked this pull request as ready for review January 18, 2025 19:59
@MrJul
Copy link
Member

MrJul commented Jan 20, 2025

Diff for API review:

 namespace Avalonia.Controls.Primitives
 {
     public class UniformGrid
     {
+        public static readonly StyledProperty<double> RowSpacingProperty;
+        public static readonly StyledProperty<double> ColumnSpacingProperty;
+        public double RowSpacing { get; set; }
+        public double ColumnSpacing { get; set; }
     }
 }

@avaloniaui-bot
Copy link

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

@MrJul
Copy link
Member

MrJul commented Jan 21, 2025

Notes from the API review:

We're happy with the proposed API, which matches WinUI.
(Ideally we'd like to add it to Grid in the future.)

@MrJul MrJul added api-approved The new public APIs have been approved. and removed needs-api-review The PR adds new public APIs that should be reviewed. labels Jan 21, 2025
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.

A couple of changes are needed and this PR will be good to merge.
The unit tests are really appreciated!

@avaloniaui-bot
Copy link

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

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.

LGTM, thank you!

@MrJul MrJul added this pull request to the merge queue Jan 22, 2025
Merged via the queue into AvaloniaUI:master with commit fdadfb8 Jan 22, 2025
10 checks passed
@Enzx Enzx deleted the add-uniformgrid-spacing-issue8406 branch January 22, 2025 10:54
@MrJul MrJul added feature and removed enhancement labels Jan 23, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
api-approved The new public APIs have been approved. feature
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants