Browse Source

Fluent API: refactor Line and LineExtensions to improve structure and usability

Marcin Ziąbek 3 weeks ago
parent
commit
45d515a749
2 changed files with 53 additions and 56 deletions
  1. 1 6
      Source/QuestPDF/Elements/Line.cs
  2. 52 50
      Source/QuestPDF/Fluent/LineExtensions.cs

+ 1 - 6
Source/QuestPDF/Elements/Line.cs

@@ -6,18 +6,13 @@ using QuestPDF.Skia;
 
 
 namespace QuestPDF.Elements
 namespace QuestPDF.Elements
 {
 {
-    public interface ILine
-    {
-        
-    }
-    
     internal enum LineType
     internal enum LineType
     {
     {
         Vertical,
         Vertical,
         Horizontal
         Horizontal
     }
     }
 
 
-    internal sealed class Line : Element, ILine, IStateful
+    internal sealed class Line : Element, IStateful
     {
     {
         public LineType Type { get; set; } = LineType.Vertical;
         public LineType Type { get; set; } = LineType.Vertical;
         public Color Color { get; set; } = Colors.Black;
         public Color Color { get; set; } = Colors.Black;

+ 52 - 50
Source/QuestPDF/Fluent/LineExtensions.cs

@@ -1,62 +1,22 @@
 using System;
 using System;
 using System.Linq;
 using System.Linq;
 using QuestPDF.Elements;
 using QuestPDF.Elements;
-using QuestPDF.Helpers;
 using QuestPDF.Infrastructure;
 using QuestPDF.Infrastructure;
 
 
 namespace QuestPDF.Fluent
 namespace QuestPDF.Fluent
 {
 {
-    public static class LineExtensions
+    public class LineDescriptor
     {
     {
-        private static ILine Line(this IContainer element, LineType type, float thickness)
-        {
-            if (thickness < 0)
-                throw new ArgumentOutOfRangeException(nameof(thickness), "The Line thickness cannot be negative.");
-            
-            var line = new Line
-            {
-                Thickness = thickness,
-                Type = type
-            };
-
-            element.Element(line);
-            return line;
-        }
-        
-        /// <summary>
-        /// Renders a vertical line with a specified thickness.
-        /// <a href="https://www.questpdf.com/api-reference/line.html">Learn more</a>
-        /// </summary>
-        /// <remarks>
-        /// The line is not just a visual element; it occupies actual space within the document.
-        /// </remarks>
-        /// <returns>A descriptor to modify line attributes.</returns>
-        public static ILine LineVertical(this IContainer element, float thickness, Unit unit = Unit.Point)
-        {
-            return element.Line(LineType.Vertical, thickness.ToPoints(unit));
-        }
-        
-        /// <summary>
-        /// Renders a horizontal line with a specified thickness.
-        /// <a href="https://www.questpdf.com/api-reference/line.html">Learn more</a>
-        /// </summary>
-        /// <remarks>
-        /// The line is not just a visual element; it occupies actual space within the document.
-        /// </remarks>
-        /// <returns>A descriptor to modify line attributes.</returns>
-        public static ILine LineHorizontal(this IContainer element, float thickness, Unit unit = Unit.Point)
-        {
-            return element.Line(LineType.Horizontal, thickness.ToPoints(unit));
-        }
+        internal Line Line { get; } = new Line();
         
         
         /// <summary>
         /// <summary>
         /// Specifies the color for the line.
         /// Specifies the color for the line.
         /// </summary>
         /// </summary>
         /// <include file='../Resources/Documentation.xml' path='documentation/doc[@for="colorParam"]/*' />
         /// <include file='../Resources/Documentation.xml' path='documentation/doc[@for="colorParam"]/*' />
-        public static ILine LineColor(this ILine descriptor, Color color)
+        public LineDescriptor LineColor(Color color)
         {
         {
-            (descriptor as Line).Color = color;
-            return descriptor;
+            Line.Color = color;
+            return this;
         }
         }
 
 
         /// <summary>
         /// <summary>
@@ -64,7 +24,7 @@ namespace QuestPDF.Fluent
         /// For example, a pattern of [2, 3] creates a dash of 2 units followed by a gap of 3 units.
         /// For example, a pattern of [2, 3] creates a dash of 2 units followed by a gap of 3 units.
         /// </summary>
         /// </summary>
         /// <param name="dashPattern">The length of this array must be even.</param>
         /// <param name="dashPattern">The length of this array must be even.</param>
-        public static ILine LineDashPattern(this ILine descriptor, float[] dashPattern, Unit unit = Unit.Point)
+        public LineDescriptor LineDashPattern(float[] dashPattern, Unit unit = Unit.Point)
         {
         {
             if (dashPattern == null)
             if (dashPattern == null)
                 throw new ArgumentNullException(nameof(dashPattern), "The dash pattern cannot be null.");
                 throw new ArgumentNullException(nameof(dashPattern), "The dash pattern cannot be null.");
@@ -75,14 +35,14 @@ namespace QuestPDF.Fluent
             if (dashPattern.Length % 2 != 0)
             if (dashPattern.Length % 2 != 0)
                 throw new ArgumentException("The dash pattern must contain an even number of elements.", nameof(dashPattern));
                 throw new ArgumentException("The dash pattern must contain an even number of elements.", nameof(dashPattern));
             
             
-            (descriptor as Line).DashPattern = dashPattern.Select(x => x.ToPoints(unit)).ToArray();
-            return descriptor;
+            Line.DashPattern = dashPattern.Select(x => x.ToPoints(unit)).ToArray();
+            return this;
         }
         }
 
 
         /// <summary>
         /// <summary>
         /// Applies a linear gradient to a line using the specified colors.
         /// Applies a linear gradient to a line using the specified colors.
         /// </summary>
         /// </summary>
-        public static ILine LineGradient(this ILine descriptor, Color[] colors)
+        public LineDescriptor LineGradient(Color[] colors)
         {
         {
             if (colors == null)
             if (colors == null)
                 throw new ArgumentNullException(nameof(colors), "The gradient colors cannot be null.");
                 throw new ArgumentNullException(nameof(colors), "The gradient colors cannot be null.");
@@ -90,8 +50,50 @@ namespace QuestPDF.Fluent
             if (colors.Length == 0)
             if (colors.Length == 0)
                 throw new ArgumentException("The gradient colors cannot be empty.", nameof(colors));
                 throw new ArgumentException("The gradient colors cannot be empty.", nameof(colors));
 
 
-            (descriptor as Line).GradientColors = colors;
+            Line.GradientColors = colors;
+            return this;
+        }
+    }
+    
+    public static class LineExtensions
+    {
+        private static LineDescriptor Line(this IContainer element, LineType type, float thickness)
+        {
+            if (thickness < 0)
+                throw new ArgumentOutOfRangeException(nameof(thickness), "The Line thickness cannot be negative.");
+            
+            var descriptor = new LineDescriptor();
+            descriptor.Line.Thickness = thickness;
+            descriptor.Line.Type = type;
+            
+            element.Element(descriptor.Line);
             return descriptor;
             return descriptor;
         }
         }
+        
+        /// <summary>
+        /// Renders a vertical line with a specified thickness.
+        /// <a href="https://www.questpdf.com/api-reference/line.html">Learn more</a>
+        /// </summary>
+        /// <remarks>
+        /// The line is not just a visual element; it occupies actual space within the document.
+        /// </remarks>
+        /// <returns>A descriptor to modify line attributes.</returns>
+        public static LineDescriptor LineVertical(this IContainer element, float thickness, Unit unit = Unit.Point)
+        {
+            return element.Line(LineType.Vertical, thickness.ToPoints(unit));
+        }
+        
+        /// <summary>
+        /// Renders a horizontal line with a specified thickness.
+        /// <a href="https://www.questpdf.com/api-reference/line.html">Learn more</a>
+        /// </summary>
+        /// <remarks>
+        /// The line is not just a visual element; it occupies actual space within the document.
+        /// </remarks>
+        /// <returns>A descriptor to modify line attributes.</returns>
+        public static LineDescriptor LineHorizontal(this IContainer element, float thickness, Unit unit = Unit.Point)
+        {
+            return element.Line(LineType.Horizontal, thickness.ToPoints(unit));
+        }
     }
     }
 }
 }