From 0a27b80b732a411db2ac94a4ba805b5f0ee4e4e7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marcin=20Zi=C4=85bek?= Date: Sat, 18 Dec 2021 16:54:35 +0100 Subject: [PATCH] Row: added support for mixed columns (constant + relational), good when performing visual column merging --- QuestPDF/Elements/Row.cs | 50 ++++++++++---------------------- QuestPDF/Fluent/RowExtensions.cs | 12 ++++---- 2 files changed, 23 insertions(+), 39 deletions(-) diff --git a/QuestPDF/Elements/Row.cs b/QuestPDF/Elements/Row.cs index 8a2c023..3cc24f2 100644 --- a/QuestPDF/Elements/Row.cs +++ b/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() - .Sum(x => x.Size); - - var relativeWidth = Children - .Where(x => x is RelativeRowElement) - .Cast() - .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() - .ToList() - .ForEach(x => x.SetWidth(x.Size * widthPerRelativeUnit)); + foreach (var row in Children) + { + row.SetWidth(row.ConstantSize + row.RelativeSize * widthPerRelativeUnit); + } } private static ICollection AddSpacing(ICollection 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(); } diff --git a/QuestPDF/Fluent/RowExtensions.cs b/QuestPDF/Fluent/RowExtensions.cs index 6910650..6b9ac37 100644 --- a/QuestPDF/Fluent/RowExtensions.cs +++ b/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;