From add0e87546df75db37f1fb6a834d56f1c3583649 Mon Sep 17 00:00:00 2001 From: Martin Zikmund Date: Wed, 4 Dec 2024 17:16:01 +0100 Subject: [PATCH 1/4] test: Translation should be applied after Loaded --- .../Tests/Windows_UI_Xaml/Given_UIElement.cs | 22 +++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/src/Uno.UI.RuntimeTests/Tests/Windows_UI_Xaml/Given_UIElement.cs b/src/Uno.UI.RuntimeTests/Tests/Windows_UI_Xaml/Given_UIElement.cs index 47a99a8412ef..967f9f2d0a3f 100644 --- a/src/Uno.UI.RuntimeTests/Tests/Windows_UI_Xaml/Given_UIElement.cs +++ b/src/Uno.UI.RuntimeTests/Tests/Windows_UI_Xaml/Given_UIElement.cs @@ -1566,6 +1566,28 @@ public async Task When_Element_Has_Translation_And_Visual_Has_Offset() } #endif + [TestMethod] + [RunsOnUIThread] + public async Task When_Translation_On_Load() + { + var sut = new Rectangle() + { + Width = 100, + Height = 100, + Fill = new SolidColorBrush(Microsoft.UI.Colors.Blue), + }; + var canvas = new Canvas(); + canvas.Width = 200; + canvas.Height = 200; + canvas.Children.Add(sut); + TestServices.WindowHelper.WindowContent = canvas; + await TestServices.WindowHelper.WaitForLoaded(sut); + sut.Translation += new Vector3(200, 0, 128); + await TestServices.WindowHelper.WaitForIdle(); + var bitmap = await UITestHelper.ScreenShot(canvas); + ImageAssert.DoesNotHaveColorAt(bitmap, new Windows.Foundation.Point(50, 50), Microsoft.UI.Colors.Blue, tolerance: 25); + } + #if HAS_UNO [TestMethod] [RunsOnUIThread] From db8bd3c6aec0a96ab77c4147655ab98cd5ed0958 Mon Sep 17 00:00:00 2001 From: Martin Zikmund Date: Wed, 4 Dec 2024 17:16:15 +0100 Subject: [PATCH 2/4] fix: Ensure element translation is applied after Loaded --- src/Uno.UI/UI/Xaml/UIElement.skia.cs | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/src/Uno.UI/UI/Xaml/UIElement.skia.cs b/src/Uno.UI/UI/Xaml/UIElement.skia.cs index ea5427c5b61d..319b11b4f756 100644 --- a/src/Uno.UI/UI/Xaml/UIElement.skia.cs +++ b/src/Uno.UI/UI/Xaml/UIElement.skia.cs @@ -36,6 +36,7 @@ public partial class UIElement : DependencyObject, IVisualElement, IVisualElemen private protected ContainerVisual _visual; private Rect _lastFinalRect; private Rect? _lastClippedFrame; + private Vector3 _lastTranslation; public UIElement() { @@ -278,8 +279,10 @@ internal void ArrangeVisual(Rect finalRect, Rect? clippedFrame = default) var oldFinalRect = _lastFinalRect; var oldClippedFrame = _lastClippedFrame; + var oldTranslation = _lastTranslation; _lastFinalRect = finalRect; _lastClippedFrame = clippedFrame; + _lastTranslation = _translation; var oldRect = oldFinalRect; var newRect = finalRect; @@ -287,7 +290,10 @@ internal void ArrangeVisual(Rect finalRect, Rect? clippedFrame = default) var oldClip = oldClippedFrame; var newClip = clippedFrame; - if (oldRect != newRect || oldClip != newClip || (_renderTransform?.FlowDirectionTransform ?? Matrix3x2.Identity) != GetFlowDirectionTransform()) + if (oldRect != newRect || + oldClip != newClip || + oldTranslation != _translation || + (_renderTransform?.FlowDirectionTransform ?? Matrix3x2.Identity) != GetFlowDirectionTransform()) { if ( newRect.Width < 0 From 2f8eebee3a0828eca745ce5b197b7026bdbc928e Mon Sep 17 00:00:00 2001 From: Martin Zikmund Date: Fri, 6 Dec 2024 15:05:47 +0100 Subject: [PATCH 3/4] chore: Warn user when non-Z-only Translation is set on UIElement --- src/Uno.UI/UI/Xaml/UIElement.cs | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/src/Uno.UI/UI/Xaml/UIElement.cs b/src/Uno.UI/UI/Xaml/UIElement.cs index 9015f2d5c3bb..faa2b7fab4e8 100644 --- a/src/Uno.UI/UI/Xaml/UIElement.cs +++ b/src/Uno.UI/UI/Xaml/UIElement.cs @@ -47,6 +47,9 @@ namespace Microsoft.UI.Xaml public partial class UIElement : DependencyObject, IXUidProvider { private protected static bool _traceLayoutCycle; +#if !__SKIA__ + private bool _warnedAboutTranslation; +#endif private static readonly TypedEventHandler OnBringIntoViewRequestedHandler = (UIElement sender, BringIntoViewRequestedEventArgs args) => sender.OnBringIntoViewRequested(args); @@ -265,6 +268,19 @@ public Vector3 Translation if (_translation != value) { _translation = value; + +#if !__SKIA__ + if (!_warnedAboutTranslation && + (_translation.X != 0 || _translation.Y != 0)) + { + _warnedAboutTranslation = true; + if (this.Log().IsEnabled(LogLevel.Warning)) + { + this.Log().LogWarning("Translation supports only Z-axis on this target."); + } + } +#endif + UpdateShadow(); InvalidateArrange(); } From 640a0c0f8fc01e20f42e3cb1bd342e59e13afad2 Mon Sep 17 00:00:00 2001 From: Martin Zikmund Date: Fri, 6 Dec 2024 15:06:00 +0100 Subject: [PATCH 4/4] chore: Improve test to validate actual translation --- .../Tests/Windows_UI_Xaml/Given_UIElement.cs | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/src/Uno.UI.RuntimeTests/Tests/Windows_UI_Xaml/Given_UIElement.cs b/src/Uno.UI.RuntimeTests/Tests/Windows_UI_Xaml/Given_UIElement.cs index 967f9f2d0a3f..3a62d081f25d 100644 --- a/src/Uno.UI.RuntimeTests/Tests/Windows_UI_Xaml/Given_UIElement.cs +++ b/src/Uno.UI.RuntimeTests/Tests/Windows_UI_Xaml/Given_UIElement.cs @@ -1568,6 +1568,9 @@ public async Task When_Element_Has_Translation_And_Visual_Has_Offset() [TestMethod] [RunsOnUIThread] +#if !__SKIA__ + [Ignore("Translation X and Y axis is currently supported on Skia only")] +#endif public async Task When_Translation_On_Load() { var sut = new Rectangle() @@ -1582,10 +1585,11 @@ public async Task When_Translation_On_Load() canvas.Children.Add(sut); TestServices.WindowHelper.WindowContent = canvas; await TestServices.WindowHelper.WaitForLoaded(sut); - sut.Translation += new Vector3(200, 0, 128); + sut.Translation += new Vector3(100, 100, 128); await TestServices.WindowHelper.WaitForIdle(); var bitmap = await UITestHelper.ScreenShot(canvas); - ImageAssert.DoesNotHaveColorAt(bitmap, new Windows.Foundation.Point(50, 50), Microsoft.UI.Colors.Blue, tolerance: 25); + ImageAssert.DoesNotHaveColorAt(bitmap, new Windows.Foundation.Point(50, 50), Microsoft.UI.Colors.Blue); + ImageAssert.HasColorAt(bitmap, new Windows.Foundation.Point(150, 150), Microsoft.UI.Colors.Blue); } #if HAS_UNO