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

namespace Xamarin.Forms
{
	public partial class Grid : Layout<View>, IGridController
	{
		public static readonly BindableProperty RowProperty = BindableProperty.CreateAttached("Row", typeof(int), typeof(Grid), default(int), validateValue: (bindable, value) => (int)value >= 0);

		public static readonly BindableProperty RowSpanProperty = BindableProperty.CreateAttached("RowSpan", typeof(int), typeof(Grid), 1, validateValue: (bindable, value) => (int)value >= 1);

		public static readonly BindableProperty ColumnProperty = BindableProperty.CreateAttached("Column", typeof(int), typeof(Grid), default(int), validateValue: (bindable, value) => (int)value >= 0);

		public static readonly BindableProperty ColumnSpanProperty = BindableProperty.CreateAttached("ColumnSpan", typeof(int), typeof(Grid), 1, validateValue: (bindable, value) => (int)value >= 1);

		public static readonly BindableProperty RowSpacingProperty = BindableProperty.Create("RowSpacing", typeof(double), typeof(Grid), 6d,
			propertyChanged: (bindable, oldValue, newValue) => ((Grid)bindable).InvalidateMeasureInternal(InvalidationTrigger.MeasureChanged));

		public static readonly BindableProperty ColumnSpacingProperty = BindableProperty.Create("ColumnSpacing", typeof(double), typeof(Grid), 6d,
			propertyChanged: (bindable, oldValue, newValue) => ((Grid)bindable).InvalidateMeasureInternal(InvalidationTrigger.MeasureChanged));

		public static readonly BindableProperty ColumnDefinitionsProperty = BindableProperty.Create("ColumnDefinitions", typeof(ColumnDefinitionCollection), typeof(Grid), null,
			validateValue: (bindable, value) => value != null, propertyChanged: (bindable, oldvalue, newvalue) =>
			{
				if (oldvalue != null)
					((ColumnDefinitionCollection)oldvalue).ItemSizeChanged -= ((Grid)bindable).OnDefinitionChanged;
				if (newvalue != null)
					((ColumnDefinitionCollection)newvalue).ItemSizeChanged += ((Grid)bindable).OnDefinitionChanged;
			}, defaultValueCreator: bindable =>
			{
				var colDef = new ColumnDefinitionCollection();
				colDef.ItemSizeChanged += ((Grid)bindable).OnDefinitionChanged;
				return colDef;
			});

		public static readonly BindableProperty RowDefinitionsProperty = BindableProperty.Create("RowDefinitions", typeof(RowDefinitionCollection), typeof(Grid), null,
			validateValue: (bindable, value) => value != null, propertyChanged: (bindable, oldvalue, newvalue) =>
			{
				if (oldvalue != null)
					((RowDefinitionCollection)oldvalue).ItemSizeChanged -= ((Grid)bindable).OnDefinitionChanged;
				if (newvalue != null)
					((RowDefinitionCollection)newvalue).ItemSizeChanged += ((Grid)bindable).OnDefinitionChanged;
			}, defaultValueCreator: bindable =>
			{
				var rowDef = new RowDefinitionCollection();
				rowDef.ItemSizeChanged += ((Grid)bindable).OnDefinitionChanged;
				return rowDef;
			});

		readonly GridElementCollection _children;

		public Grid()
		{
			_children = new GridElementCollection(InternalChildren, this) { Parent = this };
		}

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

		public ColumnDefinitionCollection ColumnDefinitions
		{
			get { return (ColumnDefinitionCollection)GetValue(ColumnDefinitionsProperty); }
			set { SetValue(ColumnDefinitionsProperty, value); }
		}

		public double ColumnSpacing
		{
			get { return (double)GetValue(ColumnSpacingProperty); }
			set { SetValue(ColumnSpacingProperty, value); }
		}

		public RowDefinitionCollection RowDefinitions
		{
			get { return (RowDefinitionCollection)GetValue(RowDefinitionsProperty); }
			set { SetValue(RowDefinitionsProperty, value); }
		}

		public double RowSpacing
		{
			get { return (double)GetValue(RowSpacingProperty); }
			set { SetValue(RowSpacingProperty, value); }
		}

