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: Translation should be applied even if layout hasn't changed #19004

Merged
merged 4 commits into from
Dec 10, 2024
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
26 changes: 26 additions & 0 deletions src/Uno.UI.RuntimeTests/Tests/Windows_UI_Xaml/Given_UIElement.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1566,6 +1566,32 @@ public async Task When_Element_Has_Translation_And_Visual_Has_Offset()
}
#endif

[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()
{
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(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);
ImageAssert.HasColorAt(bitmap, new Windows.Foundation.Point(150, 150), Microsoft.UI.Colors.Blue);
}

#if HAS_UNO
[TestMethod]
[RunsOnUIThread]
Expand Down
16 changes: 16 additions & 0 deletions src/Uno.UI/UI/Xaml/UIElement.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<UIElement, BringIntoViewRequestedEventArgs> OnBringIntoViewRequestedHandler =
(UIElement sender, BringIntoViewRequestedEventArgs args) => sender.OnBringIntoViewRequested(args);
Expand Down Expand Up @@ -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();
}
Expand Down
8 changes: 7 additions & 1 deletion src/Uno.UI/UI/Xaml/UIElement.skia.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
{
Expand Down Expand Up @@ -278,16 +279,21 @@ 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;

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
Expand Down
Loading