summaryrefslogtreecommitdiff
path: root/Xamarin.Forms.Core.UITests.Shared/Utilities/Drag.cs
blob: 59f54819196111e4c3e866d52cab7a9b705f01c5 (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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
using Xamarin.UITest.Queries;

namespace Xamarin.Forms.Core.UITests
{
	internal class Drag
	{
		internal enum DragLength
		{
			Long,
			Medium,
			Short
		}

		internal enum Direction
		{
			TopToBottom,
			BottomToTop,
			RightToLeft,
			LeftToRight
		}

		AppRect dragBounds;
		float xStart;
		float yStart;
		float xEnd;
		float yEnd;
		Direction dragDirection;
		Direction oppositeDirection;
		DragLength dragLength;

		public Drag (AppRect dragbounds, float xStart, float yStart, float xEnd, float yEnd, Direction direction)
		{
			dragBounds = dragbounds;
			this.xStart = xStart;
			this.yStart = yStart;
			this.xEnd = xEnd;
			this.yEnd = yEnd;
			dragDirection = direction;
			oppositeDirection = GetOppositeDirection (direction);
		}

		public Drag (AppRect dragBounds, Direction direction, DragLength dragLength)
		{
			this.dragBounds = dragBounds;
			dragDirection = direction;
			this.dragLength = dragLength;
			SetDragForBounds (dragDirection, dragLength);
		}

		void SetDragForBounds (Direction direction, DragLength dragLength)
		{
			// percentage of bounds to scroll centered in element
			float scrollPercentage;

			switch (dragLength) {
				case DragLength.Long:
					scrollPercentage = 0.8f;
					break;
				case DragLength.Medium:
					scrollPercentage = 0.5f;
					break;
				default:
					scrollPercentage = 0.2f;
					break;
			}

			if (direction == Direction.LeftToRight) {
				yStart = dragBounds.CenterY;
				yEnd = dragBounds.CenterY;
				float xDisplacement = (dragBounds.CenterX + (dragBounds.Width / 2)) - dragBounds.X;
				float insetForScroll = (xDisplacement - (xDisplacement * scrollPercentage)) / 2;
				xStart = dragBounds.X + insetForScroll;
				xEnd = (dragBounds.CenterX + (dragBounds.Width / 2)) - insetForScroll;
			} else if (direction == Direction.RightToLeft) {
				yStart = dragBounds.CenterY;
				yEnd = dragBounds.CenterY;
				float xDisplacement = (dragBounds.CenterX + (dragBounds.Width / 2)) - dragBounds.X;
				float insetForScroll = (xDisplacement - (xDisplacement * scrollPercentage)) / 2;
				xStart = (dragBounds.CenterX + (dragBounds.Width / 2)) - insetForScroll;
				xEnd = dragBounds.X + insetForScroll;
			} else if (direction == Direction.TopToBottom) {
				xStart = dragBounds.CenterX;
				xEnd = dragBounds.CenterX;
				float yDisplacement = (dragBounds.CenterY + (dragBounds.Height / 2)) - dragBounds.Y;
				float insetForScroll = (yDisplacement - (yDisplacement * scrollPercentage)) / 2;
				yStart = dragBounds.Y + insetForScroll;
				yEnd = (dragBounds.CenterY + (dragBounds.Height / 2)) - insetForScroll;
			} else if (direction == Direction.BottomToTop) {
				xStart = dragBounds.CenterX;
				xEnd = dragBounds.CenterX;
				float yDisplacement = (dragBounds.CenterY + (dragBounds.Height / 2)) - dragBounds.Y;
				float insetForScroll = (yDisplacement - (yDisplacement * scrollPercentage)) / 2;
				yStart = (dragBounds.CenterY + (dragBounds.Height / 2)) - insetForScroll;
				yEnd = dragBounds.Y + insetForScroll;

			}
		}

		Direction GetOppositeDirection (Direction direction)
		{
			switch (direction) {
				case Direction.TopToBottom:
					return Direction.BottomToTop;
				case Direction.BottomToTop:
					return Direction.TopToBottom;
				case Direction.RightToLeft:
					return Direction.LeftToRight;
				case Direction.LeftToRight:
					return Direction.RightToLeft;
				default:
					return Direction.TopToBottom;
			}
		}

		public AppRect DragBounds
		{
			get { return dragBounds; }
		}

		public float XStart
		{
			get { return xStart; }
		}

		public float YStart
		{
			get { return yStart; }
		}

		public float XEnd
		{
			get { return xEnd; }
		}

		public float YEnd
		{
			get { return yEnd; }
		}

		public Direction DragDirection
		{
			get { return dragDirection; }
			set
			{
				if (dragDirection == value)
					return;

				dragDirection = value;
				oppositeDirection = GetOppositeDirection (dragDirection);
				OnDragDirectionChanged ();
			}
		}

		void OnDragDirectionChanged ()
		{
			SetDragForBounds (dragDirection, dragLength);
		}

		public Direction OppositeDirection
		{
			get { return oppositeDirection; }
			private set
			{
				if (oppositeDirection == value)
					return;
				
				oppositeDirection = value;
			}
		}
	}
}