Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fixed pulltorefreshlistview refresh with mouse re: #647 #787

Merged
merged 1 commit into from
Jan 18, 2017
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
using Windows.Foundation;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;

Expand Down Expand Up @@ -85,7 +86,7 @@ public class PullToRefreshListView : ListView
/// IsPullToRefreshWithMouseEnabled Dependency Property
/// </summary>
public static readonly DependencyProperty IsPullToRefreshWithMouseEnabledProperty =
DependencyProperty.Register(nameof(IsPullToRefreshWithMouseEnabled), typeof(bool), typeof(PullToRefreshListView), new PropertyMetadata(true));
DependencyProperty.Register(nameof(IsPullToRefreshWithMouseEnabled), typeof(bool), typeof(PullToRefreshListView), new PropertyMetadata(false));

private const string PartRoot = "Root";
private const string PartScroller = "ScrollViewer";
Expand All @@ -104,6 +105,7 @@ public class PullToRefreshListView : ListView
private ItemsPresenter _scrollerContent;
private TextBlock _defaultIndicatorContent;
private ContentPresenter _pullAndReleaseIndicatorContent;
private ScrollBar _scrollerVerticalScrollBar;
private double _lastOffset = 0.0;
private double _pullDistance = 0.0;
private DateTime _lastRefreshActivation = default(DateTime);
Expand Down Expand Up @@ -151,6 +153,7 @@ protected override void OnApplyTemplate()
{
if (_scroller != null)
{
_scroller.Loaded -= Scroller_Loaded;
_scroller.DirectManipulationCompleted -= Scroller_DirectManipulationCompleted;
_scroller.DirectManipulationStarted -= Scroller_DirectManipulationStarted;
}
Expand All @@ -160,6 +163,12 @@ protected override void OnApplyTemplate()
_refreshIndicatorBorder.SizeChanged -= RefreshIndicatorBorder_SizeChanged;
}

if (_root != null)
{
_root.ManipulationStarted -= Scroller_ManipulationStarted;
_root.ManipulationCompleted -= Scroller_ManipulationCompleted;
}

_root = GetTemplateChild(PartRoot) as Border;
_scroller = GetTemplateChild(PartScroller) as ScrollViewer;
_scrollerContent = GetTemplateChild(PartScrollerContent) as ItemsPresenter;
Expand All @@ -175,10 +184,14 @@ protected override void OnApplyTemplate()
_refreshIndicatorTransform != null &&
(_defaultIndicatorContent != null || _pullAndReleaseIndicatorContent != null))
{
_root.ManipulationMode = ManipulationModes.TranslateY;
_root.ManipulationDelta += Scroller_ManipulationDelta;
_root.ManipulationStarted += Scroller_ManipulationStarted;
_root.ManipulationCompleted += Scroller_ManipulationCompleted;
_scroller.Loaded += Scroller_Loaded;

if (IsPullToRefreshWithMouseEnabled)
{
_root.ManipulationMode = ManipulationModes.TranslateY;
_root.ManipulationStarted += Scroller_ManipulationStarted;
_root.ManipulationCompleted += Scroller_ManipulationCompleted;
}

_scroller.DirectManipulationCompleted += Scroller_DirectManipulationCompleted;
_scroller.DirectManipulationStarted += Scroller_DirectManipulationStarted;
Expand All @@ -201,29 +214,33 @@ protected override void OnApplyTemplate()
base.OnApplyTemplate();
}

private void Scroller_ManipulationCompleted(object sender, ManipulationCompletedRoutedEventArgs e)
{
if (!IsPullToRefreshWithMouseEnabled)
{
return;
}

OnManipulationCompleted();
}

private void Scroller_ManipulationStarted(object sender, ManipulationStartedRoutedEventArgs e)
{
// Other input are already managed by the scroll viewer
if (e.PointerDeviceType == Windows.Devices.Input.PointerDeviceType.Mouse
&& IsPullToRefreshWithMouseEnabled)
&& IsPullToRefreshWithMouseEnabled
&& _scroller.VerticalOffset < 1)
{
_root.ManipulationDelta += Scroller_ManipulationDelta;
DisplayPullToRefreshContent();
CompositionTarget.Rendering -= CompositionTarget_Rendering;
CompositionTarget.Rendering += CompositionTarget_Rendering;
_isManipulatingWithMouse = true;
}
}

private void Scroller_ManipulationCompleted(object sender, ManipulationCompletedRoutedEventArgs e)
{
_root.ManipulationDelta -= Scroller_ManipulationDelta;

if (!IsPullToRefreshWithMouseEnabled)
{
return;
}

OnManipulationCompleted();
}

private void Scroller_ManipulationDelta(object sender, Windows.UI.Xaml.Input.ManipulationDeltaRoutedEventArgs e)
{
if (!IsPullToRefreshWithMouseEnabled || _contentTransform == null)
Expand Down Expand Up @@ -458,6 +475,26 @@ private void CompositionTarget_Rendering(object sender, object e)
PullProgressChanged?.Invoke(this, new RefreshProgressEventArgs { PullProgress = pullProgress });
}

private void Scroller_Loaded(object sender, RoutedEventArgs e)
{
_scrollerVerticalScrollBar = _scroller.FindDescendantByName("VerticalScrollBar") as ScrollBar;
_scrollerVerticalScrollBar.PointerEntered += ScrollerVerticalScrollBar_PointerEntered;
_scrollerVerticalScrollBar.PointerExited += ScrollerVerticalScrollBar_PointerExited;
}

private void ScrollerVerticalScrollBar_PointerExited(object sender, PointerRoutedEventArgs e)
{
if (IsPullToRefreshWithMouseEnabled)
{
_root.ManipulationMode = ManipulationModes.TranslateY;
}
}

private void ScrollerVerticalScrollBar_PointerEntered(object sender, PointerRoutedEventArgs e)
{
_root.ManipulationMode = ManipulationModes.System;
}

/// <summary>
/// Gets or sets the Overscroll Limit. Value between 0 and 1 where 1 is the height of the control. Default is 0.3
/// </summary>
Expand Down Expand Up @@ -589,8 +626,31 @@ public object ReleaseToRefreshContent
/// </summary>
public bool IsPullToRefreshWithMouseEnabled
{
get { return (bool)GetValue(IsPullToRefreshWithMouseEnabledProperty); }
set { SetValue(IsPullToRefreshWithMouseEnabledProperty, value); }
get
{
return (bool)GetValue(IsPullToRefreshWithMouseEnabledProperty);
}

set
{
if (_root != null)
{
if (value)
{
_root.ManipulationMode = ManipulationModes.TranslateY;
_root.ManipulationStarted += Scroller_ManipulationStarted;
_root.ManipulationCompleted += Scroller_ManipulationCompleted;
}
else
{
_root.ManipulationMode = ManipulationModes.System;
_root.ManipulationStarted -= Scroller_ManipulationStarted;
_root.ManipulationCompleted -= Scroller_ManipulationCompleted;
}
}

SetValue(IsPullToRefreshWithMouseEnabledProperty, value);
}
}
}
}