Skip to content

Commit

Permalink
Merge pull request #893 from unoplatform/mergify/bp/release/stable/5.…
Browse files Browse the repository at this point in the history
…0/pr-892

fix(autoLayout): when hug and have a alignment (backport #892)
  • Loading branch information
carldebilly authored Oct 31, 2023
2 parents fce991a + e77b4da commit da183f1
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 6 deletions.
41 changes: 41 additions & 0 deletions src/Uno.Toolkit.RuntimeTests/Tests/AutoLayoutTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -484,4 +484,45 @@ public async Task When_Fixed_Dimensions_Padding_And_SpaceBetween_Vertical()
Assert.AreEqual(160, SUT.ActualHeight);
Assert.AreEqual(122, SUT.ActualWidth);
}

[TestMethod]
[RequiresFullWindow]
public async Task When_Hug_With_CounterAlignment()
{
var SUT = new AutoLayout()
{
PrimaryAxisAlignment = AutoLayoutAlignment.Center,
Background = new SolidColorBrush(Color.FromArgb(255, 0, 0, 255)),
Width = 300,
Height = 100
};

var textBlock = new TextBlock()
{
Text = "Oh no!",
};

var button = new Button()
{
Content = "Something went wrong",
};

AutoLayout.SetCounterAlignment(textBlock, AutoLayoutAlignment.Center);
AutoLayout.SetCounterAlignment(button, AutoLayoutAlignment.End);

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

await UnitTestUIContentHelperEx.SetContentAndWait(SUT);


var textBlockTransform = textBlock.TransformToVisual(SUT).TransformPoint(new Windows.Foundation.Point(0, 0));
var buttonTransform = button.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));
Assert.AreEqual(Math.Ceiling(SUT.ActualWidth - button.ActualWidth), Math.Ceiling(buttonTransform!.X));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -327,7 +327,7 @@ private static double MeasureChild(

if (child as FrameworkElement is { } frameworkElement)
{
UpdateCounterAlignmentToStretch(ref frameworkElement, isOrientationHorizontal, isPrimaryAlignmentStretch, isCounterAlignmentStretch);
UpdateCounterAlignment(ref frameworkElement, isOrientationHorizontal, isPrimaryAlignmentStretch, GetCounterAlignment(child));

var isStretch = isOrientationHorizontal ?
frameworkElement.VerticalAlignment is VerticalAlignment.Stretch :
Expand Down Expand Up @@ -481,19 +481,17 @@ private enum AutoLayoutRole : byte
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static void UpdateCounterAlignmentToStretch(ref FrameworkElement frameworkElement, bool isHorizontal, bool isPrimaryAlignmentStretch, bool isCounterAlignmentStretch)
private static void UpdateCounterAlignment(ref FrameworkElement frameworkElement, bool isHorizontal, bool isPrimaryAlignmentStretch, AutoLayoutAlignment counterAlignment)
{
if (isHorizontal)
{
frameworkElement.HorizontalAlignment = isPrimaryAlignmentStretch ? HorizontalAlignment.Stretch : frameworkElement.HorizontalAlignment;
frameworkElement.VerticalAlignment = isCounterAlignmentStretch ? VerticalAlignment.Stretch : frameworkElement.VerticalAlignment;
frameworkElement.VerticalAlignment = counterAlignment.ToVerticalAlignment();
}
else
{
frameworkElement.VerticalAlignment = isPrimaryAlignmentStretch ? VerticalAlignment.Stretch : frameworkElement.VerticalAlignment;
frameworkElement.HorizontalAlignment = isCounterAlignmentStretch ? HorizontalAlignment.Stretch : frameworkElement.HorizontalAlignment;
frameworkElement.HorizontalAlignment = counterAlignment.ToHorizontalAlignment();
}
}


}

0 comments on commit da183f1

Please sign in to comment.