[NUI][SVACE] Fix SVACE issues
[platform/core/csapi/tizenfx.git] / src / Tizen.NUI / src / public / Layouting / FlexLayout.cs
1 /* Copyright (c) 2021 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 global::System.Runtime.InteropServices.HandleRef swigCPtr;
93         private bool swigCMemOwn;
94         private bool disposed;
95         private bool isDisposeQueued = false;
96
97         private MeasureSpecification parentMeasureSpecificationWidth;
98         private MeasureSpecification parentMeasureSpecificationHeight;
99
100         private IntPtr _rootFlex;  // Pointer to the unmanaged flex layout class.
101
102         internal const float FlexUndefined = 10E20F; // Auto setting which is equivalent to WrapContent.
103
104         internal struct MeasuredSize
105         {
106             public MeasuredSize(float x, float y)
107             {
108                 width = x;
109                 height = y;
110             }
111             public float width;
112             public float height;
113         };
114
115         /// <summary>
116         /// Gets the alignment self of the child view.
117         /// </summary>
118         /// <seealso cref="SetFlexAlignmentSelf(View, AlignmentType)"/>
119         /// <param name="view">The child view.</param>
120         /// <returns> The value of alignment self.</returns>
121         /// <exception cref="ArgumentNullException">The <paramref name="view"/> cannot be null.</exception>
122         /// <since_tizen> 8 </since_tizen>
123         public static AlignmentType GetFlexAlignmentSelf(View view) => GetAttachedValue<AlignmentType>(view, FlexAlignmentSelfProperty);
124
125         /// <summary>
126         /// Gets the position type of the child view.
127         /// </summary>
128         /// <seealso cref="SetFlexPositionType(View, PositionType)"/>
129         /// <param name="view">The child view.</param>
130         /// <returns> The value of position type</returns>
131         /// <exception cref="ArgumentNullException">The <paramref name="view"/> cannot be null.</exception>
132         /// <since_tizen> 8 </since_tizen>
133         public static PositionType GetFlexPositionType(View view) => GetAttachedValue<PositionType>(view, FlexPositionTypeProperty);
134
135         /// <summary>
136         /// Gets the aspect ratio of the child view.
137         /// </summary>
138         /// <seealso cref="SetFlexAspectRatio(View, float)"/>
139         /// <param name="view">The child view.</param>
140         /// <returns> The value of aspect ratio</returns>
141         /// <exception cref="ArgumentNullException">The <paramref name="view"/> cannot be null.</exception>
142         /// <since_tizen> 8 </since_tizen>
143         public static float GetFlexAspectRatio(View view) => GetAttachedValue<float>(view, FlexAspectRatioProperty);
144
145         /// <summary>
146         /// Gets the basis of the child view.
147         /// </summary>
148         /// <seealso cref="SetFlexBasis(View, float)"/>
149         /// <param name="view">The child view.</param>
150         /// <returns> The value of basis</returns>
151         /// <exception cref="ArgumentNullException">The <paramref name="view"/> cannot be null.</exception>
152         /// <since_tizen> 8 </since_tizen>
153         public static float GetFlexBasis(View view) => GetAttachedValue<float>(view, FlexBasisProperty);
154
155         /// <summary>
156         /// Gets the shrink of the child view.
157         /// </summary>
158         /// <seealso cref="SetFlexShrink(View, float)"/>
159         /// <param name="view">The child view.</param>
160         /// <returns> The value of shrink</returns>
161         /// <exception cref="ArgumentNullException">The <paramref name="view"/> cannot be null.</exception>
162         /// <since_tizen> 8 </since_tizen>
163         public static float GetFlexShrink(View view) => GetAttachedValue<float>(view, FlexShrinkProperty);
164
165         /// <summary>
166         /// Gets the grow of the child view.
167         /// </summary>
168         /// <seealso cref="SetFlexGrow(View, float)"/>
169         /// <param name="view">The child view.</param>
170         /// <returns> The value of grow</returns>
171         /// <exception cref="ArgumentNullException">The <paramref name="view"/> cannot be null.</exception>
172         /// <since_tizen> 8 </since_tizen>
173         public static float GetFlexGrow(View view) => GetAttachedValue<float>(view, FlexGrowProperty);
174
175         /// <summary>
176         /// Sets the alignment self of the child view.<br/>
177         /// Alignment self has the same options and effect as <see cref="ItemsAlignment"/> but instead of affecting the children within a container,
178         /// you can apply this property to a single child to change its alignment within its parent.<br/>
179         /// Alignment self overrides any option set by the parent with <see cref="ItemsAlignment"/>.
180         /// </summary>
181         /// <param name="view">The child view.</param>
182         /// <param name="value">The value of alignment self.</param>
183         /// <exception cref="ArgumentNullException">The <paramref name="view"/> cannot be null.</exception>
184         /// <exception cref="ArgumentException">The <paramref name="value"/> should be <see cref="AlignmentType"/>.</exception>
185         /// <since_tizen> 8 </since_tizen>
186         public static void SetFlexAlignmentSelf(View view, AlignmentType value) => SetAttachedValue(view, FlexAlignmentSelfProperty, value);
187
188         /// <summary>
189         /// Sets the position type of the child view.<br/>
190         /// The position type of an element defines how it is positioned within its parent.
191         /// By default a child is positioned relatively. This means a child is positioned according to the normal flow of the layout,
192         /// and then offset relative to that position based on the values of <see cref="View.Margin"/>.<br/>
193         /// When positioned absolutely an element doesn't take part in the normal layout flow.
194         /// It is instead laid out independent of its siblings. The position is determined based on the <see cref="View.Margin"/>.
195         /// </summary>
196         /// <param name="view">The child view.</param>
197         /// <param name="value">The value of position type.</param>
198         /// <exception cref="ArgumentNullException">The <paramref name="view"/> cannot be null.</exception>
199         /// <exception cref="ArgumentException">The <paramref name="value"/> should be <see cref="PositionType"/>.</exception>
200         /// <since_tizen> 8 </since_tizen>
201         public static void SetFlexPositionType(View view, PositionType value) => SetAttachedValue(view, FlexPositionTypeProperty, value);
202
203         /// <summary>
204         /// Sets the aspect ratio of the child view.
205         /// Aspect ratio Defines as the ratio between the width and the height of a node
206         /// e.g. if a node has an aspect ratio of 2 then its width is twice the size of its height.<br/>
207         /// Aspect ratio accepts any floating point value > 0. this has higher priority than flex grow.
208         /// </summary>
209         /// <param name="view">The child view.</param>
210         /// <param name="value">The value of aspect ratio</param>
211         /// <exception cref="ArgumentNullException">The <paramref name="view"/> cannot be null.</exception>
212         /// <exception cref="ArgumentException">The <paramref name="value"/> cannot be less than or equal to 0.0f.</exception>
213         /// <since_tizen> 8 </since_tizen>
214         public static void SetFlexAspectRatio(View view, float value) => SetAttachedValue(view, FlexAspectRatioProperty, value);
215
216         /// <summary>
217         /// Sets the flex basis of the child view.
218         /// Flex basis is an axis-independent way of providing the default size of an item along the main axis.<br/>
219         /// 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"/>
220         /// or setting the height of a child if its parent is a container with <see cref="FlexDirection.Column"/>.<br/>
221         /// 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.
222         /// </summary>
223         /// <param name="view">The child view.</param>
224         /// <param name="value">The value of basis</param>
225         /// <exception cref="ArgumentNullException">The <paramref name="view"/> cannot be null.</exception>
226         /// <exception cref="ArgumentException">The <paramref name="value"/> cannot be less than 0.0f.</exception>
227         /// <since_tizen> 8 </since_tizen>
228         public static void SetFlexBasis(View view, float value) => SetAttachedValue(view, FlexBasisProperty, value);
229
230         /// <summary>
231         /// Sets the flex shrink of the child view.
232         /// 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/>
233         /// 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.
234         /// These two properties also work well together by allowing children to grow and shrink as needed.<br/>
235         /// 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.
236         /// </summary>
237         /// <param name="view">The child view.</param>
238         /// <param name="value">The value of shrink</param>
239         /// <exception cref="ArgumentNullException">The <paramref name="view"/> cannot be null.</exception>
240         /// <exception cref="ArgumentException">The <paramref name="value"/> cannot be less than 0.0f.</exception>
241         /// <since_tizen> 8 </since_tizen>
242         public static void SetFlexShrink(View view, float value) => SetAttachedValue(view, FlexShrinkProperty, value);
243
244         /// <summary>
245         /// Sets the grow of the child view.
246         /// Flex grow describes how any space within a container should be distributed among its children along the main axis.
247         /// After laying out its children, a container will distribute any remaining space according to the flex grow values specified by its children.<br/>
248         /// Flex grow accepts any floating point value >= 0, with 0 being the default value.<br/>
249         /// A container will distribute any remaining space among its children weighted by the child's flex grow value.
250         /// </summary>
251         /// <param name="view">The child view.</param>
252         /// <param name="value">The value of flex</param>
253         /// <exception cref="ArgumentNullException">The <paramref name="view"/> cannot be null.</exception>
254         /// <exception cref="ArgumentException">The <paramref name="value"/> cannot be less than 0.0f.</exception>
255         /// <since_tizen> 8 </since_tizen>
256         public static void SetFlexGrow(View view, float value) => SetAttachedValue(view, FlexGrowProperty, value);
257
258         [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
259         internal delegate void ChildMeasureCallback(global::System.IntPtr child, float width, int measureModeWidth, float height, int measureModeHeight, out MeasuredSize measureSize);
260
261         event ChildMeasureCallback measureChildDelegate; // Stores a delegate to the child measure callback. Used for all children of this FlexLayout.
262
263         internal FlexLayout(global::System.IntPtr cPtr, bool cMemoryOwn)
264         {
265             swigCMemOwn = cMemoryOwn;
266             swigCPtr = new global::System.Runtime.InteropServices.HandleRef(this, cPtr);
267             _rootFlex = Interop.FlexLayout.New();
268             measureChildDelegate = new ChildMeasureCallback(measureChild);
269         }
270
271         internal static global::System.Runtime.InteropServices.HandleRef getCPtr(FlexLayout obj)
272         {
273             return (obj == null) ? new global::System.Runtime.InteropServices.HandleRef(null, global::System.IntPtr.Zero) : obj.swigCPtr;
274         }
275
276         /// <summary>
277         /// Hidden API (Inhouse API).
278         /// Destructor.
279         /// </summary>
280         [EditorBrowsable(EditorBrowsableState.Never)]
281         ~FlexLayout() => Dispose(false);
282
283         /// <inheritdoc/>
284         /// <since_tizen> 6 </since_tizen>
285         public new void Dispose()
286         {
287             Dispose(true);
288             System.GC.SuppressFinalize(this);
289         }
290
291         /// <summary>
292         /// Hidden API (Inhouse API).
293         /// Dispose. 
294         /// </summary>
295         /// <remarks>
296         /// Following the guide of https://docs.microsoft.com/en-us/dotnet/standard/garbage-collection/implementing-dispose.
297         /// This will replace "protected virtual void Dispose(DisposeTypes type)" which is exactly same in functionality.
298         /// </remarks>
299         /// <param name="disposing">true in order to free managed objects</param>
300         // Protected implementation of Dispose pattern.
301         [EditorBrowsable(EditorBrowsableState.Never)]
302         protected override void Dispose(bool disposing)
303         {
304             if (disposed)
305             {
306                 return;
307             }
308
309             if (disposing)
310             {
311                 // TODO: dispose managed state (managed objects).
312                 // Explicit call. user calls Dispose()
313
314                 //Throw exception if Dispose() is called in separate thread.
315                 if (!Window.IsInstalled())
316                 {
317                     throw new System.InvalidOperationException("This API called from separate thread. This API must be called from MainThread.");
318                 }
319
320                 if (isDisposeQueued)
321                 {
322                     Dispose(DisposeTypes.Implicit);
323                 }
324                 else
325                 {
326                     Dispose(DisposeTypes.Explicit);
327                 }
328             }
329             else
330             {
331                 // Implicit call. user doesn't call Dispose(), so this object is added into DisposeQueue to be disposed automatically.
332                 if (!isDisposeQueued)
333                 {
334                     isDisposeQueued = true;
335                     DisposeQueue.Instance.Add(this);
336                 }
337             }
338
339             // TODO: free unmanaged resources (unmanaged objects) and override a finalizer below.
340             // TODO: set large fields to null.
341             base.Dispose(disposing);
342         }
343
344         /// <inheritdoc/>
345         /// <since_tizen> 6 </since_tizen>
346         protected virtual void Dispose(DisposeTypes type)
347         {
348             if (disposed)
349             {
350                 return;
351             }
352
353             if (type == DisposeTypes.Explicit)
354             {
355                 //Called by User
356                 //Release your own managed resources here.
357                 //You should release all of your own disposable objects here.
358             }
359
360             //Release your own unmanaged resources here.
361             //You should not access any managed member here except static instance.
362             //because the execution order of Finalizes is non-deterministic.
363             if (swigCPtr.Handle != global::System.IntPtr.Zero)
364             {
365                 if (swigCMemOwn)
366                 {
367                     swigCMemOwn = false;
368                     ReleaseSwigCPtr(swigCPtr);
369                 }
370                 swigCPtr = new global::System.Runtime.InteropServices.HandleRef(null, global::System.IntPtr.Zero);
371             }
372
373             disposed = true;
374         }
375
376         /// <summary>
377         /// Hidden API (Inhouse API).
378         /// Release swigCPtr.
379         /// </summary>
380         /// This will not be public opened.
381         [EditorBrowsable(EditorBrowsableState.Never)]
382         protected virtual void ReleaseSwigCPtr(System.Runtime.InteropServices.HandleRef swigCPtr)
383         {
384             Interop.FlexLayout.DeleteFlexLayout(swigCPtr);
385         }
386
387         /// <summary>
388         /// Creates a FlexLayout object.
389         /// </summary>
390         /// <since_tizen> 6 </since_tizen>
391         public FlexLayout() : this(Interop.FlexLayout.New(), true)
392         {
393             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
394         }
395
396         internal static FlexLayout DownCast(BaseHandle handle)
397         {
398             FlexLayout ret = new FlexLayout(Interop.FlexLayout.DownCast(BaseHandle.getCPtr(handle)), true);
399             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
400             return ret;
401         }
402
403         internal FlexLayout(FlexLayout other) : this(Interop.FlexLayout.NewFlexLayout(FlexLayout.getCPtr(other)), true)
404         {
405             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
406         }
407
408         internal FlexLayout.AlignmentType GetFlexAlignment()
409         {
410             FlexLayout.AlignmentType ret = (FlexLayout.AlignmentType)Interop.FlexLayout.GetFlexAlignment(swigCPtr);
411             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
412             return ret;
413         }
414
415         internal FlexLayout.AlignmentType GetFlexItemsAlignment()
416         {
417             FlexLayout.AlignmentType ret = (FlexLayout.AlignmentType)Interop.FlexLayout.GetFlexItemsAlignment(swigCPtr);
418             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
419             return ret;
420         }
421
422         /// <summary>
423         /// Gets/Sets the flex direction in the layout.
424         /// The direction of the main-axis which determines the direction that flex items are laid out.
425         /// </summary>
426         /// <exception cref="InvalidEnumArgumentException">Thrown when using invalid arguments that are enumerators.</exception>
427         /// <since_tizen> 6 </since_tizen>
428         public FlexDirection Direction
429         {
430             get => (FlexDirection)Interop.FlexLayout.GetFlexDirection(swigCPtr);
431             set
432             {
433                 if (value < FlexDirection.Column || value > FlexDirection.RowReverse)
434                     throw new InvalidEnumArgumentException(nameof(Direction));
435
436                 Interop.FlexLayout.SetFlexDirection(swigCPtr, (int)value);
437                 RequestLayout();
438             }
439         }
440
441         /// <summary>
442         /// Gets/Sets the justification in the layout.
443         /// Justify content describes how to align children within the main axis of their container.<br/>
444         /// 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"/>
445         /// or vertically within a container with <see cref="Direction"/> set to <see cref="FlexDirection.Column"/>.
446         /// </summary>
447         /// <exception cref="InvalidEnumArgumentException">Thrown when using invalid arguments that are enumerators.</exception>
448         /// <since_tizen> 6 </since_tizen>
449         public FlexJustification Justification
450         {
451             get => (FlexJustification)Interop.FlexLayout.GetFlexJustification(swigCPtr);
452             set
453             {
454                 if (value < FlexJustification.FlexStart || value > FlexJustification.SpaceEvenly)
455                     throw new InvalidEnumArgumentException(nameof(Justification));
456
457                 Interop.FlexLayout.SetFlexJustification(swigCPtr, (int)value);
458                 RequestLayout();
459             }
460         }
461
462         /// <summary>
463         /// Gets/Sets the wrap in the layout.
464         /// 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/>
465         /// By default children are forced into a single line (which can shrink elements).<br/>
466         /// 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/>
467         /// When wrapping lines <see cref="Alignment"/> can be used to specify how the lines are placed in the container.
468         /// </summary>
469         /// <exception cref="InvalidEnumArgumentException">Thrown when using invalid arguments that are enumerators.</exception>
470         /// <since_tizen> 6 </since_tizen>
471         public FlexWrapType WrapType
472         {
473             get => (FlexWrapType)Interop.FlexLayout.GetFlexWrap(swigCPtr);
474             set
475             {
476                 if (value != FlexWrapType.NoWrap && value != FlexWrapType.Wrap)
477                     throw new InvalidEnumArgumentException(nameof(WrapType));
478
479                 Interop.FlexLayout.SetFlexWrap(swigCPtr, (int)value);
480                 RequestLayout();
481
482             }
483         }
484
485         /// <summary>
486         /// Gets/Sets the alignment of the layout content.
487         /// Alignment defines the distribution of lines along the cross-axis.<br/>
488         /// This only has effect when items are wrapped to multiple lines using flex wrap.
489         /// </summary>
490         /// <exception cref="InvalidEnumArgumentException">Thrown when using invalid arguments that are enumerators.</exception>
491         /// <since_tizen> 6 </since_tizen>
492         public AlignmentType Alignment
493         {
494             get => GetFlexAlignment();
495             set
496             {
497                 if (value < AlignmentType.Auto || value > AlignmentType.Stretch)
498                     throw new InvalidEnumArgumentException(nameof(Alignment));
499
500                 Interop.FlexLayout.SetFlexAlignment(swigCPtr, (int)value);
501                 RequestLayout();
502             }
503         }
504
505         /// <summary>
506         /// Gets/Sets the alignment of the layout items.
507         /// Items alignment describes how to align children along the cross axis of their container.<br/>
508         /// Align items is very similar to <see cref="Justification"/> but instead of applying to the main axis, align items applies to the cross axis.
509         /// </summary>
510         /// <exception cref="InvalidEnumArgumentException">Thrown when using invalid arguments that are enumerators.</exception>
511         /// <since_tizen> 6 </since_tizen>
512         public AlignmentType ItemsAlignment
513         {
514             get => GetFlexItemsAlignment();
515             set
516             {
517                 if (value < AlignmentType.Auto || value > AlignmentType.Stretch)
518                     throw new InvalidEnumArgumentException(nameof(ItemsAlignment));
519
520                 Interop.FlexLayout.SetFlexItemsAlignment(swigCPtr, (int)value);
521                 RequestLayout();
522             }
523         }
524
525         /// <summary>
526         /// Enumeration for the direction of the main axis in the flex container.
527         /// This determines the direction that flex items are laid out in the flex container.
528         /// </summary>
529         /// <since_tizen> 6 </since_tizen>
530         public enum FlexDirection
531         {
532             /// <summary>
533             /// The flexible items are displayed vertically as a column
534             /// </summary>
535             Column,
536             /// <summary>
537             /// The flexible items are displayed vertically as a column, but in reverse order
538             /// </summary>
539             ColumnReverse,
540             /// <summary>
541             /// The flexible items are displayed horizontally as a row
542             /// </summary>
543             Row,
544             /// <summary>
545             /// The flexible items are displayed horizontally as a row, but in reverse order
546             /// </summary>
547             RowReverse
548         }
549
550         /// <summary>
551         /// Enumeration for the alignment of the flex items when the items do not use all available space on the main-axis.
552         /// </summary>
553         /// <since_tizen> 6 </since_tizen>
554         public enum FlexJustification
555         {
556             /// <summary>
557             /// Items are positioned at the beginning of the container.
558             /// </summary>
559             FlexStart,
560             /// <summary>
561             /// Items are positioned at the center of the container.
562             /// </summary>
563             Center,
564             /// <summary>
565             /// Items are positioned at the end of the container.
566             /// </summary>
567             FlexEnd,
568             /// <summary>
569             /// Items are positioned with equal space between the lines.
570             /// </summary>
571             SpaceBetween,
572             /// <summary>
573             /// Items are positioned with equal space before, and after the lines.<br/>
574             /// </summary>
575             SpaceAround,
576             /// <summary>
577             /// Items are positioned with equal space before, between, and after the lines.<br/>
578             /// Spaces are distributed equally to the beginning of the first child, between each child, and the end of the last child.
579             /// </summary>
580             /// <since_tizen> 9 </since_tizen>
581             SpaceEvenly
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 == null) || (child?.ExcludeLayouting ?? true))
650             {
651                 measureSize.width = 0;
652                 measureSize.height = 0;
653                 return;
654             }
655
656             LayoutItem childLayout = child.Layout;
657             Extents margin = child.Margin;
658
659             MeasureSpecification childWidthMeasureSpec = GetChildMeasureSpecification(
660                                     new MeasureSpecification(
661                                         new LayoutLength(parentMeasureSpecificationWidth.Size - (margin.Start + margin.End)),
662                                         parentMeasureSpecificationWidth.Mode),
663                                     new LayoutLength(Padding.Start + Padding.End),
664                                     new LayoutLength(child.WidthSpecification));
665
666             MeasureSpecification childHeightMeasureSpec = GetChildMeasureSpecification(
667                                     new MeasureSpecification(
668                                         new LayoutLength(parentMeasureSpecificationHeight.Size - (margin.Top + margin.Bottom)),
669                                         parentMeasureSpecificationHeight.Mode),
670                                     new LayoutLength(Padding.Top + Padding.Bottom),
671                                     new LayoutLength(child.HeightSpecification));
672
673             childLayout?.Measure(childWidthMeasureSpec, childHeightMeasureSpec);
674
675             measureSize.width = (childLayout == null) ? 0 : MeasuredWidth.Size.AsRoundedValue();
676             measureSize.height = (childLayout == null) ? 0 : MeasuredHeight.Size.AsRoundedValue();
677         }
678
679         void InsertChild(LayoutItem child)
680         {
681             // Store created node for child
682             IntPtr childPtr = Interop.FlexLayout.AddChildWithMargin(swigCPtr, View.getCPtr(child.Owner), Extents.getCPtr(child.Owner.Margin), measureChildDelegate, LayoutChildren.Count - 1);
683             HandleRef childHandleRef = new HandleRef(child.Owner, childPtr);
684             SetAttachedValue(child.Owner, FlexItemProperty, childHandleRef);
685         }
686
687         /// <summary>
688         /// Callback when child is added to container.<br />
689         /// Derived classes can use this to set their own child properties on the child layout's owner.<br />
690         /// </summary>
691         /// <param name="child">The Layout child.</param>
692         /// <exception cref="ArgumentNullException"> Thrown when child is null. </exception>
693         /// <since_tizen> 6 </since_tizen>
694         protected override void OnChildAdd(LayoutItem child)
695         {
696             if (null == child)
697             {
698                 throw new ArgumentNullException(nameof(child));
699             }
700             InsertChild(child);
701         }
702
703         /// <summary>
704         /// Callback when child is removed from container.<br />
705         /// </summary>
706         /// <param name="child">The Layout child.</param>
707         /// <exception cref="ArgumentNullException"> Thrown when child is null. </exception>
708         /// <since_tizen> 6 </since_tizen>
709         protected override void OnChildRemove(LayoutItem child)
710         {
711             if (null == child)
712             {
713                 throw new ArgumentNullException(nameof(child));
714             }
715             // When child View is removed from it's parent View (that is a Layout) then remove it from the layout too.
716             // FlexLayout refers to the child as a View not LayoutItem.
717             Interop.FlexLayout.RemoveChild(swigCPtr, child.Owner.SwigCPtr);
718         }
719
720         /// <summary>
721         /// Measure the layout and its content to determine the measured width and the measured height.<br />
722         /// </summary>
723         /// <param name="widthMeasureSpec">horizontal space requirements as imposed by the parent.</param>
724         /// <param name="heightMeasureSpec">vertical space requirements as imposed by the parent.</param>
725         /// <since_tizen> 6 </since_tizen>
726         protected override void OnMeasure(MeasureSpecification widthMeasureSpec, MeasureSpecification heightMeasureSpec)
727         {
728             bool isLayoutRtl = Owner.LayoutDirection == ViewLayoutDirectionType.RTL;
729             Extents padding = Owner.Padding;
730
731             Interop.FlexLayout.SetPadding(swigCPtr, Extents.getCPtr(padding));
732
733             float width = FlexUndefined; // Behaves as WrapContent (Flex Auto)
734             float height = FlexUndefined; // Behaves as WrapContent (Flex Auto)
735
736             if (widthMeasureSpec.Mode == MeasureSpecification.ModeType.Exactly)
737             {
738                 width = widthMeasureSpec.Size.AsDecimal();
739             }
740
741             if (heightMeasureSpec.Mode == MeasureSpecification.ModeType.Exactly)
742             {
743                 height = heightMeasureSpec.Size.AsDecimal();
744             }
745
746             // Save measureSpec to measure children.
747             // In other Layout, we can pass it as parameter to measuring child but in FlexLayout we can't
748             // because measurChild function is called by native callback.
749             parentMeasureSpecificationWidth = widthMeasureSpec;
750             parentMeasureSpecificationHeight = heightMeasureSpec;
751
752             Extents zeroMargin = new Extents();
753
754             // Assign child properties
755             for (int i = 0; i < LayoutChildren.Count; i++)
756             {
757                 LayoutItem layoutItem = LayoutChildren[i];
758                 View Child = layoutItem?.Owner;
759                 HandleRef childHandleRef = (HandleRef)Child.GetValue(FlexItemProperty);
760                 if (childHandleRef.Handle == IntPtr.Zero || Child == null)
761                     continue;
762
763                 AlignmentType flexAlignemnt = GetFlexAlignmentSelf(Child);
764                 PositionType positionType = GetFlexPositionType(Child);
765                 float flexAspectRatio = GetFlexAspectRatio(Child);
766                 float flexBasis = GetFlexBasis(Child);
767                 float flexShrink = GetFlexShrink(Child);
768                 float flexGrow = GetFlexGrow(Child);
769                 Extents childMargin = Child.ExcludeLayouting ? zeroMargin : layoutItem.Margin;
770
771                 Interop.FlexLayout.SetMargin(childHandleRef, Extents.getCPtr(childMargin));
772                 Interop.FlexLayout.SetFlexAlignmentSelf(childHandleRef, (int)flexAlignemnt);
773                 Interop.FlexLayout.SetFlexPositionType(childHandleRef, (int)positionType);
774                 Interop.FlexLayout.SetFlexAspectRatio(childHandleRef, flexAspectRatio);
775                 Interop.FlexLayout.SetFlexBasis(childHandleRef, flexBasis);
776                 Interop.FlexLayout.SetFlexShrink(childHandleRef, flexShrink);
777                 Interop.FlexLayout.SetFlexGrow(childHandleRef, flexGrow);
778             }
779
780             Interop.FlexLayout.CalculateLayout(swigCPtr, width, height, isLayoutRtl);
781             zeroMargin.Dispose();
782
783             LayoutLength flexLayoutWidth = new LayoutLength(Interop.FlexLayout.GetWidth(swigCPtr));
784             LayoutLength flexLayoutHeight = new LayoutLength(Interop.FlexLayout.GetHeight(swigCPtr));
785
786             NUILog.Debug("FlexLayout OnMeasure width:" + flexLayoutWidth.AsRoundedValue() + " height:" + flexLayoutHeight.AsRoundedValue());
787
788             SetMeasuredDimensions(GetDefaultSize(flexLayoutWidth, widthMeasureSpec),
789                                    GetDefaultSize(flexLayoutHeight, heightMeasureSpec));
790         }
791
792         /// <summary>
793         /// Assign a size and position to each of its children.<br />
794         /// </summary>
795         /// <param name="changed">This is a new size or position for this layout.</param>
796         /// <param name="left">Left position, relative to parent.</param>
797         /// <param name="top"> Top position, relative to parent.</param>
798         /// <param name="right">Right position, relative to parent.</param>
799         /// <param name="bottom">Bottom position, relative to parent.</param>
800         /// <since_tizen> 6 </since_tizen>
801         protected override void OnLayout(bool changed, LayoutLength left, LayoutLength top, LayoutLength right, LayoutLength bottom)
802         {
803
804             bool isLayoutRtl = Owner.LayoutDirection == ViewLayoutDirectionType.RTL;
805             LayoutLength width = right - left;
806             LayoutLength height = bottom - top;
807
808             // Call to FlexLayout implementation to calculate layout values for later retrieval.
809             Interop.FlexLayout.CalculateLayout(swigCPtr, width.AsDecimal(), height.AsDecimal(), isLayoutRtl);
810
811             for (int childIndex = 0; childIndex < LayoutChildren.Count; childIndex++)
812             {
813                 LayoutItem childLayout = LayoutChildren[childIndex];
814                 if (!childLayout?.Owner?.ExcludeLayouting ?? false)
815                 {
816                     // Get the frame for the child, start, top, end, bottom.
817                     Vector4 frame = new Vector4(Interop.FlexLayout.GetNodeFrame(swigCPtr, childIndex), true);
818
819                     // Child view's size is calculated in OnLayout() without considering child layout's measured size unlike other layouts' OnLayout().
820                     // This causes that the grand child view's size is calculated incorrectly if the child and grand child have MatchParent Specification.
821                     // e.g. Let parent view's width be 200 and parent has 2 children.
822                     //      Then, child layout's measured width becomes 200 and child view's width becomes 100. (by dali-toolkit's YOGA APIs)
823                     //      Then, grand child layout's measured width becomes 200 and grand child view's width becomes 200. (by NUI Layout)
824                     //
825                     // To resolve the above issue, child layout's measured size is set with the child view's size calculated by dali-toolkit's YOGA APIs.
826                     MeasureSpecification widthSpec = new MeasureSpecification(new LayoutLength(frame.Z - frame.X), MeasureSpecification.ModeType.Exactly);
827                     MeasureSpecification heightSpec = new MeasureSpecification(new LayoutLength(frame.W - frame.Y), MeasureSpecification.ModeType.Exactly);
828                     MeasureChildWithoutPadding(childLayout, widthSpec, heightSpec);
829
830                     childLayout.Layout(new LayoutLength(frame.X), new LayoutLength(frame.Y), new LayoutLength(frame.Z), new LayoutLength(frame.W));
831                     frame.Dispose();
832                 }
833             }
834         }
835     } // FLexlayout
836 } // namesspace Tizen.NUI