Extended and renamed the MinimalBox element

This commit is contained in:
MarcinZiabek
2022-10-19 21:37:10 +02:00
parent 7b7fac07b3
commit 73448f483c
5 changed files with 89 additions and 37 deletions
@@ -7,18 +7,20 @@ using QuestPDF.UnitTests.TestEngine;
namespace QuestPDF.UnitTests
{
[TestFixture]
public class BoxTests
public class ShrinkTests
{
[Test]
public void Measure() => SimpleContainerTests.Measure<MinimalBox>();
public void Measure() => SimpleContainerTests.Measure<Shrink>();
[Test]
public void Draw_Wrap()
{
TestPlan
.For(x => new MinimalBox
.For(x => new Shrink
{
Child = x.CreateChild()
Child = x.CreateChild(),
ShrinkVertical = true,
ShrinkHorizontal = true
})
.DrawElement(new Size(400, 300))
.ExpectChildMeasure(expectedInput: new Size(400, 300), returns: SpacePlan.Wrap())
@@ -29,9 +31,11 @@ namespace QuestPDF.UnitTests
public void Measure_PartialRender()
{
TestPlan
.For(x => new MinimalBox
.For(x => new Shrink
{
Child = x.CreateChild()
Child = x.CreateChild(),
ShrinkVertical = true,
ShrinkHorizontal = true
})
.MeasureElement(new Size(400, 300))
.ExpectChildMeasure(expectedInput: new Size(400, 300), returns: SpacePlan.PartialRender(200, 100))
@@ -43,9 +47,11 @@ namespace QuestPDF.UnitTests
public void Measure_FullRender()
{
TestPlan
.For(x => new MinimalBox
.For(x => new Shrink
{
Child = x.CreateChild()
Child = x.CreateChild(),
ShrinkVertical = true,
ShrinkHorizontal = true
})
.MeasureElement(new Size(500, 400))
.ExpectChildMeasure(expectedInput: new Size(500, 400), returns: SpacePlan.FullRender(300, 200))
-18
View File
@@ -1,18 +0,0 @@
using QuestPDF.Drawing;
using QuestPDF.Infrastructure;
namespace QuestPDF.Elements
{
internal class MinimalBox : ContainerElement
{
internal override void Draw(Size availableSpace)
{
var targetSize = base.Measure(availableSpace);
if (targetSize.Type == SpacePlanType.Wrap)
return;
base.Draw(targetSize);
}
}
}
+27
View File
@@ -0,0 +1,27 @@
using QuestPDF.Drawing;
using QuestPDF.Infrastructure;
namespace QuestPDF.Elements
{
internal class Shrink : ContainerElement
{
public bool ShrinkVertical { get; set; }
public bool ShrinkHorizontal { get; set; }
internal override void Draw(Size availableSpace)
{
var childSize = base.Measure(availableSpace);
if (childSize.Type == SpacePlanType.Wrap)
return;
var targetSize = new Size(
ShrinkVertical ? childSize.Width : availableSpace.Width,
ShrinkHorizontal ? childSize.Height : availableSpace.Height);
// TODO: adjust offset for RTL mode
base.Draw(targetSize);
}
}
}
-11
View File
@@ -154,17 +154,6 @@ namespace QuestPDF.Fluent
});
}
[Obsolete("This element has been renamed since version 2022.1. Please use the MinimalBox method.")]
public static IContainer Box(this IContainer element)
{
return element.Element(new MinimalBox());
}
public static IContainer MinimalBox(this IContainer element)
{
return element.Element(new MinimalBox());
}
public static IContainer Unconstrained(this IContainer element)
{
return element.Element(new Unconstrained());
+48
View File
@@ -0,0 +1,48 @@
using System;
using QuestPDF.Elements;
using QuestPDF.Infrastructure;
namespace QuestPDF.Fluent
{
public static class ShrinkExtensions
{
private static IContainer Shrink(this IContainer element, Action<Shrink> handler)
{
var shrink = element as Shrink ?? new Shrink();
handler(shrink);
return element.Element(shrink);
}
public static IContainer Shrink(this IContainer element)
{
return element.ExtendVertical().ExtendHorizontal();
}
public static IContainer ShrinkVertical(this IContainer element)
{
return element.Shrink(x => x.ShrinkVertical = true);
}
public static IContainer ShrinkHorizontal(this IContainer element)
{
return element.Shrink(x => x.ShrinkHorizontal = true);
}
#region Obsolete
[Obsolete("This element has been renamed since version 2022.1. Please use the Shrink method.")]
public static IContainer Box(this IContainer element)
{
return element.Element(new Shrink());
}
[Obsolete("This element has been renamed since version 2022.11. Please use the Shrink method.")]
public static IContainer MinimalBox(this IContainer element)
{
return element.Element(new Shrink());
}
#endregion
}
}