Browse Source

Minor refactorization

Marcin Ziąbek 4 years ago
parent
commit
78e5cbd098
1 changed files with 72 additions and 67 deletions
  1. 72 67
      QuestPDF/Elements/Row.cs

+ 72 - 67
QuestPDF/Elements/Row.cs

@@ -79,83 +79,88 @@ namespace QuestPDF.Elements
         public ICollection<RowElement> Children { get; internal set; } = new List<RowElement>();
         public float Spacing { get; set; } = 0;
         
-        public Element Compose(float availableWidth)
+        internal override ISpacePlan Measure(Size availableSpace)
         {
-            var elements = ReduceRows(AddSpacing(Children));
-            return BuildTree(elements.ToArray());
+            return Compose(availableSpace.Width).Measure(availableSpace);
+        }
 
-            ICollection<Element> ReduceRows(ICollection<RowElement> elements)
-            {
-                var constantWidth = elements
-                    .Where(x => x is ConstantRowElement)
-                    .Cast<ConstantRowElement>()
-                    .Sum(x => x.Width);
-            
-                var relativeWidth = elements
-                    .Where(x => x is RelativeRowElement)
-                    .Cast<RelativeRowElement>()
-                    .Sum(x => x.Width);
+        internal override void Draw(ICanvas canvas, Size availableSpace)
+        {
+            Compose(availableSpace.Width).Draw(canvas, availableSpace);
+        }
+        
+        #region structure
+        
+        private Element Compose(float availableWidth)
+        {
+            var elements = AddSpacing(Children, Spacing);
+            var rowElements = ReduceRows(elements, availableWidth);
+            return BuildTree(rowElements.ToArray());
+        }
+        
+        private static ICollection<Element> ReduceRows(ICollection<RowElement> elements, float availableWidth)
+        {
+            var constantWidth = elements
+                .Where(x => x is ConstantRowElement)
+                .Cast<ConstantRowElement>()
+                .Sum(x => x.Width);
+        
+            var relativeWidth = elements
+                .Where(x => x is RelativeRowElement)
+                .Cast<RelativeRowElement>()
+                .Sum(x => x.Width);
 
-                var widthPerRelativeUnit = (availableWidth - constantWidth) / relativeWidth;
-                
-                return elements
-                    .Select(x =>
-                    {
-                        if (x is RelativeRowElement r)
-                            return new ConstantRowElement(r.Width * widthPerRelativeUnit)
-                            {
-                                Child = x.Child
-                            };
-                        
-                        return x;
-                    })
-                    .Select(x => new Constrained
-                    {
-                        MinWidth = x.Width,
-                        MaxWidth = x.Width,
-                        Child = x.Child
-                    })
-                    .Cast<Element>()
-                    .ToList();
-            }
+            var widthPerRelativeUnit = (availableWidth - constantWidth) / relativeWidth;
             
-            ICollection<RowElement> AddSpacing(ICollection<RowElement> elements)
-            {
-                if (Spacing < Size.Epsilon)
-                    return elements;
-                
-                return elements
-                    .SelectMany(x => new[] { new ConstantRowElement(Spacing), x })
-                    .Skip(1)
-                    .ToList();
-            }
-
-            Element BuildTree(Span<Element> elements)
-            {
-                if (elements.IsEmpty)
-                    return Empty.Instance;
-
-                if (elements.Length == 1)
-                    return elements[0];
-
-                var half = elements.Length / 2;
-                
-                return new SimpleRow
+            return elements
+                .Select(x =>
+                {
+                    if (x is RelativeRowElement r)
+                        return new ConstantRowElement(r.Width * widthPerRelativeUnit)
+                        {
+                            Child = x.Child
+                        };
+                    
+                    return x;
+                })
+                .Select(x => new Constrained
                 {
-                    Left = BuildTree(elements.Slice(0, half)),
-                    Right = BuildTree(elements.Slice(half))
-                };
-            }
+                    MinWidth = x.Width,
+                    MaxWidth = x.Width,
+                    Child = x.Child
+                })
+                .Cast<Element>()
+                .ToList();
         }
-
-        internal override ISpacePlan Measure(Size availableSpace)
+        
+        private static ICollection<RowElement> AddSpacing(ICollection<RowElement> elements, float spacing)
         {
-            return Compose(availableSpace.Width).Measure(availableSpace);
+            if (spacing < Size.Epsilon)
+                return elements;
+            
+            return elements
+                .SelectMany(x => new[] { new ConstantRowElement(spacing), x })
+                .Skip(1)
+                .ToList();
         }
 
-        internal override void Draw(ICanvas canvas, Size availableSpace)
+        private static Element BuildTree(Span<Element> elements)
         {
-            Compose(availableSpace.Width).Draw(canvas, availableSpace);
+            if (elements.IsEmpty)
+                return Empty.Instance;
+
+            if (elements.Length == 1)
+                return elements[0];
+
+            var half = elements.Length / 2;
+            
+            return new SimpleRow
+            {
+                Left = BuildTree(elements.Slice(0, half)),
+                Right = BuildTree(elements.Slice(half))
+            };
         }
+        
+        #endregion
     }
 }