From a7ac747776544b0f466348d2abc9fcb1651f02b5 Mon Sep 17 00:00:00 2001 From: MarcinZiabek Date: Thu, 29 Sep 2022 21:28:22 +0200 Subject: [PATCH] RTL: dynamic --- QuestPDF.Examples/RightToLeftExamples.cs | 43 ++++++++++++++++++++++++ QuestPDF/Drawing/DocumentGenerator.cs | 2 +- QuestPDF/Elements/Dynamic.cs | 17 +++++++--- 3 files changed, 56 insertions(+), 6 deletions(-) diff --git a/QuestPDF.Examples/RightToLeftExamples.cs b/QuestPDF.Examples/RightToLeftExamples.cs index e045a61..744e084 100644 --- a/QuestPDF.Examples/RightToLeftExamples.cs +++ b/QuestPDF.Examples/RightToLeftExamples.cs @@ -1,4 +1,5 @@ using NUnit.Framework; +using QuestPDF.Elements; using QuestPDF.Examples.Engine; using QuestPDF.Fluent; using QuestPDF.Helpers; @@ -143,5 +144,47 @@ namespace QuestPDF.Examples .Background(Colors.Red.Medium); }); } + + [Test] + public void Dynamic() + { + RenderingTest + .Create() + .ProduceImages() + .PageSize(600, 600) + .ShowResults() + .Render(container => + { + container + .Padding(25) + .ContentFromRightToLeft() + .Dynamic(new SimpleDynamic()); + }); + } + + class SimpleDynamic : IDynamicComponent + { + public int State { get; set; } + + public DynamicComponentComposeResult Compose(DynamicContext context) + { + var content = context.CreateElement(container => + { + container + .Row(row => + { + row.ConstantItem(200).Background(Colors.Red.Lighten2).Height(200); + row.ConstantItem(150).Background(Colors.Green.Lighten2).Height(200); + row.ConstantItem(100).Background(Colors.Blue.Lighten2).Height(200); + }); + }); + + return new DynamicComponentComposeResult + { + Content = content, + HasMoreContent = false + }; + } + } } } \ No newline at end of file diff --git a/QuestPDF/Drawing/DocumentGenerator.cs b/QuestPDF/Drawing/DocumentGenerator.cs index eb9a9ab..bf7c5e1 100644 --- a/QuestPDF/Drawing/DocumentGenerator.cs +++ b/QuestPDF/Drawing/DocumentGenerator.cs @@ -162,7 +162,7 @@ namespace QuestPDF.Drawing return debuggingState; } - private static void ApplyContentDirection(Element? content, ContentDirection direction) + internal static void ApplyContentDirection(this Element? content, ContentDirection direction) { if (content == null) return; diff --git a/QuestPDF/Elements/Dynamic.cs b/QuestPDF/Elements/Dynamic.cs index 482704a..41385bf 100644 --- a/QuestPDF/Elements/Dynamic.cs +++ b/QuestPDF/Elements/Dynamic.cs @@ -6,13 +6,14 @@ using QuestPDF.Infrastructure; namespace QuestPDF.Elements { - internal class DynamicHost : Element, IStateResettable + internal class DynamicHost : Element, IStateResettable, IContentDirectionAware { private DynamicComponentProxy Child { get; } private object InitialComponentState { get; set; } internal TextStyle TextStyle { get; set; } = TextStyle.Default; - + public ContentDirection ContentDirection { get; set; } + public DynamicHost(DynamicComponentProxy child) { Child = child; @@ -51,12 +52,14 @@ namespace QuestPDF.Elements var context = new DynamicContext { - PageNumber = PageContext.CurrentPage, - TotalPages = PageContext.GetLocation(Infrastructure.PageContext.DocumentLocation).PageEnd, PageContext = PageContext, Canvas = Canvas, - TextStyle = TextStyle, + TextStyle = TextStyle, + ContentDirection = ContentDirection, + + PageNumber = PageContext.CurrentPage, + TotalPages = PageContext.GetLocation(Infrastructure.PageContext.DocumentLocation).PageEnd, AvailableSize = availableSize }; @@ -73,7 +76,9 @@ namespace QuestPDF.Elements { internal IPageContext PageContext { get; set; } internal ICanvas Canvas { get; set; } + internal TextStyle TextStyle { get; set; } + internal ContentDirection ContentDirection { get; set; } public int PageNumber { get; internal set; } public int TotalPages { get; internal set; } @@ -85,6 +90,8 @@ namespace QuestPDF.Elements content(container); container.ApplyDefaultTextStyle(TextStyle); + container.ApplyContentDirection(ContentDirection); + container.VisitChildren(x => x?.Initialize(PageContext, Canvas)); container.VisitChildren(x => (x as IStateResettable)?.ResetState());