		public static int GetColumn(BindableObject bindable)
		{
			return (int)bindable.GetValue(ColumnProperty);
		}

		public static int GetColumnSpan(BindableObject bindable)
		{
			return (int)bindable.GetValue(ColumnSpanProperty);
		}

		public static int GetRow(BindableObject bindable)
		{
			return (int)bindable.GetValue(RowProperty);
		}

		public static int GetRowSpan(BindableObject bindable)
		{
			return (int)bindable.GetValue(RowSpanProperty);
		}

		public static void SetColumn(BindableObject bindable, int value)
		{
			bindable.SetValue(ColumnProperty, value);
		}

		public static void SetColumnSpan(BindableObject bindable, int value)
		{
			bindable.SetValue(ColumnSpanProperty, value);
		}

		public static void SetRow(BindableObject bindable, int value)
		{
			bindable.SetValue(RowProperty, value);
		}

		public static void SetRowSpan(BindableObject bindable, int value)
		{
			bindable.SetValue(RowSpanProperty, value);
		}

		protected override void OnAdded(View view)
		{
			base.OnAdded(view);
			view.PropertyChanged += OnItemPropertyChanged;
		}

		protected override void OnBindingContextChanged()
		{
			UpdateInheritedBindingContexts();
			base.OnBindingContextChanged();
		}

		protected override void OnRemoved(View view)
		{
			base.OnRemoved(view);
			view.PropertyChanged -= OnItemPropertyChanged;
		}

		internal override void ComputeConstraintForView(View view)
		{
			LayoutOptions vOptions = view.VerticalOptions;
			LayoutOptions hOptions = view.HorizontalOptions;

			var result = LayoutConstraint.None;

			if (_rows == null || _columns == null)
				EnsureRowsColumnsInitialized();

			if (vOptions.Alignment == LayoutAlignment.Fill)
			{
				int row = GetRow(view);
				int rowSpan = GetRowSpan(view);
				List<RowDefinition> rowDefinitions = _rows;

				var canFix = true;

				for (int i = row; i < row + rowSpan && i < rowDefinitions.Count; i++)
				{
					GridLength height = rowDefinitions[i].Height;
					if (height.IsAuto)
					{
						canFix = false;
						break;
					}
					if ((Constraint & LayoutConstraint.VerticallyFixed) == 0 && height.IsStar)
					{
						canFix = false;
						break;
					}
				}

				if (canFix)
					result |= LayoutConstraint.VerticallyFixed;
			}

			if (hOptions.Alignment == LayoutAlignment.Fill)
			{
				int col = GetColumn(view);
				int colSpan = GetColumnSpan(view);
				List<ColumnDefinition> columnDefinitions = _columns;

				var canFix = true;

				for (int i = col; i < col + colSpan && i < columnDefinitions.Count; i++)
				{
					GridLength width = columnDefinitions[i].Width;
					if (width.IsAuto)
					{
						canFix = false;
						break;
					}
					if ((Constraint & LayoutConstraint.HorizontallyFixed) == 0 && width.IsStar)
					{
						canFix = false;
						break;
					}
				}

				if (canFix)
					result |= LayoutConstraint.HorizontallyFixed;
			}

			view.ComputedConstraint = result;
		}

		[EditorBrowsable(EditorBrowsableState.Never)]
		public void InvalidateMeasureInernalNonVirtual(InvalidationTrigger trigger)
		{
			InvalidateMeasureInternal(trigger);
		}
		internal override void InvalidateMeasureInternal(InvalidationTrigger trigger)
		{
			base.InvalidateMeasureInternal(trigger);
			_columns = null;
			_rows = null;
		}

		void OnDefinitionChanged(object sender, EventArgs args)
		{
			ComputeConstrainsForChildren();
			UpdateInheritedBindingContexts();
			InvalidateLayout();
		}

