summaryrefslogtreecommitdiff
path: root/Xamarin.Forms.Core/RelativeLayout.cs
blob: 85c64d0c85df2640c3dd0f4ac45b3fdf7c44e0bf (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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
´╗┐using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Linq.Expressions;
using Xamarin.Forms.Internals;

namespace Xamarin.Forms
{
	public class RelativeLayout : Layout<View>
	{
		public static readonly BindableProperty XConstraintProperty = BindableProperty.CreateAttached("XConstraint", typeof(Constraint), typeof(RelativeLayout), null, propertyChanged: ConstraintChanged);

		public static readonly BindableProperty YConstraintProperty = BindableProperty.CreateAttached("YConstraint", typeof(Constraint), typeof(RelativeLayout), null, propertyChanged: ConstraintChanged);

		public static readonly BindableProperty WidthConstraintProperty = BindableProperty.CreateAttached("WidthConstraint", typeof(Constraint), typeof(RelativeLayout), null, propertyChanged: ConstraintChanged);

		public static readonly BindableProperty HeightConstraintProperty = BindableProperty.CreateAttached("HeightConstraint", typeof(Constraint), typeof(RelativeLayout), null, propertyChanged: ConstraintChanged);

		public static readonly BindableProperty BoundsConstraintProperty = BindableProperty.CreateAttached("BoundsConstraint", typeof(BoundsConstraint), typeof(RelativeLayout), null);

		readonly RelativeElementCollection _children;

		IEnumerable<View> _childrenInSolveOrder;

		public RelativeLayout()
		{
			VerticalOptions = HorizontalOptions = LayoutOptions.FillAndExpand;
			_children = new RelativeElementCollection(InternalChildren, this);
			_children.Parent = this;
		}

		public new IRelativeList<View> Children
		{
			get { return _children; }
		}

		IEnumerable<View> ChildrenInSolveOrder
		{
			get
			{
				if (_childrenInSolveOrder != null)
					return _childrenInSolveOrder;

				var result = new List<View>();
				var solveTable = new Dictionary<View, bool>();
				foreach (View child in Children.Cast<View>())
				{
					solveTable[child] = false;
				}

				List<View> unsolvedChildren = Children.Cast<View>().ToList();
				while (unsolvedChildren.Any())
				{
					List<View> copy = unsolvedChildren.ToList();
					var solvedChild = false;
					foreach (View child in copy)
					{
						if (CanSolveView(child, solveTable))
						{
							result.Add(child);
							solveTable[child] = true;
							unsolvedChildren.Remove(child);
							solvedChild = true;
						}
					}
					if (!solvedChild)
						throw new UnsolvableConstraintsException("Constraints as specified contain an unsolvable loop.");
				}

				_childrenInSolveOrder = result;
				return _childrenInSolveOrder;
			}
		}

		static void ConstraintChanged(BindableObject bindable, object oldValue, object newValue)
		{
			View view = bindable as View;

			(view?.Parent as RelativeLayout)?.UpdateBoundsConstraint(view);
		}

		void UpdateBoundsConstraint(View view)
		{
			if (GetBoundsConstraint(view) == null)
				return; // Bounds constraint hasn't been calculated yet, no need to update just yet

			CreateBoundsFromConstraints(view, GetXConstraint(view), GetYConstraint(view), GetWidthConstraint(view), GetHeightConstraint(view));

			_childrenInSolveOrder = null; // New constraints may have impact on solve order

			InvalidateLayout();
		}

		public static BoundsConstraint GetBoundsConstraint(BindableObject bindable)
		{
			return (BoundsConstraint)bindable.GetValue(BoundsConstraintProperty);
		}

		public static Constraint GetHeightConstraint(BindableObject bindable)
		{
			return (Constraint)bindable.GetValue(HeightConstraintProperty);
		}

		public static Constraint GetWidthConstraint(BindableObject bindable)
		{
			return (Constraint)bindable.GetValue(WidthConstraintProperty);
		}

		public static Constraint GetXConstraint(BindableObject bindable)
		{
			return (Constraint)bindable.GetValue(XConstraintProperty);
		}

		public static Constraint GetYConstraint(BindableObject bindable)
		{
			return (Constraint)bindable.GetValue(YConstraintProperty);
		}

		public static void SetBoundsConstraint(BindableObject bindable, BoundsConstraint value)
		{
			bindable.SetValue(BoundsConstraintProperty, value);
		}

		public static void SetHeightConstraint(BindableObject bindable, Constraint value)
		{
			bindable.SetValue(HeightConstraintProperty, value);
		}

		public static void SetWidthConstraint(BindableObject bindable, Constraint value)
		{
			bindable.SetValue(WidthConstraintProperty, value);
		}

		public static void SetXConstraint(BindableObject bindable, Constraint value)
		{
			bindable.SetValue(XConstraintProperty, value);
		}

		public static void SetYConstraint(BindableObject bindable, Constraint value)
		{
			bindable.SetValue(YConstraintProperty, value);
		}

		protected override void LayoutChildren(double x, double y, double width, double height)
		{
			foreach (View child in ChildrenInSolveOrder)
			{
				LayoutChildIntoBoundingRegion(child, SolveView(child));
			}
		}

		protected override void OnAdded(View view)
		{
			BoundsConstraint boundsConstraint = GetBoundsConstraint(view);
			if (boundsConstraint == null)
			{
				// user probably added the view through the strict Add method.
				CreateBoundsFromConstraints(view, GetXConstraint(view), GetYConstraint(view), GetWidthConstraint(view), GetHeightConstraint(view));
			}

			_childrenInSolveOrder = null;
			base.OnAdded(view);
		}

		protected override void OnRemoved(View view)
		{
			_childrenInSolveOrder = null;
			base.OnRemoved(view);
		}

		[Obsolete("Use OnMeasure")]
		protected override SizeRequest OnSizeRequest(double widthConstraint, double heightConstraint)
		{
			double mockWidth = double.IsPositiveInfinity(widthConstraint) ? ParentView.Width : widthConstraint;
			double mockHeight = double.IsPositiveInfinity(heightConstraint) ? ParentView.Height : heightConstraint;
			MockBounds(new Rectangle(0, 0, mockWidth, mockHeight));

			var boundsRectangle = new Rectangle();
			var set = false;
			foreach (View child in ChildrenInSolveOrder)
			{
				Rectangle bounds = SolveView(child);
				child.MockBounds(bounds);
				if (!set)
				{
					boundsRectangle = bounds;
					set = true;
				}
				else
				{
					boundsRectangle.Left = Math.Min(boundsRectangle.Left, bounds.Left);
					boundsRectangle.Top = Math.Min(boundsRectangle.Top, bounds.Top);
					boundsRectangle.Right = Math.Max(boundsRectangle.Right, bounds.Right);
					boundsRectangle.Bottom = Math.Max(boundsRectangle.Bottom, bounds.Bottom);
				}
			}

			foreach (View child in ChildrenInSolveOrder)
				child.UnmockBounds();

			UnmockBounds();

			return new SizeRequest(new Size(boundsRectangle.Right, boundsRectangle.Bottom));
		}

		bool CanSolveView(View view, Dictionary<View, bool> solveTable)
		{
			BoundsConstraint boundsConstraint = GetBoundsConstraint(view);
			var parents = new List<View>();
			if (boundsConstraint == null)
			{
				throw new Exception("BoundsConstraint should not be null at this point");
			}
			parents.AddRange(boundsConstraint.RelativeTo);
			// expressions probably referenced the base layout somewhere
			while (parents.Remove(this)) // because winphone does not have RemoveAll...
				;

			if (!parents.Any())
				return true;

			for (var i = 0; i < parents.Count; i++)
			{
				View p = parents[i];

				bool solvable;
				if (!solveTable.TryGetValue(p, out solvable))
				{
					throw new InvalidOperationException("Views that have relationships to or from them must be kept in the RelativeLayout.");
				}

				if (!solvable)
					return false;
			}

			return true;
		}

		void CreateBoundsFromConstraints(View view, Constraint xConstraint, Constraint yConstraint, Constraint widthConstraint, Constraint heightConstraint)
		{
			var parents = new List<View>();

			Func<double> x;
			if (xConstraint != null)
			{
				x = () => xConstraint.Compute(this);
				if (xConstraint.RelativeTo != null)
					parents.AddRange(xConstraint.RelativeTo);
			}
			else
				x = () => 0;

			Func<double> y;
			if (yConstraint != null)
			{
				y = () => yConstraint.Compute(this);
				if (yConstraint.RelativeTo != null)
					parents.AddRange(yConstraint.RelativeTo);
			}
			else
				y = () => 0;

			Func<double> width;
			if (widthConstraint != null)
			{
				width = () => widthConstraint.Compute(this);
				if (widthConstraint.RelativeTo != null)
					parents.AddRange(widthConstraint.RelativeTo);
			}
			else
				width = () => view.Measure(Width, Height, MeasureFlags.IncludeMargins).Request.Width;

			Func<double> height;
			if (heightConstraint != null)
			{
				height = () => heightConstraint.Compute(this);
				if (heightConstraint.RelativeTo != null)
					parents.AddRange(heightConstraint.RelativeTo);
			}
			else
				height = () => view.Measure(Width, Height, MeasureFlags.IncludeMargins).Request.Height;

			BoundsConstraint bounds = BoundsConstraint.FromExpression(() => new Rectangle(x(), y(), width(), height()), parents.Distinct().ToArray());
			SetBoundsConstraint(view, bounds);
		}

		static Rectangle SolveView(View view)
		{
			BoundsConstraint boundsConstraint = GetBoundsConstraint(view);

			if (boundsConstraint == null)
			{
				throw new Exception("BoundsConstraint should not be null at this point");
			}

			var result = boundsConstraint.Compute();

			return result;
		}

		public interface IRelativeList<T> : IList<T> where T : View
		{
			void Add(T view, Expression<Func<Rectangle>> bounds);

			void Add(T view, Expression<Func<double>> x = null, Expression<Func<double>> y = null, Expression<Func<double>> width = null, Expression<Func<double>> height = null);

			void Add(T view, Constraint xConstraint = null, Constraint yConstraint = null, Constraint widthConstraint = null, Constraint heightConstraint = null);
		}

		class RelativeElementCollection : ElementCollection<View>, IRelativeList<View>
		{
			public RelativeElementCollection(ObservableCollection<Element> inner, RelativeLayout parent) : base(inner)
			{
				Parent = parent;
			}

			internal RelativeLayout Parent { get; set; }

			public void Add(View view, Expression<Func<Rectangle>> bounds)
			{
				if (bounds == null)
					throw new ArgumentNullException(nameof(bounds));
				SetBoundsConstraint(view, BoundsConstraint.FromExpression(bounds));

				base.Add(view);
			}

			public void Add(View view, Expression<Func<double>> x = null, Expression<Func<double>> y = null, Expression<Func<double>> width = null, Expression<Func<double>> height = null)
			{
				Func<double> xCompiled = x != null ? x.Compile() : () => 0;
				Func<double> yCompiled = y != null ? y.Compile() : () => 0;
				Func<double> widthCompiled = width != null ? width.Compile() : () => view.Measure(Parent.Width, Parent.Height, MeasureFlags.IncludeMargins).Request.Width;
				Func<double> heightCompiled = height != null ? height.Compile() : () => view.Measure(Parent.Width, Parent.Height, MeasureFlags.IncludeMargins).Request.Height;

				var parents = new List<View>();
				parents.AddRange(ExpressionSearch.Default.FindObjects<View>(x));
				parents.AddRange(ExpressionSearch.Default.FindObjects<View>(y));
				parents.AddRange(ExpressionSearch.Default.FindObjects<View>(width));
				parents.AddRange(ExpressionSearch.Default.FindObjects<View>(height));

				BoundsConstraint bounds = BoundsConstraint.FromExpression(() => new Rectangle(xCompiled(), yCompiled(), widthCompiled(), heightCompiled()), parents.Distinct().ToArray());

				SetBoundsConstraint(view, bounds);

				base.Add(view);
			}

			public void Add(View view, Constraint xConstraint = null, Constraint yConstraint = null, Constraint widthConstraint = null, Constraint heightConstraint = null)
			{
				view.BatchBegin();

				RelativeLayout.SetXConstraint(view, xConstraint);
				RelativeLayout.SetYConstraint(view, yConstraint);
				RelativeLayout.SetWidthConstraint(view, widthConstraint);
				RelativeLayout.SetHeightConstraint(view, heightConstraint);

				view.BatchCommit();

				base.Add(view);
			}
		}
	}
}