summaryrefslogtreecommitdiff
path: root/Xamarin.Forms.Core/StackLayout.cs
blob: 4dc7bfa379b7100015b7275bc1d6009d4ef81c0e (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
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
using System;

namespace Xamarin.Forms
{
	public class StackLayout : Layout<View>
	{
		public static readonly BindableProperty OrientationProperty = BindableProperty.Create("Orientation", typeof(StackOrientation), typeof(StackLayout), StackOrientation.Vertical,
			propertyChanged: (bindable, oldvalue, newvalue) => ((StackLayout)bindable).InvalidateLayout());

		public static readonly BindableProperty SpacingProperty = BindableProperty.Create("Spacing", typeof(double), typeof(StackLayout), 6d,
			propertyChanged: (bindable, oldvalue, newvalue) => ((StackLayout)bindable).InvalidateLayout());

		LayoutInformation _layoutInformation = new LayoutInformation();

		public StackOrientation Orientation
		{
			get { return (StackOrientation)GetValue(OrientationProperty); }
			set { SetValue(OrientationProperty, value); }
		}

		public double Spacing
		{
			get { return (double)GetValue(SpacingProperty); }
			set { SetValue(SpacingProperty, value); }
		}

		protected override void LayoutChildren(double x, double y, double width, double height)
		{
			if (!HasVisibileChildren())
			{
				return;
			}

			if (width == _layoutInformation.Constraint.Width && height == _layoutInformation.Constraint.Height)
			{
				StackOrientation orientation = Orientation;

				AlignOffAxis(_layoutInformation, orientation, width, height);
				ProcessExpanders(_layoutInformation, orientation, x, y, width, height);
			}
			else
			{
				CalculateLayout(_layoutInformation, x, y, width, height, true);
			}

			LayoutInformation layoutInformationCopy = _layoutInformation;

			for (var i = 0; i < LogicalChildren.Count; i++)
			{
				var child = (View)LogicalChildren[i];
				if (child.IsVisible)
					LayoutChildIntoBoundingRegion(child, layoutInformationCopy.Plots[i], layoutInformationCopy.Requests[i]);
			}
		}

		[Obsolete("Use OnMeasure")]
		protected override SizeRequest OnSizeRequest(double widthConstraint, double heightConstraint)
		{
			if (!HasVisibileChildren())
			{
				return new SizeRequest();
			}

			// calculate with padding inset for X,Y so we can hopefully re-use this in the layout pass
			Thickness padding = Padding;
			CalculateLayout(_layoutInformation, padding.Left, padding.Top, widthConstraint, heightConstraint, false);
			var result = new SizeRequest(_layoutInformation.Bounds.Size, _layoutInformation.MinimumSize);
			return result;
		}

		internal override void ComputeConstraintForView(View view)
		{
			ComputeConstraintForView(view, false);
		}

		internal override void InvalidateMeasure(InvalidationTrigger trigger)
		{
			_layoutInformation = new LayoutInformation();
			base.InvalidateMeasure(trigger);
		}

		void AlignOffAxis(LayoutInformation layout, StackOrientation orientation, double widthConstraint, double heightConstraint)
		{
			for (var i = 0; i < layout.Plots.Length; i++)
			{
				if (!((View)LogicalChildren[i]).IsVisible)
					continue;
				if (orientation == StackOrientation.Vertical)
				{
					layout.Plots[i].Width = widthConstraint;
				}
				else
				{
					layout.Plots[i].Height = heightConstraint;
				}
			}
		}

		void CalculateLayout(LayoutInformation layout, double x, double y, double widthConstraint, double heightConstraint, bool processExpanders)
		{
			layout.Constraint = new Size(widthConstraint, heightConstraint);
			layout.Expanders = 0;
			layout.CompressionSpace = 0;
			layout.Plots = new Rectangle[Children.Count];
			layout.Requests = new SizeRequest[Children.Count];

			StackOrientation orientation = Orientation;

			CalculateNaiveLayout(layout, orientation, x, y, widthConstraint, heightConstraint);
			CompressNaiveLayout(layout, orientation, widthConstraint, heightConstraint);

			if (processExpanders)
			{
				AlignOffAxis(layout, orientation, widthConstraint, heightConstraint);
				ProcessExpanders(layout, orientation, x, y, widthConstraint, heightConstraint);
			}
		}

		void CalculateNaiveLayout(LayoutInformation layout, StackOrientation orientation, double x, double y, double widthConstraint, double heightConstraint)
		{
			layout.CompressionSpace = 0;

			double xOffset = x;
			double yOffset = y;
			double boundsWidth = 0;
			double boundsHeight = 0;
			double minimumWidth = 0;
			double minimumHeight = 0;
			double spacing = Spacing;
			if (orientation == StackOrientation.Vertical)
			{
				View expander = null;
				for (var i = 0; i < LogicalChildren.Count; i++)
				{
					var child = (View)LogicalChildren[i];
					if (!child.IsVisible)
						continue;

					if (child.VerticalOptions.Expands)
					{
						layout.Expanders++;
						if (expander != null)
						{
							// we have multiple expanders, make sure previous expanders are reset to not be fixed because they no logner are
							ComputeConstraintForView(child, false);
						}
						expander = child;
					}
					SizeRequest request = child.Measure(widthConstraint, double.PositiveInfinity, MeasureFlags.IncludeMargins);

					var bounds = new Rectangle(x, yOffset, request.Request.Width, request.Request.Height);
					layout.Plots[i] = bounds;
					layout.Requests[i] = request;
					layout.CompressionSpace += Math.Max(0, request.Request.Height - request.Minimum.Height);
					yOffset = bounds.Bottom + spacing;

					boundsWidth = Math.Max(boundsWidth, request.Request.Width);
					boundsHeight = bounds.Bottom - y;
					minimumHeight += request.Minimum.Height + spacing;
					minimumWidth = Math.Max(minimumWidth, request.Minimum.Width);
				}
				minimumHeight -= spacing;
				if (expander != null)
					ComputeConstraintForView(expander, layout.Expanders == 1); // warning : slightly obtuse, but we either need to setup the expander or clear the last one
			}
			else
			{
				View expander = null;
				for (var i = 0; i < LogicalChildren.Count; i++)
				{
					var child = (View)LogicalChildren[i];
					if (!child.IsVisible)
						continue;

					if (child.HorizontalOptions.Expands)
					{
						layout.Expanders++;
						if (expander != null)
						{
							ComputeConstraintForView(child, false);
						}
						expander = child;
					}
					SizeRequest request = child.Measure(double.PositiveInfinity, heightConstraint, MeasureFlags.IncludeMargins);

					var bounds = new Rectangle(xOffset, y, request.Request.Width, request.Request.Height);
					layout.Plots[i] = bounds;
					layout.Requests[i] = request;
					layout.CompressionSpace += Math.Max(0, request.Request.Width - request.Minimum.Width);
					xOffset = bounds.Right + spacing;

					boundsWidth = bounds.Right - x;
					boundsHeight = Math.Max(boundsHeight, request.Request.Height);
					minimumWidth += request.Minimum.Width + spacing;
					minimumHeight = Math.Max(minimumHeight, request.Minimum.Height);
				}
				minimumWidth -= spacing;
				if (expander != null)
					ComputeConstraintForView(expander, layout.Expanders == 1);
			}

			layout.Bounds = new Rectangle(x, y, boundsWidth, boundsHeight);
			layout.MinimumSize = new Size(minimumWidth, minimumHeight);
		}

		void CompressHorizontalLayout(LayoutInformation layout, double widthConstraint, double heightConstraint)
		{
			double xOffset = 0;

			if (widthConstraint >= layout.Bounds.Width)
			{
				// no need to compress
				return;
			}

			double requiredCompression = layout.Bounds.Width - widthConstraint;
			double compressionSpace = layout.CompressionSpace;
			double compressionPressure = (requiredCompression / layout.CompressionSpace).Clamp(0, 1);

			for (var i = 0; i < layout.Plots.Length; i++)
			{
				var child = (View)LogicalChildren[i];
				if (!child.IsVisible)
					continue;

				Size minimum = layout.Requests[i].Minimum;

				layout.Plots[i].X -= xOffset;

				Rectangle plot = layout.Plots[i];
				double availableSpace = plot.Width - minimum.Width;
				if (availableSpace <= 0)
					continue;

				compressionSpace -= availableSpace;

				double compression = availableSpace * compressionPressure;
				xOffset += compression;

				double newWidth = plot.Width - compression;
				SizeRequest newRequest = child.Measure(newWidth, heightConstraint, MeasureFlags.IncludeMargins);

				layout.Requests[i] = newRequest;

				plot.Height = newRequest.Request.Height;

				if (newRequest.Request.Width < newWidth)
				{
					double delta = newWidth - newRequest.Request.Width;
					newWidth = newRequest.Request.Width;
					xOffset += delta;
					requiredCompression = requiredCompression - xOffset;
					compressionPressure = (requiredCompression / compressionSpace).Clamp(0, 1);
				}
				plot.Width = newWidth;

				layout.Bounds.Height = Math.Max(layout.Bounds.Height, plot.Height);

				layout.Plots[i] = plot;
			}
		}

		void CompressNaiveLayout(LayoutInformation layout, StackOrientation orientation, double widthConstraint, double heightConstraint)
		{
			if (layout.CompressionSpace <= 0)
				return;

			if (orientation == StackOrientation.Vertical)
			{
				CompressVerticalLayout(layout, widthConstraint, heightConstraint);
			}
			else
			{
				CompressHorizontalLayout(layout, widthConstraint, heightConstraint);
			}
		}

		void CompressVerticalLayout(LayoutInformation layout, double widthConstraint, double heightConstraint)
		{
			double yOffset = 0;

			if (heightConstraint >= layout.Bounds.Height)
			{
				// no need to compress
				return;
			}

			double requiredCompression = layout.Bounds.Height - heightConstraint;
			double compressionSpace = layout.CompressionSpace;
			double compressionPressure = (requiredCompression / layout.CompressionSpace).Clamp(0, 1);

			for (var i = 0; i < layout.Plots.Length; i++)
			{
				var child = (View)LogicalChildren[i];
				if (!child.IsVisible)
					continue;

				Size minimum = layout.Requests[i].Minimum;

				layout.Plots[i].Y -= yOffset;

				Rectangle plot = layout.Plots[i];
				double availableSpace = plot.Height - minimum.Height;
				if (availableSpace <= 0)
					continue;

				compressionSpace -= availableSpace;

				double compression = availableSpace * compressionPressure;
				yOffset += compression;

				double newHeight = plot.Height - compression;
				SizeRequest newRequest = child.Measure(widthConstraint, newHeight, MeasureFlags.IncludeMargins);

				layout.Requests[i] = newRequest;

				plot.Width = newRequest.Request.Width;

				if (newRequest.Request.Height < newHeight)
				{
					double delta = newHeight - newRequest.Request.Height;
					newHeight = newRequest.Request.Height;
					yOffset += delta;
					requiredCompression = requiredCompression - yOffset;
					compressionPressure = (requiredCompression / compressionSpace).Clamp(0, 1);
				}
				plot.Height = newHeight;

				layout.Bounds.Width = Math.Max(layout.Bounds.Width, plot.Width);

				layout.Plots[i] = plot;
			}
		}

		void ComputeConstraintForView(View view, bool isOnlyExpander)
		{
			if (Orientation == StackOrientation.Horizontal)
			{
				if ((Constraint & LayoutConstraint.VerticallyFixed) != 0 && view.VerticalOptions.Alignment == LayoutAlignment.Fill)
				{
					if (isOnlyExpander && view.HorizontalOptions.Alignment == LayoutAlignment.Fill && Constraint == LayoutConstraint.Fixed)
					{
						view.ComputedConstraint = LayoutConstraint.Fixed;
					}
					else
					{
						view.ComputedConstraint = LayoutConstraint.VerticallyFixed;
					}
				}
				else
				{
					view.ComputedConstraint = LayoutConstraint.None;
				}
			}
			else
			{
				if ((Constraint & LayoutConstraint.HorizontallyFixed) != 0 && view.HorizontalOptions.Alignment == LayoutAlignment.Fill)
				{
					if (isOnlyExpander && view.VerticalOptions.Alignment == LayoutAlignment.Fill && Constraint == LayoutConstraint.Fixed)
					{
						view.ComputedConstraint = LayoutConstraint.Fixed;
					}
					else
					{
						view.ComputedConstraint = LayoutConstraint.HorizontallyFixed;
					}
				}
				else
				{
					view.ComputedConstraint = LayoutConstraint.None;
				}
			}
		}

		bool HasVisibileChildren()
		{
			for (var index = 0; index < InternalChildren.Count; index++)
			{
				var child = (VisualElement)InternalChildren[index];
				if (child.IsVisible)
					return true;
			}
			return false;
		}

		void ProcessExpanders(LayoutInformation layout, StackOrientation orientation, double x, double y, double widthConstraint, double heightConstraint)
		{
			if (layout.Expanders <= 0)
				return;

			if (orientation == StackOrientation.Vertical)
			{
				double extraSpace = heightConstraint - layout.Bounds.Height;
				if (extraSpace <= 0)
					return;

				double spacePerExpander = extraSpace / layout.Expanders;
				double yOffset = 0;

				for (var i = 0; i < LogicalChildren.Count; i++)
				{
					var child = (View)LogicalChildren[i];
					if (!child.IsVisible)
						continue;
					Rectangle plot = layout.Plots[i];
					plot.Y += yOffset;

					if (child.VerticalOptions.Expands)
					{
						plot.Height += spacePerExpander;
						yOffset += spacePerExpander;
					}

					layout.Plots[i] = plot;
				}

				layout.Bounds.Height = heightConstraint;
			}
			else
			{
				double extraSpace = widthConstraint - layout.Bounds.Width;
				if (extraSpace <= 0)
					return;

				double spacePerExpander = extraSpace / layout.Expanders;
				double xOffset = 0;

				for (var i = 0; i < LogicalChildren.Count; i++)
				{
					var child = (View)LogicalChildren[i];
					if (!child.IsVisible)
						continue;
					Rectangle plot = layout.Plots[i];
					plot.X += xOffset;

					if (child.HorizontalOptions.Expands)
					{
						plot.Width += spacePerExpander;
						xOffset += spacePerExpander;
					}

					layout.Plots[i] = plot;
				}

				layout.Bounds.Width = widthConstraint;
			}
		}

		class LayoutInformation
		{
			public Rectangle Bounds;
			public double CompressionSpace;
			public Size Constraint;
			public int Expanders;
			public Size MinimumSize;
			public Rectangle[] Plots;
			public SizeRequest[] Requests;
		}
	}
}