		void OnItemPropertyChanged(object sender, PropertyChangedEventArgs e)
		{
			if (e.PropertyName == ColumnProperty.PropertyName || e.PropertyName == ColumnSpanProperty.PropertyName || e.PropertyName == RowProperty.PropertyName ||
				e.PropertyName == RowSpanProperty.PropertyName)
			{
				var child = sender as View;
				if (child != null)
				{
					ComputeConstraintForView(child);
				}

				InvalidateLayout();
			}
		}

		void UpdateInheritedBindingContexts()
		{
			object bindingContext = BindingContext;
			RowDefinitionCollection rowDefs = RowDefinitions;
			if (rowDefs != null)
			{
				for (var i = 0; i < rowDefs.Count; i++)
				{
					RowDefinition rowdef = rowDefs[i];
					SetInheritedBindingContext(rowdef, bindingContext);
				}
			}

			ColumnDefinitionCollection colDefs = ColumnDefinitions;
			if (colDefs != null)
			{
				for (var i = 0; i < colDefs.Count; i++)
				{
					ColumnDefinition coldef = colDefs[i];
					SetInheritedBindingContext(coldef, bindingContext);
				}
			}
		}

		public interface IGridList<T> : IList<T> where T : View
		{
			void Add(View view, int left, int top);
			void Add(View view, int left, int right, int top, int bottom);
			void AddHorizontal(IEnumerable<View> views);
			void AddHorizontal(View view);
			void AddVertical(IEnumerable<View> views);
			void AddVertical(View view);
		}

		class GridElementCollection : ElementCollection<View>, IGridList<View>
		{
			public GridElementCollection(ObservableCollection<Element> inner, Grid parent) : base(inner)
			{
				Parent = parent;
			}

			internal Grid Parent { get; set; }

			public void Add(View view, int left, int top)
			{
				if (left < 0)
					throw new ArgumentOutOfRangeException("left");
				if (top < 0)
					throw new ArgumentOutOfRangeException("top");
				Add(view, left, left + 1, top, top + 1);
			}

			public void Add(View view, int left, int right, int top, int bottom)
			{
				if (left < 0)
					throw new ArgumentOutOfRangeException("left");
				if (top < 0)
					throw new ArgumentOutOfRangeException("top");
				if (left >= right)
					throw new ArgumentOutOfRangeException("right");
				if (top >= bottom)
					throw new ArgumentOutOfRangeException("bottom");
				if (view == null)
					throw new ArgumentNullException("view");

				SetRow(view, top);
				SetRowSpan(view, bottom - top);
				SetColumn(view, left);
				SetColumnSpan(view, right - left);

				Add(view);
			}

			public void AddHorizontal(IEnumerable<View> views)
			{
				if (views == null)
					throw new ArgumentNullException("views");

				views.ForEach(AddHorizontal);
			}

			public void AddHorizontal(View view)
			{
				if (view == null)
					throw new ArgumentNullException("view");

				int lastRow = this.Any() ? this.Max(w => GetRow(w) + GetRowSpan(w) - 1) : -1;
				lastRow = Math.Max(lastRow, Parent.RowDefinitions.Count - 1);
				int lastCol = this.Any() ? this.Max(w => GetColumn(w) + GetColumnSpan(w) - 1) : -1;
				lastCol = Math.Max(lastCol, Parent.ColumnDefinitions.Count - 1);

				Add(view, lastCol + 1, lastCol + 2, 0, Math.Max(1, lastRow));
			}

			public void AddVertical(IEnumerable<View> views)
			{
				if (views == null)
					throw new ArgumentNullException("views");

				views.ForEach(AddVertical);
			}

			public void AddVertical(View view)
			{
				if (view == null)
					throw new ArgumentNullException("view");

				int lastRow = this.Any() ? this.Max(w => GetRow(w) + GetRowSpan(w) - 1) : -1;
				lastRow = Math.Max(lastRow, Parent.RowDefinitions.Count - 1);
				int lastCol = this.Any() ? this.Max(w => GetColumn(w) + GetColumnSpan(w) - 1) : -1;
				lastCol = Math.Max(lastCol, Parent.ColumnDefinitions.Count - 1);

				Add(view, 0, Math.Max(1, lastCol), lastRow + 1, lastRow + 2);
			}
		}
	}
}