Browse Source

Merge branch 'previewer' into previewer

Marcin Ziąbek 3 years ago
parent
commit
c801894ec4

+ 12 - 12
QuestPDF.Previewer/InteractiveCanvas.cs

@@ -23,12 +23,14 @@ class InteractiveCanvas : ICustomDrawOperation
     private const float MaxScale = 10f;
     private const float MaxScale = 10f;
 
 
     private const float PageSpacing = 25f;
     private const float PageSpacing = 25f;
-    private const float SafeZone = 50f;
+    private const float SafeZone = InnerGradientSize;
 
 
-    public float TotalHeight => Pages.Sum(x => x.Size.Height) + (Pages.Count - 1) * PageSpacing;
+    public float TotalPagesHeight => Pages.Sum(x => x.Size.Height) + (Pages.Count - 1) * PageSpacing;
+    public float TotalHeight => TotalPagesHeight + SafeZone * 2 / Scale;
     public float MaxWidth => Pages.Any() ? Pages.Max(x => x.Size.Width) : 0;
     public float MaxWidth => Pages.Any() ? Pages.Max(x => x.Size.Width) : 0;
+    
     public float MaxTranslateY => -(Height / 2 - SafeZone) / Scale;
     public float MaxTranslateY => -(Height / 2 - SafeZone) / Scale;
