Browse Source

Layout debugging fixes, minor code refactoring

MarcinZiabek 3 years ago
parent
commit
42c6c75862

+ 1 - 1
QuestPDF/Drawing/DocumentGenerator.cs

@@ -183,7 +183,7 @@ namespace QuestPDF.Drawing
 
 
             content.VisitChildren(x =>
             content.VisitChildren(x =>
             {
             {
-                x.CreateProxy(y => new DebuggingProxy(debuggingState, y));
+                x.CreateProxy(y => y is ElementProxy ? y : new DebuggingProxy(debuggingState, y));
             });
             });
 
 
             return debuggingState;
             return debuggingState;

+ 1 - 2
QuestPDF/Drawing/Proxy/DebugStackItem.cs

@@ -8,7 +8,6 @@ namespace QuestPDF.Drawing.Proxy
         public IElement Element { get; internal set; }
         public IElement Element { get; internal set; }
         public Size AvailableSpace { get; internal set; }
         public Size AvailableSpace { get; internal set; }
         public SpacePlan SpacePlan { get; internal set; }
         public SpacePlan SpacePlan { get; internal set; }
-
-        public ICollection<DebugStackItem> Stack { get; internal set; } = new List<DebugStackItem>();
+        public ICollection<DebugStackItem> Stack { get; } = new List<DebugStackItem>();
     }
     }
 }
 }

+ 2 - 36
QuestPDF/Drawing/Proxy/DebuggingState.cs

@@ -68,7 +68,8 @@ namespace QuestPDF.Drawing.Proxy
                 return  new LayoutRenderingTrace
                 return  new LayoutRenderingTrace
                 {
                 {
                     ElementType = item.Element.GetType().Name,
                     ElementType = item.Element.GetType().Name,
-                    ElementProperties = GetElementConfiguration(item.Element).ToList(),
+                    IsSingleChildContainer = item.Element is ContainerElement,
+                    ElementProperties = item.Element.GetElementConfiguration().ToList(),
                     AvailableSpace = new QuestPDF.Previewer.Size()
                     AvailableSpace = new QuestPDF.Previewer.Size()
                     {
                     {
                         Width = item.AvailableSpace.Width,
                         Width = item.AvailableSpace.Width,
@@ -83,41 +84,6 @@ namespace QuestPDF.Drawing.Proxy
                     Children = item.Stack.Select(Traverse).ToList()
                     Children = item.Stack.Select(Traverse).ToList()
                 };
                 };
             }
             }
-
-            static IEnumerable<DocumentElementProperty> GetElementConfiguration(IElement element)
-            {
-                if (element is DebugPointer)
-                    return Enumerable.Empty<DocumentElementProperty>();
-                
-                return element
-                    .GetType()
-                    .GetProperties()
-                    .Select(x => new
-                    {
-                        Property = x.Name.PrettifyName(),
-                        Value = x.GetValue(element)
-                    })
-                    .Where(x => !(x.Value is IElement))
-                    .Where(x => x.Value is string || !(x.Value is IEnumerable))
-                    .Where(x => !(x.Value is TextStyle))
-                    .Select(x => new DocumentElementProperty
-                    {
-                        Label = x.Property,
-                        Value = FormatValue(x.Value)
-                    });
-
-                string FormatValue(object value)
-                {
-                    const int maxLength = 100;
-                    
-                    var text = value?.ToString() ?? "-";
-
-                    if (text.Length < maxLength)
-                        return text;
-
-                    return text.AsSpan(0, maxLength).ToString() + "...";
-                }
-            }
         }
         }
     }
     }
 }
 }

+ 45 - 0
QuestPDF/Drawing/Proxy/Helpers.cs

