Skip to content
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

ArrangeCore handling of Stretch and Margins is inconsistent with WPF #16487

Open
stevemonaco opened this issue Jul 26, 2024 · 1 comment
Open
Labels
Milestone

Comments

@stevemonaco
Copy link
Contributor

stevemonaco commented Jul 26, 2024

Describe the bug

When an item with a Stretch alignment is arranged in an area smaller than its size, Avalonia offsets the arrange bounds by half of a computed Margin (centering?). This moves the Control outside of the finalRect bounds up and to the left.

switch (horizontalAlignment)
{
case HorizontalAlignment.Center:
case HorizontalAlignment.Stretch:
originX += (availableSizeMinusMargins.Width - size.Width) / 2;
break;
case HorizontalAlignment.Right:
originX += availableSizeMinusMargins.Width - size.Width;
break;
}
switch (verticalAlignment)
{
case VerticalAlignment.Center:
case VerticalAlignment.Stretch:
originY += (availableSizeMinusMargins.Height - size.Height) / 2;
break;
case VerticalAlignment.Bottom:
originY += availableSizeMinusMargins.Height - size.Height;
break;
}

The WPF behavior is to instead treat this scenario as if the alignment were Left (and/or Top) instead of Stretch. In WPF's definitions of those, anyways.

https://github.com/dotnet/wpf/blob/df3f4bf5568830adc3ed2d3da244ee7a17d551df/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/FrameworkElement.cs#L4827-L4854

border-resize

To Reproduce

<Window ...>
    <Border
        Height="100"
        MinWidth="300"
        Margin="0,0,150,0"
        ClipToBounds="False">
        <Border.Background>
            <LinearGradientBrush StartPoint="0%,0%" EndPoint="100%,100%">
                <GradientStop Offset="0" Color="Orange" />
                <GradientStop Offset="1" Color="Purple" />
            </LinearGradientBrush>
        </Border.Background>
    </Border>
</Window>

(Gradient only to demonstrate the visual more clearly. ClipToBounds shows same behavior whether true or false.)

Expected behavior

I expect either WPF behavior or for this to be documented.

However, I think Avalonia's handling, especially Left alignment (not shown, but the visual gets clipped while staying in-place), is better than WPF's preference to prefer showing the margin more than the visual.

Avalonia version

11.1.0, 11.0.10

OS

Windows

Additional context

This leads to subtle bugs in components that are direct WPF ports, namely WrapPanel. See #16191 (comment)

I have locally been able to fix the problem with either approach: 1. customizing ArrangeCore to be more WPF-like (full margin offsetting instead of halving) or 2. direct fixes to WrapPanel. I need some feedback on this area before I can address the correct area.

@maxkatz6 maxkatz6 added this to the 12.0 milestone Aug 6, 2024
@SlimeNull
Copy link

This also affects the porting of RelativePanel. #16769

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants