/
FlexLayout.cs
executable file
·740 lines (656 loc) · 36.9 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
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
/* Copyright (c) 2019 Samsung Electronics Co., Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
using System;
using System.ComponentModel;
using Tizen.NUI.BaseComponents;
using System.Runtime.InteropServices;
using System.Diagnostics;
using Tizen.NUI.Binding;
namespace Tizen.NUI
{
/// <summary>
/// This class implements a flex layout.
/// The flex layout implementation is based on open source Facebook Yoga layout engine.
/// For more information about the flex layout API and how to use it please refer to https://yogalayout.com/docs/
/// We implement the subset of the API in the class below.
/// </summary>
public class FlexLayout : LayoutGroup, global::System.IDisposable
{
/// <summary>
/// FlexItemProperty
/// </summary>
[EditorBrowsable(EditorBrowsableState.Never)]
internal static readonly BindableProperty FlexItemProperty = BindableProperty.CreateAttached("FlexItem", typeof(HandleRef), typeof(FlexLayout), default(HandleRef));
/// <summary>
/// FlexAlignmentSelfProperty
/// </summary>
[EditorBrowsable(EditorBrowsableState.Never)]
public static readonly BindableProperty FlexAlignmentSelfProperty = BindableProperty.CreateAttached("FlexAlignmentSelf", typeof(AlignmentType), typeof(FlexLayout), AlignmentType.Auto, validateValue: ValidateEnum((int)AlignmentType.Auto, (int)AlignmentType.Stretch), propertyChanged: OnChildPropertyChanged);
/// <summary>
/// FlexPositionTypeProperty
/// </summary>
[EditorBrowsable(EditorBrowsableState.Never)]
public static readonly BindableProperty FlexPositionTypeProperty = BindableProperty.CreateAttached("FlexPositionType", typeof(PositionType), typeof(FlexLayout), PositionType.Relative, validateValue: ValidateEnum((int)PositionType.Relative, (int)PositionType.Absolute), propertyChanged: OnChildPropertyChanged);
/// <summary>
/// AspectRatioProperty
/// </summary>
[EditorBrowsable(EditorBrowsableState.Never)]
public static readonly BindableProperty FlexAspectRatioProperty = BindableProperty.CreateAttached("FlexAspectRatio", typeof(float), typeof(FlexLayout), FlexUndefined, validateValue: (bindable, value) => (float)value > 0, propertyChanged: OnChildPropertyChanged);
/// <summary>
/// FlexBasisProperty
/// </summary>
[EditorBrowsable(EditorBrowsableState.Never)]
public static readonly BindableProperty FlexBasisProperty = BindableProperty.CreateAttached("FlexBasis", typeof(float), typeof(FlexLayout), FlexUndefined, validateValue: (bindable, value) => (float)value >= 0, propertyChanged: OnChildPropertyChanged);
/// <summary>
/// FlexShrinkProperty
/// </summary>
[EditorBrowsable(EditorBrowsableState.Never)]
public static readonly BindableProperty FlexShrinkProperty = BindableProperty.CreateAttached("FlexShrink", typeof(float), typeof(FlexLayout), 1.0f, validateValue: (bindable, value) => (float)value >= 0, propertyChanged: OnChildPropertyChanged);
/// <summary>
/// FlexGrowProperty
/// </summary>
[EditorBrowsable(EditorBrowsableState.Never)]
public static readonly BindableProperty FlexGrowProperty = BindableProperty.CreateAttached("FlexGrow", typeof(float), typeof(FlexLayout), FlexUndefined, validateValue: (bindable, value) => (float)value >= 0, propertyChanged: OnChildPropertyChanged);
private static bool LayoutDebugFlex = false; // Debug flag
private global::System.Runtime.InteropServices.HandleRef swigCPtr;
private bool swigCMemOwn;
private bool disposed;
private bool isDisposeQueued = false;
private MeasureSpecification parentMeasureSpecificationWidth;
private MeasureSpecification parentMeasureSpecificationHeight;
private IntPtr _rootFlex; // Pointer to the unmanged flex layout class.
internal const float FlexUndefined = 10E20F; // Auto setting which is equivalent to WrapContent.
internal struct MeasuredSize
{
public MeasuredSize(float x, float y)
{
width = x;
height = y;
}
public float width;
public float height;
};
/// <summary>
/// Gets the alignment self of the child view.
/// </summary>
/// <seealso cref="SetFlexAlignmentSelf(View, AlignmentType)"/>
/// <param name="view">The child view.</param>
/// <returns> The value of alignment self.</returns>
/// <exception cref="ArgumentNullException">The <paramref name="view"/> cannot be null.</exception>
/// <since_tizen> 8 </since_tizen>
public static AlignmentType GetFlexAlignmentSelf(View view) => GetAttachedValue<AlignmentType>(view, FlexAlignmentSelfProperty);
/// <summary>
/// Gets the position type of the child view.
/// </summary>
/// <seealso cref="SetFlexPositionType(View, PositionType)"/>
/// <param name="view">The child view.</param>
/// <returns> The value of position type</returns>
/// <exception cref="ArgumentNullException">The <paramref name="view"/> cannot be null.</exception>
/// <since_tizen> 8 </since_tizen>
public static PositionType GetFlexPositionType(View view) => GetAttachedValue<PositionType>(view, FlexPositionTypeProperty);
/// <summary>
/// Gets the aspect ratio of the child view.
/// </summary>
/// <seealso cref="SetFlexAspectRatio(View, float)"/>
/// <param name="view">The child view.</param>
/// <returns> The value of aspect ratio</returns>
/// <exception cref="ArgumentNullException">The <paramref name="view"/> cannot be null.</exception>
/// <since_tizen> 8 </since_tizen>
public static float GetFlexAspectRatio(View view) => GetAttachedValue<float>(view, FlexAspectRatioProperty);
/// <summary>
/// Gets the basis of the child view.
/// </summary>
/// <seealso cref="SetFlexBasis(View, float)"/>
/// <param name="view">The child view.</param>
/// <returns> The value of basis</returns>
/// <exception cref="ArgumentNullException">The <paramref name="view"/> cannot be null.</exception>
/// <since_tizen> 8 </since_tizen>
public static float GetFlexBasis(View view) => GetAttachedValue<float>(view, FlexBasisProperty);
/// <summary>
/// Gets the shrink of the child view.
/// </summary>
/// <seealso cref="SetFlexShrink(View, float)"/>
/// <param name="view">The child view.</param>
/// <returns> The value of shrink</returns>
/// <exception cref="ArgumentNullException">The <paramref name="view"/> cannot be null.</exception>
/// <since_tizen> 8 </since_tizen>
public static float GetFlexShrink(View view) => GetAttachedValue<float>(view, FlexShrinkProperty);
/// <summary>
/// Gets the grow of the child view.
/// </summary>
/// <seealso cref="SetFlexGrow(View, float)"/>
/// <param name="view">The child view.</param>
/// <returns> The value of grow</returns>
/// <exception cref="ArgumentNullException">The <paramref name="view"/> cannot be null.</exception>
/// <since_tizen> 8 </since_tizen>
public static float GetFlexGrow(View view) => GetAttachedValue<float>(view, FlexGrowProperty);
/// <summary>
/// Sets the alignment self of the child view.<br/>
/// Alignment self has the same options and effect as <see cref="ItemsAlignment"/> but instead of affecting the children within a container,
/// you can apply this property to a single child to change its alignment within its parent.<br/>
/// Alignment self overrides any option set by the parent with <see cref="ItemsAlignment"/>.
/// </summary>
/// <param name="view">The child view.</param>
/// <param name="value">The value of alignment self.</param>
/// <exception cref="ArgumentNullException">The <paramref name="view"/> cannot be null.</exception>
/// <exception cref="ArgumentException">The <paramref name="value"/> should be <see cref="AlignmentType"/>.</exception>
/// <since_tizen> 8 </since_tizen>
public static void SetFlexAlignmentSelf(View view, AlignmentType value) => SetAttachedValue(view, FlexAlignmentSelfProperty, value);
/// <summary>
/// Sets the position type of the child view.<br/>
/// The position type of an element defines how it is positioned within its parent.
/// By default a child is positioned relatively. This means a child is positioned according to the normal flow of the layout,
/// and then offset relative to that position based on the values of <see cref="View.Margin"/>.<br/>
/// When positioned absolutely an element doesn't take part in the normal layout flow.
/// It is instead laid out independent of its siblings. The position is determined based on the <see cref="View.Margin"/>.
/// </summary>
/// <param name="view">The child view.</param>
/// <param name="value">The value of position type.</param>
/// <exception cref="ArgumentNullException">The <paramref name="view"/> cannot be null.</exception>
/// <exception cref="ArgumentException">The <paramref name="value"/> should be <see cref="PositionType"/>.</exception>
/// <since_tizen> 8 </since_tizen>
public static void SetFlexPositionType(View view, PositionType value) => SetAttachedValue(view, FlexPositionTypeProperty, value);
/// <summary>
/// Sets the aspect ratio of the child view.
/// Aspect ratio Defines as the ratio between the width and the height of a node
/// e.g. if a node has an aspect ratio of 2 then its width is twice the size of its height.<br/>
/// Aspect ratio accepts any floating point value > 0. this has higher priority than flex grow.
/// </summary>
/// <param name="view">The child view.</param>
/// <param name="value">The value of aspect ratio</param>
/// <exception cref="ArgumentNullException">The <paramref name="view"/> cannot be null.</exception>
/// <exception cref="ArgumentException">The <paramref name="value"/> cannot be less than or equal to 0.0f.</exception>
/// <since_tizen> 8 </since_tizen>
public static void SetFlexAspectRatio(View view, float value) => SetAttachedValue(view, FlexAspectRatioProperty, value);
/// <summary>
/// Sets the flex basis of the child view.
/// Flex basis is an axis-independent way of providing the default size of an item along the main axis.<br/>
/// Setting the flex basis of a child is similar to setting the width of that child if its parent is a container with <see cref="FlexDirection.Row"/>
/// or setting the height of a child if its parent is a container with <see cref="FlexDirection.Column"/>.<br/>
/// The flex basis of an item is the default size of that item, the size of the item before any flex grow and flex shrink calculations are performed.
/// </summary>
/// <param name="view">The child view.</param>
/// <param name="value">The value of basis</param>
/// <exception cref="ArgumentNullException">The <paramref name="view"/> cannot be null.</exception>
/// <exception cref="ArgumentException">The <paramref name="value"/> cannot be less than 0.0f.</exception>
/// <since_tizen> 8 </since_tizen>
public static void SetFlexBasis(View view, float value) => SetAttachedValue(view, FlexBasisProperty, value);
/// <summary>
/// Sets the flex shrink of the child view.
/// Flex shrink describes how to shrink children along the main axis in the case that the total size of the children overflow the size of the container on the main axis.<br/>
/// Flex shrink is very similar to flex grow and can be thought of in the same way if any overflowing size is considered to be negative remaining space.
/// These two properties also work well together by allowing children to grow and shrink as needed.<br/>
/// Flex shrink accepts any floating point value >= 0, with 1 being the default value. A container will shrink its children weighted by the child's flex shrink value.
/// </summary>
/// <param name="view">The child view.</param>
/// <param name="value">The value of shrink</param>
/// <exception cref="ArgumentNullException">The <paramref name="view"/> cannot be null.</exception>
/// <exception cref="ArgumentException">The <paramref name="value"/> cannot be less than 0.0f.</exception>
/// <since_tizen> 8 </since_tizen>
public static void SetFlexShrink(View view, float value) => SetAttachedValue(view, FlexShrinkProperty, value);
/// <summary>
/// Sets the grow of the child view.
/// Flex grow describes how any space within a container should be distributed among its children along the main axis.
/// After laying out its children, a container will distribute any remaining space according to the flex grow values specified by its children.<br/>
/// Flex grow accepts any floating point value >= 0, with 0 being the default value.<br/>
/// A container will distribute any remaining space among its children weighted by the child's flex grow value.
/// </summary>
/// <param name="view">The child view.</param>
/// <param name="value">The value of flex</param>
/// <exception cref="ArgumentNullException">The <paramref name="view"/> cannot be null.</exception>
/// <exception cref="ArgumentException">The <paramref name="value"/> cannot be less than 0.0f.</exception>
/// <since_tizen> 8 </since_tizen>
public static void SetFlexGrow(View view, float value) => SetAttachedValue(view, FlexGrowProperty, value);
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
internal delegate void ChildMeasureCallback( global::System.IntPtr child, float width, int measureModeWidth, float height, int measureModeHeight, out MeasuredSize measureSize );
event ChildMeasureCallback measureChildDelegate; // Stores a delegate to the child measure callback. Used for all children of this FlexLayout.
internal FlexLayout(global::System.IntPtr cPtr, bool cMemoryOwn)
{
swigCMemOwn = cMemoryOwn;
swigCPtr = new global::System.Runtime.InteropServices.HandleRef(this, cPtr);
_rootFlex = Interop.FlexLayout.FlexLayout_New();
measureChildDelegate = new ChildMeasureCallback(measureChild);
}
internal static global::System.Runtime.InteropServices.HandleRef getCPtr(FlexLayout obj)
{
return (obj == null) ? new global::System.Runtime.InteropServices.HandleRef(null, global::System.IntPtr.Zero) : obj.swigCPtr;
}
/// <inheritdoc/>
/// <since_tizen> 6 </since_tizen>
public void Dispose()
{
// Throw exception if Dispose() is called in separate thread.
if (!Window.IsInstalled())
{
throw new System.InvalidOperationException("This API called from separate thread. This API must be called from MainThread.");
}
if (isDisposeQueued)
{
Dispose(DisposeTypes.Implicit);
}
else
{
Dispose(DisposeTypes.Explicit);
System.GC.SuppressFinalize(this);
}
}
/// <inheritdoc/>
/// <since_tizen> 6 </since_tizen>
protected virtual void Dispose(DisposeTypes type)
{
if (disposed)
{
return;
}
if (type == DisposeTypes.Explicit)
{
// Called by User
// Release your own managed resources here.
// You should release all of your own disposable objects here.
}
// Release your own unmanaged resources here.
// You should not access any managed member here except static instance.
// because the execution order of Finalizes is non-deterministic.
if (swigCPtr.Handle != global::System.IntPtr.Zero)
{
if (swigCMemOwn)
{
swigCMemOwn = false;
Interop.FlexLayout.delete_FlexLayout(swigCPtr);
}
swigCPtr = new global::System.Runtime.InteropServices.HandleRef(null, global::System.IntPtr.Zero);
}
disposed = true;
}
/// <summary>
/// Creates a FlexLayout object.
/// </summary>
/// <since_tizen> 6 </since_tizen>
public FlexLayout() : this(Interop.FlexLayout.FlexLayout_New(), true)
{
if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
}
internal static FlexLayout DownCast(BaseHandle handle)
{
FlexLayout ret = new FlexLayout(Interop.FlexLayout.FlexLayout_DownCast(BaseHandle.getCPtr(handle)), true);
if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
return ret;
}
internal FlexLayout(FlexLayout other) : this(Interop.FlexLayout.new_FlexLayout__SWIG_1(FlexLayout.getCPtr(other)), true)
{
if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
}
internal FlexLayout.AlignmentType GetFlexAlignment()
{
FlexLayout.AlignmentType ret = (FlexLayout.AlignmentType)Interop.FlexLayout.FlexLayout_GetFlexAlignment(swigCPtr);
if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
return ret;
}
internal FlexLayout.AlignmentType GetFlexItemsAlignment()
{
FlexLayout.AlignmentType ret = (FlexLayout.AlignmentType)Interop.FlexLayout.FlexLayout_GetFlexItemsAlignment(swigCPtr);
if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
return ret;
}
/// <summary>
/// Gets/Sets the flex direction in the layout.
/// The direction of the main-axis which determines the direction that flex items are laid out.
/// </summary>
/// <exception cref="InvalidEnumArgumentException">Thrown when using invalid arguments that are enumerators.</exception>
/// <since_tizen> 6 </since_tizen>
public FlexDirection Direction
{
get => (FlexDirection)Interop.FlexLayout.FlexLayout_GetFlexDirection(swigCPtr);
set
{
if (value < FlexDirection.Column || value > FlexDirection.RowReverse)
throw new InvalidEnumArgumentException(nameof(Direction));
Interop.FlexLayout.FlexLayout_SetFlexDirection(swigCPtr, (int)value);
RequestLayout();
}
}
/// <summary>
/// Gets/Sets the justification in the layout.
/// Justify content describes how to align children within the main axis of their container.<br/>
/// For example, you can use this property to center a child horizontally within a container with <see cref="Direction"/> set to <see cref="FlexDirection.Row"/>
/// or vertically within a container with <see cref="Direction"/> set to <see cref="FlexDirection.Column"/>.
/// </summary>
/// <exception cref="InvalidEnumArgumentException">Thrown when using invalid arguments that are enumerators.</exception>
/// <since_tizen> 6 </since_tizen>
public FlexJustification Justification
{
get => (FlexJustification)Interop.FlexLayout.FlexLayout_GetFlexJustification(swigCPtr);
set
{
if (value < FlexJustification.FlexStart || value > FlexJustification.SpaceEvenly)
throw new InvalidEnumArgumentException(nameof(Justification));
Interop.FlexLayout.FlexLayout_SetFlexJustification(swigCPtr, (int)value);
RequestLayout();
}
}
/// <summary>
/// Gets/Sets the wrap in the layout.
/// The flex wrap property is set on containers and controls what happens when children overflow the size of the container along the main axis.<br/>
/// By default children are forced into a single line (which can shrink elements).<br/>
/// If wrapping is allowed items are wrapped into multiple lines along the main axis if needed. wrap reverse behaves the same, but the order of the lines is reversed.<br/>
/// When wrapping lines <see cref="Alignment"/> can be used to specify how the lines are placed in the container.
/// </summary>
/// <exception cref="InvalidEnumArgumentException">Thrown when using invalid arguments that are enumerators.</exception>
/// <since_tizen> 6 </since_tizen>
public FlexWrapType WrapType
{
get => (FlexWrapType)Interop.FlexLayout.FlexLayout_GetFlexWrap(swigCPtr);
set
{
if (value != FlexWrapType.NoWrap && value != FlexWrapType.Wrap)
throw new InvalidEnumArgumentException(nameof(WrapType));
Interop.FlexLayout.FlexLayout_SetFlexWrap(swigCPtr, (int)value);
RequestLayout();
}
}
/// <summary>
/// Gets/Sets the alignment of the layout content.
/// Alignment defines the distribution of lines along the cross-axis.<br/>
/// This only has effect when items are wrapped to multiple lines using flex wrap.
/// </summary>
/// <exception cref="InvalidEnumArgumentException">Thrown when using invalid arguments that are enumerators.</exception>
/// <since_tizen> 6 </since_tizen>
public AlignmentType Alignment
{
get => GetFlexAlignment();
set
{
if (value < AlignmentType.Auto || value > AlignmentType.Stretch)
throw new InvalidEnumArgumentException(nameof(Alignment));
Interop.FlexLayout.FlexLayout_SetFlexAlignment(swigCPtr, (int)value);
RequestLayout();
}
}
/// <summary>
/// Gets/Sets the alignment of the layout items.
/// Items alignment describes how to align children along the cross axis of their container.<br/>
/// Align items is very similar to <see cref="Justification"/> but instead of applying to the main axis, align items applies to the cross axis.
/// </summary>
/// <exception cref="InvalidEnumArgumentException">Thrown when using invalid arguments that are enumerators.</exception>
/// <since_tizen> 6 </since_tizen>
public AlignmentType ItemsAlignment
{
get => GetFlexItemsAlignment();
set
{
if (value < AlignmentType.Auto || value > AlignmentType.Stretch)
throw new InvalidEnumArgumentException(nameof(ItemsAlignment));
Interop.FlexLayout.FlexLayout_SetFlexItemsAlignment(swigCPtr, (int)value);
RequestLayout();
}
}
/// <summary>
/// Enumeration for the direction of the main axis in the flex container.
/// This determines the direction that flex items are laid out in the flex container.
/// </summary>
/// <since_tizen> 6 </since_tizen>
public enum FlexDirection
{
/// <summary>
/// The flexible items are displayed vertically as a column
/// </summary>
Column,
/// <summary>
/// The flexible items are displayed vertically as a column, but in reverse order
/// </summary>
ColumnReverse,
/// <summary>
/// The flexible items are displayed horizontally as a row
/// </summary>
Row,
/// <summary>
/// The flexible items are displayed horizontally as a row, but in reverse order
/// </summary>
RowReverse
}
/// <summary>
/// Enumeration for the alignment of the flex items when the items do not use all available space on the main-axis.
/// </summary>
/// <since_tizen> 6 </since_tizen>
public enum FlexJustification
{
/// <summary>
/// Items are positioned at the beginning of the container.
/// </summary>
FlexStart,
/// <summary>
/// Items are positioned at the center of the container.
/// </summary>
Center,
/// <summary>
/// Items are positioned at the end of the container.
/// </summary>
FlexEnd,
/// <summary>
/// Items are positioned with equal space between the lines.
/// </summary>
SpaceBetween,
/// <summary>
/// Items are positioned with equal space before, and after the lines.<br/>
/// </summary>
SpaceAround,
/// <summary>
/// Items are positioned with equal space before, between, and after the lines.<br/>
/// Compared to <see cref="FlexJustification.SpaceBetween"/> using <see cref="FlexJustification.SpaceEvenly"/>
/// will result in space being distributed to the beginning of the first child and end of the last child.
/// </summary>
[EditorBrowsable(EditorBrowsableState.Never)]
SpaceEvenly
}
/// <summary>
/// Enumeration for the wrap type of the flex container when there is no enough room for all the items on one flex line.
/// </summary>
/// <since_tizen> 6 </since_tizen>
public enum FlexWrapType
{
/// <summary>
/// Flex items laid out in single line (shrunk to fit the flex container along the main axis)
/// </summary>
NoWrap,
/// <summary>
/// Flex items laid out in multiple lines if needed
/// </summary>
Wrap
}
/// <summary>
/// Enumeration for the alignment of the flex items or lines when the items or lines do not use all the available space on the cross-axis.
/// </summary>
/// <since_tizen> 6 </since_tizen>
public enum AlignmentType
{
/// <summary>
/// Inherits the same alignment from the parent
/// </summary>
Auto,
/// <summary>
/// At the beginning of the container
/// </summary>
FlexStart,
/// <summary>
/// At the center of the container
/// </summary>
Center,
/// <summary>
/// At the end of the container
/// </summary>
FlexEnd,
/// <summary>
/// Stretch to fit the container
/// </summary>
Stretch
}
/// <summary>
/// Enumeration for the position type of the flex item how it is positioned within its parent.
/// </summary>
/// <since_tizen> 8 </since_tizen>
public enum PositionType
{
/// <summary>
/// Flex items laid out relatively.
/// </summary>
Relative,
/// <summary>
/// Flex items laid out absolutely.
/// </summary>
Absolute
}
private void measureChild(global::System.IntPtr childPtr, float width, int measureModeWidth, float height, int measureModeHeight, out MeasuredSize measureSize)
{
// We need to measure child layout
View child = Registry.GetManagedBaseHandleFromNativePtr(childPtr) as View;
if (child == null)
{
measureSize.width = 0;
measureSize.height = 0;
return;
}
LayoutItem childLayout = child.Layout;
MeasureSpecification childWidthMeasureSpec = GetChildMeasureSpecification(
new MeasureSpecification(
new LayoutLength(parentMeasureSpecificationWidth.Size - (child.Margin.Start + child.Margin.End)),
parentMeasureSpecificationWidth.Mode),
new LayoutLength(Padding.Start + Padding.End),
new LayoutLength(child.WidthSpecification));
MeasureSpecification childHeightMeasureSpec = GetChildMeasureSpecification(
new MeasureSpecification(
new LayoutLength(parentMeasureSpecificationHeight.Size - (child.Margin.Top + child.Margin.Bottom)),
parentMeasureSpecificationHeight.Mode),
new LayoutLength(Padding.Top + Padding.Bottom),
new LayoutLength(child.HeightSpecification));
childLayout.Measure(childWidthMeasureSpec, childHeightMeasureSpec);
measureSize.width = childLayout.MeasuredWidth.Size.AsRoundedValue();
measureSize.height = childLayout.MeasuredHeight.Size.AsRoundedValue();
}
void InsertChild(LayoutItem child)
{
// Store created node for child
IntPtr childPtr = Interop.FlexLayout.FlexLayout_AddChildWithMargin(swigCPtr, View.getCPtr(child.Owner), Extents.getCPtr(child.Owner.Margin), measureChildDelegate, LayoutChildren.Count - 1);
HandleRef childHandleRef = new HandleRef(child.Owner, childPtr);
SetAttachedValue(child.Owner, FlexItemProperty, childHandleRef);
}
/// <summary>
/// Callback when child is added to container.<br />
/// Derived classes can use this to set their own child properties on the child layout's owner.<br />
/// </summary>
/// <param name="child">The Layout child.</param>
/// <since_tizen> 6 </since_tizen>
protected override void OnChildAdd(LayoutItem child)
{
InsertChild(child);
}
/// <summary>
/// Callback when child is removed from container.<br />
/// </summary>
/// <param name="child">The Layout child.</param>
/// <since_tizen> 6 </since_tizen>
protected override void OnChildRemove(LayoutItem child)
{
// When child View is removed from it's parent View (that is a Layout) then remove it from the layout too.
// FlexLayout refers to the child as a View not LayoutItem.
Interop.FlexLayout.FlexLayout_RemoveChild(swigCPtr, child.Owner.SwigCPtr);
}
/// <summary>
/// Measure the layout and its content to determine the measured width and the measured height.<br />
/// </summary>
/// <param name="widthMeasureSpec">horizontal space requirements as imposed by the parent.</param>
/// <param name="heightMeasureSpec">vertical space requirements as imposed by the parent.</param>
/// <since_tizen> 6 </since_tizen>
protected override void OnMeasure(MeasureSpecification widthMeasureSpec, MeasureSpecification heightMeasureSpec)
{
bool isLayoutRtl = Owner.LayoutDirection == ViewLayoutDirectionType.RTL;
Extents padding = Owner.Padding;
Extents margin = Owner.Margin;
Interop.FlexLayout.FlexLayout_SetMargin(swigCPtr, Extents.getCPtr(margin));
Interop.FlexLayout.FlexLayout_SetPadding(swigCPtr, Extents.getCPtr(padding));
float width = FlexUndefined; // Behaves as WrapContent (Flex Auto)
float height = FlexUndefined; // Behaves as WrapContent (Flex Auto)
if (widthMeasureSpec.Mode == MeasureSpecification.ModeType.Exactly || widthMeasureSpec.Mode == MeasureSpecification.ModeType.AtMost)
{
width = widthMeasureSpec.Size.AsDecimal();
}
if (heightMeasureSpec.Mode == MeasureSpecification.ModeType.Exactly || heightMeasureSpec.Mode == MeasureSpecification.ModeType.AtMost)
{
height = heightMeasureSpec.Size.AsDecimal();
}
// Save measureSpec to measure children.
// In other Layout, we can pass it as parameter to measuring child but in FlexLayout we can't
// because measurChild function is called by native callback.
parentMeasureSpecificationWidth = widthMeasureSpec;
parentMeasureSpecificationHeight = heightMeasureSpec;
// Assign child properties
for (int i = 0; i < LayoutChildren.Count; i++)
{
LayoutItem layoutItem = LayoutChildren[i];
View Child = layoutItem?.Owner;
HandleRef childHandleRef = (HandleRef)Child.GetValue(FlexItemProperty);
if (childHandleRef.Handle == IntPtr.Zero || Child == null)
continue;
AlignmentType flexAlignemnt = GetFlexAlignmentSelf(Child);
PositionType flexPosition = GetFlexPositionType(Child);
float flexAspectRatio = GetFlexAspectRatio(Child);
float flexBasis = GetFlexBasis(Child);
float flexShrink = GetFlexShrink(Child);
float flexGrow = GetFlexGrow(Child);
Interop.FlexLayout.FlexLayout_SetFlexAlignmentSelf(childHandleRef, (int)flexAlignemnt);
Interop.FlexLayout.FlexLayout_SetFlexPositionType(childHandleRef, (int)flexPosition);
Interop.FlexLayout.FlexLayout_SetFlexAspectRatio(childHandleRef, flexAspectRatio);
Interop.FlexLayout.FlexLayout_SetFlexBasis(childHandleRef, flexBasis);
Interop.FlexLayout.FlexLayout_SetFlexShrink(childHandleRef, flexShrink);
Interop.FlexLayout.FlexLayout_SetFlexGrow(childHandleRef, flexGrow);
}
Interop.FlexLayout.FlexLayout_CalculateLayout(swigCPtr, width, height, isLayoutRtl);
LayoutLength flexLayoutWidth = new LayoutLength(Interop.FlexLayout.FlexLayout_GetWidth(swigCPtr));
LayoutLength flexLayoutHeight = new LayoutLength(Interop.FlexLayout.FlexLayout_GetHeight(swigCPtr));
Debug.WriteLineIf(LayoutDebugFlex, "FlexLayout OnMeasure width:" + flexLayoutWidth.AsRoundedValue()
+ " height:" + flexLayoutHeight.AsRoundedValue());
SetMeasuredDimensions(GetDefaultSize(flexLayoutWidth, widthMeasureSpec),
GetDefaultSize(flexLayoutHeight, heightMeasureSpec));
}
/// <summary>
/// Assign a size and position to each of its children.<br />
/// </summary>
/// <param name="changed">This is a new size or position for this layout.</param>
/// <param name="left">Left position, relative to parent.</param>
/// <param name="top"> Top position, relative to parent.</param>
/// <param name="right">Right position, relative to parent.</param>
/// <param name="bottom">Bottom position, relative to parent.</param>
/// <since_tizen> 6 </since_tizen>
protected override void OnLayout(bool changed, LayoutLength left, LayoutLength top, LayoutLength right, LayoutLength bottom)
{
bool isLayoutRtl = Owner.LayoutDirection == ViewLayoutDirectionType.RTL;
LayoutLength width = right - left;
LayoutLength height = bottom - top;
// Call to FlexLayout implementation to calculate layout values for later retrieval.
Interop.FlexLayout.FlexLayout_CalculateLayout(swigCPtr, width.AsDecimal(), height.AsDecimal(), isLayoutRtl);
int count = LayoutChildren.Count;
for (int childIndex = 0; childIndex < count; childIndex++)
{
LayoutItem childLayout = LayoutChildren[childIndex];
if (childLayout != null)
{
// Get the frame for the child, start, top, end, bottom.
Vector4 frame = new Vector4(Interop.FlexLayout.FlexLayout_GetNodeFrame(swigCPtr, childIndex), true);
childLayout.Layout(new LayoutLength(frame.X), new LayoutLength(frame.Y), new LayoutLength(frame.Z), new LayoutLength(frame.W));
}
}
}
} // FLexlayout
} // namesspace Tizen.NUI