1 /* Copyright (c) 2019 Samsung Electronics Co., Ltd.
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
7 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 using System.ComponentModel;
19 using Tizen.NUI.BaseComponents;
20 using System.Runtime.InteropServices;
25 /// [Draft] This class implements a flex layout.
26 /// The flex layout implementation is based on open source Facebook Yoga layout engine.
27 /// For more information about the flex layout API and how to use it please refer to https://yogalayout.com/docs/
28 /// We implement the subset of the API in the class below.
30 public class FlexLayout : LayoutGroup, global::System.IDisposable
32 float Flex{ get; set;}
33 int AlignSelf{get; set;}
35 private global::System.Runtime.InteropServices.HandleRef swigCPtr;
36 private bool swigCMemOwn;
37 private bool disposed;
38 private bool isDisposeQueued = false;
40 private IntPtr _rootFlex; // Pointer to the unmanged flex layout class.
42 internal struct MeasuredSize
44 public MeasuredSize(float x, float y)
53 [UnmanagedFunctionPointer(CallingConvention.StdCall)]
54 internal delegate MeasuredSize ChildMeasureCallback( global::System.IntPtr child, float width, int measureModeWidth, float height, int measureModeHeight );
56 event ChildMeasureCallback measureChildDelegate; // Stores a delegate to the child measure callback. Used for all children of this FlexLayout.
58 internal FlexLayout(global::System.IntPtr cPtr, bool cMemoryOwn)
60 swigCMemOwn = cMemoryOwn;
61 swigCPtr = new global::System.Runtime.InteropServices.HandleRef(this, cPtr);
62 _rootFlex = Interop.FlexLayout.FlexLayout_New();
63 measureChildDelegate = new ChildMeasureCallback(measureChild);
66 internal static global::System.Runtime.InteropServices.HandleRef getCPtr(FlexLayout obj)
68 return (obj == null) ? new global::System.Runtime.InteropServices.HandleRef(null, global::System.IntPtr.Zero) : obj.swigCPtr;
74 /// <since_tizen> 6 </since_tizen>
77 // Throw exception if Dispose() is called in separate thread.
78 if (!Window.IsInstalled())
80 throw new System.InvalidOperationException("This API called from separate thread. This API must be called from MainThread.");
85 Dispose(DisposeTypes.Implicit);
89 Dispose(DisposeTypes.Explicit);
90 System.GC.SuppressFinalize(this);
97 /// <since_tizen> 6 </since_tizen>
98 protected virtual void Dispose(DisposeTypes type)
105 if (type == DisposeTypes.Explicit)
108 // Release your own managed resources here.
109 // You should release all of your own disposable objects here.
113 // Release your own unmanaged resources here.
114 // You should not access any managed member here except static instance.
115 // because the execution order of Finalizes is non-deterministic.
116 if (swigCPtr.Handle != global::System.IntPtr.Zero)
121 Interop.FlexLayout.delete_FlexLayout(swigCPtr);
123 swigCPtr = new global::System.Runtime.InteropServices.HandleRef(null, global::System.IntPtr.Zero);
129 /// [Draft] Creates a FlexLayout object.
131 /// <since_tizen> 6 </since_tizen>
132 public FlexLayout() : this(Interop.FlexLayout.FlexLayout_New(), true)
134 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
137 internal static FlexLayout DownCast(BaseHandle handle)
139 FlexLayout ret = new FlexLayout(Interop.FlexLayout.FlexLayout_DownCast(BaseHandle.getCPtr(handle)), true);
140 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
144 internal FlexLayout(FlexLayout other) : this(Interop.FlexLayout.new_FlexLayout__SWIG_1(FlexLayout.getCPtr(other)), true)
146 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
149 internal FlexLayout Assign(FlexLayout other)
151 FlexLayout ret = new FlexLayout(Interop.FlexLayout.FlexLayout_Assign(swigCPtr, FlexLayout.getCPtr(other)), false);
152 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
156 internal void SetFlexDirection(FlexLayout.FlexDirection flexDirection)
158 Interop.FlexLayout.FlexLayout_SetFlexDirection(swigCPtr, (int)flexDirection);
162 internal FlexLayout.FlexDirection GetFlexDirection()
164 FlexLayout.FlexDirection ret = (FlexLayout.FlexDirection)Interop.FlexLayout.FlexLayout_GetFlexDirection(swigCPtr);
169 internal void SetFlexJustification(FlexLayout.FlexJustification flexJustification)
171 Interop.FlexLayout.FlexLayout_SetFlexJustification(swigCPtr, (int)flexJustification);
175 internal FlexLayout.FlexJustification GetFlexJustification()
177 FlexLayout.FlexJustification ret = (FlexLayout.FlexJustification)Interop.FlexLayout.FlexLayout_GetFlexJustification(swigCPtr);
181 internal void SetFlexWrap(FlexLayout.FlexWrapType flexWrap)
183 Interop.FlexLayout.FlexLayout_SetFlexWrap(swigCPtr, (int)flexWrap);
187 internal FlexLayout.FlexWrapType GetFlexWrap()
189 FlexLayout.FlexWrapType ret = (FlexLayout.FlexWrapType)Interop.FlexLayout.FlexLayout_GetFlexWrap(swigCPtr);
193 internal void SetFlexAlignment(FlexLayout.AlignmentType flexAlignment)
195 Interop.FlexLayout.FlexLayout_SetFlexAlignment(swigCPtr, (int)flexAlignment);
199 internal FlexLayout.AlignmentType GetFlexAlignment()
201 FlexLayout.AlignmentType ret = (FlexLayout.AlignmentType)Interop.FlexLayout.FlexLayout_GetFlexAlignment(swigCPtr);
202 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
206 internal void SetFlexItemsAlignment(FlexLayout.AlignmentType flexAlignment)
208 Interop.FlexLayout.FlexLayout_SetFlexItemsAlignment(swigCPtr, (int)flexAlignment);
212 internal FlexLayout.AlignmentType GetFlexItemsAlignment()
214 FlexLayout.AlignmentType ret = (FlexLayout.AlignmentType)Interop.FlexLayout.FlexLayout_GetFlexItemsAlignment(swigCPtr);
215 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
220 /// [Draft] Get/Set the flex direction in the layout.
221 /// The direction of the main-axis which determines the direction that flex items are laid out.
223 /// <since_tizen> 6 </since_tizen>
224 public FlexDirection Direction
228 return GetFlexDirection();
232 SetFlexDirection(value);
237 /// [Draft] Get/Set the justification in the layout.
239 /// <since_tizen> 6 </since_tizen>
240 public FlexJustification Justification
244 return GetFlexJustification();
248 SetFlexJustification(value);
253 /// [Draft] Get/Set the wrap in the layout.
255 /// <since_tizen> 6 </since_tizen>
256 public FlexWrapType WrapType
260 return GetFlexWrap();
269 /// [Draft] Get/Set the alignment of the layout content.
271 /// <since_tizen> 6 </since_tizen>
272 public AlignmentType Alignment
276 return GetFlexAlignment();
280 SetFlexAlignment(value);
285 /// [Draft] Get/Set the alignment of the layout items.
287 /// <since_tizen> 6 </since_tizen>
288 public AlignmentType ItemsAlignment
292 return GetFlexItemsAlignment();
296 SetFlexItemsAlignment(value);
301 /// [Draft] Enumeration for the direction of the main axis in the flex container.
302 /// This determines the direction that flex items are laid out in the flex container.
304 /// <since_tizen> 6 </since_tizen>
305 public enum FlexDirection
308 /// The flexible items are displayed vertically as a column
312 /// The flexible items are displayed vertically as a column, but in reverse order
316 /// The flexible items are displayed horizontally as a row
320 /// The flexible items are displayed horizontally as a row, but in reverse order
326 /// [Draft] Enumeration for the alignment of the flex items when the items do not use all available space on the main-axis.
328 /// <since_tizen> 6 </since_tizen>
329 public enum FlexJustification
332 /// Items are positioned at the beginning of the container
336 /// Items are positioned at the center of the container
340 /// Items are positioned at the end of the container
344 /// Items are positioned with equal space between the lines
348 /// Items are positioned with equal space before, between, and after the lines
354 /// [Draft] Enumeration for the wrap type of the flex container when there is no enough room for all the items on one flex line.
356 /// <since_tizen> 6 </since_tizen>
357 public enum FlexWrapType
360 /// Flex items laid out in single line (shrunk to fit the flex container along the main axis)
364 /// Flex items laid out in multiple lines if needed
370 /// [Draft] 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.
372 /// <since_tizen> 6 </since_tizen>
373 public enum AlignmentType
376 /// Inherits the same alignment from the parent
380 /// At the beginning of the container
384 /// At the center of the container
388 /// At the end of the container
392 /// Stretch to fit the container
397 private MeasuredSize measureChild(global::System.IntPtr child, float width, int measureModeWidth, float height, int measureModeHeight)
399 View view = Registry.GetManagedBaseHandleFromNativePtr(child) as View;
400 Size2D viewSize = new Size2D(8,8);
403 viewSize = view.NaturalSize2D;
405 return new MeasuredSize(viewSize.Width,viewSize.Height);
408 void InsertChild( LayoutItem child )
410 // Store created node for child
411 Interop.FlexLayout.FlexLayout_AddChild(swigCPtr, View.getCPtr(child.Owner), measureChildDelegate, LayoutChildren.Count-1);
415 /// Callback when child is added to container.<br />
416 /// Derived classes can use this to set their own child properties on the child layout's owner.<br />
418 /// <param name="child">The Layout child.</param>
419 /// <since_tizen> 6 </since_tizen>
420 protected override void OnChildAdd(LayoutItem child)
426 /// Callback when child is removed from container.<br />
428 /// <param name="child">The Layout child.</param>
429 /// <since_tizen> 6 </since_tizen>
430 protected override void OnChildRemove(LayoutItem child)
432 // When child View is removed from it's parent View (that is a Layout) then remove it from the layout too.
433 // FlexLayout refers to the child as a View not LayoutItem.
434 Interop.FlexLayout.FlexLayout_RemoveChild(swigCPtr, child);
438 /// Measure the layout and its content to determine the measured width and the measured height.<br />
440 /// <param name="widthMeasureSpec">horizontal space requirements as imposed by the parent.</param>
441 /// <param name="heightMeasureSpec">vertical space requirements as imposed by the parent.</param>
442 /// <since_tizen> 6 </since_tizen>
443 protected override void OnMeasure( MeasureSpecification widthMeasureSpec, MeasureSpecification heightMeasureSpec )
445 bool isLayoutRtl = Owner.LayoutDirection == ViewLayoutDirectionType.RTL;
446 Extents padding = Owner.Padding;
447 Extents margin = Owner.Margin;
449 Interop.FlexLayout.FlexLayout_SetMargin(swigCPtr, Extents.getCPtr(margin));
450 Interop.FlexLayout.FlexLayout_SetPadding(swigCPtr, Extents.getCPtr(padding));
455 if( widthMeasureSpec.Mode == MeasureSpecification.ModeType.Exactly || widthMeasureSpec.Mode == MeasureSpecification.ModeType.AtMost )
457 width = widthMeasureSpec.Size.AsDecimal();
460 if ( heightMeasureSpec.Mode == MeasureSpecification.ModeType.Exactly || heightMeasureSpec.Mode == MeasureSpecification.ModeType.AtMost )
462 height = heightMeasureSpec.Size.AsDecimal();
465 Interop.FlexLayout.FlexLayout_CalculateLayout( swigCPtr, width, height, isLayoutRtl );
467 SetMeasuredDimensions( GetDefaultSize( new LayoutLength( (float)Interop.FlexLayout.FlexLayout_GetWidth(swigCPtr) ), widthMeasureSpec ),
468 GetDefaultSize( new LayoutLength( (float)Interop.FlexLayout.FlexLayout_GetHeight(swigCPtr) ), heightMeasureSpec ) );
472 /// Assign a size and position to each of its children.<br />
474 /// <param name="changed">This is a new size or position for this layout.</param>
475 /// <param name="left">Left position, relative to parent.</param>
476 /// <param name="top"> Top position, relative to parent.</param>
477 /// <param name="right">Right position, relative to parent.</param>
478 /// <param name="bottom">Bottom position, relative to parent.</param>
479 /// <since_tizen> 6 </since_tizen>
480 protected override void OnLayout( bool changed, LayoutLength left, LayoutLength top, LayoutLength right, LayoutLength bottom )
483 bool isLayoutRtl = Owner.LayoutDirection == ViewLayoutDirectionType.RTL;
484 LayoutLength width = right - left;
485 LayoutLength height = bottom - top;
487 // Call to FlexLayout implementation to calculate layout values for later retrieval.
488 Interop.FlexLayout.FlexLayout_CalculateLayout( swigCPtr, width.AsDecimal(), height.AsDecimal(), isLayoutRtl );
490 int count = LayoutChildren.Count;
491 for( int childIndex = 0; childIndex < count; childIndex++)
493 LayoutItem childLayout = LayoutChildren[childIndex];
494 if( childLayout != null )
496 // Get the frame for the child, start, top, end, bottom.
497 Vector4 frame = new Vector4(Interop.FlexLayout.FlexLayout_GetNodeFrame(swigCPtr, childIndex ), true);
498 childLayout.Layout( new LayoutLength(frame.X), new LayoutLength(frame.Y), new LayoutLength(frame.Z), new LayoutLength(frame.W) );
504 } // namesspace Tizen.NUI