Browse Source

Use correct bounds in VisualNode.

Steven Kirk 9 years ago
parent
commit
76ec613dea

+ 16 - 5
src/Avalonia.Visuals/Rect.cs

@@ -419,12 +419,23 @@ namespace Avalonia
         /// <returns>The union.</returns>
         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));
+            }
         }
 
         /// <summary>

+ 1 - 1
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)
             {

+ 3 - 0
src/Avalonia.Visuals/Rendering/SceneGraph/ISceneNode.cs

@@ -11,6 +11,9 @@ namespace Avalonia.Rendering.SceneGraph
     /// </summary>
     public interface ISceneNode
     {
+        /// <summary>
+        /// Gets the bounds of the visible content in the node.
+        /// </summary>
         Rect Bounds { get; }
 
         /// <summary>

+ 19 - 3
src/Avalonia.Visuals/Rendering/SceneGraph/VisualNode.cs

@@ -14,7 +14,7 @@ namespace Avalonia.Rendering.SceneGraph
     public class VisualNode : IVisualNode
     {
         private Rect? _bounds;
-        
+
         /// <summary>
         /// Initializes a new instance of the <see cref="VisualNode"/> class.
         /// </summary>
@@ -44,8 +44,8 @@ namespace Avalonia.Rendering.SceneGraph
         /// <inheritdoc/>
         public Matrix Transform { get; set; }
 
-        // TODO: Calculate real bounds.
-        public Rect Bounds => ClipBounds;
+        /// <inheritdoc/>
+        public Rect Bounds => _bounds ?? CalculateBounds();
 
         /// <inheritdoc/>
         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;
+        }
     }
 }