From c21744e7de7d5f497a272f058c35bda7e5720faf Mon Sep 17 00:00:00 2001 From: MarcinZiabek Date: Wed, 23 Mar 2022 23:31:27 +0100 Subject: [PATCH] Fixed scrolling math --- QuestPDF.Previewer/InteractiveCanvas.cs | 23 ++++++++++----------- QuestPDF.Previewer/PreviewerWindow.axaml | 11 +++++----- QuestPDF.Previewer/PreviewerWindow.axaml.cs | 7 +++---- 3 files changed, 19 insertions(+), 22 deletions(-) diff --git a/QuestPDF.Previewer/InteractiveCanvas.cs b/QuestPDF.Previewer/InteractiveCanvas.cs index 63ca72b..26a6789 100644 --- a/QuestPDF.Previewer/InteractiveCanvas.cs +++ b/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)) diff --git a/QuestPDF.Previewer/PreviewerWindow.axaml b/QuestPDF.Previewer/PreviewerWindow.axaml index 34a2a14..50daf0e 100644 --- a/QuestPDF.Previewer/PreviewerWindow.axaml +++ b/QuestPDF.Previewer/PreviewerWindow.axaml @@ -26,9 +26,9 @@ - + TextAlignment="Center" Text="QuestPDF Previewer" FontSize="14" Foreground="#DFFF" FontWeight="Regular" IsHitTestVisible="False" /> - - - diff --git a/QuestPDF.Previewer/PreviewerWindow.axaml.cs b/QuestPDF.Previewer/PreviewerWindow.axaml.cs index 27de11e..d2524a4 100644 --- a/QuestPDF.Previewer/PreviewerWindow.axaml.cs +++ b/QuestPDF.Previewer/PreviewerWindow.axaml.cs @@ -26,7 +26,7 @@ namespace QuestPDF.Previewer - public static readonly StyledProperty CurrentScrollProperty = AvaloniaProperty.Register(nameof(CurrentScroll)); + public static readonly StyledProperty CurrentScrollProperty = AvaloniaProperty.Register(nameof(CurrentScroll)); public float CurrentScroll { @@ -34,7 +34,7 @@ namespace QuestPDF.Previewer set => SetValue(CurrentScrollProperty, value); } - public static readonly StyledProperty ScrollViewportSizeProperty = AvaloniaProperty.Register(nameof(ScrollViewportSize)); + public static readonly StyledProperty ScrollViewportSizeProperty = AvaloniaProperty.Register(nameof(ScrollViewportSize)); public float ScrollViewportSize { @@ -42,7 +42,7 @@ namespace QuestPDF.Previewer set => SetValue(ScrollViewportSizeProperty, value); } - public static readonly StyledProperty VerticalScrollbarVisibleProperty = AvaloniaProperty.Register(nameof(VerticalScrollbarVisible)); + public static readonly StyledProperty VerticalScrollbarVisibleProperty = AvaloniaProperty.Register(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