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

This commit is contained in:
Marcin Ziąbek
2021-12-18 16:54:35 +01:00
parent 656d1a6ecf
commit 0a27b80b73
2 changed files with 23 additions and 39 deletions
+16 -34
View File
@@ -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
View File
@@ -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;