Skip to content

Commit

Permalink
fix(reg): Fix offset computation of native only elements
Browse files Browse the repository at this point in the history
  • Loading branch information
dr1rrb committed May 12, 2023
1 parent 1cb82c0 commit 5630f84
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 31 deletions.
21 changes: 11 additions & 10 deletions src/Uno.UI/UI/Xaml/UIElement.Android.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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.
/// </summary>
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)
Expand All @@ -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<ScrollViewer>();
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 <Horizontal|Vertical>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];
Expand All @@ -291,17 +292,17 @@ 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:
// We reached the top of the window without any UIElement in the hierarchy,
// 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);
Expand Down
9 changes: 0 additions & 9 deletions src/Uno.UI/UI/Xaml/UIElement.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
15 changes: 8 additions & 7 deletions src/Uno.UI/UI/Xaml/UIElement.iOS.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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.
/// </summary>
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)
Expand Down Expand Up @@ -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;

Expand All @@ -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:
Expand All @@ -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)
{
Expand Down
11 changes: 6 additions & 5 deletions src/Uno.UI/UI/Xaml/UIElement.macOS.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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:
Expand All @@ -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);
Expand Down

0 comments on commit 5630f84

Please sign in to comment.