Browse Source

Fixed scrolling math

MarcinZiabek 3 years ago
parent
commit
c21744e7de

+ 11 - 12
QuestPDF.Previewer/InteractiveCanvas.cs

@@ -23,12 +23,13 @@ class InteractiveCanvas : ICustomDrawOperation
     private const float MaxScale = 10f;
 
     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.Max(x => x.Size.Width);
     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
     {
@@ -46,10 +47,8 @@ class InteractiveCanvas : ICustomDrawOperation
     {
         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 +62,14 @@ class InteractiveCanvas : ICustomDrawOperation
     
     private void LimitTranslate()
     {
-        if (TotalHeight * Scale > Height)
+        if (TotalPagesHeight * Scale > Height)
         {
             TranslateY = Math.Min(TranslateY, MaxTranslateY);
             TranslateY = Math.Max(TranslateY, MinTranslateY);
         }
         else
         {
-            TranslateY = -TotalHeight / 2;
+            TranslateY = -TotalPagesHeight / 2;
         }
 
         if (Width / Scale < MaxWidth)
@@ -178,15 +177,15 @@ class InteractiveCanvas : ICustomDrawOperation
 
     #region inner viewport gradient
 
-    private const float InnerGradientSize = 24f;
+    private const int InnerGradientSize = 24;
     private static readonly SKColor InnerGradientColor = SKColor.Parse("#666");
     
     private void DrawInnerGradient(SKCanvas canvas)
     {
         // gamma correction
         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 => (byte)(x * 255))
             .Select(x => InnerGradientColor.WithAlpha(x))

+ 5 - 6
QuestPDF.Previewer/PreviewerWindow.axaml

@@ -26,9 +26,9 @@
 				<ColumnDefinition Width="Auto" />
 			</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" 
-			           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"
 			                            HorizontalAlignment="Stretch" 
@@ -36,13 +36,12 @@
 			                            CurrentScroll="{Binding #Window.CurrentScroll, Mode=TwoWay}"
 			                            ScrollViewportSize="{Binding #Window.ScrollViewportSize, Mode=OneWayToSource}"
 			                            Pages="{Binding #Window.DocumentRenderer.Pages, Mode=OneWay}" />
-
-
-			<ScrollBar Grid.Row="1" Grid.Column="1" 
+			
+			<ScrollBar Grid.Row="1" Grid.Column="1"
 			           Orientation="Vertical" 
 			           AllowAutoHide="False" 
 			           Minimum="0" Maximum="1" 
-			           IsVisible="{Binding #Window.VerticalScrollbarVisible}"
+			           IsVisible="{Binding #Window.VerticalScrollbarVisible, Mode=OneWay}"
 			           Value="{Binding #Window.CurrentScroll, Mode=TwoWay}" 
 			           ViewportSize="{Binding #Window.ScrollViewportSize, Mode=OneWay}"></ScrollBar>
 		</Grid>

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

@@ -26,7 +26,7 @@ namespace QuestPDF.Previewer
         
         
         
-        public static readonly StyledProperty<float> CurrentScrollProperty = AvaloniaProperty.Register<PreviewerControl, float>(nameof(CurrentScroll));
+        public static readonly StyledProperty<float> CurrentScrollProperty = AvaloniaProperty.Register<PreviewerWindow, float>(nameof(CurrentScroll));
         
         public float CurrentScroll
         {
@@ -34,7 +34,7 @@ namespace QuestPDF.Previewer
             set => SetValue(CurrentScrollProperty, value);
         }
         
-        public static readonly StyledProperty<float> ScrollViewportSizeProperty = AvaloniaProperty.Register<PreviewerControl, float>(nameof(ScrollViewportSize));
+        public static readonly StyledProperty<float> ScrollViewportSizeProperty = AvaloniaProperty.Register<PreviewerWindow, float>(nameof(ScrollViewportSize));
         
         public float ScrollViewportSize
         {
@@ -42,7 +42,7 @@ namespace QuestPDF.Previewer
             set => SetValue(ScrollViewportSizeProperty, value);
         }
         
-        public static readonly StyledProperty<bool> VerticalScrollbarVisibleProperty = AvaloniaProperty.Register<PreviewerControl, bool>(nameof(VerticalScrollbarVisible));
+        public static readonly StyledProperty<bool> VerticalScrollbarVisibleProperty = AvaloniaProperty.Register<PreviewerWindow, bool>(nameof(VerticalScrollbarVisible));
         
         public bool VerticalScrollbarVisible
         {
@@ -58,7 +58,6 @@ namespace QuestPDF.Previewer
             ScrollViewportSizeProperty.Changed.Subscribe(_ =>
             {
                 VerticalScrollbarVisible = ScrollViewportSize < 1;
-                Debug.WriteLine($"Scrollbar visible: {VerticalScrollbarVisible}");
             });
             
             // this.FindControl<Button>("GeneratePdf")