summaryrefslogtreecommitdiff
path: root/Xamarin.Forms.Platform.Android/Renderers/CarouselViewExtensions.cs
blob: 6349b2b712ed7ab8e8430eb2eba022be7eb21e16 (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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
using System;
using System.Diagnostics;
using System.Linq;
using Android.Content;
using Android.Graphics;

namespace Xamarin.Forms.Platform.Android
{
	internal static class CarouselViewExtensions
	{
		internal static int Area(this System.Drawing.Rectangle rectangle)
		{
			return rectangle.Width * rectangle.Height;
		}

		internal static IntVector BoundTranslation(this System.Drawing.Rectangle viewport, IntVector delta, System.Drawing.Rectangle bound)
		{
			// TODO: generalize the math
			Debug.Assert(delta.X == 0 || delta.Y == 0);

			IntVector start = viewport.LeadingCorner(delta);
			IntVector end = start + delta;
			IntVector clampedEnd = end.Clamp(bound);
			IntVector clampedDelta = clampedEnd - start;
			return clampedDelta;
		}

		internal static IntVector Center(this System.Drawing.Rectangle rectangle)
		{
			return (IntVector)rectangle.Location + (IntVector)rectangle.Size / 2;
		}

		internal static IntVector Clamp(this IntVector position, System.Drawing.Rectangle bound)
		{
			return new IntVector(position.X.Clamp(bound.Left, bound.Right), position.Y.Clamp(bound.Top, bound.Bottom));
		}

		internal static IntVector LeadingCorner(this System.Drawing.Rectangle rectangle, IntVector delta)
		{
			return new IntVector(delta.X < 0 ? rectangle.Left : rectangle.Right, delta.Y < 0 ? rectangle.Top : rectangle.Bottom);
		}

		internal static bool LexicographicallyLess(this System.Drawing.Point source, System.Drawing.Point target)
		{
			if (source.X < target.X)
				return true;

			if (source.X > target.X)
				return false;

			return source.Y < target.Y;
		}

		internal static Rect ToAndroidRectangle(this System.Drawing.Rectangle rectangle)
		{
			return new Rect(rectangle.Left, right: rectangle.Right, top: rectangle.Top, bottom: rectangle.Bottom);
		}

		internal static Rectangle ToFormsRectangle(this System.Drawing.Rectangle rectangle, Context context)
		{
			return new Rectangle(context.FromPixels(rectangle.Left), context.FromPixels(rectangle.Top), context.FromPixels(rectangle.Width), context.FromPixels(rectangle.Height));
		}

		internal static int[] ToRange(this Tuple<int, int> startAndCount)
		{
			return Enumerable.Range(startAndCount.Item1, startAndCount.Item2).ToArray();
		}
	}
}