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;
21 using System.Diagnostics;
26 /// [Draft] This class implements a flex layout.
27 /// The flex layout implementation is based on open source Facebook Yoga layout engine.
28 /// For more information about the flex layout API and how to use it please refer to https://yogalayout.com/docs/
29 /// We implement the subset of the API in the class below.
31 public class FlexLayout : LayoutGroup, global::System.IDisposable
33 private static bool LayoutDebugFlex = false; // Debug flag
34 float Flex{ get; set;}
35 int AlignSelf{get; set;}
37 private global::System.Runtime.InteropServices.HandleRef swigCPtr;
38 private bool swigCMemOwn;
39 private bool disposed;
40 private bool isDisposeQueued = false;
42 private IntPtr _rootFlex; // Pointer to the unmanged flex layout class.
44 internal const float FlexUndefined = 10E20F; // Auto setting which is equivalent to WrapContent.
46 internal struct MeasuredSize
48 public MeasuredSize(float x, float y)
57 [UnmanagedFunctionPointer(CallingConvention.StdCall)]
58 internal delegate MeasuredSize ChildMeasureCallback( global::System.IntPtr child, float width, int measureModeWidth, float height, int measureModeHeight );
60 event ChildMeasureCallback measureChildDelegate; // Stores a delegate to the child measure callback. Used for all children of this FlexLayout.
62 internal FlexLayout(global::System.IntPtr cPtr, bool cMemoryOwn)
64 swigCMemOwn = cMemoryOwn;
65 swigCPtr = new global::System.Runtime.InteropServices.HandleRef(this, cPtr);
66 _rootFlex = Interop.FlexLayout.FlexLayout_New();
67 measureChildDelegate = new ChildMeasureCallback(measureChild);
70 internal static global::System.Runtime.InteropServices.HandleRef getCPtr(FlexLayout obj)
72 return (obj == null) ? new global::System.Runtime.InteropServices.HandleRef(null, global::System.IntPtr.Zero) : obj.swigCPtr;
78 /// <since_tizen> 6 </since_tizen>
81 // Throw exception if Dispose() is called in separate thread.
82 if (!Window.IsInstalled())
84 throw new System.InvalidOperationException("This API called from separate thread. This API must be called from MainThread.");
89 Dispose(DisposeTypes.Implicit);
93 Dispose(DisposeTypes.Explicit);
94 System.GC.SuppressFinalize(this);
101 /// <since_tizen> 6 </since_tizen>
102 protected virtual void Dispose(DisposeTypes type)
109 if (type == DisposeTypes.Explicit)
112 // Release your own managed resources here.
113 // You should release all of your own disposable objects here.
117 // Release your own unmanaged resources here.
118 // You should not access any managed member here except static instance.
119 // because the execution order of Finalizes is non-deterministic.
120 if (swigCPtr.Handle != global::System.IntPtr.Zero)
125 Interop.FlexLayout.delete_FlexLayout(swigCPtr);
127 swigCPtr = new global::System.Runtime.InteropServices.HandleRef(null, global::System.IntPtr.Zero);
133 /// [Draft] Creates a FlexLayout object.
135 /// <since_tizen> 6 </since_tizen>
136 public FlexLayout() : this(Interop.FlexLayout.FlexLayout_New(), true)
138 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
141 internal static FlexLayout DownCast(BaseHandle handle)
143 FlexLayout ret = new FlexLayout(Interop.FlexLayout.FlexLayout_DownCast(BaseHandle.getCPtr(handle)), true);
144 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
148 internal FlexLayout(FlexLayout other) : this(Interop.FlexLayout.new_FlexLayout__SWIG_1(FlexLayout.getCPtr(other)), true)
150 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
153 internal FlexLayout Assign(FlexLayout other)
155 FlexLayout ret = new FlexLayout(Interop.FlexLayout.FlexLayout_Assign(swigCPtr, FlexLayout.getCPtr(other)), false);
156 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
160 internal void SetFlexDirection(FlexLayout.FlexDirection flexDirection)
162 Interop.FlexLayout.FlexLayout_SetFlexDirection(swigCPtr, (int)flexDirection);
166 internal FlexLayout.FlexDirection GetFlexDirection()
168 FlexLayout.FlexDirection ret = (FlexLayout.FlexDirection)Interop.FlexLayout.FlexLayout_GetFlexDirection(swigCPtr);
173 internal void SetFlexJustification(FlexLayout.FlexJustification flexJustification)
175 Interop.FlexLayout.FlexLayout_SetFlexJustification(swigCPtr, (int)flexJustification);
179 internal FlexLayout.FlexJustification GetFlexJustification()
181 FlexLayout.FlexJustification ret = (FlexLayout.FlexJustification)Interop.FlexLayout.FlexLayout_GetFlexJustification(swigCPtr);
185 internal void SetFlexWrap(FlexLayout.FlexWrapType flexWrap)
187 Interop.FlexLayout.FlexLayout_SetFlexWrap(swigCPtr, (int)flexWrap);
191 internal FlexLayout.FlexWrapType GetFlexWrap()
193 FlexLayout.FlexWrapType ret = (FlexLayout.FlexWrapType)Interop.FlexLayout.FlexLayout_GetFlexWrap(swigCPtr);
197 internal void SetFlexAlignment(FlexLayout.AlignmentType flexAlignment)
199 Interop.FlexLayout.FlexLayout_SetFlexAlignment(swigCPtr, (int)flexAlignment);
203 internal FlexLayout.AlignmentType GetFlexAlignment()
205 FlexLayout.AlignmentType ret = (FlexLayout.AlignmentType)Interop.FlexLayout.FlexLayout_GetFlexAlignment(swigCPtr);
206 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
210 internal void SetFlexItemsAlignment(FlexLayout.AlignmentType flexAlignment)
212 Interop.FlexLayout.FlexLayout_SetFlexItemsAlignment(swigCPtr, (int)flexAlignment);
216 internal FlexLayout.AlignmentType GetFlexItemsAlignment()
218 FlexLayout.AlignmentType ret = (FlexLayout.AlignmentType)Interop.FlexLayout.FlexLayout_GetFlexItemsAlignment(swigCPtr);
219 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
224 /// [Draft] Get/Set the flex direction in the layout.
225 /// The direction of the main-axis which determines the direction that flex items are laid out.
227 /// <since_tizen> 6 </since_tizen>
228 public FlexDirection Direction
232 return GetFlexDirection();
236 SetFlexDirection(value);
241 /// [Draft] Get/Set the justification in the layout.
243 /// <since_tizen> 6 </since_tizen>
244 public FlexJustification Justification
248 return GetFlexJustification();
252 SetFlexJustification(value);
257 /// [Draft] Get/Set the wrap in the layout.
259 /// <since_tizen> 6 </since_tizen>
260 public FlexWrapType WrapType
264 return GetFlexWrap();
273 /// [Draft] Get/Set the alignment of the layout content.
275 /// <since_tizen> 6 </since_tizen>
276 public AlignmentType Alignment
280 return GetFlexAlignment();
284 SetFlexAlignment(value);
289 /// [Draft] Get/Set the alignment of the layout items.
291 /// <since_tizen> 6 </since_tizen>
292 public AlignmentType ItemsAlignment
296 return GetFlexItemsAlignment();
300 SetFlexItemsAlignment(value);
305 /// [Draft] Enumeration for the direction of the main axis in the flex container.
306 /// This determines the direction that flex items are laid out in the flex container.
308 /// <since_tizen> 6 </since_tizen>
309 public enum FlexDirection
312 /// The flexible items are displayed vertically as a column
316 /// The flexible items are displayed vertically as a column, but in reverse order
320 /// The flexible items are displayed horizontally as a row
324 /// The flexible items are displayed horizontally as a row, but in reverse order
330 /// [Draft] Enumeration for the alignment of the flex items when the items do not use all available space on the main-axis.
332 /// <since_tizen> 6 </since_tizen>
333 public enum FlexJustification
336 /// Items are positioned at the beginning of the container
340 /// Items are positioned at the center of the container
344 /// Items are positioned at the end of the container
348 /// Items are positioned with equal space between the lines
352 /// Items are positioned with equal space before, between, and after the lines
358 /// [Draft] Enumeration for the wrap type of the flex container when there is no enough room for all the items on one flex line.
360 /// <since_tizen> 6 </since_tizen>
361 public enum FlexWrapType
364 /// Flex items laid out in single line (shrunk to fit the flex container along the main axis)
368 /// Flex items laid out in multiple lines if needed
374 /// [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.
376 /// <since_tizen> 6 </since_tizen>
377 public enum AlignmentType
380 /// Inherits the same alignment from the parent
384 /// At the beginning of the container
388 /// At the center of the container
392 /// At the end of the container
396 /// Stretch to fit the container
401 private MeasuredSize measureChild(global::System.IntPtr child, float width, int measureModeWidth, float height, int measureModeHeight)
403 View view = Registry.GetManagedBaseHandleFromNativePtr(child) as View;
404 Size2D viewSize = new Size2D(8,8);
407 viewSize = view.Size2D;
410 // If size not set on child then use NaturalSize
411 if(viewSize.Width ==0 && viewSize.Height==0)
413 viewSize = view.NaturalSize2D;
416 Debug.WriteLineIf( LayoutDebugFlex, "FlexLayout measureChild View:" + view.Name
417 + "Size:" + viewSize.Width
421 return new MeasuredSize(viewSize.Width,viewSize.Height);
424 void InsertChild( LayoutItem child )
426 // Store created node for child
427 Interop.FlexLayout.FlexLayout_AddChild(swigCPtr, View.getCPtr(child.Owner), measureChildDelegate, LayoutChildren.Count-1);
431 /// Callback when child is added to container.<br />
432 /// Derived classes can use this to set their own child properties on the child layout's owner.<br />
434 /// <param name="child">The Layout child.</param>
435 /// <since_tizen> 6 </since_tizen>
436 protected override void OnChildAdd(LayoutItem child)
442 /// Callback when child is removed from container.<br />
444 /// <param name="child">The Layout child.</param>
445 /// <since_tizen> 6 </since_tizen>
446 protected override void OnChildRemove(LayoutItem child)
448 // When child View is removed from it's parent View (that is a Layout) then remove it from the layout too.
449 // FlexLayout refers to the child as a View not LayoutItem.
450 Interop.FlexLayout.FlexLayout_RemoveChild(swigCPtr, child);
454 /// Measure the layout and its content to determine the measured width and the measured height.<br />
456 /// <param name="widthMeasureSpec">horizontal space requirements as imposed by the parent.</param>
457 /// <param name="heightMeasureSpec">vertical space requirements as imposed by the parent.</param>
458 /// <since_tizen> 6 </since_tizen>
459 protected override void OnMeasure( MeasureSpecification widthMeasureSpec, MeasureSpecification heightMeasureSpec )
461 bool isLayoutRtl = Owner.LayoutDirection == ViewLayoutDirectionType.RTL;
462 Extents padding = Owner.Padding;
463 Extents margin = Owner.Margin;
465 Interop.FlexLayout.FlexLayout_SetMargin(swigCPtr, Extents.getCPtr(margin));
466 Interop.FlexLayout.FlexLayout_SetPadding(swigCPtr, Extents.getCPtr(padding));
468 float width = FlexUndefined; // Behaves as WrapContent (Flex Auto)
469 float height = FlexUndefined; // Behaves as WrapContent (Flex Auto)
471 if( widthMeasureSpec.Mode == MeasureSpecification.ModeType.Exactly || widthMeasureSpec.Mode == MeasureSpecification.ModeType.AtMost )
473 width = widthMeasureSpec.Size.AsDecimal();
476 if ( heightMeasureSpec.Mode == MeasureSpecification.ModeType.Exactly || heightMeasureSpec.Mode == MeasureSpecification.ModeType.AtMost )
478 height = heightMeasureSpec.Size.AsDecimal();
481 Interop.FlexLayout.FlexLayout_CalculateLayout( swigCPtr, width, height, isLayoutRtl );
483 LayoutLength flexLayoutWidth = new LayoutLength( Interop.FlexLayout.FlexLayout_GetWidth(swigCPtr) );
484 LayoutLength flexLayoutHeight = new LayoutLength( Interop.FlexLayout.FlexLayout_GetHeight(swigCPtr) );
486 Debug.WriteLineIf( LayoutDebugFlex, "FlexLayout OnMeasure width:" + flexLayoutWidth.AsRoundedValue()
487 + " height:" + flexLayoutHeight.AsRoundedValue() );
489 SetMeasuredDimensions( GetDefaultSize(flexLayoutWidth, widthMeasureSpec ),
490 GetDefaultSize(flexLayoutHeight, heightMeasureSpec ) );
494 /// Assign a size and position to each of its children.<br />
496 /// <param name="changed">This is a new size or position for this layout.</param>
497 /// <param name="left">Left position, relative to parent.</param>
498 /// <param name="top"> Top position, relative to parent.</param>
499 /// <param name="right">Right position, relative to parent.</param>
500 /// <param name="bottom">Bottom position, relative to parent.</param>
501 /// <since_tizen> 6 </since_tizen>
502 protected override void OnLayout( bool changed, LayoutLength left, LayoutLength top, LayoutLength right, LayoutLength bottom )
505 bool isLayoutRtl = Owner.LayoutDirection == ViewLayoutDirectionType.RTL;
506 LayoutLength width = right - left;
507 LayoutLength height = bottom - top;
509 // Call to FlexLayout implementation to calculate layout values for later retrieval.
510 Interop.FlexLayout.FlexLayout_CalculateLayout( swigCPtr, width.AsDecimal(), height.AsDecimal(), isLayoutRtl );
512 int count = LayoutChildren.Count;
513 for( int childIndex = 0; childIndex < count; childIndex++)
515 LayoutItem childLayout = LayoutChildren[childIndex];
516 if( childLayout != null )
518 // Get the frame for the child, start, top, end, bottom.
519 Vector4 frame = new Vector4(Interop.FlexLayout.FlexLayout_GetNodeFrame(swigCPtr, childIndex ), true);
520 childLayout.Layout( new LayoutLength(frame.X), new LayoutLength(frame.Y), new LayoutLength(frame.Z), new LayoutLength(frame.W) );
526 } // namesspace Tizen.NUI