@@ -0,0 +1,45 @@
+using System;
+using System.Collections;
+using System.Collections.Generic;
+using System.Linq;
+using QuestPDF.Helpers;
+using QuestPDF.Infrastructure;
+using QuestPDF.Previewer;
+
+namespace QuestPDF.Drawing.Proxy;
+
+internal static class Helpers
+{
+    public static IEnumerable<DocumentElementProperty> GetElementConfiguration(this IElement element)
+    {
+        return element
+            .GetType()
+            .GetProperties()
+            .Select(x => new
+            {
+                Property = x.Name.PrettifyName(),
+                Value = x.GetValue(element)
+            })
+            .Where(x => !(x.Value is IElement))
+            .Where(x => x.Value is string || !(x.Value is IEnumerable))
+            .Where(x => !(x.Value is TextStyle))
+            .Select(x => new DocumentElementProperty
+            {
+                Label = x.Property,
+                Value = FormatValue(x.Value)
+            })
+            .ToList();
+
+        string FormatValue(object value)
+        {
+            const int maxLength = 100;
+                
+            var text = value?.ToString() ?? "-";
+
+            if (text.Length < maxLength)
+                return text;
+
+            return text.AsSpan(0, maxLength).ToString() + "...";
+        }
+    }
+}

+ 1 - 1
QuestPDF/Previewer/Helpers.cs

@@ -12,7 +12,7 @@ internal static class Helpers
 {
 {
     public static Task<HttpResponseMessage> PostAsJsonAsync<TValue>(this HttpClient client, string? requestUri, TValue value, JsonSerializerOptions? options = null, CancellationToken cancellationToken = default)
     public static Task<HttpResponseMessage> PostAsJsonAsync<TValue>(this HttpClient client, string? requestUri, TValue value, JsonSerializerOptions? options = null, CancellationToken cancellationToken = default)
     {
     {
-        var json = JsonSerializer.Serialize(value);
+        var json = JsonSerializer.Serialize(value, options);
         var jsonContent = new StringContent(json, Encoding.UTF8, "application/json");
         var jsonContent = new StringContent(json, Encoding.UTF8, "application/json");
         
         
         return client.PostAsync(requestUri, jsonContent, cancellationToken);
         return client.PostAsync(requestUri, jsonContent, cancellationToken);

+ 2 - 34
QuestPDF/Previewer/Inspection/DocumentHierarchyProcessor.cs

@@ -102,43 +102,11 @@ public static class DocumentHierarchyProcessor
             return new InspectionElement
             return new InspectionElement
             {
             {
                 ElementType = inspectionProxy.Child.GetType().Name,
                 ElementType = inspectionProxy.Child.GetType().Name,
+                IsSingleChildContainer = inspectionProxy.Child is ContainerElement,
                 Location = locations,
                 Location = locations,
-                Properties = GetElementConfiguration(inspectionProxy.Child),
+                Properties = inspectionProxy.Child.GetElementConfiguration().ToList(),
                 Children = new List<InspectionElement>()
                 Children = new List<InspectionElement>()
             };
             };
         }
         }
-        
-        static IReadOnlyCollection<DocumentElementProperty> GetElementConfiguration(IElement element)
-        {
-            return element
-                .GetType()
-                .GetProperties()
-                .Select(x => new
-                {
-                    Property = x.Name.PrettifyName(),
-                    Value = x.GetValue(element)
-                })
-                .Where(x => !(x.Value is IElement))
-                .Where(x => x.Value is string || !(x.Value is IEnumerable))
-                .Where(x => !(x.Value is TextStyle))
-                .Select(x => new DocumentElementProperty
-                {
-                    Label = x.Property,
-                    Value = FormatValue(x.Value)
-                })
-                .ToList();
-
-            string FormatValue(object value)
-            {
-                const int maxLength = 100;
-                
-                var text = value?.ToString() ?? "-";
-
-                if (text.Length < maxLength)
-                    return text;
-
-                return text.AsSpan(0, maxLength).ToString() + "...";
-            }
-        }
     }
     }
 }
 }

+ 4 - 3
QuestPDF/Previewer/Inspection/InspectionStateItem.cs

