Skip to content

Commit

Permalink
chore: undo changes from unoplatform#16502
Browse files Browse the repository at this point in the history
  • Loading branch information
ramezgerges committed May 3, 2024
1 parent 334b039 commit 6d4ee55
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 57 deletions.
74 changes: 19 additions & 55 deletions src/Uno.UI.Composition/Composition/ShapeVisual.skia.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
#nullable enable

using System.Numerics;
using SkiaSharp;
using Uno.Extensions;
using Uno.UI.Composition;
using Windows.Foundation;

namespace Microsoft.UI.Composition;

Expand All @@ -20,14 +18,10 @@ internal override void Render(in DrawingSession parentSession)
// First we render the shapes (a.k.a. the "local content")
// For UIElement, those are background and border or shape's content
// WARNING: As we are overriding the "Render" method, at this point we are still in the parent's coordinate system

// This shouldn't be inside the if condition.
// When inside the if, the session.Dispose will be called before base.Render,
// which can cause clipping to be removed before rendering children.
using var session = BeginShapesDrawing(in parentSession);

if (_shapes is { Count: not 0 } shapes)
{
using var session = BeginShapesDrawing(in parentSession);

for (var i = 0; i < shapes.Count; i++)
{
// TODO: If the shape will end up being not in Window bounds,
Expand All @@ -47,19 +41,29 @@ internal override void Render(in DrawingSession parentSession)
base.Render(in parentSession);
}

/// <inheritdoc />
internal override void Draw(in DrawingSession session)
{
if (ViewBox is { } viewBox)
{
session.Canvas.ClipRect(viewBox.GetSKRect(), antialias: true);
}

base.Draw(in session);
}

private DrawingSession BeginShapesDrawing(in DrawingSession parentSession)
{
parentSession.Canvas.Save();

var totalOffset = this.GetTotalOffset();
// Set the position of the visual on the canvas (i.e. change coordinates system to the "XAML element" one)
parentSession.Canvas.Translate(totalOffset.X + AnchorPoint.X, totalOffset.Y + AnchorPoint.Y);

var transform = this.GetTransform().ToSKMatrix();

if (ViewBox is { } viewBox)
{
if (!viewBox.IsAncestorClip)
{
ApplyTranslation(parentSession.Canvas);
}

// We apply the transformed viewbox clipping
if (transform.IsIdentity)
{
Expand All @@ -68,25 +72,10 @@ private DrawingSession BeginShapesDrawing(in DrawingSession parentSession)
else
{
var shape = new SKPath();
var clipRect = new SKRect(viewBox.Offset.X, viewBox.Offset.Y, viewBox.Offset.X + viewBox.Size.X, viewBox.Offset.Y + viewBox.Size.Y);

shape.AddRect(clipRect);
if (!viewBox.IsAncestorClip)
{
shape.Transform(transform);
}

shape.AddRect(new SKRect(viewBox.Offset.X, viewBox.Offset.Y, viewBox.Offset.X + viewBox.Size.X, viewBox.Offset.Y + viewBox.Size.Y));
shape.Transform(transform);
parentSession.Canvas.ClipPath(shape, antialias: true);
}

if (viewBox.IsAncestorClip)
{
ApplyTranslation(parentSession.Canvas);
}
}
else
{
ApplyTranslation(parentSession.Canvas);
}

if (!transform.IsIdentity)
Expand All @@ -104,29 +93,4 @@ private DrawingSession BeginShapesDrawing(in DrawingSession parentSession)

return session;
}

private void ApplyTranslation(SKCanvas canvas)
{
var totalOffset = this.GetTotalOffset();
// Set the position of the visual on the canvas (i.e. change coordinates system to the "XAML element" one)
canvas.Translate(totalOffset.X + AnchorPoint.X, totalOffset.Y + AnchorPoint.Y);
}

internal Rect? GetViewBoxRectInElementCoordinateSpace()
{
if (ViewBox is null)
{
return null;
}

var rect = ViewBox.GetRect();
if (ViewBox.IsAncestorClip)
{
var totalOffset = this.GetTotalOffset();
rect.X -= totalOffset.X;
rect.Y -= totalOffset.Y;
}

return rect;
}
}
3 changes: 1 addition & 2 deletions src/Uno.UI/UI/Xaml/Media/VisualTreeHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -492,10 +492,9 @@ internal static IReadOnlyList<_View> ClearChildren(UIElement view)

// The maximum region where the current element and its children might draw themselves
// This is expressed in the window (absolute) coordinate space.
var clippingBounds = element.Visual.GetViewBoxRectInElementCoordinateSpace() is { } rect
var clippingBounds = element.Visual.ViewBox?.GetRect() is { } rect
? transformToElement.Transform(rect)
: Rect.Infinite;

if (element.Visual.Clip?.GetBounds(element.Visual) is { } clip)
{
clippingBounds = clippingBounds.IntersectWith(transformToElement.Transform(clip)) ?? default;
Expand Down

0 comments on commit 6d4ee55

Please sign in to comment.