-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
FlexLayout.cs
490 lines (425 loc) · 17.4 KB
/
FlexLayout.cs
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
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
#nullable disable
using System;
using System.ComponentModel;
using System.Runtime.CompilerServices;
using Microsoft.Maui.Controls.Internals;
using Microsoft.Maui.Graphics;
using Microsoft.Maui.Layouts;
using Flex = Microsoft.Maui.Layouts.Flex;
namespace Microsoft.Maui.Controls.Compatibility
{
[ContentProperty(nameof(Children))]
public class FlexLayout : Layout<View>
{
/// <summary>Bindable property for <see cref="Direction"/>.</summary>
public static readonly BindableProperty DirectionProperty =
BindableProperty.Create(nameof(Direction), typeof(FlexDirection), typeof(FlexLayout), FlexDirection.Row,
propertyChanged: OnDirectionPropertyChanged);
/// <summary>Bindable property for <see cref="JustifyContent"/>.</summary>
public static readonly BindableProperty JustifyContentProperty =
BindableProperty.Create(nameof(JustifyContent), typeof(FlexJustify), typeof(FlexLayout), FlexJustify.Start,
propertyChanged: OnJustifyContentPropertyChanged);
/// <summary>Bindable property for <see cref="AlignContent"/>.</summary>
public static readonly BindableProperty AlignContentProperty =
BindableProperty.Create(nameof(AlignContent), typeof(FlexAlignContent), typeof(FlexLayout), FlexAlignContent.Stretch,
propertyChanged: OnAlignContentPropertyChanged);
/// <summary>Bindable property for <see cref="AlignItems"/>.</summary>
public static readonly BindableProperty AlignItemsProperty =
BindableProperty.Create(nameof(AlignItems), typeof(FlexAlignItems), typeof(FlexLayout), FlexAlignItems.Stretch,
propertyChanged: OnAlignItemsPropertyChanged);
/// <summary>Bindable property for <see cref="Position"/>.</summary>
public static readonly BindableProperty PositionProperty =
BindableProperty.Create(nameof(Position), typeof(FlexPosition), typeof(FlexLayout), FlexPosition.Relative,
propertyChanged: OnPositionPropertyChanged);
/// <summary>Bindable property for <see cref="Wrap"/>.</summary>
public static readonly BindableProperty WrapProperty =
BindableProperty.Create(nameof(Wrap), typeof(FlexWrap), typeof(FlexLayout), FlexWrap.NoWrap,
propertyChanged: OnWrapPropertyChanged);
static readonly BindableProperty FlexItemProperty =
BindableProperty.CreateAttached("FlexItem", typeof(Flex.Item), typeof(FlexLayout), null);
/// <summary>Bindable property for attached property <c>Order</c>.</summary>
public static readonly BindableProperty OrderProperty =
BindableProperty.CreateAttached("Order", typeof(int), typeof(FlexLayout), default(int),
propertyChanged: OnOrderPropertyChanged);
/// <summary>Bindable property for attached property <c>Grow</c>.</summary>
public static readonly BindableProperty GrowProperty =
BindableProperty.CreateAttached("Grow", typeof(float), typeof(FlexLayout), default(float),
propertyChanged: OnGrowPropertyChanged, validateValue: (bindable, value) => (float)value >= 0);
/// <summary>Bindable property for attached property <c>Shrink</c>.</summary>
public static readonly BindableProperty ShrinkProperty =
BindableProperty.CreateAttached("Shrink", typeof(float), typeof(FlexLayout), 1f,
propertyChanged: OnShrinkPropertyChanged, validateValue: (bindable, value) => (float)value >= 0);
/// <summary>Bindable property for attached property <c>AlignSelf</c>.</summary>
public static readonly BindableProperty AlignSelfProperty =
BindableProperty.CreateAttached("AlignSelf", typeof(FlexAlignSelf), typeof(FlexLayout), FlexAlignSelf.Auto,
propertyChanged: OnAlignSelfPropertyChanged);
/// <summary>Bindable property for attached property <c>Basis</c>.</summary>
public static readonly BindableProperty BasisProperty =
BindableProperty.CreateAttached("Basis", typeof(FlexBasis), typeof(FlexLayout), FlexBasis.Auto,
propertyChanged: OnBasisPropertyChanged);
public FlexLayout() =>
Hosting.CompatibilityCheck.CheckForCompatibility();
public FlexDirection Direction
{
get => (FlexDirection)GetValue(DirectionProperty);
set => SetValue(DirectionProperty, value);
}
public FlexJustify JustifyContent
{
get => (FlexJustify)GetValue(JustifyContentProperty);
set => SetValue(JustifyContentProperty, value);
}
public FlexAlignContent AlignContent
{
get => (FlexAlignContent)GetValue(AlignContentProperty);
set => SetValue(AlignContentProperty, value);
}
public FlexAlignItems AlignItems
{
get => (FlexAlignItems)GetValue(AlignItemsProperty);
set => SetValue(AlignItemsProperty, value);
}
public FlexPosition Position
{
get => (FlexPosition)GetValue(PositionProperty);
set => SetValue(PositionProperty, value);
}
public FlexWrap Wrap
{
get => (FlexWrap)GetValue(WrapProperty);
set => SetValue(WrapProperty, value);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
static Flex.Item GetFlexItem(BindableObject bindable)
=> (Flex.Item)bindable.GetValue(FlexItemProperty);
static void SetFlexItem(BindableObject bindable, Flex.Item node)
=> bindable.SetValue(FlexItemProperty, node);
public static int GetOrder(BindableObject bindable)
=> (int)bindable.GetValue(OrderProperty);
public static void SetOrder(BindableObject bindable, int value)
=> bindable.SetValue(OrderProperty, value);
public static float GetGrow(BindableObject bindable)
=> (float)bindable.GetValue(GrowProperty);
public static void SetGrow(BindableObject bindable, float value)
=> bindable.SetValue(GrowProperty, value);
public static float GetShrink(BindableObject bindable)
=> (float)bindable.GetValue(ShrinkProperty);
public static void SetShrink(BindableObject bindable, float value)
=> bindable.SetValue(ShrinkProperty, value);
public static FlexAlignSelf GetAlignSelf(BindableObject bindable)
=> (FlexAlignSelf)bindable.GetValue(AlignSelfProperty);
public static void SetAlignSelf(BindableObject bindable, FlexAlignSelf value)
=> bindable.SetValue(AlignSelfProperty, value);
public static FlexBasis GetBasis(BindableObject bindable)
=> (FlexBasis)bindable.GetValue(BasisProperty);
public static void SetBasis(BindableObject bindable, FlexBasis value)
=> bindable.SetValue(BasisProperty, value);
static void OnOrderPropertyChanged(BindableObject bindable, object oldValue, object newValue)
{
if (!bindable.IsSet(FlexItemProperty))
return;
GetFlexItem(bindable).Order = (int)newValue;
((VisualElement)bindable).InvalidateMeasureInternal(InvalidationTrigger.Undefined);
}
static void OnGrowPropertyChanged(BindableObject bindable, object oldValue, object newValue)
{
if (!bindable.IsSet(FlexItemProperty))
return;
GetFlexItem(bindable).Grow = (float)newValue;
((VisualElement)bindable).InvalidateMeasureInternal(InvalidationTrigger.MeasureChanged);
}
static void OnShrinkPropertyChanged(BindableObject bindable, object oldValue, object newValue)
{
if (!bindable.IsSet(FlexItemProperty))
return;
GetFlexItem(bindable).Shrink = (float)newValue;
((VisualElement)bindable).InvalidateMeasureInternal(InvalidationTrigger.MeasureChanged);
}
static void OnAlignSelfPropertyChanged(BindableObject bindable, object oldValue, object newValue)
{
if (!bindable.IsSet(FlexItemProperty))
return;
GetFlexItem(bindable).AlignSelf = (Flex.AlignSelf)(FlexAlignSelf)newValue;
((VisualElement)bindable).InvalidateMeasureInternal(InvalidationTrigger.MeasureChanged);
}
static void OnBasisPropertyChanged(BindableObject bindable, object oldValue, object newValue)
{
if (!bindable.IsSet(FlexItemProperty))
return;
GetFlexItem(bindable).Basis = ((FlexBasis)newValue).ToFlexBasis();
((VisualElement)bindable).InvalidateMeasureInternal(InvalidationTrigger.MeasureChanged);
}
static void OnDirectionPropertyChanged(BindableObject bindable, object oldValue, object newValue)
{
var flexLayout = bindable as FlexLayout;
if (flexLayout._root == null)
return;
flexLayout._root.Direction = (Flex.Direction)(FlexDirection)newValue;
flexLayout.InvalidateLayout();
}
static void OnJustifyContentPropertyChanged(BindableObject bindable, object oldValue, object newValue)
{
var flexLayout = bindable as FlexLayout;
if (flexLayout._root == null)
return;
flexLayout._root.JustifyContent = (Flex.Justify)(FlexJustify)newValue;
flexLayout.InvalidateLayout();
}
static void OnAlignContentPropertyChanged(BindableObject bindable, object oldValue, object newValue)
{
var flexLayout = bindable as FlexLayout;
if (flexLayout._root == null)
return;
flexLayout._root.AlignContent = (Flex.AlignContent)(FlexAlignContent)newValue;
flexLayout.InvalidateLayout();
}
static void OnAlignItemsPropertyChanged(BindableObject bindable, object oldValue, object newValue)
{
var flexLayout = (FlexLayout)bindable;
if (flexLayout._root == null)
return;
flexLayout._root.AlignItems = (Flex.AlignItems)(FlexAlignItems)newValue;
flexLayout.InvalidateLayout();
}
static void OnPositionPropertyChanged(BindableObject bindable, object oldValue, object newValue)
{
var flexLayout = (FlexLayout)bindable;
if (flexLayout._root == null)
return;
flexLayout._root.Position = (Flex.Position)(FlexPosition)newValue;
flexLayout.InvalidateLayout();
}
static void OnWrapPropertyChanged(BindableObject bindable, object oldValue, object newValue)
{
var flexLayout = bindable as FlexLayout;
if (flexLayout._root == null)
return;
flexLayout._root.Wrap = (Flex.Wrap)(FlexWrap)newValue;
flexLayout.InvalidateLayout();
}
Flex.Item _root;
//this should only be used in unitTests. layout creation will normally happen on OnParentSet
internal override void OnIsPlatformEnabledChanged()
{
base.OnIsPlatformEnabledChanged();
if (IsPlatformEnabled && _root == null)
PopulateLayout();
else if (!IsPlatformEnabled && _root != null)
ClearLayout();
}
protected override void OnParentSet()
{
base.OnParentSet();
if (Parent != null && _root == null)
PopulateLayout();
else if (Parent == null && _root != null)
ClearLayout();
}
void PopulateLayout()
{
InitLayoutProperties(_root = new Flex.Item());
foreach (var child in Children)
AddChild(child);
}
void InitLayoutProperties(Flex.Item item)
{
item.AlignContent = (Flex.AlignContent)(FlexAlignContent)GetValue(AlignContentProperty);
item.AlignItems = (Flex.AlignItems)(FlexAlignItems)GetValue(AlignItemsProperty);
item.Direction = (Flex.Direction)(FlexDirection)GetValue(DirectionProperty);
item.JustifyContent = (Flex.Justify)(FlexJustify)GetValue(JustifyContentProperty);
item.Wrap = (Flex.Wrap)(FlexWrap)GetValue(WrapProperty);
}
void ClearLayout()
{
foreach (var child in Children)
RemoveChild(child);
_root = null;
}
protected override void OnAdded(View view)
{
AddChild(view);
view.PropertyChanged += OnChildPropertyChanged;
base.OnAdded(view);
}
protected override void OnRemoved(View view)
{
view.PropertyChanged -= OnChildPropertyChanged;
RemoveChild(view);
base.OnRemoved(view);
}
void AddChild(View view)
{
if (_root == null)
return;
var item = (view as FlexLayout)?._root ?? new Flex.Item();
InitItemProperties(view, item);
if (!(view is FlexLayout))
{ //inner layouts don't get measured
item.SelfSizing = (Flex.Item it, ref float w, ref float h) =>
{
var sizeConstrains = item.GetConstraints();
sizeConstrains.Width = (_measuring && sizeConstrains.Width == 0) ? double.PositiveInfinity : sizeConstrains.Width;
sizeConstrains.Height = (_measuring && sizeConstrains.Height == 0) ? double.PositiveInfinity : sizeConstrains.Height;
var request = view.Measure(sizeConstrains.Width, sizeConstrains.Height).Request;
w = (float)request.Width;
h = (float)request.Height;
};
}
_root.InsertAt(Children.IndexOf(view), item);
SetFlexItem(view, item);
}
void InitItemProperties(View view, Flex.Item item)
{
item.Order = (int)view.GetValue(OrderProperty);
item.Grow = (float)view.GetValue(GrowProperty);
item.Shrink = (float)view.GetValue(ShrinkProperty);
item.Basis = ((FlexBasis)view.GetValue(BasisProperty)).ToFlexBasis();
item.AlignSelf = (Flex.AlignSelf)(FlexAlignSelf)view.GetValue(AlignSelfProperty);
var (mleft, mtop, mright, mbottom) = (Thickness)view.GetValue(MarginProperty);
item.MarginLeft = (float)mleft;
item.MarginTop = (float)mtop;
item.MarginRight = (float)mright;
item.MarginBottom = (float)mbottom;
var width = (double)view.GetValue(WidthRequestProperty);
item.Width = width < 0 ? float.NaN : (float)width;
var height = (double)view.GetValue(HeightRequestProperty);
item.Height = height < 0 ? float.NaN : (float)height;
item.IsVisible = (bool)view.GetValue(IsVisibleProperty);
if (view is FlexLayout)
{
var (pleft, ptop, pright, pbottom) = (Thickness)view.GetValue(PaddingProperty);
item.PaddingLeft = (float)pleft;
item.PaddingTop = (float)ptop;
item.PaddingRight = (float)pright;
item.PaddingBottom = (float)pbottom;
}
}
void RemoveChild(View view)
{
if (_root == null)
return;
var item = GetFlexItem(view);
_root.Remove(item);
view.ClearValue(FlexItemProperty);
}
void OnChildPropertyChanged(object sender, PropertyChangedEventArgs e)
{
if (e.PropertyName == WidthRequestProperty.PropertyName
|| e.PropertyName == HeightRequestProperty.PropertyName)
{
var item = (sender as FlexLayout)?._root ?? GetFlexItem((BindableObject)sender);
if (item == null)
return;
item.Width = ((View)sender).WidthRequest < 0 ? float.NaN : (float)((View)sender).WidthRequest;
item.Height = ((View)sender).HeightRequest < 0 ? float.NaN : (float)((View)sender).HeightRequest;
InvalidateMeasureInternal(InvalidationTrigger.MeasureChanged);
return;
}
if (e.PropertyName == MarginProperty.PropertyName)
{
var item = (sender as FlexLayout)?._root ?? GetFlexItem((BindableObject)sender);
if (item == null)
return;
var margin = (Thickness)((View)sender).GetValue(MarginProperty);
item.MarginLeft = (float)margin.Left;
item.MarginTop = (float)margin.Top;
item.MarginRight = (float)margin.Right;
item.MarginBottom = (float)margin.Bottom;
InvalidateMeasureInternal(InvalidationTrigger.MeasureChanged);
return;
}
if (e.PropertyName == PaddingProperty.PropertyName)
{
var item = (sender as FlexLayout)?._root ?? GetFlexItem((BindableObject)sender);
if (item == null)
return;
var padding = (Thickness)((View)sender).GetValue(PaddingProperty);
item.PaddingLeft = (float)padding.Left;
item.PaddingTop = (float)padding.Top;
item.PaddingRight = (float)padding.Right;
item.PaddingBottom = (float)padding.Bottom;
InvalidateMeasureInternal(InvalidationTrigger.MeasureChanged);
return;
}
if (e.PropertyName == IsVisibleProperty.PropertyName)
{
var item = (sender as FlexLayout)?._root ?? GetFlexItem((BindableObject)sender);
if (item == null)
return;
item.IsVisible = (bool)((View)sender).GetValue(IsVisibleProperty);
InvalidateMeasureInternal(InvalidationTrigger.MeasureChanged);
return;
}
}
protected override void LayoutChildren(double x, double y, double width, double height)
{
if (_root == null)
return;
Layout(width, height);
foreach (var child in Children)
{
var frame = GetFlexItem(child).GetFrame();
if (double.IsNaN(frame.X)
|| double.IsNaN(frame.Y)
|| double.IsNaN(frame.Width)
|| double.IsNaN(frame.Height))
throw new Exception("something is deeply wrong");
frame = frame.Offset(x, y); //flex doesn't support offset on _root
child.Layout(frame);
}
}
bool _measuring;
protected override SizeRequest OnMeasure(double widthConstraint, double heightConstraint)
{
if (_root == null)
return new SizeRequest(new Size(widthConstraint, heightConstraint));
//All of this is a HACK as X.Flex doesn't supports measuring
if (!double.IsPositiveInfinity(widthConstraint) && !double.IsPositiveInfinity(heightConstraint))
return new SizeRequest(new Size(widthConstraint, heightConstraint));
_measuring = true;
//1. Set Shrink to 0, set align-self to start (to avoid stretching)
// Set Image.Aspect to Fill to get the value we expect in measuring
foreach (var child in Children)
{
if (GetFlexItem(child) is Flex.Item item)
{
item.Shrink = 0;
item.AlignSelf = Flex.AlignSelf.Start;
}
}
Layout(widthConstraint, heightConstraint);
//2. look at the children location
if (double.IsPositiveInfinity(widthConstraint))
{
widthConstraint = 0;
foreach (var item in _root)
widthConstraint = Math.Max(widthConstraint, item.Frame[0] + item.Frame[2] + item.MarginRight);
}
if (double.IsPositiveInfinity(heightConstraint))
{
heightConstraint = 0;
foreach (var item in _root)
heightConstraint = Math.Max(heightConstraint, item.Frame[1] + item.Frame[3] + item.MarginBottom);
}
//3. reset Shrink, align-self, and image.aspect
foreach (var child in Children)
{
if (GetFlexItem(child) is Flex.Item item)
{
item.Shrink = (float)child.GetValue(ShrinkProperty);
item.AlignSelf = (Flex.AlignSelf)(FlexAlignSelf)child.GetValue(AlignSelfProperty);
}
}
_measuring = false;
return new SizeRequest(new Size(widthConstraint, heightConstraint));
}
void Layout(double width, double height)
{
if (_root.Parent != null) //Layout is only computed at root level
return;
_root.Width = !double.IsPositiveInfinity((width)) ? (float)width : 0;
_root.Height = !double.IsPositiveInfinity((height)) ? (float)height : 0;
_root.Layout();
}
}
}