[NUI] enhance FlexLayout documentation (#2004)
[platform/core/csapi/tizenfx.git] / src / Tizen.NUI / src / public / Layouting / FlexLayout.cs
1 /* Copyright (c) 2019 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, global::System.IDisposable
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, 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, propertyChanged: OnChildPropertyChanged);
51
52         /// <summary>
53         /// AspectRatioProperty
54         /// </summary>
55         [EditorBrowsable(EditorBrowsableState.Never)]
56         public static readonly BindableProperty FlexAspectRatioProperty = BindableProperty.CreateAttached("FlexAspectRatio", typeof(float), typeof(FlexLayout), FlexUndefined, validateValue: (bindable, value) => (float)value > 0, propertyChanged: OnChildPropertyChanged);
57
58         /// <summary>
59         /// FlexBasisProperty
60         /// </summary>
61         [EditorBrowsable(EditorBrowsableState.Never)]
62         public static readonly BindableProperty FlexBasisProperty = BindableProperty.CreateAttached("FlexBasis", typeof(float), typeof(FlexLayout), FlexUndefined, validateValue: (bindable, value) => (float)value >= 0, propertyChanged: OnChildPropertyChanged);
63
64         /// <summary>
65         /// FlexShrinkProperty
66         /// </summary>
67         [EditorBrowsable(EditorBrowsableState.Never)]
68         public static readonly BindableProperty FlexShrinkProperty = BindableProperty.CreateAttached("FlexShrink", typeof(float), typeof(FlexLayout), 1.0f, validateValue: (bindable, value) => (float)value >= 0, propertyChanged: OnChildPropertyChanged);
69
70         /// <summary>
71         /// FlexGrowProperty
72         /// </summary>
73         [EditorBrowsable(EditorBrowsableState.Never)]
74         public static readonly BindableProperty FlexGrowProperty = BindableProperty.CreateAttached("FlexGrow", typeof(float), typeof(FlexLayout), FlexUndefined, validateValue: (bindable, value) => (float)value >= 0, propertyChanged: OnChildPropertyChanged);
75
76         private static bool LayoutDebugFlex = false; // Debug flag
77
78         private global::System.Runtime.InteropServices.HandleRef swigCPtr;
79         private bool swigCMemOwn;
80         private bool disposed;
81         private bool isDisposeQueued = false;
82
83         private MeasureSpecification parentMeasureSpecificationWidth;
84         private MeasureSpecification parentMeasureSpecificationHeight;
85
86         private IntPtr _rootFlex;  // Pointer to the unmanged flex layout class.
87
88         internal const float FlexUndefined = 10E20F; // Auto setting which is equivalent to WrapContent.
89
90         internal struct MeasuredSize
91         {
92             public MeasuredSize(float x, float y)
93             {
94                 width = x;
95                 height = y;
96             }
97             float width;
98             float height;
99         };
100
101         /// <summary>
102         /// Gets the alignment self of the child view.
103         /// </summary>
104         /// <seealso cref="SetFlexAlignmentSelf(View, AlignmentType)"/>
105         /// <param name="view">The child view.</param>
106         /// <returns> The value of alignment self.</returns>
107         /// <since_tizen> 8 </since_tizen>
108         public static AlignmentType GetFlexAlignmentSelf(View view)
109         {
110             return (AlignmentType)view.GetValue(FlexAlignmentSelfProperty);
111         }
112
113         /// <summary>
114         /// Gets the position type of the child view.
115         /// </summary>
116         /// <seealso cref="SetFlexPositionType(View, PositionType)"/>
117         /// <param name="view">The child view.</param>
118         /// <returns> The value of position type</returns>
119         /// <since_tizen> 8 </since_tizen>
120         public static PositionType GetFlexPositionType(View view)
121         {
122             return (PositionType)view.GetValue(FlexPositionTypeProperty);
123         }
124
125         /// <summary>
126         /// Gets the aspect ratio of the child view.
127         /// </summary>
128         /// <seealso cref="SetFlexAspectRatio(View, float)"/>
129         /// <param name="view">The child view.</param>
130         /// <returns> The value of aspect ratio</returns>
131         /// <since_tizen> 8 </since_tizen>
132         public static float GetFlexAspectRatio(View view)
133         {
134             return (float)view.GetValue(FlexAspectRatioProperty);
135         }
136
137         /// <summary>
138         /// Gets the basis of the child view.
139         /// </summary>
140         /// <seealso cref="SetFlexBasis(View, float)"/>
141         /// <param name="view">The child view.</param>
142         /// <returns> The value of basis</returns>
143         /// <since_tizen> 8 </since_tizen>
144         public static float GetFlexBasis(View view)
145         {
146             return (float)view.GetValue(FlexBasisProperty);
147         }
148
149         /// <summary>
150         /// Gets the shrink of the child view.
151         /// </summary>
152         /// <seealso cref="SetFlexShrink(View, float)"/>
153         /// <param name="view">The child view.</param>
154         /// <returns> The value of shrink</returns>
155         /// <since_tizen> 8 </since_tizen>
156         public static float GetFlexShrink(View view)
157         {
158             return (float)view.GetValue(FlexShrinkProperty);
159         }
160
161         /// <summary>
162         /// Gets the grow of the child view.
163         /// </summary>
164         /// <seealso cref="SetFlexGrow(View, float)"/>
165         /// <param name="view">The child view.</param>
166         /// <returns> The value of grow</returns>
167         /// <since_tizen> 8 </since_tizen>
168         public static float GetFlexGrow(View view)
169         {
170             return (float)view.GetValue(FlexGrowProperty);
171         }
172
173         /// <summary>
174         /// Sets the alignment self of the child view.<br/>
175         /// Alignment self has the same options and effect as <see cref="ItemsAlignment"/> but instead of affecting the children within a container,
176         /// you can apply this property to a single child to change its alignment within its parent.<br/>
177         /// Alignment self overrides any option set by the parent with <see cref="ItemsAlignment"/>.
178         /// </summary>
179         /// <param name="view">The child view.</param>
180         /// <param name="value">The value of alignment self.</param>
181         /// <since_tizen> 8 </since_tizen>
182         public static void SetFlexAlignmentSelf(View view, AlignmentType value)
183         {
184             SetChildValue(view, FlexAlignmentSelfProperty, value);
185         }
186
187         /// <summary>
188         /// Sets the position type of the child view.<br/>
189         /// The position type of an element defines how it is positioned within its parent.
190         /// By default a child is positioned relatively. This means a child is positioned according to the normal flow of the layout,
191         /// and then offset relative to that position based on the values of <see cref="View.Margin"/>.<br/>
192         /// When positioned absolutely an element doesn't take part in the normal layout flow.
193         /// It is instead laid out independent of its siblings. The position is determined based on the <see cref="View.Margin"/>.
194         /// </summary>
195         /// <param name="view">The child view.</param>
196         /// <param name="value">The value of position type.</param>
197         /// <since_tizen> 8 </since_tizen>
198         public static void SetFlexPositionType(View view, PositionType value)
199         {
200             SetChildValue(view, FlexPositionTypeProperty, value);
201         }
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         /// <since_tizen> 8 </since_tizen>
212         public static void SetFlexAspectRatio(View view, float value)
213         {
214             SetChildValue(view, FlexAspectRatioProperty, value);
215         }
216
217         /// <summary>
218         /// Sets the flex basis of the child view.
219         /// Flex basis is an axis-independent way of providing the default size of an item along the main axis.<br/>
220         /// 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"/>
221         /// or setting the height of a child if its parent is a container with <see cref="FlexDirection.Column"/>.<br/>
222         /// 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.
223         /// </summary>
224         /// <param name="view">The child view.</param>
225         /// <param name="value">The value of basis</param>
226         /// <since_tizen> 8 </since_tizen>
227         public static void SetFlexBasis(View view, float value)
228         {
229             SetChildValue(view, FlexBasisProperty, value);
230         }
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         /// <since_tizen> 8 </since_tizen>
242         public static void SetFlexShrink(View view, float value)
243         {
244             SetChildValue(view, FlexShrinkProperty, value);
245         }
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         /// <since_tizen> 8 </since_tizen>
257         public static void SetFlexGrow(View view, float value)
258         {
259             SetChildValue(view, FlexGrowProperty, value);
260         }
261
262         [UnmanagedFunctionPointer(CallingConvention.StdCall)]
263         internal delegate MeasuredSize ChildMeasureCallback(global::System.IntPtr child, float width, int measureModeWidth, float height, int measureModeHeight);
264
265         event ChildMeasureCallback measureChildDelegate; // Stores a delegate to the child measure callback. Used for all children of this FlexLayout.
266
267         internal FlexLayout(global::System.IntPtr cPtr, bool cMemoryOwn)
268         {
269             swigCMemOwn = cMemoryOwn;
270             swigCPtr = new global::System.Runtime.InteropServices.HandleRef(this, cPtr);
271             _rootFlex = Interop.FlexLayout.FlexLayout_New();
272             measureChildDelegate = new ChildMeasureCallback(measureChild);
273         }
274
275         internal static global::System.Runtime.InteropServices.HandleRef getCPtr(FlexLayout obj)
276         {
277             return (obj == null) ? new global::System.Runtime.InteropServices.HandleRef(null, global::System.IntPtr.Zero) : obj.swigCPtr;
278         }
279
280         /// <inheritdoc/>
281         /// <since_tizen> 6 </since_tizen>
282         public void Dispose()
283         {
284             // Throw exception if Dispose() is called in separate thread.
285             if (!Window.IsInstalled())
286             {
287                 throw new System.InvalidOperationException("This API called from separate thread. This API must be called from MainThread.");
288             }
289
290             if (isDisposeQueued)
291             {
292                 Dispose(DisposeTypes.Implicit);
293             }
294             else
295             {
296                 Dispose(DisposeTypes.Explicit);
297                 System.GC.SuppressFinalize(this);
298             }
299         }
300
301         /// <inheritdoc/>
302         /// <since_tizen> 6 </since_tizen>
303         protected virtual void Dispose(DisposeTypes type)
304         {
305             if (disposed)
306             {
307                 return;
308             }
309
310             if (type == DisposeTypes.Explicit)
311             {
312                 // Called by User
313                 // Release your own managed resources here.
314                 // You should release all of your own disposable objects here.
315
316             }
317
318             // Release your own unmanaged resources here.
319             // You should not access any managed member here except static instance.
320             // because the execution order of Finalizes is non-deterministic.
321             if (swigCPtr.Handle != global::System.IntPtr.Zero)
322             {
323                 if (swigCMemOwn)
324                 {
325                     swigCMemOwn = false;
326                     Interop.FlexLayout.delete_FlexLayout(swigCPtr);
327                 }
328                 swigCPtr = new global::System.Runtime.InteropServices.HandleRef(null, global::System.IntPtr.Zero);
329             }
330             disposed = true;
331         }
332
333         /// <summary>
334         /// Creates a FlexLayout object.
335         /// </summary>
336         /// <since_tizen> 6 </since_tizen>
337         public FlexLayout() : this(Interop.FlexLayout.FlexLayout_New(), true)
338         {
339             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
340         }
341
342         internal static FlexLayout DownCast(BaseHandle handle)
343         {
344             FlexLayout ret = new FlexLayout(Interop.FlexLayout.FlexLayout_DownCast(BaseHandle.getCPtr(handle)), true);
345             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
346             return ret;
347         }
348
349         internal FlexLayout(FlexLayout other) : this(Interop.FlexLayout.new_FlexLayout__SWIG_1(FlexLayout.getCPtr(other)), true)
350         {
351             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
352         }
353
354         internal FlexLayout Assign(FlexLayout other)
355         {
356             FlexLayout ret = new FlexLayout(Interop.FlexLayout.FlexLayout_Assign(swigCPtr, FlexLayout.getCPtr(other)), false);
357             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
358             return ret;
359         }
360
361         internal void SetFlexDirection(FlexLayout.FlexDirection flexDirection)
362         {
363             Interop.FlexLayout.FlexLayout_SetFlexDirection(swigCPtr, (int)flexDirection);
364             RequestLayout();
365         }
366
367         internal FlexLayout.FlexDirection GetFlexDirection()
368         {
369             FlexLayout.FlexDirection ret = (FlexLayout.FlexDirection)Interop.FlexLayout.FlexLayout_GetFlexDirection(swigCPtr);
370             return ret;
371
372         }
373
374         internal void SetFlexJustification(FlexLayout.FlexJustification flexJustification)
375         {
376             Interop.FlexLayout.FlexLayout_SetFlexJustification(swigCPtr, (int)flexJustification);
377             RequestLayout();
378         }
379
380         internal FlexLayout.FlexJustification GetFlexJustification()
381         {
382             FlexLayout.FlexJustification ret = (FlexLayout.FlexJustification)Interop.FlexLayout.FlexLayout_GetFlexJustification(swigCPtr);
383             return ret;
384         }
385
386         internal void SetFlexWrap(FlexLayout.FlexWrapType flexWrap)
387         {
388             Interop.FlexLayout.FlexLayout_SetFlexWrap(swigCPtr, (int)flexWrap);
389             RequestLayout();
390         }
391
392         internal FlexLayout.FlexWrapType GetFlexWrap()
393         {
394             FlexLayout.FlexWrapType ret = (FlexLayout.FlexWrapType)Interop.FlexLayout.FlexLayout_GetFlexWrap(swigCPtr);
395             return ret;
396         }
397
398         internal void SetFlexAlignment(FlexLayout.AlignmentType flexAlignment)
399         {
400             Interop.FlexLayout.FlexLayout_SetFlexAlignment(swigCPtr, (int)flexAlignment);
401             RequestLayout();
402         }
403
404         internal FlexLayout.AlignmentType GetFlexAlignment()
405         {
406             FlexLayout.AlignmentType ret = (FlexLayout.AlignmentType)Interop.FlexLayout.FlexLayout_GetFlexAlignment(swigCPtr);
407             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
408             return ret;
409         }
410
411         internal void SetFlexItemsAlignment(FlexLayout.AlignmentType flexAlignment)
412         {
413             Interop.FlexLayout.FlexLayout_SetFlexItemsAlignment(swigCPtr, (int)flexAlignment);
414             RequestLayout();
415         }
416
417         internal FlexLayout.AlignmentType GetFlexItemsAlignment()
418         {
419             FlexLayout.AlignmentType ret = (FlexLayout.AlignmentType)Interop.FlexLayout.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         /// <since_tizen> 6 </since_tizen>
429         public FlexDirection Direction
430         {
431             get
432             {
433                 return GetFlexDirection();
434             }
435             set
436             {
437                 SetFlexDirection(value);
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         /// <since_tizen> 6 </since_tizen>
448         public FlexJustification Justification
449         {
450             get
451             {
452                 return GetFlexJustification();
453             }
454             set
455             {
456                 SetFlexJustification(value);
457             }
458         }
459
460         /// <summary>
461         /// Gets/Sets the wrap in the layout.
462         /// 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/>
463         /// By default children are forced into a single line (which can shrink elements).<br/>
464         /// 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/>
465         /// When wrapping lines <see cref="Alignment"/> can be used to specify how the lines are placed in the container.
466         /// </summary>
467         /// <since_tizen> 6 </since_tizen>
468         public FlexWrapType WrapType
469         {
470             get
471             {
472                 return GetFlexWrap();
473             }
474             set
475             {
476                 SetFlexWrap(value);
477             }
478         }
479
480         /// <summary>
481         /// Gets/Sets the alignment of the layout content.
482         /// Alignment defines the distribution of lines along the cross-axis.<br/>
483         /// This only has effect when items are wrapped to multiple lines using flex wrap.
484         /// </summary>
485         /// <since_tizen> 6 </since_tizen>
486         public AlignmentType Alignment
487         {
488             get
489             {
490                 return GetFlexAlignment();
491             }
492             set
493             {
494                 SetFlexAlignment(value);
495             }
496         }
497
498         /// <summary>
499         /// Gets/Sets the alignment of the layout items.
500         /// Items alignment describes how to align children along the cross axis of their container.<br/>
501         /// Align items is very similar to <see cref="Justification"/> but instead of applying to the main axis, align items applies to the cross axis.
502         /// </summary>
503         /// <since_tizen> 6 </since_tizen>
504         public AlignmentType ItemsAlignment
505         {
506             get
507             {
508                 return GetFlexItemsAlignment();
509             }
510             set
511             {
512                 SetFlexItemsAlignment(value);
513             }
514         }
515
516         /// <summary>
517         /// Enumeration for the direction of the main axis in the flex container.
518         /// This determines the direction that flex items are laid out in the flex container.
519         /// </summary>
520         /// <since_tizen> 6 </since_tizen>
521         public enum FlexDirection
522         {
523             /// <summary>
524             /// The flexible items are displayed vertically as a column
525             /// </summary>
526             Column,
527             /// <summary>
528             /// The flexible items are displayed vertically as a column, but in reverse order
529             /// </summary>
530             ColumnReverse,
531             /// <summary>
532             /// The flexible items are displayed horizontally as a row
533             /// </summary>
534             Row,
535             /// <summary>
536             /// The flexible items are displayed horizontally as a row, but in reverse order
537             /// </summary>
538             RowReverse
539         }
540
541         /// <summary>
542         /// Enumeration for the alignment of the flex items when the items do not use all available space on the main-axis.
543         /// </summary>
544         /// <since_tizen> 6 </since_tizen>
545         public enum FlexJustification
546         {
547             /// <summary>
548             /// Items are positioned at the beginning of the container.
549             /// </summary>
550             FlexStart,
551             /// <summary>
552             /// Items are positioned at the center of the container.
553             /// </summary>
554             Center,
555             /// <summary>
556             /// Items are positioned at the end of the container.
557             /// </summary>
558             FlexEnd,
559             /// <summary>
560             /// Items are positioned with equal space between the lines.
561             /// </summary>
562             SpaceBetween,
563             /// <summary>
564             /// Items are positioned with equal space before, between, and after the lines.<br/>
565             /// Compared to <see cref="FlexJustification.SpaceBetween"/> using <see cref="FlexJustification.SpaceAround"/>
566             /// will result in space being distributed to the beginning of the first child and end of the last child.
567             /// </summary>
568             SpaceAround
569         }
570
571         /// <summary>
572         /// Enumeration for the wrap type of the flex container when there is no enough room for all the items on one flex line.
573         /// </summary>
574         /// <since_tizen> 6 </since_tizen>
575         public enum FlexWrapType
576         {
577             /// <summary>
578             /// Flex items laid out in single line (shrunk to fit the flex container along the main axis)
579             /// </summary>
580             NoWrap,
581             /// <summary>
582             /// Flex items laid out in multiple lines if needed
583             /// </summary>
584             Wrap
585         }
586
587         /// <summary>
588         /// 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.
589         /// </summary>
590         /// <since_tizen> 6 </since_tizen>
591         public enum AlignmentType
592         {
593             /// <summary>
594             /// Inherits the same alignment from the parent
595             /// </summary>
596             Auto,
597             /// <summary>
598             /// At the beginning of the container
599             /// </summary>
600             FlexStart,
601             /// <summary>
602             /// At the center of the container
603             /// </summary>
604             Center,
605             /// <summary>
606             /// At the end of the container
607             /// </summary>
608             FlexEnd,
609             /// <summary>
610             /// Stretch to fit the container
611             /// </summary>
612             Stretch
613         }
614
615         /// <summary>
616         /// Enumeration for the position type of the flex item how it is positioned within its parent.
617         /// </summary>
618         /// <since_tizen> 8 </since_tizen>
619         public enum PositionType
620         {
621             /// <summary>
622             /// Flex items laid out relatively.
623             /// </summary>
624             Relative,
625             /// <summary>
626             /// Flex items laid out absolutely.
627             /// </summary>
628             Absolute
629         }
630
631         private MeasuredSize measureChild(global::System.IntPtr childPtr, float width, int measureModeWidth, float height, int measureModeHeight)
632         {
633             // We need to measure child layout
634             View child = Registry.GetManagedBaseHandleFromNativePtr(childPtr) as View;
635
636             LayoutItem childLayout = child.Layout;
637
638             MeasureSpecification childWidthMeasureSpec = GetChildMeasureSpecification(
639                                     new MeasureSpecification(
640                                         new LayoutLength(parentMeasureSpecificationWidth.Size - (child.Margin.Start + child.Margin.End)),
641                                         parentMeasureSpecificationWidth.Mode),
642                                     new LayoutLength(Padding.Start + Padding.End),
643                                     new LayoutLength(child.WidthSpecification));
644
645             MeasureSpecification childHeightMeasureSpec = GetChildMeasureSpecification(
646                                     new MeasureSpecification(
647                                         new LayoutLength(parentMeasureSpecificationHeight.Size - (child.Margin.Top + child.Margin.Bottom)),
648                                         parentMeasureSpecificationHeight.Mode),
649                                     new LayoutLength(Padding.Top + Padding.Bottom),
650                                     new LayoutLength(child.HeightSpecification));
651
652             childLayout.Measure(childWidthMeasureSpec, childHeightMeasureSpec);
653
654             return new MeasuredSize(childLayout.MeasuredWidth.Size.AsRoundedValue(), childLayout.MeasuredHeight.Size.AsRoundedValue());
655         }
656
657         void InsertChild(LayoutItem child)
658         {
659             // Store created node for child
660             IntPtr childPtr = Interop.FlexLayout.FlexLayout_AddChildWithMargin(swigCPtr, View.getCPtr(child.Owner), Extents.getCPtr(child.Owner.Margin), measureChildDelegate, LayoutChildren.Count - 1);
661             HandleRef childHandleRef = new HandleRef(child.Owner, childPtr);
662             SetChildValue(child.Owner, FlexItemProperty, childHandleRef);
663         }
664
665         /// <summary>
666         /// Callback when child is added to container.<br />
667         /// Derived classes can use this to set their own child properties on the child layout's owner.<br />
668         /// </summary>
669         /// <param name="child">The Layout child.</param>
670         /// <since_tizen> 6 </since_tizen>
671         protected override void OnChildAdd(LayoutItem child)
672         {
673             InsertChild(child);
674         }
675
676         /// <summary>
677         /// Callback when child is removed from container.<br />
678         /// </summary>
679         /// <param name="child">The Layout child.</param>
680         /// <since_tizen> 6 </since_tizen>
681         protected override void OnChildRemove(LayoutItem child)
682         {
683             // When child View is removed from it's parent View (that is a Layout) then remove it from the layout too.
684             // FlexLayout refers to the child as a View not LayoutItem.
685             Interop.FlexLayout.FlexLayout_RemoveChild(swigCPtr, child);
686         }
687
688         /// <summary>
689         /// Measure the layout and its content to determine the measured width and the measured height.<br />
690         /// </summary>
691         /// <param name="widthMeasureSpec">horizontal space requirements as imposed by the parent.</param>
692         /// <param name="heightMeasureSpec">vertical space requirements as imposed by the parent.</param>
693         /// <since_tizen> 6 </since_tizen>
694         protected override void OnMeasure(MeasureSpecification widthMeasureSpec, MeasureSpecification heightMeasureSpec)
695         {
696             bool isLayoutRtl = Owner.LayoutDirection == ViewLayoutDirectionType.RTL;
697             Extents padding = Owner.Padding;
698             Extents margin = Owner.Margin;
699
700             Interop.FlexLayout.FlexLayout_SetMargin(swigCPtr, Extents.getCPtr(margin));
701             Interop.FlexLayout.FlexLayout_SetPadding(swigCPtr, Extents.getCPtr(padding));
702
703             float width = FlexUndefined; // Behaves as WrapContent (Flex Auto)
704             float height = FlexUndefined; // Behaves as WrapContent (Flex Auto)
705
706             if (widthMeasureSpec.Mode == MeasureSpecification.ModeType.Exactly || widthMeasureSpec.Mode == MeasureSpecification.ModeType.AtMost)
707             {
708                 width = widthMeasureSpec.Size.AsDecimal();
709             }
710
711             if (heightMeasureSpec.Mode == MeasureSpecification.ModeType.Exactly || heightMeasureSpec.Mode == MeasureSpecification.ModeType.AtMost)
712             {
713                 height = heightMeasureSpec.Size.AsDecimal();
714             }
715
716             // Save measureSpec to measure children.
717             // In other Layout, we can pass it as parameter to measuring child but in FlexLayout we can't
718             // because measurChild function is called by native callback.
719             parentMeasureSpecificationWidth = widthMeasureSpec;
720             parentMeasureSpecificationHeight = heightMeasureSpec;
721
722             // Assign child properties
723             for (int i = 0; i < LayoutChildren.Count; i++)
724             {
725                 LayoutItem layoutItem = LayoutChildren[i];
726                 View Child = layoutItem?.Owner;
727                 HandleRef childHandleRef = (HandleRef)Child.GetValue(FlexItemProperty);
728                 if (childHandleRef.Handle == IntPtr.Zero || Child == null)
729                     continue;
730
731                 AlignmentType flexAlignemnt = GetFlexAlignmentSelf(Child);
732                 PositionType flexPosition = GetFlexPositionType(Child);
733                 float flexAspectRatio = GetFlexAspectRatio(Child);
734                 float flexBasis = GetFlexBasis(Child);
735                 float flexShrink = GetFlexShrink(Child);
736                 float flexGrow = GetFlexGrow(Child);
737
738                 Interop.FlexLayout.FlexLayout_SetFlexAlignmentSelf(childHandleRef, (int)flexAlignemnt);
739                 Interop.FlexLayout.FlexLayout_SetFlexPositionType(childHandleRef, (int)flexPosition);
740                 Interop.FlexLayout.FlexLayout_SetFlexAspectRatio(childHandleRef, flexAspectRatio);
741                 Interop.FlexLayout.FlexLayout_SetFlexBasis(childHandleRef, flexBasis);
742                 Interop.FlexLayout.FlexLayout_SetFlexShrink(childHandleRef, flexShrink);
743                 Interop.FlexLayout.FlexLayout_SetFlexGrow(childHandleRef, flexGrow);
744             }
745
746             Interop.FlexLayout.FlexLayout_CalculateLayout(swigCPtr, width, height, isLayoutRtl);
747
748             LayoutLength flexLayoutWidth = new LayoutLength(Interop.FlexLayout.FlexLayout_GetWidth(swigCPtr));
749             LayoutLength flexLayoutHeight = new LayoutLength(Interop.FlexLayout.FlexLayout_GetHeight(swigCPtr));
750
751             Debug.WriteLineIf(LayoutDebugFlex, "FlexLayout OnMeasure width:" + flexLayoutWidth.AsRoundedValue()
752                                                 + " height:" + flexLayoutHeight.AsRoundedValue());
753
754             SetMeasuredDimensions(GetDefaultSize(flexLayoutWidth, widthMeasureSpec),
755                                    GetDefaultSize(flexLayoutHeight, heightMeasureSpec));
756         }
757
758         /// <summary>
759         /// Assign a size and position to each of its children.<br />
760         /// </summary>
761         /// <param name="changed">This is a new size or position for this layout.</param>
762         /// <param name="left">Left position, relative to parent.</param>
763         /// <param name="top"> Top position, relative to parent.</param>
764         /// <param name="right">Right position, relative to parent.</param>
765         /// <param name="bottom">Bottom position, relative to parent.</param>
766         /// <since_tizen> 6 </since_tizen>
767         protected override void OnLayout(bool changed, LayoutLength left, LayoutLength top, LayoutLength right, LayoutLength bottom)
768         {
769
770             bool isLayoutRtl = Owner.LayoutDirection == ViewLayoutDirectionType.RTL;
771             LayoutLength width = right - left;
772             LayoutLength height = bottom - top;
773
774             // Call to FlexLayout implementation to calculate layout values for later retrieval.
775             Interop.FlexLayout.FlexLayout_CalculateLayout(swigCPtr, width.AsDecimal(), height.AsDecimal(), isLayoutRtl);
776
777             int count = LayoutChildren.Count;
778             for (int childIndex = 0; childIndex < count; childIndex++)
779             {
780                 LayoutItem childLayout = LayoutChildren[childIndex];
781                 if (childLayout != null)
782                 {
783                     // Get the frame for the child, start, top, end, bottom.
784                     Vector4 frame = new Vector4(Interop.FlexLayout.FlexLayout_GetNodeFrame(swigCPtr, childIndex), true);
785                     childLayout.Layout(new LayoutLength(frame.X), new LayoutLength(frame.Y), new LayoutLength(frame.Z), new LayoutLength(frame.W));
786                 }
787             }
788         }
789
790     } // FLexlayout
791 } // namesspace Tizen.NUI