diff --git a/src/Avalonia.Visuals/Rect.cs b/src/Avalonia.Visuals/Rect.cs
index a663beb912..7093250ceb 100644
--- a/src/Avalonia.Visuals/Rect.cs
+++ b/src/Avalonia.Visuals/Rect.cs
@@ -419,12 +419,23 @@ namespace Avalonia
/// The union.
public Rect Union(Rect rect)
{
- var x1 = Math.Min(this.X, rect.X);
- var x2 = Math.Max(this.Right, rect.Right);
- var y1 = Math.Min(this.Y, rect.Y);
- var y2 = Math.Max(this.Bottom, rect.Bottom);
+ if (IsEmpty)
+ {
+ return rect;
+ }
+ else if (rect.IsEmpty)
+ {
+ return this;
+ }
+ else
+ {
+ var x1 = Math.Min(this.X, rect.X);
+ var x2 = Math.Max(this.Right, rect.Right);
+ var y1 = Math.Min(this.Y, rect.Y);
+ var y2 = Math.Max(this.Bottom, rect.Bottom);
- return new Rect(new Point(x1, y1), new Point(x2, y2));
+ return new Rect(new Point(x1, y1), new Point(x2, y2));
+ }
}
///
diff --git a/src/Avalonia.Visuals/Rendering/DeferredRenderer.cs b/src/Avalonia.Visuals/Rendering/DeferredRenderer.cs
index 2ff5056568..2c5b31e63e 100644
--- a/src/Avalonia.Visuals/Rendering/DeferredRenderer.cs
+++ b/src/Avalonia.Visuals/Rendering/DeferredRenderer.cs
@@ -72,7 +72,7 @@ namespace Avalonia.Rendering
private void Render(IDrawingContextImpl context, IVisualNode node, Rect clipBounds)
{
- clipBounds = node.Bounds.Intersect(clipBounds);
+ clipBounds = node.ClipBounds.Intersect(clipBounds);
if (!clipBounds.IsEmpty)
{
diff --git a/src/Avalonia.Visuals/Rendering/SceneGraph/ISceneNode.cs b/src/Avalonia.Visuals/Rendering/SceneGraph/ISceneNode.cs
index 278bac97ba..1f9921a20a 100644
--- a/src/Avalonia.Visuals/Rendering/SceneGraph/ISceneNode.cs
+++ b/src/Avalonia.Visuals/Rendering/SceneGraph/ISceneNode.cs
@@ -11,6 +11,9 @@ namespace Avalonia.Rendering.SceneGraph
///
public interface ISceneNode
{
+ ///
+ /// Gets the bounds of the visible content in the node.
+ ///
Rect Bounds { get; }
///
diff --git a/src/Avalonia.Visuals/Rendering/SceneGraph/VisualNode.cs b/src/Avalonia.Visuals/Rendering/SceneGraph/VisualNode.cs
index fc7ef78ec9..7bd6039a62 100644
--- a/src/Avalonia.Visuals/Rendering/SceneGraph/VisualNode.cs
+++ b/src/Avalonia.Visuals/Rendering/SceneGraph/VisualNode.cs
@@ -14,7 +14,7 @@ namespace Avalonia.Rendering.SceneGraph
public class VisualNode : IVisualNode
{
private Rect? _bounds;
-
+
///
/// Initializes a new instance of the class.
///
@@ -44,8 +44,8 @@ namespace Avalonia.Rendering.SceneGraph
///
public Matrix Transform { get; set; }
- // TODO: Calculate real bounds.
- public Rect Bounds => ClipBounds;
+ ///
+ public Rect Bounds => _bounds ?? CalculateBounds();
///
public Rect ClipBounds { get; set; }
@@ -146,5 +146,21 @@ namespace Avalonia.Rendering.SceneGraph
context.PopOpacity();
}
}
+
+ private Rect CalculateBounds()
+ {
+ var result = new Rect();
+
+ foreach (var child in Children)
+ {
+ if (!(child is IVisualNode))
+ {
+ result = result.Union(child.Bounds);
+ }
+ }
+
+ _bounds = result;
+ return result;
+ }
}
}