[NUI]Fix build warning[CA1001]
[platform/core/csapi/tizenfx.git] / src / Tizen.NUI / src / public / Layouting / FlexLayout.cs
1 /* Copyright (c) 2020 Samsung Electronics Co., Ltd.
2  *
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  *
15  */
16
17 using System;
18 using System.ComponentModel;
19 using Tizen.NUI.BaseComponents;
20 using System.Runtime.InteropServices;
21 using System.Diagnostics;
22 using Tizen.NUI.Binding;
23
24 namespace Tizen.NUI
25 {
26     /// <summary>
27     /// This class implements a flex layout.
28     /// The flex layout implementation is based on open source Facebook Yoga layout engine.
29     /// For more information about the flex layout API and how to use it please refer to https://yogalayout.com/docs/
30     /// We implement the subset of the API in the class below.
31     /// </summary>
32     public class FlexLayout : LayoutGroup
33     {
34         /// <summary>
35         /// FlexItemProperty
36         /// </summary>
37         [EditorBrowsable(EditorBrowsableState.Never)]
38         internal static readonly BindableProperty FlexItemProperty = BindableProperty.CreateAttached("FlexItem", typeof(HandleRef), typeof(FlexLayout), default(HandleRef));
39
40         /// <summary>
41         /// FlexAlignmentSelfProperty
42         /// </summary>
43         [EditorBrowsable(EditorBrowsableState.Never)]
44         public static readonly BindableProperty FlexAlignmentSelfProperty = BindableProperty.CreateAttached("FlexAlignmentSelf", typeof(AlignmentType), typeof(FlexLayout), AlignmentType.Auto, validateValue: ValidateEnum((int)AlignmentType.Auto, (int)AlignmentType.Stretch), propertyChanged: OnChildPropertyChanged);
45
46         /// <summary>
47         /// FlexPositionTypeProperty
48         /// </summary>
49         [EditorBrowsable(EditorBrowsableState.Never)]
50         public static readonly BindableProperty FlexPositionTypeProperty = BindableProperty.CreateAttached("FlexPositionType", typeof(PositionType), typeof(FlexLayout), PositionType.Relative, validateValue: ValidateEnum((int)PositionType.Relative, (int)PositionType.Absolute),
51         propertyChanged: (bindable, oldValue, newValue) =>
52         {
53             if (bindable is View view)
54             {
55                 view.ExcludeLayouting = (PositionType)newValue == PositionType.Absolute;
56             }
57         },
58         defaultValueCreator: (bindable) =>
59         {
60             var view = (View)bindable;
61             if (view.ExcludeLayouting)
62                 return PositionType.Absolute;
63
64             return PositionType.Relative;
65         });
66
67
68         /// <summary>
69         /// AspectRatioProperty
70         /// </summary>
71         [EditorBrowsable(EditorBrowsableState.Never)]
72         public static readonly BindableProperty FlexAspectRatioProperty = BindableProperty.CreateAttached("FlexAspectRatio", typeof(float), typeof(FlexLayout), FlexUndefined, validateValue: (bindable, value) => (float)value > 0, propertyChanged: OnChildPropertyChanged);
73
74         /// <summary>
75         /// FlexBasisProperty
76         /// </summary>
77         [EditorBrowsable(EditorBrowsableState.Never)]
78         public static readonly BindableProperty FlexBasisProperty = BindableProperty.CreateAttached("FlexBasis", typeof(float), typeof(FlexLayout), FlexUndefined, validateValue: (bindable, value) => (float)value >= 0, propertyChanged: OnChildPropertyChanged);
79
80         /// <summary>
81         /// FlexShrinkProperty
82         /// </summary>
83         [EditorBrowsable(EditorBrowsableState.Never)]
84         public static readonly BindableProperty FlexShrinkProperty = BindableProperty.CreateAttached("FlexShrink", typeof(float), typeof(FlexLayout), 1.0f, validateValue: (bindable, value) => (float)value >= 0, propertyChanged: OnChildPropertyChanged);
85
86         /// <summary>
87         /// FlexGrowProperty
88         /// </summary>
89         [EditorBrowsable(EditorBrowsableState.Never)]
90         public static readonly BindableProperty FlexGrowProperty = BindableProperty.CreateAttached("FlexGrow", typeof(float), typeof(FlexLayout), FlexUndefined, validateValue: (bindable, value) => (float)value >= 0, propertyChanged: OnChildPropertyChanged);
91
92         private static bool LayoutDebugFlex = false; // Debug flag
93
94         private global::System.Runtime.InteropServices.HandleRef swigCPtr;
95         private bool swigCMemOwn;
96         private bool disposed;
97         private bool isDisposeQueued = false;
98         private bool disposedThis = false;
99
100         private MeasureSpecification parentMeasureSpecificationWidth;
101         private MeasureSpecification parentMeasureSpecificationHeight;
102
103         private IntPtr _rootFlex;  // Pointer to the unmanged flex layout class.
104
105         internal const float FlexUndefined = 10E20F; // Auto setting which is equivalent to WrapContent.
106
107         internal struct MeasuredSize
108         {
109             public MeasuredSize(float x, float y)
110             {
111                 width = x;
112                 height = y;
113             }
114             public float width;
115             public float height;
116         };
117
118         /// <summary>
119         /// Gets the alignment self of the child view.
120         /// </summary>
121         /// <seealso cref="SetFlexAlignmentSelf(View, AlignmentType)"/>
122         /// <param name="view">The child view.</param>
123         /// <returns> The value of alignment self.</returns>
124         /// <exception cref="ArgumentNullException">The <paramref name="view"/> cannot be null.</exception>
125         /// <since_tizen> 8 </since_tizen>
126         public static AlignmentType GetFlexAlignmentSelf(View view) => GetAttachedValue<AlignmentType>(view, FlexAlignmentSelfProperty);
127
128         /// <summary>
129         /// Gets the position type of the child view.
130         /// </summary>
131         /// <seealso cref="SetFlexPositionType(View, PositionType)"/>
132         /// <param name="view">The child view.</param>
133         /// <returns> The value of position type</returns>
134         /// <exception cref="ArgumentNullException">The <paramref name="view"/> cannot be null.</exception>
135         /// <since_tizen> 8 </since_tizen>
136         public static PositionType GetFlexPositionType(View view) => GetAttachedValue<PositionType>(view, FlexPositionTypeProperty);
137
138         /// <summary>
139         /// Gets the aspect ratio of the child view.
140         /// </summary>
141         /// <seealso cref="SetFlexAspectRatio(View, float)"/>
142         /// <param name="view">The child view.</param>
143         /// <returns> The value of aspect ratio</returns>
144         /// <exception cref="ArgumentNullException">The <paramref name="view"/> cannot be null.</exception>
145         /// <since_tizen> 8 </since_tizen>
146         public static float GetFlexAspectRatio(View view) => GetAttachedValue<float>(view, FlexAspectRatioProperty);
147
148         /// <summary>
149         /// Gets the basis of the child view.
150         /// </summary>
151         /// <seealso cref="SetFlexBasis(View, float)"/>
152         /// <param name="view">The child view.</param>
153         /// <returns> The value of basis</returns>
154         /// <exception cref="ArgumentNullException">The <paramref name="view"/> cannot be null.</exception>
155         /// <since_tizen> 8 </since_tizen>
156         public static float GetFlexBasis(View view) => GetAttachedValue<float>(view, FlexBasisProperty);
157
158         /// <summary>
159         /// Gets the shrink of the child view.
160         /// </summary>
161         /// <seealso cref="SetFlexShrink(View, float)"/>
162         /// <param name="view">The child view.</param>
163         /// <returns> The value of shrink</returns>
164         /// <exception cref="ArgumentNullException">The <paramref name="view"/> cannot be null.</exception>
165         /// <since_tizen> 8 </since_tizen>
166         public static float GetFlexShrink(View view) => GetAttachedValue<float>(view, FlexShrinkProperty);
167
168         /// <summary>
169         /// Gets the grow of the child view.
170         /// </summary>
171         /// <seealso cref="SetFlexGrow(View, float)"/>
172         /// <param name="view">The child view.</param>
173         /// <returns> The value of grow</returns>
174         /// <exception cref="ArgumentNullException">The <paramref name="view"/> cannot be null.</exception>
175         /// <since_tizen> 8 </since_tizen>
176         public static float GetFlexGrow(View view) => GetAttachedValue<float>(view, FlexGrowProperty);
177
178         /// <summary>
179         /// Sets the alignment self of the child view.<br/>
180         /// Alignment self has the same options and effect as <see cref="ItemsAlignment"/> but instead of affecting the children within a container,
181         /// you can apply this property to a single child to change its alignment within its parent.<br/>
182         /// Alignment self overrides any option set by the parent with <see cref="ItemsAlignment"/>.
183         /// </summary>
184         /// <param name="view">The child view.</param>
185         /// <param name="value">The value of alignment self.</param>
186         /// <exception cref="ArgumentNullException">The <paramref name="view"/> cannot be null.</exception>
187         /// <exception cref="ArgumentException">The <paramref name="value"/> should be <see cref="AlignmentType"/>.</exception>
188         /// <since_tizen> 8 </since_tizen>
189         public static void SetFlexAlignmentSelf(View view, AlignmentType value) => SetAttachedValue(view, FlexAlignmentSelfProperty, value);
190
191         /// <summary>
192         /// Sets the position type of the child view.<br/>
193         /// The position type of an element defines how it is positioned within its parent.
194         /// By default a child is positioned relatively. This means a child is positioned according to the normal flow of the layout,
195         /// and then offset relative to that position based on the values of <see cref="View.Margin"/>.<br/>
196         /// When positioned absolutely an element doesn't take part in the normal layout flow.
197         /// It is instead laid out independent of its siblings. The position is determined based on the <see cref="View.Margin"/>.
198         /// </summary>
199         /// <param name="view">The child view.</param>
200         /// <param name="value">The value of position type.</param>
201         /// <exception cref="ArgumentNullException">The <paramref name="view"/> cannot be null.</exception>
202         /// <exception cref="ArgumentException">The <paramref name="value"/> should be <see cref="PositionType"/>.</exception>
203         /// <since_tizen> 8 </since_tizen>
204         public static void SetFlexPositionType(View view, PositionType value) => SetAttachedValue(view, FlexPositionTypeProperty, value);
205
206         /// <summary>
207         /// Sets the aspect ratio of the child view.
208         /// Aspect ratio Defines as the ratio between the width and the height of a node
209         /// e.g. if a node has an aspect ratio of 2 then its width is twice the size of its height.<br/>
210         /// Aspect ratio accepts any floating point value > 0. this has higher priority than flex grow.
211         /// </summary>
212         /// <param name="view">The child view.</param>
213         /// <param name="value">The value of aspect ratio</param>
214         /// <exception cref="ArgumentNullException">The <paramref name="view"/> cannot be null.</exception>
215         /// <exception cref="ArgumentException">The <paramref name="value"/> cannot be less than or equal to 0.0f.</exception>
216         /// <since_tizen> 8 </since_tizen>
217         public static void SetFlexAspectRatio(View view, float value) => SetAttachedValue(view, FlexAspectRatioProperty, value);
218
219         /// <summary>
220         /// Sets the flex basis of the child view.
221         /// Flex basis is an axis-independent way of providing the default size of an item along the main axis.<br/>
222         /// 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"/>
223         /// or setting the height of a child if its parent is a container with <see cref="FlexDirection.Column"/>.<br/>
224         /// 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.
225         /// </summary>
226         /// <param name="view">The child view.</param>
227         /// <param name="value">The value of basis</param>
228         /// <exception cref="ArgumentNullException">The <paramref name="view"/> cannot be null.</exception>
229         /// <exception cref="ArgumentException">The <paramref name="value"/> cannot be less than 0.0f.</exception>
230         /// <since_tizen> 8 </since_tizen>
231         public static void SetFlexBasis(View view, float value) => SetAttachedValue(view, FlexBasisProperty, value);
232
233         /// <summary>
234         /// Sets the flex shrink of the child view.
235         /// 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/>
236         /// 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.
237         /// These two properties also work well together by allowing children to grow and shrink as needed.<br/>
238         /// 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.
239         /// </summary>
240         /// <param name="view">The child view.</param>
241         /// <param name="value">The value of shrink</param>
242         /// <exception cref="ArgumentNullException">The <paramref name="view"/> cannot be null.</exception>
243         /// <exception cref="ArgumentException">The <paramref name="value"/> cannot be less than 0.0f.</exception>
244         /// <since_tizen> 8 </since_tizen>
245         public static void SetFlexShrink(View view, float value) => SetAttachedValue(view, FlexShrinkProperty, value);
246
247         /// <summary>
248         /// Sets the grow of the child view.
249         /// Flex grow describes how any space within a container should be distributed among its children along the main axis.
250         /// After laying out its children, a container will distribute any remaining space according to the flex grow values specified by its children.<br/>
251         /// Flex grow accepts any floating point value >= 0, with 0 being the default value.<br/>
252         /// A container will distribute any remaining space among its children weighted by the child's flex grow value.
253         /// </summary>
254         /// <param name="view">The child view.</param>
255         /// <param name="value">The value of flex</param>
256         /// <exception cref="ArgumentNullException">The <paramref name="view"/> cannot be null.</exception>
257         /// <exception cref="ArgumentException">The <paramref name="value"/> cannot be less than 0.0f.</exception>
258         /// <since_tizen> 8 </since_tizen>
259         public static void SetFlexGrow(View view, float value) => SetAttachedValue(view, FlexGrowProperty, value);
260
261         [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
262         internal delegate void ChildMeasureCallback(global::System.IntPtr child, float width, int measureModeWidth, float height, int measureModeHeight, out MeasuredSize measureSize);
263
264         event ChildMeasureCallback measureChildDelegate; // Stores a delegate to the child measure callback. Used for all children of this FlexLayout.
265
266         internal FlexLayout(global::System.IntPtr cPtr, bool cMemoryOwn)
267         {
268             swigCMemOwn = cMemoryOwn;
269             swigCPtr = new global::System.Runtime.InteropServices.HandleRef(this, cPtr);
270             _rootFlex = Interop.FlexLayout.New();
271             measureChildDelegate = new ChildMeasureCallback(measureChild);
272         }
273
274         internal static global::System.Runtime.InteropServices.HandleRef getCPtr(FlexLayout obj)
275         {
276             return (obj == null) ? new global::System.Runtime.InteropServices.HandleRef(null, global::System.IntPtr.Zero) : obj.swigCPtr;
277         }
278
279         /// <summary>
280         /// Hidden API (Inhouse API).
281         /// Destructor.
282         /// </summary>
283         [EditorBrowsable(EditorBrowsableState.Never)]
284         ~FlexLayout() => Dispose(false);
285
286         /// <inheritdoc/>
287         /// <since_tizen> 6 </since_tizen>
288         public new void Dispose()
289         {
290             Dispose(true);
291             System.GC.SuppressFinalize(this);
292         }
293
294         /// <summary>
295         /// Hidden API (Inhouse API).
296         /// Dispose. 
297         /// </summary>
298         /// <remarks>
299         /// Following the guide of https://docs.microsoft.com/en-us/dotnet/standard/garbage-collection/implementing-dispose.
300         /// This will replace "protected virtual void Dispose(DisposeTypes type)" which is exactly same in functionality.
301         /// </remarks>
302         /// <param name="disposing">true in order to free managed objects</param>
303         // Protected implementation of Dispose pattern.
304         [EditorBrowsable(EditorBrowsableState.Never)]
305         protected override void Dispose(bool disposing)
306         {
307             if (disposedThis)
308             {
309                 return;
310             }
311
312             if (disposing)
313             {
314                 // TODO: dispose managed state (managed objects).
315                 // Explicit call. user calls Dispose()
316
317                 //Throw excpetion if Dispose() is called in separate thread.
318                 if (!Window.IsInstalled())
319                 {
320                     throw new System.InvalidOperationException("This API called from separate thread. This API must be called from MainThread.");
321                 }
322
323                 if (isDisposeQueued)
324                 {
325                     Dispose(DisposeTypes.Implicit);
326                 }
327                 else
328                 {
329                     Dispose(DisposeTypes.Explicit);
330                 }
331             }
332             else
333             {
334                 // Implicit call. user doesn't call Dispose(), so this object is added into DisposeQueue to be disposed automatically.
335                 if (!isDisposeQueued)
336                 {
337                     isDisposeQueued = true;
338                     DisposeQueue.Instance.Add(this);
339                 }
340             }
341
342             // TODO: free unmanaged resources (unmanaged objects) and override a finalizer below.
343             // TODO: set large fields to null.
344             disposedThis = true;
345             base.Dispose(disposing);
346         }
347
348         /// <inheritdoc/>
349         /// <since_tizen> 6 </since_tizen>
350         protected virtual void Dispose(DisposeTypes type)
351         {
352             if (disposed)
353             {
354                 return;
355             }
356
357             if (type == DisposeTypes.Explicit)
358             {
359                 //Called by User
360                 //Release your own managed resources here.
361                 //You should release all of your own disposable objects here.
362             }
363
364             //Release your own unmanaged resources here.
365             //You should not access any managed member here except static instance.
366             //because the execution order of Finalizes is non-deterministic.
367             if (swigCPtr.Handle != global::System.IntPtr.Zero)
368             {
369                 if (swigCMemOwn)
370                 {
371                     swigCMemOwn = false;
372                     ReleaseSwigCPtr(swigCPtr);
373                 }
374                 swigCPtr = new global::System.Runtime.InteropServices.HandleRef(null, global::System.IntPtr.Zero);
375             }
376
377             disposed = true;
378         }
379
380         /// <summary>
381         /// Hidden API (Inhouse API).
382         /// Release swigCPtr.
383         /// </summary>
384         /// This will not be public opened.
385         [EditorBrowsable(EditorBrowsableState.Never)]
386         protected virtual void ReleaseSwigCPtr(System.Runtime.InteropServices.HandleRef swigCPtr)
387         {
388             Interop.FlexLayout.DeleteFlexLayout(swigCPtr);
389         }
390
391         /// <summary>
392         /// Creates a FlexLayout object.
393         /// </summary>
394         /// <since_tizen> 6 </since_tizen>
395         public FlexLayout() : this(Interop.FlexLayout.New(), true)
396         {
397             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
398         }
399
400         internal static FlexLayout DownCast(BaseHandle handle)
401         {
402             FlexLayout ret = new FlexLayout(Interop.FlexLayout.DownCast(BaseHandle.getCPtr(handle)), true);
403             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
404             return ret;
405         }
406
407         internal FlexLayout(FlexLayout other) : this(Interop.FlexLayout.NewFlexLayout(FlexLayout.getCPtr(other)), true)
408         {
409             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
410         }
411
412         internal FlexLayout.AlignmentType GetFlexAlignment()
413         {
414             FlexLayout.AlignmentType ret = (FlexLayout.AlignmentType)Interop.FlexLayout.GetFlexAlignment(swigCPtr);
415             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
416             return ret;
417         }
418
419         internal FlexLayout.AlignmentType GetFlexItemsAlignment()
420         {
421             FlexLayout.AlignmentType ret = (FlexLayout.AlignmentType)Interop.FlexLayout.GetFlexItemsAlignment(swigCPtr);
422             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
423             return ret;
424         }
425
426         /// <summary>
427         /// Gets/Sets the flex direction in the layout.
428         /// The direction of the main-axis which determines the direction that flex items are laid out.
429         /// </summary>
430         /// <exception cref="InvalidEnumArgumentException">Thrown when using invalid arguments that are enumerators.</exception>
431         /// <since_tizen> 6 </since_tizen>
432         public FlexDirection Direction
433         {
434             get => (FlexDirection)Interop.FlexLayout.GetFlexDirection(swigCPtr);
435             set
436             {
437                 if (value < FlexDirection.Column || value > FlexDirection.RowReverse)
438                     throw new InvalidEnumArgumentException(nameof(Direction));
439
440                 Interop.FlexLayout.SetFlexDirection(swigCPtr, (int)value);
441                 RequestLayout();
442             }
443         }
444
445         /// <summary>
446         /// Gets/Sets the justification in the layout.
447         /// Justify content describes how to align children within the main axis of their container.<br/>
448         /// 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"/>
449         /// or vertically within a container with <see cref="Direction"/> set to <see cref="FlexDirection.Column"/>.
450         /// </summary>
451         /// <exception cref="InvalidEnumArgumentException">Thrown when using invalid arguments that are enumerators.</exception>
452         /// <since_tizen> 6 </since_tizen>
453         public FlexJustification Justification
454         {
455             get => (FlexJustification)Interop.FlexLayout.GetFlexJustification(swigCPtr);
456             set
457             {
458                 if (value < FlexJustification.FlexStart || value > FlexJustification.SpaceAround)
459                     throw new InvalidEnumArgumentException(nameof(Justification));
460
461                 Interop.FlexLayout.SetFlexJustification(swigCPtr, (int)value);
462                 RequestLayout();
463             }
464         }
465
466         /// <summary>
467         /// Gets/Sets the wrap in the layout.
468         /// 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/>
469         /// By default children are forced into a single line (which can shrink elements).<br/>
470         /// 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/>
471         /// When wrapping lines <see cref="Alignment"/> can be used to specify how the lines are placed in the container.
472         /// </summary>
473         /// <exception cref="InvalidEnumArgumentException">Thrown when using invalid arguments that are enumerators.</exception>
474         /// <since_tizen> 6 </since_tizen>
475         public FlexWrapType WrapType
476         {
477             get => (FlexWrapType)Interop.FlexLayout.GetFlexWrap(swigCPtr);
478             set
479             {
480                 if (value != FlexWrapType.NoWrap && value != FlexWrapType.Wrap)
481                     throw new InvalidEnumArgumentException(nameof(WrapType));
482
483                 Interop.FlexLayout.SetFlexWrap(swigCPtr, (int)value);
484                 RequestLayout();
485
486             }
487         }
488
489         /// <summary>
490         /// Gets/Sets the alignment of the layout content.
491         /// Alignment defines the distribution of lines along the cross-axis.<br/>
492         /// This only has effect when items are wrapped to multiple lines using flex wrap.
493         /// </summary>
494         /// <exception cref="InvalidEnumArgumentException">Thrown when using invalid arguments that are enumerators.</exception>
495         /// <since_tizen> 6 </since_tizen>
496         public AlignmentType Alignment
497         {
498             get => GetFlexAlignment();
499             set
500             {
501                 if (value < AlignmentType.Auto || value > AlignmentType.Stretch)
502                     throw new InvalidEnumArgumentException(nameof(Alignment));
503
504                 Interop.FlexLayout.SetFlexAlignment(swigCPtr, (int)value);
505                 RequestLayout();
506             }
507         }
508
509         /// <summary>
510         /// Gets/Sets the alignment of the layout items.
511         /// Items alignment describes how to align children along the cross axis of their container.<br/>
512         /// Align items is very similar to <see cref="Justification"/> but instead of applying to the main axis, align items applies to the cross axis.
513         /// </summary>
514         /// <exception cref="InvalidEnumArgumentException">Thrown when using invalid arguments that are enumerators.</exception>
515         /// <since_tizen> 6 </since_tizen>
516         public AlignmentType ItemsAlignment
517         {
518             get => GetFlexItemsAlignment();
519             set
520             {
521                 if (value < AlignmentType.Auto || value > AlignmentType.Stretch)
522                     throw new InvalidEnumArgumentException(nameof(ItemsAlignment));
523
524                 Interop.FlexLayout.SetFlexItemsAlignment(swigCPtr, (int)value);
525                 RequestLayout();
526             }
527         }
528
529         /// <summary>
530         /// Enumeration for the direction of the main axis in the flex container.
531         /// This determines the direction that flex items are laid out in the flex container.
532         /// </summary>
533         /// <since_tizen> 6 </since_tizen>
534         public enum FlexDirection
535         {
536             /// <summary>
537             /// The flexible items are displayed vertically as a column
538             /// </summary>
539             Column,
540             /// <summary>
541             /// The flexible items are displayed vertically as a column, but in reverse order
542             /// </summary>
543             ColumnReverse,
544             /// <summary>
545             /// The flexible items are displayed horizontally as a row
546             /// </summary>
547             Row,
548             /// <summary>
549             /// The flexible items are displayed horizontally as a row, but in reverse order
550             /// </summary>
551             RowReverse
552         }
553
554         /// <summary>
555         /// Enumeration for the alignment of the flex items when the items do not use all available space on the main-axis.
556         /// </summary>
557         /// <since_tizen> 6 </since_tizen>
558         public enum FlexJustification
559         {
560             /// <summary>
561             /// Items are positioned at the beginning of the container.
562             /// </summary>
563             FlexStart,
564             /// <summary>
565             /// Items are positioned at the center of the container.
566             /// </summary>
567             Center,
568             /// <summary>
569             /// Items are positioned at the end of the container.
570             /// </summary>
571             FlexEnd,
572             /// <summary>
573             /// Items are positioned with equal space between the lines.
574             /// </summary>
575             SpaceBetween,
576             /// <summary>
577             /// Items are positioned with equal space before, between, and after the lines.<br/>
578             /// Compared to <see cref="FlexJustification.SpaceBetween"/> using <see cref="FlexJustification.SpaceAround"/>
579             /// will result in space being distributed to the beginning of the first child and end of the last child.
580             /// </summary>
581             SpaceAround
582         }
583
584         /// <summary>
585         /// Enumeration for the wrap type of the flex container when there is no enough room for all the items on one flex line.
586         /// </summary>
587         /// <since_tizen> 6 </since_tizen>
588         public enum FlexWrapType
589         {
590             /// <summary>
591             /// Flex items laid out in single line (shrunk to fit the flex container along the main axis)
592             /// </summary>
593             NoWrap,
594             /// <summary>
595             /// Flex items laid out in multiple lines if needed
596             /// </summary>
597             Wrap
598         }
599
600         /// <summary>
601         /// 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.
602         /// </summary>
603         /// <since_tizen> 6 </since_tizen>
604         public enum AlignmentType
605         {
606             /// <summary>
607             /// Inherits the same alignment from the parent
608             /// </summary>
609             Auto,
610             /// <summary>
611             /// At the beginning of the container
612             /// </summary>
613             FlexStart,
614             /// <summary>
615             /// At the center of the container
616             /// </summary>
617             Center,
618             /// <summary>
619             /// At the end of the container
620             /// </summary>
621             FlexEnd,
622             /// <summary>
623             /// Stretch to fit the container
624             /// </summary>
625             Stretch
626         }
627
628         /// <summary>
629         /// Enumeration for the position type of the flex item how it is positioned within its parent.
630         /// </summary>
631         /// <since_tizen> 8 </since_tizen>
632         public enum PositionType
633         {
634             /// <summary>
635             /// Flex items laid out relatively.
636             /// </summary>
637             Relative,
638             /// <summary>
639             /// Flex items laid out absolutely.
640             /// </summary>
641             Absolute
642         }
643
644         private void measureChild(global::System.IntPtr childPtr, float width, int measureModeWidth, float height, int measureModeHeight, out MeasuredSize measureSize)
645         {
646             // We need to measure child layout
647             View child = Registry.GetManagedBaseHandleFromNativePtr(childPtr) as View;
648             // independent child will be measured in LayoutGroup.OnMeasureIndependentChildren().
649             if (child?.ExcludeLayouting ?? true)
650             {
651                 measureSize.width = 0;
652                 measureSize.height = 0;
653                 return;
654             }
655
656             LayoutItem childLayout = child.Layout;
657
658             MeasureSpecification childWidthMeasureSpec = GetChildMeasureSpecification(
659                                     new MeasureSpecification(
660                                         new LayoutLength(parentMeasureSpecificationWidth.Size - (child.Margin.Start + child.Margin.End)),
661                                         parentMeasureSpecificationWidth.Mode),
662                                     new LayoutLength(Padding.Start + Padding.End),
663                                     new LayoutLength(child.WidthSpecification));
664
665             MeasureSpecification childHeightMeasureSpec = GetChildMeasureSpecification(
666                                     new MeasureSpecification(
667                                         new LayoutLength(parentMeasureSpecificationHeight.Size - (child.Margin.Top + child.Margin.Bottom)),
668                                         parentMeasureSpecificationHeight.Mode),
669                                     new LayoutLength(Padding.Top + Padding.Bottom),
670                                     new LayoutLength(child.HeightSpecification));
671
672             childLayout.Measure(childWidthMeasureSpec, childHeightMeasureSpec);
673
674             measureSize.width = childLayout.MeasuredWidth.Size.AsRoundedValue();
675             measureSize.height = childLayout.MeasuredHeight.Size.AsRoundedValue();
676         }
677
678         void InsertChild(LayoutItem child)
679         {
680             // Store created node for child
681             IntPtr childPtr = Interop.FlexLayout.AddChildWithMargin(swigCPtr, View.getCPtr(child.Owner), Extents.getCPtr(child.Owner.Margin), measureChildDelegate, LayoutChildren.Count - 1);
682             HandleRef childHandleRef = new HandleRef(child.Owner, childPtr);
683             SetAttachedValue(child.Owner, FlexItemProperty, childHandleRef);
684         }
685
686         /// <summary>
687         /// Callback when child is added to container.<br />
688         /// Derived classes can use this to set their own child properties on the child layout's owner.<br />
689         /// </summary>
690         /// <param name="child">The Layout child.</param>
691         /// <exception cref="ArgumentNullException"> Thrown when child is null. </exception>
692         /// <since_tizen> 6 </since_tizen>
693         protected override void OnChildAdd(LayoutItem child)
694         {
695             if (null == child)
696             {
697                 throw new ArgumentNullException(nameof(child));
698             }
699             InsertChild(child);
700         }
701
702         /// <summary>
703         /// Callback when child is removed from container.<br />
704         /// </summary>
705         /// <param name="child">The Layout child.</param>
706         /// <since_tizen> 6 </since_tizen>
707         protected override void OnChildRemove(LayoutItem child)
708         {
709             // When child View is removed from it's parent View (that is a Layout) then remove it from the layout too.
710             // FlexLayout refers to the child as a View not LayoutItem.
711             Interop.FlexLayout.RemoveChild(swigCPtr, child);
712         }
713
714         /// <summary>
715         /// Measure the layout and its content to determine the measured width and the measured height.<br />
716         /// </summary>
717         /// <param name="widthMeasureSpec">horizontal space requirements as imposed by the parent.</param>
718         /// <param name="heightMeasureSpec">vertical space requirements as imposed by the parent.</param>
719         /// <since_tizen> 6 </since_tizen>
720         protected override void OnMeasure(MeasureSpecification widthMeasureSpec, MeasureSpecification heightMeasureSpec)
721         {
722             bool isLayoutRtl = Owner.LayoutDirection == ViewLayoutDirectionType.RTL;
723             Extents padding = Owner.Padding;
724
725             Interop.FlexLayout.SetPadding(swigCPtr, Extents.getCPtr(padding));
726
727             float width = FlexUndefined; // Behaves as WrapContent (Flex Auto)
728             float height = FlexUndefined; // Behaves as WrapContent (Flex Auto)
729
730             if (widthMeasureSpec.Mode == MeasureSpecification.ModeType.Exactly || widthMeasureSpec.Mode == MeasureSpecification.ModeType.AtMost)
731             {
732                 width = widthMeasureSpec.Size.AsDecimal();
733             }
734
735             if (heightMeasureSpec.Mode == MeasureSpecification.ModeType.Exactly || heightMeasureSpec.Mode == MeasureSpecification.ModeType.AtMost)
736             {
737                 height = heightMeasureSpec.Size.AsDecimal();
738             }
739
740             // Save measureSpec to measure children.
741             // In other Layout, we can pass it as parameter to measuring child but in FlexLayout we can't
742             // because measurChild function is called by native callback.
743             parentMeasureSpecificationWidth = widthMeasureSpec;
744             parentMeasureSpecificationHeight = heightMeasureSpec;
745
746             Extents zeroMargin = new Extents();
747
748             // Assign child properties
749             for (int i = 0; i < LayoutChildren.Count; i++)
750             {
751                 LayoutItem layoutItem = LayoutChildren[i];
752                 View Child = layoutItem?.Owner;
753                 HandleRef childHandleRef = (HandleRef)Child.GetValue(FlexItemProperty);
754                 if (childHandleRef.Handle == IntPtr.Zero || Child == null)
755                     continue;
756
757                 AlignmentType flexAlignemnt = GetFlexAlignmentSelf(Child);
758                 PositionType positionType = GetFlexPositionType(Child);
759                 float flexAspectRatio = GetFlexAspectRatio(Child);
760                 float flexBasis = GetFlexBasis(Child);
761                 float flexShrink = GetFlexShrink(Child);
762                 float flexGrow = GetFlexGrow(Child);
763                 Extents childMargin = Child.ExcludeLayouting ? zeroMargin : layoutItem.Margin;
764
765                 Interop.FlexLayout.SetMargin(childHandleRef, Extents.getCPtr(childMargin));
766                 Interop.FlexLayout.SetFlexAlignmentSelf(childHandleRef, (int)flexAlignemnt);
767                 Interop.FlexLayout.SetFlexPositionType(childHandleRef, (int)positionType);
768                 Interop.FlexLayout.SetFlexAspectRatio(childHandleRef, flexAspectRatio);
769                 Interop.FlexLayout.SetFlexBasis(childHandleRef, flexBasis);
770                 Interop.FlexLayout.SetFlexShrink(childHandleRef, flexShrink);
771                 Interop.FlexLayout.SetFlexGrow(childHandleRef, flexGrow);
772             }
773
774             Interop.FlexLayout.CalculateLayout(swigCPtr, width, height, isLayoutRtl);
775             zeroMargin.Dispose();
776
777             LayoutLength flexLayoutWidth = new LayoutLength(Interop.FlexLayout.GetWidth(swigCPtr));
778             LayoutLength flexLayoutHeight = new LayoutLength(Interop.FlexLayout.GetHeight(swigCPtr));
779
780             Debug.WriteLineIf(LayoutDebugFlex, "FlexLayout OnMeasure width:" + flexLayoutWidth.AsRoundedValue()
781                                                 + " height:" + flexLayoutHeight.AsRoundedValue());
782
783             SetMeasuredDimensions(GetDefaultSize(flexLayoutWidth, widthMeasureSpec),
784                                    GetDefaultSize(flexLayoutHeight, heightMeasureSpec));
785         }
786
787         /// <summary>
788         /// Assign a size and position to each of its children.<br />
789         /// </summary>
790         /// <param name="changed">This is a new size or position for this layout.</param>
791         /// <param name="left">Left position, relative to parent.</param>
792         /// <param name="top"> Top position, relative to parent.</param>
793         /// <param name="right">Right position, relative to parent.</param>
794         /// <param name="bottom">Bottom position, relative to parent.</param>
795         /// <since_tizen> 6 </since_tizen>
796         protected override void OnLayout(bool changed, LayoutLength left, LayoutLength top, LayoutLength right, LayoutLength bottom)
797         {
798
799             bool isLayoutRtl = Owner.LayoutDirection == ViewLayoutDirectionType.RTL;
800             LayoutLength width = right - left;
801             LayoutLength height = bottom - top;
802
803             // Call to FlexLayout implementation to calculate layout values for later retrieval.
804             Interop.FlexLayout.CalculateLayout(swigCPtr, width.AsDecimal(), height.AsDecimal(), isLayoutRtl);
805
806             for (int childIndex = 0; childIndex < LayoutChildren.Count; childIndex++)
807             {
808                 LayoutItem childLayout = LayoutChildren[childIndex];
809                 if (!childLayout?.Owner?.ExcludeLayouting ?? false)
810                 {
811                     // Get the frame for the child, start, top, end, bottom.
812                     Vector4 frame = new Vector4(Interop.FlexLayout.GetNodeFrame(swigCPtr, childIndex), true);
813                     childLayout.Layout(new LayoutLength(frame.X), new LayoutLength(frame.Y), new LayoutLength(frame.Z), new LayoutLength(frame.W));
814                 }
815             }
816         }
817     } // FLexlayout
818 } // namesspace Tizen.NUI