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

fix: hug center now properly centered #933

Merged
merged 1 commit into from
Nov 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 39 additions & 0 deletions src/Uno.Toolkit.RuntimeTests/Tests/AutoLayoutTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -573,4 +573,43 @@ public async Task When_Hug_With_CounterAlignment()
Assert.AreEqual(Math.Ceiling(autoLayoutAcutalWidth - textBlockCenter), Math.Ceiling(textBlockTransform!.X));
Assert.AreEqual(Math.Ceiling(SUT.ActualWidth - button.ActualWidth), Math.Ceiling(buttonTransform!.X));
}

[TestMethod]
[RequiresFullWindow]
public async Task When_Hug_With_Padding()
{
var SUT = new AutoLayout()
{
PrimaryAxisAlignment = AutoLayoutAlignment.Center,
Background = new SolidColorBrush(Color.FromArgb(255, 0, 0, 255)),
Width = 300,
Height = 300,
Padding = new Thickness(50)
Copy link
Contributor

Choose a reason for hiding this comment

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

What if we have cases with no uniform Padding?

};

var autolayout = new AutoLayout()
{
PrimaryAxisAlignment = AutoLayoutAlignment.Center,
};

var textBlock = new TextBlock()
{
Text = "should be center",
};

AutoLayout.SetCounterAlignment(textBlock, AutoLayoutAlignment.Center);
AutoLayout.SetCounterAlignment(autolayout, AutoLayoutAlignment.Center);

SUT.Children.Add(autolayout);
autolayout.Children.Add(textBlock);

await UnitTestUIContentHelperEx.SetContentAndWait(SUT);

var textBlockTransform = textBlock.TransformToVisual(SUT).TransformPoint(new Windows.Foundation.Point(0, 0));

var autoLayoutAcutalWidth = SUT.ActualWidth / 2;
var textBlockCenter = textBlock.ActualWidth / 2;

Assert.AreEqual(Math.Ceiling(autoLayoutAcutalWidth - textBlockCenter), Math.Ceiling(textBlockTransform!.X));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ protected override Size ArrangeOverride(Size finalSize)

// Calculate the position of the child by applying the alignment instructions
var counterAlignment = GetCounterAlignment(child.Element);
var isStretch = counterAlignment is AutoLayoutAlignment.Stretch || (child.Element is FrameworkElement fe && fe.GetCounterLength(orientation) is double.NaN);
var isStretch = counterAlignment is AutoLayoutAlignment.Stretch || (child.Element is FrameworkElement fe && fe.GetCounterLength(orientation) is double.NaN) && child.Role is not AutoLayoutRole.Hug;
var haveCounterStartPadding = isStretch || counterAlignment is AutoLayoutAlignment.Start;
var counterStartPadding = haveCounterStartPadding ? (isHorizontal ? padding.Top : padding.Left) : 0;

Expand Down Expand Up @@ -287,13 +287,13 @@ private static void ApplyMinMaxValues(UIElement element, Orientation orientation

[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static double ComputeCounterAlignmentOffset(
AutoLayoutAlignment autoLayoutAlignment,
AutoLayoutAlignment counterAlignment,
double childCounterLength,
double availableCounterLength,
Thickness padding,
bool isHorizontal)
{
switch (autoLayoutAlignment)
switch (counterAlignment)
{
case AutoLayoutAlignment.Center:
var counterStartPadding = isHorizontal ? padding.Top : padding.Left;
Expand Down
Loading