Skip to content

Commit

Permalink
Merge branch 'develop'
Browse files Browse the repository at this point in the history
  • Loading branch information
bezzad committed Oct 25, 2020
2 parents 2b5551d + 83c46f0 commit bc2b4dd
Show file tree
Hide file tree
Showing 3 changed files with 148 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/PdfiumViewer/PdfiumViewer.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<PropertyGroup>
<TargetFramework>netcoreapp3.1</TargetFramework>
<UseWPF>true</UseWPF>
<Version>1.0.2</Version>
<Version>1.0.3</Version>
<Authors>Behzad Khosravifar</Authors>
<Description>PDF viewer based on Google's PDFium port to .Net Core.</Description>
<Copyright>Copyright © 2019-2020 Behzad Khosravifar</Copyright>
Expand Down
141 changes: 141 additions & 0 deletions src/PdfiumViewer/ScrollPanel.Paning.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
using System;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Input;

namespace PdfiumViewer
{
public partial class ScrollPanel
{
private const double DefaultFriction = 0.94;
private Point ScrollStartPoint { get; set; }
private Point ScrollStartOffset { get; set; }
private Point PreviousPoint { get; set; }
private Vector Velocity { get; set; }
private Point _scrollTarget;
private int InertiaHandlerInterval { get; set; } = 20; // milliseconds
private int InertiaMaxAnimationTime { get; set; } = 3000; // milliseconds


#region Friction

/// <summary>
/// Friction Attached Dependency Property
/// </summary>
public static readonly DependencyProperty FrictionProperty =
DependencyProperty.RegisterAttached(nameof(Friction), typeof(double), typeof(ScrollPanel), new FrameworkPropertyMetadata(DefaultFriction));

public double Friction
{
get => (double)GetValue(FrictionProperty);
set => SetValue(FrictionProperty, value);
}

#endregion

#region IsMouseDown

/// <summary>
/// IsMouseDown Attached Dependency Property
/// </summary>
private static readonly DependencyProperty IsMouseDownProperty = DependencyProperty.RegisterAttached(nameof(IsMouseDown), typeof(bool), typeof(ScrollPanel), new FrameworkPropertyMetadata(false));

private bool IsMouseDown
{
get => (bool)GetValue(IsMouseDownProperty);
set => SetValue(IsMouseDownProperty, value);
}

#endregion

#region Mouse Events

protected override void OnPreviewMouseDown(MouseButtonEventArgs e)
{
base.OnPreviewMouseDown(e);

if (IsMouseOver)
{
Cursor = Cursors.ScrollAll;
// Save starting point, used later when
// determining how much to scroll.
ScrollStartPoint = e.GetPosition(this);
ScrollStartOffset = new Point(HorizontalOffset, VerticalOffset);
IsMouseDown = true;
}
}

protected override void OnPreviewMouseMove(MouseEventArgs e)
{
base.OnPreviewMouseMove(e);

if (IsMouseDown)
{
var currentPoint = e.GetPosition(this);
// Determine the new amount to scroll.
_scrollTarget = GetScrollTarget(currentPoint);
InertiaHandleMouseMove();
// Scroll to the new position.
ScrollToHorizontalOffset(_scrollTarget.X);
ScrollToVerticalOffset(_scrollTarget.Y);
}
}

protected override void OnPreviewMouseUp(MouseButtonEventArgs e)
{
base.OnPreviewMouseUp(e);

Cursor = Cursors.Arrow;
IsMouseDown = false;
var currentPoint = e.GetPosition(this);
// Determine the new amount to scroll.
_scrollTarget = GetScrollTarget(currentPoint);
InertiaHandleMouseUp();
}

private Point GetScrollTarget(Point currentPoint)
{
var delta = new Point(ScrollStartPoint.X - currentPoint.X, ScrollStartPoint.Y - currentPoint.Y);
var scrollTarget = new Point(ScrollStartOffset.X + delta.X, ScrollStartOffset.Y + delta.Y);

if (scrollTarget.Y < 0)
{
scrollTarget.Y = 0;
}

if (scrollTarget.Y > ScrollableHeight)
{
scrollTarget.Y = ScrollableHeight;
}

return scrollTarget;
}



private void InertiaHandleMouseMove()
{
var currentPoint = Mouse.GetPosition(this);
Velocity = PreviousPoint - currentPoint;
PreviousPoint = currentPoint;
}

private async void InertiaHandleMouseUp()
{
for (var i = 0; i < InertiaMaxAnimationTime / InertiaHandlerInterval; i++)
{
if (IsMouseDown || Velocity.Length <= 1 || Environment.TickCount64 - MouseWheelUpdateTime < InertiaHandlerInterval)
break;

ScrollToHorizontalOffset(_scrollTarget.X);
ScrollToVerticalOffset(_scrollTarget.Y);
_scrollTarget.X += Velocity.X;
_scrollTarget.Y += Velocity.Y;
Velocity *= Friction;
await Task.Delay(InertiaHandlerInterval);
}
}

#endregion
}
}
7 changes: 6 additions & 1 deletion src/PdfiumViewer/ScrollPanel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,8 @@ public ScrollPanel()
protected Image[] Frames { get; set; }
protected Size CurrentPageSize { get; set; }
protected int ScrollWidth { get; set; }
protected int MouseWheelDelta { get; set; }
protected long MouseWheelUpdateTime { get; set; }

public event PropertyChangedEventHandler PropertyChanged;
public PdfDocument Document { get; set; }
Expand Down Expand Up @@ -250,6 +252,9 @@ protected override void OnPreviewMouseWheel(MouseWheelEventArgs e)
{
base.OnPreviewMouseWheel(e);

MouseWheelUpdateTime = Environment.TickCount64;
MouseWheelDelta = e.Delta;

if (IsDocumentLoaded)
{
if (MouseWheelMode == MouseWheelMode.Zoom)
Expand Down Expand Up @@ -350,7 +355,7 @@ protected override void OnScrollChanged(ScrollChangedEventArgs e)
{
base.OnScrollChanged(e);
if (IsDocumentLoaded &&
PagesDisplayMode == PdfViewerPagesDisplayMode.ContinuousMode &&
PagesDisplayMode == PdfViewerPagesDisplayMode.ContinuousMode &&
Frames != null)
{
var startOffset = e.VerticalOffset;
Expand Down

0 comments on commit bc2b4dd

Please sign in to comment.