Bladeren bron

Row: added support for mixed columns (constant + relational), good when performing visual column merging

Marcin Ziąbek 4 jaren geleden
bovenliggende
commit
561d6a004c
2 gewijzigde bestanden met toevoegingen van 23 en 39 verwijderingen
  1. 16 34
      QuestPDF/Elements/Row.cs
  2. 7 5
      QuestPDF/Fluent/RowExtensions.cs

+ 16 - 34
QuestPDF/Elements/Row.cs

@@ -6,9 +6,16 @@ using QuestPDF.Infrastructure;
 
 namespace QuestPDF.Elements
 {
-    internal abstract class RowElement : Constrained
+    internal class RowElement : Constrained
     {
-        public float Size { get; protected set;  }
+        public float ConstantSize { get; set;  }
+        public float RelativeSize { get; set;  }
+
+        public RowElement(float constantSize, float relativeSize)
+        {
+            ConstantSize = constantSize;
+            RelativeSize = relativeSize;
+        }
         
         public void SetWidth(float width)
         {
@@ -17,23 +24,6 @@ namespace QuestPDF.Elements
         }
     }
     
-    internal class ConstantRowElement : RowElement
-    {
-        public ConstantRowElement(float size)
-        {
-            Size = size;
-            SetWidth(size);
-        }
-    }
-    
-    internal class RelativeRowElement : RowElement
-    {
-        public RelativeRowElement(float size)
-        {
-            Size = size;
-        }
-    }
-    
     internal class BinaryRow : Element, ICacheable, IStateResettable
     {
         internal Element Left { get; set; }
@@ -145,23 +135,15 @@ namespace QuestPDF.Elements
 
         private void UpdateElementsWidth(float availableWidth)
         {
-            var constantWidth = Children
-                .Where(x => x is ConstantRowElement)
-                .Cast<ConstantRowElement>()
-                .Sum(x => x.Size);
-        
-            var relativeWidth = Children
-                .Where(x => x is RelativeRowElement)
-                .Cast<RelativeRowElement>()
-                .Sum(x => x.Size);
+            var constantWidth = Children.Sum(x => x.ConstantSize);
+            var relativeWidth = Children.Sum(x => x.RelativeSize);
 
             var widthPerRelativeUnit = (availableWidth - constantWidth) / relativeWidth;
             
-            Children
-                .Where(x => x is RelativeRowElement)
-                .Cast<RelativeRowElement>()
-                .ToList()
-                .ForEach(x => x.SetWidth(x.Size * widthPerRelativeUnit));
+            foreach (var row in Children)
+            {
+                row.SetWidth(row.ConstantSize + row.RelativeSize * widthPerRelativeUnit);
+            }
         }
         
         private static ICollection<RowElement> AddSpacing(ICollection<RowElement> elements, float spacing)
@@ -170,7 +152,7 @@ namespace QuestPDF.Elements
                 return elements;
             
             return elements
-                .SelectMany(x => new[] { new ConstantRowElement(spacing), x })
+                .SelectMany(x => new[] { new RowElement(spacing, 0), x })
                 .Skip(1)
                 .ToList();
         }

+ 7 - 5
QuestPDF/Fluent/RowExtensions.cs

@@ -15,15 +15,17 @@ namespace QuestPDF.Fluent
         
         public IContainer ConstantColumn(float width)
         {
-            var element = new ConstantRowElement(width);
-            
-            Row.Children.Add(element);
-            return element;
+            return ComplexColumn(constantWidth: width);
         }
         
         public IContainer RelativeColumn(float width = 1)
         {
-            var element = new RelativeRowElement(width);
+            return ComplexColumn(relativeWidth: width);
+        }
+        
+        public IContainer ComplexColumn(float constantWidth = 0, float relativeWidth = 0)
+        {
+            var element = new RowElement(constantWidth, relativeWidth);
             
             Row.Children.Add(element);
             return element;