From 73448f483cf7bf8594587553c0d0a80a941586e3 Mon Sep 17 00:00:00 2001 From: MarcinZiabek Date: Wed, 19 Oct 2022 21:37:10 +0200 Subject: [PATCH] Extended and renamed the MinimalBox element --- .../{BoxTests.cs => ShrinkTests.cs} | 22 +++++---- QuestPDF/Elements/MinimalBox.cs | 18 ------- QuestPDF/Elements/Shrink.cs | 27 +++++++++++ QuestPDF/Fluent/ElementExtensions.cs | 11 ----- QuestPDF/Fluent/ShrinkExtensions.cs | 48 +++++++++++++++++++ 5 files changed, 89 insertions(+), 37 deletions(-) rename QuestPDF.UnitTests/{BoxTests.cs => ShrinkTests.cs} (68%) delete mode 100644 QuestPDF/Elements/MinimalBox.cs create mode 100644 QuestPDF/Elements/Shrink.cs create mode 100644 QuestPDF/Fluent/ShrinkExtensions.cs diff --git a/QuestPDF.UnitTests/BoxTests.cs b/QuestPDF.UnitTests/ShrinkTests.cs similarity index 68% rename from QuestPDF.UnitTests/BoxTests.cs rename to QuestPDF.UnitTests/ShrinkTests.cs index 4a49dfc..90a532f 100644 --- a/QuestPDF.UnitTests/BoxTests.cs +++ b/QuestPDF.UnitTests/ShrinkTests.cs @@ -7,18 +7,20 @@ using QuestPDF.UnitTests.TestEngine; namespace QuestPDF.UnitTests { [TestFixture] - public class BoxTests + public class ShrinkTests { [Test] - public void Measure() => SimpleContainerTests.Measure(); + public void Measure() => SimpleContainerTests.Measure(); [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)) diff --git a/QuestPDF/Elements/MinimalBox.cs b/QuestPDF/Elements/MinimalBox.cs deleted file mode 100644 index 741b439..0000000 --- a/QuestPDF/Elements/MinimalBox.cs +++ /dev/null @@ -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); - } - } -} \ No newline at end of file diff --git a/QuestPDF/Elements/Shrink.cs b/QuestPDF/Elements/Shrink.cs new file mode 100644 index 0000000..d31f89c --- /dev/null +++ b/QuestPDF/Elements/Shrink.cs @@ -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); + } + } +} \ No newline at end of file diff --git a/QuestPDF/Fluent/ElementExtensions.cs b/QuestPDF/Fluent/ElementExtensions.cs index 804dc84..f2f0a73 100644 --- a/QuestPDF/Fluent/ElementExtensions.cs +++ b/QuestPDF/Fluent/ElementExtensions.cs @@ -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()); diff --git a/QuestPDF/Fluent/ShrinkExtensions.cs b/QuestPDF/Fluent/ShrinkExtensions.cs new file mode 100644 index 0000000..53510de --- /dev/null +++ b/QuestPDF/Fluent/ShrinkExtensions.cs @@ -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 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 + } +} \ No newline at end of file