-    public float MinTranslateY => (Height / 2 - SafeZone) / Scale - TotalHeight;
+    public float MinTranslateY => (Height / 2 + SafeZone) / Scale - TotalHeight;
 
 
     public float ScrollPercentY
     public float ScrollPercentY
     {
     {
@@ -46,10 +48,8 @@ class InteractiveCanvas : ICustomDrawOperation
     {
     {
         get
         get
         {
         {
-            if (TotalHeight * Scale < Height)
-                return 1;
-            
-            return Math.Clamp(Height / Scale / (MaxTranslateY - MinTranslateY), 0, 1);
+            var viewPortSize = Height / Scale / TotalHeight;
+            return Math.Clamp(viewPortSize, 0, 1);
         }
         }
     }
     }
 
 
@@ -63,14 +63,14 @@ class InteractiveCanvas : ICustomDrawOperation
     
     
     private void LimitTranslate()
     private void LimitTranslate()
     {
     {
-        if (TotalHeight * Scale > Height)
+        if (TotalPagesHeight * Scale > Height)
         {
         {
             TranslateY = Math.Min(TranslateY, MaxTranslateY);
             TranslateY = Math.Min(TranslateY, MaxTranslateY);
             TranslateY = Math.Max(TranslateY, MinTranslateY);
             TranslateY = Math.Max(TranslateY, MinTranslateY);
         }
         }
         else
         else
         {
         {
-            TranslateY = -TotalHeight / 2;
+            TranslateY = -TotalPagesHeight / 2;
         }
         }
 
 
         if (Width / Scale < MaxWidth)
         if (Width / Scale < MaxWidth)
@@ -178,15 +178,15 @@ class InteractiveCanvas : ICustomDrawOperation
 
 
     #region inner viewport gradient
     #region inner viewport gradient
 
 
-    private const float InnerGradientSize = 24f;
+    private const int InnerGradientSize = 24;
     private static readonly SKColor InnerGradientColor = SKColor.Parse("#666");
     private static readonly SKColor InnerGradientColor = SKColor.Parse("#666");
     
     
     private void DrawInnerGradient(SKCanvas canvas)
     private void DrawInnerGradient(SKCanvas canvas)
     {
     {
         // gamma correction
         // gamma correction
         var colors = Enumerable
         var colors = Enumerable
-            .Range(0, 17)
-            .Select(x => 1f - x / 16f)
+            .Range(0, InnerGradientSize)
+            .Select(x => 1f - x / (float) InnerGradientSize)
             .Select(x => Math.Pow(x, 2f))
             .Select(x => Math.Pow(x, 2f))
             .Select(x => (byte)(x * 255))
             .Select(x => (byte)(x * 255))
             .Select(x => InnerGradientColor.WithAlpha(x))
             .Select(x => InnerGradientColor.WithAlpha(x))

+ 5 - 6
QuestPDF.Previewer/PreviewerWindow.axaml

@@ -26,9 +26,9 @@
 				<ColumnDefinition Width="Auto" />
 				<ColumnDefinition Width="Auto" />
 			</Grid.ColumnDefinitions>
 			</Grid.ColumnDefinitions>
 			
 			
-			<TextBlock Grid.Row="0" Grid.Column="0" Grid.ColumnSpan="2" 
+			<TextBlock Grid.Row="0" Grid.Column="0" Grid.ColumnSpan="2"
 			           VerticalAlignment="Center" HorizontalAlignment="Center" 
 			           VerticalAlignment="Center" HorizontalAlignment="Center" 
-			           TextAlignment="Center" Text="QuestPDF Document Preview" FontSize="14" Foreground="#DFFF" FontWeight="Regular" IsHitTestVisible="False" />
+			           TextAlignment="Center" Text="QuestPDF Previewer" FontSize="14" Foreground="#DFFF" FontWeight="Regular" IsHitTestVisible="False" />
 			
 			
 			<previewer:PreviewerControl Grid.Row="1" Grid.Column="0" Grid.ColumnSpan="2"
 			<previewer:PreviewerControl Grid.Row="1" Grid.Column="0" Grid.ColumnSpan="2"
 			                            HorizontalAlignment="Stretch" 
 			                            HorizontalAlignment="Stretch" 
@@ -36,13 +36,12 @@
 			                            CurrentScroll="{Binding #Window.CurrentScroll, Mode=TwoWay}"
 			                            CurrentScroll="{Binding #Window.CurrentScroll, Mode=TwoWay}"
 			                            ScrollViewportSize="{Binding #Window.ScrollViewportSize, Mode=OneWayToSource}"
 			                            ScrollViewportSize="{Binding #Window.ScrollViewportSize, Mode=OneWayToSource}"
 			                            Pages="{Binding #Window.DocumentRenderer.Pages, Mode=OneWay}" />
 			                            Pages="{Binding #Window.DocumentRenderer.Pages, Mode=OneWay}" />
-
-
-			<ScrollBar Grid.Row="1" Grid.Column="1" 
+			
+			<ScrollBar Grid.Row="1" Grid.Column="1"
 			           Orientation="Vertical" 
 			           Orientation="Vertical" 
 			           AllowAutoHide="False" 
 			           AllowAutoHide="False" 
 			           Minimum="0" Maximum="1" 
 			           Minimum="0" Maximum="1" 
-			           IsVisible="{Binding #Window.VerticalScrollbarVisible}"
+			           IsVisible="{Binding #Window.VerticalScrollbarVisible, Mode=OneWay}"
 			           Value="{Binding #Window.CurrentScroll, Mode=TwoWay}" 
 			           Value="{Binding #Window.CurrentScroll, Mode=TwoWay}" 
 			           ViewportSize="{Binding #Window.ScrollViewportSize, Mode=OneWay}"></ScrollBar>
 			           ViewportSize="{Binding #Window.ScrollViewportSize, Mode=OneWay}"></ScrollBar>
 		</Grid>
 		</Grid>

+ 4 - 8
QuestPDF.Previewer/PreviewerWindow.axaml.cs

@@ -24,8 +24,7 @@ namespace QuestPDF.Previewer
             set => SetValue(DocumentProperty, value);
             set => SetValue(DocumentProperty, value);
         }
         }
 
 
-        public static readonly StyledProperty<float> CurrentScrollProperty =
-            AvaloniaProperty.Register<PreviewerWindow, float>(nameof(CurrentScroll));
+        public static readonly StyledProperty<float> CurrentScrollProperty = AvaloniaProperty.Register<PreviewerWindow, float>(nameof(CurrentScroll));
 
 
         public float CurrentScroll
         public float CurrentScroll
         {
         {
@@ -33,8 +32,7 @@ namespace QuestPDF.Previewer
             set => SetValue(CurrentScrollProperty, value);
             set => SetValue(CurrentScrollProperty, value);
         }
         }
 
 
-        public static readonly StyledProperty<float> ScrollViewportSizeProperty =
-            AvaloniaProperty.Register<PreviewerWindow, float>(nameof(ScrollViewportSize));
+        public static readonly StyledProperty<float> ScrollViewportSizeProperty = AvaloniaProperty.Register<PreviewerWindow, float>(nameof(ScrollViewportSize));
 
 
         public float ScrollViewportSize
         public float ScrollViewportSize
         {
         {
@@ -42,8 +40,7 @@ namespace QuestPDF.Previewer
             set => SetValue(ScrollViewportSizeProperty, value);
             set => SetValue(ScrollViewportSizeProperty, value);
         }
         }
 
 
-        public static readonly StyledProperty<bool> VerticalScrollbarVisibleProperty =
-            AvaloniaProperty.Register<PreviewerWindow, bool>(nameof(VerticalScrollbarVisible));
+        public static readonly StyledProperty<bool> VerticalScrollbarVisibleProperty = AvaloniaProperty.Register<PreviewerWindow, bool>(nameof(VerticalScrollbarVisible));
 
 
         public bool VerticalScrollbarVisible
         public bool VerticalScrollbarVisible
         {
         {
@@ -59,9 +56,8 @@ namespace QuestPDF.Previewer
                 .Subscribe(e => Dispatcher.UIThread.Post(() =>
                 .Subscribe(e => Dispatcher.UIThread.Post(() =>
                 {
                 {
                     VerticalScrollbarVisible = e.NewValue.Value < 1;
                     VerticalScrollbarVisible = e.NewValue.Value < 1;
-                    Debug.WriteLine($"Scrollbar visible: {VerticalScrollbarVisible}");
                 }));
                 }));
-
+                
             // this.FindControl<Button>("GeneratePdf")
             // this.FindControl<Button>("GeneratePdf")
             //     .Click += (_, _) => _ = PreviewerUtils.SavePdfWithDialog(Document, this);
             //     .Click += (_, _) => _ = PreviewerUtils.SavePdfWithDialog(Document, this);