Browse Source

ShowIf implementation

Marcin Ziąbek 2 years ago
parent
commit
77c36aea25

+ 2 - 2
Source/QuestPDF.Examples/ShowOnceExample.cs

@@ -54,7 +54,7 @@ namespace QuestPDF.Examples
         }
         }
         
         
         [Test]
         [Test]
-        public void ShowWhen()
+        public void ShowIf()
         {
         {
             RenderingTest
             RenderingTest
                 .Create()
                 .Create()
@@ -89,7 +89,7 @@ namespace QuestPDF.Examples
                                 }   
                                 }   
                                 
                                 
                                 column.Item().PageBreak();
                                 column.Item().PageBreak();
-                                column.Item().ShowWhen(x => x.PageNumber % 2 == 0).PageBreak();
+                                column.Item().ShowIf(x => x.PageNumber % 2 == 0).PageBreak();
                             }
                             }
                         });
                         });
                         
                         

+ 3 - 1
Source/QuestPDF/Drawing/DocumentGenerator.cs

@@ -194,10 +194,13 @@ namespace QuestPDF.Drawing
 
 
             while(true)
             while(true)
             {
             {
+                pageContext.IncrementPageNumber();
                 var spacePlan = content.Measure(Size.Max);
                 var spacePlan = content.Measure(Size.Max);
 
 
                 if (spacePlan.Type == SpacePlanType.Wrap)
                 if (spacePlan.Type == SpacePlanType.Wrap)
                 {
                 {
+                    pageContext.DecrementPageNumber();
+                    
                     if (Settings.EnableDebugging)
                     if (Settings.EnableDebugging)
                     {
                     {
                         ApplyLayoutDebugging();
                         ApplyLayoutDebugging();
@@ -210,7 +213,6 @@ namespace QuestPDF.Drawing
 
 
                 try
                 try
                 {
                 {
-                    pageContext.IncrementPageNumber();
                     canvas.BeginPage(spacePlan);
                     canvas.BeginPage(spacePlan);
                     content.Draw(spacePlan);
                     content.Draw(spacePlan);
                 }
                 }

+ 4 - 4
Source/QuestPDF/Elements/ShowWhen.cs → Source/QuestPDF/Elements/ShowIf.cs

@@ -4,15 +4,15 @@ using QuestPDF.Infrastructure;
 
 
 namespace QuestPDF.Elements;
 namespace QuestPDF.Elements;
 
 
-public class ShowWhenContext
+public class ShowIfContext
 {
 {
     public int PageNumber { get; internal set; }
     public int PageNumber { get; internal set; }
     public int TotalPages { get; internal set; }
     public int TotalPages { get; internal set; }
 }
 }
 
 
-internal class ShowWhen : ContainerElement
+internal class ShowIf : ContainerElement
 {
 {
-    public Predicate<ShowWhenContext> VisibilityPredicate { get; set; }
+    public Predicate<ShowIfContext> VisibilityPredicate { get; set; }
     
     
     internal override SpacePlan Measure(Size availableSpace)
     internal override SpacePlan Measure(Size availableSpace)
     {
     {
@@ -30,7 +30,7 @@ internal class ShowWhen : ContainerElement
 
 
     private bool CheckVisibility()
     private bool CheckVisibility()
     {
     {
-        var context = new ShowWhenContext
+        var context = new ShowIfContext
         {
         {
             PageNumber = PageContext.CurrentPage,
             PageNumber = PageContext.CurrentPage,
             TotalPages = PageContext.DocumentLength
             TotalPages = PageContext.DocumentLength

+ 15 - 9
Source/QuestPDF/Fluent/ElementExtensions.cs

@@ -119,14 +119,6 @@ namespace QuestPDF.Fluent
                 Text = text ?? string.Empty
                 Text = text ?? string.Empty
             });
             });
         }
         }
-        
-        public static IContainer ShowWhen(this IContainer element, Predicate<ShowWhenContext> predicate)
-        {
-            return element.Element(new ShowWhen
-            {
-                VisibilityPredicate = predicate
-            });
-        }
 
 
         /// <summary>
         /// <summary>
         /// If the container spans multiple pages, its content appears only on the first one.
         /// If the container spans multiple pages, its content appears only on the first one.
@@ -283,7 +275,7 @@ namespace QuestPDF.Fluent
         }
         }
         
         
         /// <summary>
         /// <summary>
-        /// Conditionally draws or hides its content.
+        /// Conditionally draws or hides its inner content.
         /// <a href="https://www.questpdf.com/api-reference/show-if.html">Learn more</a>
         /// <a href="https://www.questpdf.com/api-reference/show-if.html">Learn more</a>
         /// </summary>
         /// </summary>
         /// <param name="condition">If the value is <see langword="true"/>, its content is visible. Otherwise, it's hidden.</param>
         /// <param name="condition">If the value is <see langword="true"/>, its content is visible. Otherwise, it's hidden.</param>
@@ -292,6 +284,20 @@ namespace QuestPDF.Fluent
             return condition ? element : new Container();
             return condition ? element : new Container();
         }
         }
         
         
+        /// <summary>
+        /// Conditionally draws or hides its inner content depending on drawing context.
+        /// Please use carefully as certain predicates may produce unstable layouts resulting with unexpected content or exceptions.
+        /// <a href="https://www.questpdf.com/api-reference/show-if.html">Learn more</a>
+        /// </summary>
+        /// <param name="predicate">If the predicate returns <see langword="true"/>, its content is visible. Otherwise, it's hidden.</param>
+        public static IContainer ShowIf(this IContainer element, Predicate<ShowIfContext> predicate)
+        {
+            return element.Element(new ShowIf
+            {
+                VisibilityPredicate = predicate
+            });
+        }
+        
         /// <summary>
         /// <summary>
         /// Provides direct access to the low-level SkiaSharp API.
         /// Provides direct access to the low-level SkiaSharp API.
         /// <a href="https://www.questpdf.com/api-reference/canvas.html">Learn more</a>
         /// <a href="https://www.questpdf.com/api-reference/canvas.html">Learn more</a>

+ 5 - 0
Source/QuestPDF/Infrastructure/PageContext.cs

@@ -21,6 +21,11 @@ namespace QuestPDF.Infrastructure
             CurrentPage = 0;
             CurrentPage = 0;
         }
         }
         
         
+        internal void DecrementPageNumber()
+        {
+            CurrentPage--;
+        }
+        
         internal void IncrementPageNumber()
         internal void IncrementPageNumber()
         {
         {
             CurrentPage++;
             CurrentPage++;