summaryrefslogtreecommitdiff
path: root/Xamarin.Forms.Platform.MacOS/Extensions/NSScrollViewExtensions.cs
blob: f9104d4a7042d8f207d3fe89b3b6b6f69e0df22e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
´╗┐using System.Threading.Tasks;
using AppKit;
using PointF = CoreGraphics.CGPoint;

namespace Xamarin.Forms.Platform.MacOS
{
	internal static class NSScrollViewExtensions
	{
		public static Task ScrollToPositionAsync(this NSScrollView scrollView, PointF point, bool animate,
			double duration = 0.5)
		{
			if (!animate)
			{
				var nsView = scrollView.DocumentView as NSView;
				nsView?.ScrollPoint(point);
				return Task.FromResult(true);
			}

			TaskCompletionSource<bool> source = new TaskCompletionSource<bool>();

			NSAnimationContext.BeginGrouping();

			NSAnimationContext.CurrentContext.CompletionHandler += () => { source.TrySetResult(true); };

			NSAnimationContext.CurrentContext.Duration = duration;

			var animator = scrollView.ContentView.Animator as NSView;

			animator?.SetBoundsOrigin(point);

			NSAnimationContext.EndGrouping();

			return source.Task;
		}
	}
}