From 5630f8410b5a592d12cdc3cbc4edb23cba271dbb Mon Sep 17 00:00:00 2001 From: David Date: Tue, 9 May 2023 11:30:48 -0400 Subject: [PATCH] fix(reg): Fix offset computation of native only elements --- src/Uno.UI/UI/Xaml/UIElement.Android.cs | 21 +++++++++++---------- src/Uno.UI/UI/Xaml/UIElement.cs | 9 --------- src/Uno.UI/UI/Xaml/UIElement.iOS.cs | 15 ++++++++------- src/Uno.UI/UI/Xaml/UIElement.macOS.cs | 11 ++++++----- 4 files changed, 25 insertions(+), 31 deletions(-) diff --git a/src/Uno.UI/UI/Xaml/UIElement.Android.cs b/src/Uno.UI/UI/Xaml/UIElement.Android.cs index 52333140c14b..d693e5da439b 100644 --- a/src/Uno.UI/UI/Xaml/UIElement.Android.cs +++ b/src/Uno.UI/UI/Xaml/UIElement.Android.cs @@ -5,6 +5,7 @@ using System; using System.Collections.Generic; using System.Linq; +using System.Numerics; using System.Runtime.CompilerServices; using AndroidX.Core.View; using Windows.Foundation; @@ -237,7 +238,7 @@ internal static GeneralTransform TransformToVisual(View element, View visual) /// Note: Offsets are only an approximation which does not take in consideration possible transformations /// applied by a 'ViewGroup' between this element and its parent UIElement. /// - private bool TryGetParentUIElementForTransformToVisual(out UIElement parentElement, ref double offsetX, ref double offsetY, ref TransformToVisualContext context) + private bool TryGetParentUIElementForTransformToVisual(out UIElement parentElement, ref Matrix3x2 matrix, ref TransformToVisualContext context) { var parent = this.GetVisualTreeParent(); switch (parent) @@ -261,17 +262,17 @@ private bool TryGetParentUIElementForTransformToVisual(out UIElement parentEleme // cf. https://github.com/unoplatform/uno/issues/2754 // 1. Undo what was done by the shared code - offsetX -= LayoutSlotWithMarginsAndAlignments.X; - offsetY -= LayoutSlotWithMarginsAndAlignments.Y; + matrix.M31 -= (float)LayoutSlotWithMarginsAndAlignments.X; + matrix.M32 -= (float)LayoutSlotWithMarginsAndAlignments.Y; // 2.Natively compute the offset of this current item relative to this ScrollViewer and adjust offsets var sv = lv.FindFirstParent(); var offset = GetPosition(this, relativeTo: sv); - offsetX += offset.X; - offsetY += offset.Y; + matrix.M31 += (float)offset.X; + matrix.M32 += (float)offset.Y; // We return the parent of the ScrollViewer, so we bypass the Offset (and the Scale) handling in shared code. - return sv.TryGetParentUIElementForTransformToVisual(out parentElement, ref offsetX, ref offsetY, ref context); + return sv.TryGetParentUIElementForTransformToVisual(out parentElement, ref matrix, ref context); case View view: // Android.View and Android.IViewParent var windowToFirstParent = new int[2]; @@ -291,8 +292,8 @@ private bool TryGetParentUIElementForTransformToVisual(out UIElement parentEleme eltParent.GetLocationInWindow(windowToEltParent); parentElement = eltParent; - offsetX += ViewHelper.PhysicalToLogicalPixels(windowToFirstParent[0] - windowToEltParent[0]); - offsetY += ViewHelper.PhysicalToLogicalPixels(windowToFirstParent[1] - windowToEltParent[1]); + matrix.M31 += (float)ViewHelper.PhysicalToLogicalPixels(windowToFirstParent[0] - windowToEltParent[0]); + matrix.M31 += (float)ViewHelper.PhysicalToLogicalPixels(windowToFirstParent[1] - windowToEltParent[1]); return true; case null: @@ -300,8 +301,8 @@ private bool TryGetParentUIElementForTransformToVisual(out UIElement parentEleme // so we adjust offsets using the X/Y position of the original 'view' in the window. parentElement = null; - offsetX += ViewHelper.PhysicalToLogicalPixels(windowToFirstParent[0]); - offsetY += ViewHelper.PhysicalToLogicalPixels(windowToFirstParent[1]); + matrix.M31 += (float)ViewHelper.PhysicalToLogicalPixels(windowToFirstParent[0]); + matrix.M31 += (float)ViewHelper.PhysicalToLogicalPixels(windowToFirstParent[1]); return false; } } while (true); diff --git a/src/Uno.UI/UI/Xaml/UIElement.cs b/src/Uno.UI/UI/Xaml/UIElement.cs index 21f44eee5f40..48d86015865c 100644 --- a/src/Uno.UI/UI/Xaml/UIElement.cs +++ b/src/Uno.UI/UI/Xaml/UIElement.cs @@ -631,15 +631,6 @@ private bool TryGetParentUIElementForTransformToVisual(out UIElement parentEleme return false; } } -#else - private bool TryGetParentUIElementForTransformToVisual(out UIElement parentElement, ref Matrix3x2 matrix) - { - double offsetX = 0.0, offsetY = 0.0; - var hasParent = TryGetParentUIElementForTransformToVisual(out parentElement, ref offsetX, ref offsetY); - matrix.M31 += (float)offsetX; - matrix.M32 += (float)offsetX; - return hasParent; - } #endif protected virtual void OnIsHitTestVisibleChanged(bool oldValue, bool newValue) diff --git a/src/Uno.UI/UI/Xaml/UIElement.iOS.cs b/src/Uno.UI/UI/Xaml/UIElement.iOS.cs index df04da00dbba..4616accdc261 100644 --- a/src/Uno.UI/UI/Xaml/UIElement.iOS.cs +++ b/src/Uno.UI/UI/Xaml/UIElement.iOS.cs @@ -2,6 +2,7 @@ using System.Collections.Generic; using System.Diagnostics; using System.Linq; +using System.Numerics; using System.Runtime.CompilerServices; using CoreAnimation; using CoreGraphics; @@ -161,7 +162,7 @@ internal Windows.Foundation.Point GetPosition(Point position, global::Windows.UI /// Note: Offsets are only an approximation which does not take in consideration possible transformations /// applied by a 'UIView' between this element and its parent UIElement. /// - private bool TryGetParentUIElementForTransformToVisual(out UIElement parentElement, ref double offsetX, ref double offsetY, ref TransformToVisualContext context) + private bool TryGetParentUIElementForTransformToVisual(out UIElement parentElement, ref Matrix3x2 matrix, ref TransformToVisualContext context) { var parent = this.GetVisualTreeParent(); switch (parent) @@ -192,8 +193,8 @@ private bool TryGetParentUIElementForTransformToVisual(out UIElement parentEleme container.LayoutSlot.Left == container.Margin.Left && container.LayoutSlot.Top == container.Margin.Top) { - offsetX += parent.Frame.X; - offsetY += parent.Frame.Y; + matrix.M31 += (float)parent.Frame.X; + matrix.M32 += (float)parent.Frame.Y; } return true; @@ -215,8 +216,8 @@ private bool TryGetParentUIElementForTransformToVisual(out UIElement parentEleme var offset = view?.ConvertPointToCoordinateSpace(default, eltParent) ?? default; parentElement = eltParent; - offsetX += offset.X; - offsetY += offset.Y; + matrix.M31 += (float)offset.X; + matrix.M32 += (float)offset.Y; return true; case null: @@ -225,8 +226,8 @@ private bool TryGetParentUIElementForTransformToVisual(out UIElement parentEleme offset = view.ConvertRectToView(default, null).Location; parentElement = null; - offsetX += offset.X; - offsetY += offset.Y; + matrix.M31 += (float)offset.X; + matrix.M32 += (float)offset.Y; if (this.FindViewController() is { } vc) { diff --git a/src/Uno.UI/UI/Xaml/UIElement.macOS.cs b/src/Uno.UI/UI/Xaml/UIElement.macOS.cs index 6127b8904be1..162db706cb02 100644 --- a/src/Uno.UI/UI/Xaml/UIElement.macOS.cs +++ b/src/Uno.UI/UI/Xaml/UIElement.macOS.cs @@ -4,6 +4,7 @@ using Windows.System; using System; using System.Linq; +using System.Numerics; using System.Runtime.CompilerServices; using Windows.UI.Xaml; using Windows.UI.Xaml.Controls; @@ -191,7 +192,7 @@ private protected override void OnNativeFlagsChanged(NSEvent evt) private bool CheckFlagKeyDown(NSEventModifierMask flag, NSEventModifierMask newMask) => !_lastFlags.HasFlag(flag) && newMask.HasFlag(flag); - private bool TryGetParentUIElementForTransformToVisual(out UIElement parentElement, ref double offsetX, ref double offsetY, ref TransformToVisualContext context) + private bool TryGetParentUIElementForTransformToVisual(out UIElement parentElement, ref Matrix3x2 matrix, ref TransformToVisualContext context) { var parent = this.GetVisualTreeParent(); switch (parent) @@ -220,8 +221,8 @@ private bool TryGetParentUIElementForTransformToVisual(out UIElement parentEleme var offset = view?.ConvertPointToView(default, eltParent) ?? default; parentElement = eltParent; - offsetX += offset.X; - offsetY += offset.Y; + matrix.M31 += (float)offset.X; + matrix.M32 += (float)offset.Y; return true; case null: @@ -231,8 +232,8 @@ private bool TryGetParentUIElementForTransformToVisual(out UIElement parentEleme offset = view.ConvertRectToView(default, null).Location; parentElement = null; - offsetX += offset.X; - offsetY += offset.Y; + matrix.M31 += (float)offset.X; + matrix.M32 += (float)offset.Y; return false; } } while (true);