summaryrefslogtreecommitdiff
path: root/Xamarin.Forms.Core/AbsoluteLayout.cs
blob: 43924831ff8d2405b00888577cba8e8dadcee886 (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
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using Xamarin.Forms.Internals;

namespace Xamarin.Forms
{
	public class AbsoluteLayout : Layout<View>
	{
		public static readonly BindableProperty LayoutFlagsProperty = BindableProperty.CreateAttached("LayoutFlags", typeof(AbsoluteLayoutFlags), typeof(AbsoluteLayout), AbsoluteLayoutFlags.None);

		public static readonly BindableProperty LayoutBoundsProperty = BindableProperty.CreateAttached("LayoutBounds", typeof(Rectangle), typeof(AbsoluteLayout), new Rectangle(0, 0, AutoSize, AutoSize));

		readonly AbsoluteElementCollection _children;

		public AbsoluteLayout()
		{
			_children = new AbsoluteElementCollection(InternalChildren, this);
		}

		public static double AutoSize
		{
			get { return -1; }
		}

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

		[TypeConverter(typeof(BoundsTypeConverter))]
		public static Rectangle GetLayoutBounds(BindableObject bindable)
		{
			return (Rectangle)bindable.GetValue(LayoutBoundsProperty);
		}

		public static AbsoluteLayoutFlags GetLayoutFlags(BindableObject bindable)
		{
			return (AbsoluteLayoutFlags)bindable.GetValue(LayoutFlagsProperty);
		}

		public static void SetLayoutBounds(BindableObject bindable, Rectangle bounds)
		{
			bindable.SetValue(LayoutBoundsProperty, bounds);
		}

		public static void SetLayoutFlags(BindableObject bindable, AbsoluteLayoutFlags flags)
		{
			bindable.SetValue(LayoutFlagsProperty, flags);
		}

		protected override void LayoutChildren(double x, double y, double width, double height)
		{
			foreach (View child in LogicalChildrenInternal)
			{
				Rectangle rect = ComputeLayoutForRegion(child, new Size(width, height));
				rect.X += x;
				rect.Y += y;

				LayoutChildIntoBoundingRegion(child, rect);
			}
		}

		protected override void OnChildAdded(Element child)
		{
			base.OnChildAdded(child);
			child.PropertyChanged += ChildOnPropertyChanged;
		}

		protected override void OnChildRemoved(Element child)
		{
			child.PropertyChanged -= ChildOnPropertyChanged;
			base.OnChildRemoved(child);
		}

		[Obsolete("Use OnMeasure")]
		protected override SizeRequest OnSizeRequest(double widthConstraint, double heightConstraint)
		{
			var bestFitSize = new Size();
			var minimum = new Size();
			foreach (View child in LogicalChildrenInternal)
			{
				SizeRequest desiredSize = ComputeBoundingRegionDesiredSize(child);

				bestFitSize.Width = Math.Max(bestFitSize.Width, desiredSize.Request.Width);
				bestFitSize.Height = Math.Max(bestFitSize.Height, desiredSize.Request.Height);
				minimum.Width = Math.Max(minimum.Width, desiredSize.Minimum.Width);
				minimum.Height = Math.Max(minimum.Height, desiredSize.Minimum.Height);
			}

			return new SizeRequest(bestFitSize, minimum);
		}

		internal override void ComputeConstraintForView(View view)
		{
			AbsoluteLayoutFlags layoutFlags = GetLayoutFlags(view);

			if ((layoutFlags & AbsoluteLayoutFlags.SizeProportional) == AbsoluteLayoutFlags.SizeProportional)
			{
				view.ComputedConstraint = Constraint;
				return;
			}

			var result = LayoutConstraint.None;
			Rectangle layoutBounds = GetLayoutBounds(view);
			if ((layoutFlags & AbsoluteLayoutFlags.HeightProportional) != 0)
			{
				bool widthLocked = layoutBounds.Width != AutoSize;
				result = Constraint & LayoutConstraint.VerticallyFixed;
				if (widthLocked)
					result |= LayoutConstraint.HorizontallyFixed;
			}
			else if ((layoutFlags & AbsoluteLayoutFlags.WidthProportional) != 0)
			{
				bool heightLocked = layoutBounds.Height != AutoSize;
				result = Constraint & LayoutConstraint.HorizontallyFixed;
				if (heightLocked)
					result |= LayoutConstraint.VerticallyFixed;
			}
			else
			{
				if (layoutBounds.Width != AutoSize)
					result |= LayoutConstraint.HorizontallyFixed;
				if (layoutBounds.Height != AutoSize)
					result |= LayoutConstraint.VerticallyFixed;
			}

			view.ComputedConstraint = result;
		}

		void ChildOnPropertyChanged(object sender, PropertyChangedEventArgs e)
		{
			if (e.PropertyName == LayoutFlagsProperty.PropertyName || e.PropertyName == LayoutBoundsProperty.PropertyName)
			{
				InvalidateMeasureInternal(InvalidationTrigger.MeasureChanged);
				UpdateChildrenLayout();
			}
		}

		static SizeRequest ComputeBoundingRegionDesiredSize(View view)
		{
			var width = 0.0;
			var height = 0.0;

			var sizeRequest = new Lazy<SizeRequest>(() => view.Measure(double.PositiveInfinity, double.PositiveInfinity, MeasureFlags.IncludeMargins));

			Rectangle bounds = GetLayoutBounds(view);
			AbsoluteLayoutFlags absFlags = GetLayoutFlags(view);
			bool widthIsProportional = (absFlags & AbsoluteLayoutFlags.WidthProportional) != 0;
			bool heightIsProportional = (absFlags & AbsoluteLayoutFlags.HeightProportional) != 0;
			bool xIsProportional = (absFlags & AbsoluteLayoutFlags.XProportional) != 0;
			bool yIsProportional = (absFlags & AbsoluteLayoutFlags.YProportional) != 0;

			// add in required x values
			if (!xIsProportional)
			{
				width += bounds.X;
			}

			if (!yIsProportional)
			{
				height += bounds.Y;
			}

			double minWidth = width;
			double minHeight = height;

			if (!widthIsProportional && bounds.Width != AutoSize)
			{
				// fixed size
				width += bounds.Width;
				minWidth += bounds.Width;
			}
			else if (!widthIsProportional)
			{
				// auto size
				width += sizeRequest.Value.Request.Width;
				minWidth += sizeRequest.Value.Minimum.Width;
			}
			else
			{
				// proportional size
				width += sizeRequest.Value.Request.Width / Math.Max(0.25, bounds.Width);
				//minWidth += 0;
			}

			if (!heightIsProportional && bounds.Height != AutoSize)
			{
				// fixed size
				height += bounds.Height;
				minHeight += bounds.Height;
			}
			else if (!heightIsProportional)
			{
				// auto size
				height += sizeRequest.Value.Request.Height;
				minHeight += sizeRequest.Value.Minimum.Height;
			}
			else
			{
				// proportional size
				height += sizeRequest.Value.Request.Height / Math.Max(0.25, bounds.Height);
				//minHeight += 0;
			}

			return new SizeRequest(new Size(width, height), new Size(minWidth, minHeight));
		}

		static Rectangle ComputeLayoutForRegion(View view, Size region)
		{
			var result = new Rectangle();

			SizeRequest sizeRequest;
			Rectangle bounds = GetLayoutBounds(view);
			AbsoluteLayoutFlags absFlags = GetLayoutFlags(view);
			bool widthIsProportional = (absFlags & AbsoluteLayoutFlags.WidthProportional) != 0;
			bool heightIsProportional = (absFlags & AbsoluteLayoutFlags.HeightProportional) != 0;
			bool xIsProportional = (absFlags & AbsoluteLayoutFlags.XProportional) != 0;
			bool yIsProportional = (absFlags & AbsoluteLayoutFlags.YProportional) != 0;

			if (widthIsProportional)
			{
				result.Width = Math.Round(region.Width * bounds.Width);
			}
			else if (bounds.Width != AutoSize)
			{
				result.Width = bounds.Width;
			}

			if (heightIsProportional)
			{
				result.Height = Math.Round(region.Height * bounds.Height);
			}
			else if (bounds.Height != AutoSize)
			{
				result.Height = bounds.Height;
			}

			if (!widthIsProportional && bounds.Width == AutoSize)
			{
				if (!heightIsProportional && bounds.Width == AutoSize)
				{
					// Width and Height are auto
					sizeRequest = view.Measure(region.Width, region.Height, MeasureFlags.IncludeMargins);
					result.Width = sizeRequest.Request.Width;
					result.Height = sizeRequest.Request.Height;
				}
				else
				{
					// Only width is auto
					sizeRequest = view.Measure(region.Width, result.Height, MeasureFlags.IncludeMargins);
					result.Width = sizeRequest.Request.Width;
				}
			}
			else if (!heightIsProportional && bounds.Height == AutoSize)
			{
				// Only height is auto
				sizeRequest = view.Measure(result.Width, region.Height, MeasureFlags.IncludeMargins);
				result.Height = sizeRequest.Request.Height;
			}

			if (xIsProportional)
			{
				result.X = Math.Round((region.Width - result.Width) * bounds.X);
			}
			else
			{
				result.X = bounds.X;
			}

			if (yIsProportional)
			{
				result.Y = Math.Round((region.Height - result.Height) * bounds.Y);
			}
			else
			{
				result.Y = bounds.Y;
			}

			return result;
		}

		public interface IAbsoluteList<T> : IList<T> where T : View
		{
			void Add(View view, Rectangle bounds, AbsoluteLayoutFlags flags = AbsoluteLayoutFlags.None);

			void Add(View view, Point position);
		}

		class AbsoluteElementCollection : ElementCollection<View>, IAbsoluteList<View>
		{
			public AbsoluteElementCollection(ObservableCollection<Element> inner, AbsoluteLayout parent) : base(inner)
			{
				Parent = parent;
			}

			internal AbsoluteLayout Parent { get; set; }

			public void Add(View view, Rectangle bounds, AbsoluteLayoutFlags flags = AbsoluteLayoutFlags.None)
			{
				SetLayoutBounds(view, bounds);
				SetLayoutFlags(view, flags);
				Add(view);
			}

			public void Add(View view, Point position)
			{
				SetLayoutBounds(view, new Rectangle(position.X, position.Y, AutoSize, AutoSize));
				Add(view);
			}
		}
	}
}