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