@@ -25,9 +25,10 @@ namespace QuestPDF.Previewer.Inspection
     
     
     internal class InspectionElement
     internal class InspectionElement
     {
     {
-        public string ElementType { get; internal set; }
-        public IReadOnlyCollection<InspectionElementLocation> Location { get; internal set; }
-        public IReadOnlyCollection<DocumentElementProperty> Properties { get; internal set; }
+        public string ElementType { get; set; }
+        public bool IsSingleChildContainer { get; set; }
+        public IReadOnlyCollection<InspectionElementLocation> Location { get; set; }
+        public IReadOnlyCollection<DocumentElementProperty> Properties { get; set; }
         public ICollection<InspectionElement> Children { get; set; }
         public ICollection<InspectionElement> Children { get; set; }
     }
     }
 }
 }

+ 1 - 0
QuestPDF/Previewer/Models.cs

@@ -26,6 +26,7 @@ internal sealed class SpacePlan
 internal sealed class LayoutRenderingTrace
 internal sealed class LayoutRenderingTrace
 {
 {
     public string ElementType { get; set; }
     public string ElementType { get; set; }
+    public bool IsSingleChildContainer { get; internal set; }
     public IReadOnlyCollection<DocumentElementProperty> ElementProperties { get; set; }
     public IReadOnlyCollection<DocumentElementProperty> ElementProperties { get; set; }
     public Size AvailableSpace { get; set; }
     public Size AvailableSpace { get; set; }
     public SpacePlan SpacePlan { get; set; }
     public SpacePlan SpacePlan { get; set; }

+ 13 - 1
QuestPDF/Previewer/PreviewerService.cs

@@ -6,6 +6,7 @@ using System.Diagnostics;
 using System.Net.Http;
 using System.Net.Http;
 using System.Text;
 using System.Text;
 using System.Text.Json;
 using System.Text.Json;
+using System.Text.Json.Serialization;
 using System.Threading.Tasks;
 using System.Threading.Tasks;
 using QuestPDF.Drawing;
 using QuestPDF.Drawing;
 using QuestPDF.Drawing.Exceptions;
 using QuestPDF.Drawing.Exceptions;
@@ -16,6 +17,7 @@ namespace QuestPDF.Previewer
     internal sealed class NotifyPresenceRequest
     internal sealed class NotifyPresenceRequest
     {
     {
         public string Id { get; set; }
         public string Id { get; set; }
+        public string LibraryVersion { get; set; }
         public bool IsDotnet6OrBeyond { get; set; }
         public bool IsDotnet6OrBeyond { get; set; }
         public bool IsDotnet3OrBeyond { get; set; }
         public bool IsDotnet3OrBeyond { get; set; }
         public bool IsExecutedInUnitTest { get; set; }
         public bool IsExecutedInUnitTest { get; set; }
@@ -149,6 +151,7 @@ namespace QuestPDF.Previewer
             var payload = new NotifyPresenceRequest
             var payload = new NotifyPresenceRequest
             {
             {
                 Id = ClientId,
                 Id = ClientId,
+                LibraryVersion = System.Reflection.Assembly.GetExecutingAssembly().GetName().Version?.ToString(),
                 IsDotnet6OrBeyond = RuntimeDetector.IsNet6OrGreater,
                 IsDotnet6OrBeyond = RuntimeDetector.IsNet6OrGreater,
                 IsDotnet3OrBeyond = RuntimeDetector.IsNet3OrGreater,
                 IsDotnet3OrBeyond = RuntimeDetector.IsNet3OrGreater,
                 IsExecutedInUnitTest = UnitTestDetector.RunningInUnitTest
                 IsExecutedInUnitTest = UnitTestDetector.RunningInUnitTest
@@ -228,7 +231,16 @@ namespace QuestPDF.Previewer
                 Trace = exception.ElementTrace
                 Trace = exception.ElementTrace
             };
             };
 
 
-            await HttpClient.PostAsJsonAsync("/v1/update/error/layout", payload);
+            var jsonSerializerOptions = new JsonSerializerOptions
+            {
+                MaxDepth = 256,
+                Converters =
+                {
+                    new JsonStringEnumConverter(JsonNamingPolicy.CamelCase)
+                }
+            };
+            
+            await HttpClient.PostAsJsonAsync("/v1/update/error/layout", payload, jsonSerializerOptions);
         }
         }