Skip to content

Commit

Permalink
Merge pull request #3347 from macpawluk/issues/immediate-renderer
Browse files Browse the repository at this point in the history
Fix for issue #3344 - ImmediateRenderer trimming content
  • Loading branch information
jmacato authored Jan 8, 2020
2 parents d9bf203 + fea0d76 commit 08c9710
Show file tree
Hide file tree
Showing 2 changed files with 139 additions and 1 deletion.
4 changes: 3 additions & 1 deletion src/Avalonia.Visuals/Rendering/ImmediateRenderer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -307,7 +307,9 @@ private void Render(DrawingContext context, IVisual visual, Rect clipRect)

if (!child.ClipToBounds || clipRect.Intersects(childBounds))
{
var childClipRect = clipRect.Translate(-childBounds.Position);
var childClipRect = child.RenderTransform == null
? clipRect.Translate(-childBounds.Position)
: clipRect;
Render(context, child, childClipRect);
}
else
Expand Down
136 changes: 136 additions & 0 deletions tests/Avalonia.Visuals.UnitTests/Rendering/ImmediateRendererTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -178,5 +178,141 @@ public void Should_Render_Child_In_Parent_With_RenderTransform2()

Assert.Equal(1, rendered);
}

[Fact]
public void Should_Not_Clip_Children_With_RenderTransform_When_In_Bounds()
{
const int RootWidth = 300;
const int RootHeight = 300;

var rootGrid = new Grid
{
Width = RootWidth,
Height = RootHeight,
ClipToBounds = true
};

var stackPanel = new StackPanel
{
Orientation = Orientation.Horizontal,
VerticalAlignment = VerticalAlignment.Top,
HorizontalAlignment = HorizontalAlignment.Right,
Margin = new Thickness(0, 10, 0, 0),
RenderTransformOrigin = new RelativePoint(new Point(0, 0), RelativeUnit.Relative),
RenderTransform = new TransformGroup
{
Children =
{
new RotateTransform { Angle = 90 },
new TranslateTransform { X = 240 }
}
}
};

rootGrid.Children.Add(stackPanel);

TestControl CreateControl()
=> new TestControl
{
Width = 80,
Height = 40,
Margin = new Thickness(0, 0, 5, 0),
ClipToBounds = true
};

var control1 = CreateControl();
var control2 = CreateControl();
var control3 = CreateControl();

stackPanel.Children.Add(control1);
stackPanel.Children.Add(control2);
stackPanel.Children.Add(control3);

var root = new TestRoot(rootGrid);
root.Renderer = new ImmediateRenderer(root);
root.LayoutManager.ExecuteInitialLayoutPass(root);

var rootSize = new Size(RootWidth, RootHeight);
root.Measure(rootSize);
root.Arrange(new Rect(rootSize));

root.Renderer.Paint(root.Bounds);

Assert.True(control1.Rendered);
Assert.True(control2.Rendered);
Assert.True(control3.Rendered);
}

[Fact]
public void Should_Not_Render_Clipped_Child_With_RenderTransform_When_Not_In_Bounds()
{
const int RootWidth = 300;
const int RootHeight = 300;

var rootGrid = new Grid
{
Width = RootWidth,
Height = RootHeight,
ClipToBounds = true
};

var stackPanel = new StackPanel
{
Orientation = Orientation.Horizontal,
VerticalAlignment = VerticalAlignment.Top,
HorizontalAlignment = HorizontalAlignment.Right,
Margin = new Thickness(0, 10, 0, 0),
RenderTransformOrigin = new RelativePoint(new Point(0, 0), RelativeUnit.Relative),
RenderTransform = new TransformGroup
{
Children =
{
new RotateTransform { Angle = 90 },
new TranslateTransform { X = 280 }
}
}
};

rootGrid.Children.Add(stackPanel);

TestControl CreateControl()
=> new TestControl
{
Width = 160,
Height = 40,
Margin = new Thickness(0, 0, 5, 0),
ClipToBounds = true
};

var control1 = CreateControl();
var control2 = CreateControl();
var control3 = CreateControl();

stackPanel.Children.Add(control1);
stackPanel.Children.Add(control2);
stackPanel.Children.Add(control3);

var root = new TestRoot(rootGrid);
root.Renderer = new ImmediateRenderer(root);
root.LayoutManager.ExecuteInitialLayoutPass(root);

var rootSize = new Size(RootWidth, RootHeight);
root.Measure(rootSize);
root.Arrange(new Rect(rootSize));

root.Renderer.Paint(root.Bounds);

Assert.True(control1.Rendered);
Assert.True(control2.Rendered);
Assert.False(control3.Rendered);
}

private class TestControl : Control
{
public bool Rendered { get; private set; }

public override void Render(DrawingContext context)
=> Rendered = true;
}
}
}

0 comments on commit 08c9710

Please sign in to comment.