2 * Copyright (c) 2021 Samsung Electronics Co., Ltd.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
18 using System.Collections.Generic;
19 using System.ComponentModel;
22 using Tizen.NUI.BaseComponents;
23 using Tizen.NUI.Binding.Internals;
24 using static Tizen.NUI.Binding.BindableObject;
29 /// [Draft] LayoutGroup class providing container functionality.
31 public class LayoutGroup : LayoutItem, ILayoutParent
34 /// [Draft] List of child layouts in this container.
36 /// <since_tizen> 6 </since_tizen>
37 protected List<LayoutItem> LayoutChildren { get; } // Children of this LayoutGroup
40 /// [Draft] Constructor
42 /// <since_tizen> 6 </since_tizen>
45 LayoutChildren = new List<LayoutItem>();
49 /// returns an enumerable collection of the child layouts that owner's <see cref="View.ExcludeLayouting"/> is false.
51 /// <returns>An enumerable collection of the child layouts that affected by this layout.</returns>
52 [EditorBrowsable(EditorBrowsableState.Never)]
53 protected IEnumerable<LayoutItem> IterateLayoutChildren()
55 return LayoutChildren.Where<LayoutItem>(childLayout => childLayout.SetPositionByLayout);
59 /// From ILayoutParent.<br />
61 /// <exception cref="ArgumentNullException"> Thrown when childLayout is null. </exception>
62 /// <since_tizen> 6 </since_tizen>
63 /// <param name="childLayout">LayoutItem to add to the layout group.</param>
64 public virtual void Add(LayoutItem childLayout)
66 if (null == childLayout)
68 throw new ArgumentNullException(nameof(childLayout));
70 LayoutChildren.Add(childLayout);
71 childLayout.SetParent(this);
72 // Child added to use a Add transition.
73 childLayout.ConditionForAnimation = ConditionForAnimation | TransitionCondition.Add;
74 // Child's parent sets all other children not being added to a ChangeOnAdd transition.
75 SetConditionsForAnimationOnLayoutGroup(TransitionCondition.ChangeOnAdd);
76 OnChildAdd(childLayout);
81 /// Remove all layout children.<br />
83 /// <since_tizen> 6 </since_tizen>
84 public void RemoveAll()
86 foreach (LayoutItem childLayout in LayoutChildren)
88 childLayout.ConditionForAnimation = ConditionForAnimation | TransitionCondition.Remove;
89 childLayout.Owner = null;
91 LayoutChildren.Clear();
92 // todo ensure child LayoutItems are still not parented to this group.
97 /// From ILayoutParent
99 /// <param name="layoutItem">LayoutItem to remove from the layout group.</param>
100 /// <since_tizen> 6 </since_tizen>
101 public virtual void Remove(LayoutItem layoutItem)
103 bool childRemoved = false;
104 foreach (LayoutItem childLayout in LayoutChildren.ToList())
106 if (childLayout == layoutItem)
108 childLayout.ClearReplaceFlag();
109 LayoutChildren.Remove(childLayout);
111 if (LayoutWithTransition)
113 if (!childLayout.IsReplaceFlag())
115 NUIApplication.GetDefaultWindow().LayoutController.AddToRemovalStack(childLayout);
118 childLayout.ConditionForAnimation = childLayout.ConditionForAnimation | TransitionCondition.Remove;
119 // Add LayoutItem to the transition stack so can animate it out.
120 NUIApplication.GetDefaultWindow().LayoutController.AddTransitionDataEntry(new LayoutData(layoutItem, ConditionForAnimation, 0, 0, 0, 0));
123 // Reset condition for animation ready for next transition when required.
124 // SetFrame usually would do this but this LayoutItem is being removed.
125 childLayout.ConditionForAnimation = TransitionCondition.Unspecified;
134 // If child removed then set all siblings not being added to a ChangeOnRemove transition.
135 SetConditionsForAnimationOnLayoutGroup(TransitionCondition.ChangeOnRemove);
137 OnChildRemove(layoutItem);
143 /// Sets the sibling order of the layout item so the layout can be defined within the same parent.
145 /// <param name="order">the sibling order of the layout item</param>
146 /// <since_tizen> 6 </since_tizen>
147 /// This will be public opened in tizen_next after ACR done. Before ACR, need to be hidden as inhouse API.
148 [EditorBrowsable(EditorBrowsableState.Never)]
149 public void ChangeLayoutSiblingOrder(int order)
153 var ownerParent = Owner.GetParent() as View;
154 if (ownerParent != null)
156 var parent = ownerParent.Layout as LayoutGroup;
158 if (parent != null && parent.LayoutChildren.Count > order)
160 parent.LayoutChildren.Remove(this);
161 parent.LayoutChildren.Insert(order, this);
168 // Attaches to View ChildAdded signal so called when a View is added to a view.
169 private void AddChildToLayoutGroup(View child)
171 if (View.LayoutingDisabled)
175 // Only give children a layout if their parent is an explicit container or a pure View.
176 // Pure View meaning not derived from a View, e.g a Legacy container.
177 // layoutSet flag is true when the View became a layout using the set Layout API opposed to automatically due to it's parent.
178 // First time the set Layout API is used by any View the Window no longer has layoutingDisabled.
180 // If child already has a Layout then don't change it.
181 if (null == child.Layout)
183 // Only wrap View with a Layout if a child a pure View or Layout explicitly set on this Layout
184 if ((true == Owner.LayoutSet || GetType() == typeof(View)))
186 // If child of this layout is a pure View then assign it a LayoutGroup
187 // If the child is derived from a View then it may be a legacy or existing container hence will do layouting itself.
188 child.Layout = child.CreateDefaultLayout();
195 // Parent transitions are not attached to children.
199 /// If the child has a layout then it is removed from the parent layout.
201 /// <param name="child">Child View to remove.</param>
202 internal void RemoveChildFromLayoutGroup(View child)
204 if (child.Layout != null)
206 Remove(child.Layout);
211 /// Set all children in a LayoutGroup to the supplied condition.
212 /// Children with Add or Remove conditions should not be changed.
214 private void SetConditionsForAnimationOnLayoutGroup(TransitionCondition conditionToSet)
216 foreach (LayoutItem childLayout in LayoutChildren)
218 switch (conditionToSet)
220 case TransitionCondition.ChangeOnAdd:
222 // If other children also being added (TransitionCondition.Add) then do not change their
223 // conditions, Continue to use their Add transitions.
224 if (childLayout.ConditionForAnimation.HasFlag(TransitionCondition.Add))
226 break; // Child being Added so don't update it's condition
230 // Set siblings for the child being added to use the ChangeOnAdd transition.
231 childLayout.ConditionForAnimation = TransitionCondition.ChangeOnAdd;
235 case TransitionCondition.ChangeOnRemove:
237 if (childLayout.ConditionForAnimation.HasFlag(TransitionCondition.Remove))
239 break; // Child being Removed so don't update it's condition
243 childLayout.ConditionForAnimation = TransitionCondition.ChangeOnRemove;
247 case TransitionCondition.LayoutChanged:
249 childLayout.ConditionForAnimation = TransitionCondition.LayoutChanged;
258 /// Callback for View.ChildAdded event
260 /// <param name="sender">The object triggering the event.</param>
261 /// <param name="childAddedEvent">Arguments from the event.</param>
262 void OnChildAddedToOwner(object sender, View.ChildAddedEventArgs childAddedEvent)
264 AddChildToLayoutGroup(childAddedEvent.Added);
268 /// Callback for View.ChildRemoved event
270 /// <param name="sender">The object triggering the event.</param>
271 /// <param name="childRemovedEvent">Arguments from the event.</param>
272 void OnChildRemovedFromOwner(object sender, View.ChildRemovedEventArgs childRemovedEvent)
274 RemoveChildFromLayoutGroup(childRemovedEvent.Removed);
278 /// Calculate the right measure spec for this child.
279 /// Does the hard part of MeasureChildren: figuring out the MeasureSpec to
280 /// pass to a particular child. This method figures out the right MeasureSpec
281 /// for one dimension (height or width) of one child view.<br />
283 /// <param name="parentMeasureSpec">The requirements for this view. MeasureSpecification.</param>
284 /// <param name="padding">The padding of this view for the current dimension and margins, if applicable. LayoutLength.</param>
285 /// <param name="childDimension"> How big the child wants to be in the current dimension. LayoutLength.</param>
286 /// <returns>a MeasureSpec for the child.</returns>
287 public static MeasureSpecification GetChildMeasureSpecification(MeasureSpecification parentMeasureSpec, LayoutLength padding, LayoutLength childDimension)
289 MeasureSpecification.ModeType specMode = parentMeasureSpec.Mode;
290 MeasureSpecification.ModeType resultMode = MeasureSpecification.ModeType.Unspecified;
292 // Child only can use parent's size without parent's padding and own margin.
293 LayoutLength resultSize = new LayoutLength(Math.Max(0.0f, (parentMeasureSpec.Size - padding).AsDecimal()));
296 // Parent has imposed an exact size on us
297 case MeasureSpecification.ModeType.Exactly:
299 if ((int)childDimension.AsRoundedValue() == LayoutParamPolicies.MatchParent)
301 resultMode = MeasureSpecification.ModeType.Exactly;
303 else if ((int)childDimension.AsRoundedValue() == LayoutParamPolicies.WrapContent)
305 resultMode = MeasureSpecification.ModeType.AtMost;
309 resultSize = childDimension;
310 resultMode = MeasureSpecification.ModeType.Exactly;
316 // Parent has imposed a maximum size on us
317 case MeasureSpecification.ModeType.AtMost:
319 if (childDimension.AsRoundedValue() == LayoutParamPolicies.MatchParent)
321 // Crashed. Cannot calculate.
323 // Child wants to be our size, but our size is not fixed.
324 // Constrain child to not be bigger than us.
325 resultMode = MeasureSpecification.ModeType.AtMost;
327 else if (childDimension.AsRoundedValue() == LayoutParamPolicies.WrapContent)
329 // Child wants to determine its own size. It can't be
332 // Don't need parent's size. Size of this child will be determined by its children.
333 resultMode = MeasureSpecification.ModeType.AtMost;
337 // Child wants a specific size... so be it
338 resultSize = childDimension;
339 resultMode = MeasureSpecification.ModeType.Exactly;
345 // Parent asked to see how big we want to be
346 case MeasureSpecification.ModeType.Unspecified:
348 if ((childDimension.AsRoundedValue() == LayoutParamPolicies.MatchParent))
350 // Child wants to be our size... find out how big it should be
352 // There is no one who has exact size in parent hierarchy.
354 resultMode = MeasureSpecification.ModeType.Unspecified;
356 else if (childDimension.AsRoundedValue() == (LayoutParamPolicies.WrapContent))
358 // Child wants to determine its own size.... find out how big
360 resultMode = MeasureSpecification.ModeType.Unspecified;
364 // Child wants a specific size... let him have it
365 resultSize = childDimension;
366 resultMode = MeasureSpecification.ModeType.Exactly;
372 return new MeasureSpecification(resultSize, resultMode);
376 /// Measure the layout and its content to determine the measured width and the measured height.<br />
377 /// If this method is overridden, it is the subclass's responsibility to make
378 /// sure the measured height and width are at least the layout's minimum height
379 /// and width. <br />
381 /// <param name="widthMeasureSpec">horizontal space requirements as imposed by the parent.</param>
382 /// <param name="heightMeasureSpec">vertical space requirements as imposed by the parent.</param>
383 /// <since_tizen> 6 </since_tizen>
384 protected override void OnMeasure(MeasureSpecification widthMeasureSpec, MeasureSpecification heightMeasureSpec)
386 LayoutLength measuredWidth = new LayoutLength(0.0f);
387 LayoutLength measuredHeight = new LayoutLength(0.0f);
389 // Layout takes size of largest child width and largest child height dimensions
390 foreach (LayoutItem childLayout in LayoutChildren)
392 if (childLayout != null)
394 MeasureChildWithMargins(childLayout, widthMeasureSpec, new LayoutLength(0), heightMeasureSpec, new LayoutLength(0));
395 LayoutLength childWidth = new LayoutLength(childLayout.MeasuredWidth.Size);
396 LayoutLength childHeight = new LayoutLength(childLayout.MeasuredHeight.Size);
398 Extents childMargin = childLayout.Margin;
399 measuredWidth = new LayoutLength(Math.Max(measuredWidth.AsDecimal(), childWidth.AsDecimal() + childMargin.Start + childMargin.End));
400 measuredHeight = new LayoutLength(Math.Max(measuredHeight.AsDecimal(), childHeight.AsDecimal() + childMargin.Top + childMargin.Bottom));
404 if (0 == LayoutChildren.Count)
406 // Must be a leaf as has no children
407 measuredWidth = GetDefaultSize(SuggestedMinimumWidth, widthMeasureSpec);
408 measuredHeight = GetDefaultSize(SuggestedMinimumHeight, heightMeasureSpec);
411 SetMeasuredDimensions(new MeasuredSize(measuredWidth, MeasuredSize.StateType.MeasuredSizeOK),
412 new MeasuredSize(measuredHeight, MeasuredSize.StateType.MeasuredSizeOK));
415 internal override void OnMeasureIndependentChildren(MeasureSpecification widthMeasureSpec, MeasureSpecification heightMeasureSpec)
417 foreach (var childLayout in LayoutChildren)
419 if (!childLayout.SetPositionByLayout)
421 MeasureChildWithoutPadding(childLayout, widthMeasureSpec, heightMeasureSpec);
427 /// Called from Layout() when this layout should assign a size and position to each of its children.<br />
428 /// Derived classes with children should override this method and call Layout() on each of their children.<br />
430 /// <param name="changed">This is a new size or position for this layout.</param>
431 /// <param name="left">Left position, relative to parent.</param>
432 /// <param name="top"> Top position, relative to parent.</param>
433 /// <param name="right">Right position, relative to parent.</param>
434 /// <param name="bottom">Bottom position, relative to parent.</param>
435 /// <since_tizen> 6 </since_tizen>
436 protected override void OnLayout(bool changed, LayoutLength left, LayoutLength top, LayoutLength right, LayoutLength bottom)
438 foreach (LayoutItem childLayout in LayoutChildren)
440 if (childLayout != null)
442 // Use position if explicitly set to child otherwise will be top left.
443 var childLeft = new LayoutLength(childLayout.Owner.PositionX);
444 var childTop = new LayoutLength(childLayout.Owner.PositionY);
450 // Margin and Padding only supported when child anchor point is TOP_LEFT.
451 if (owner.PivotPoint == PivotPoint.TopLeft || (owner.PositionUsesPivotPoint == false))
453 childLeft = childLeft + owner.Padding.Start + childLayout.Margin.Start;
454 childTop = childTop + owner.Padding.Top + childLayout.Margin.Top;
457 childLayout.Layout(childLeft, childTop, childLeft + childLayout.MeasuredWidth.Size, childTop + childLayout.MeasuredHeight.Size);
463 /// Layout independent children those Owners have true ExcludeLayouting. <br />
464 /// These children are required not to be affected by this layout. <br />
466 internal override void OnLayoutIndependentChildren(bool changed, LayoutLength left, LayoutLength top, LayoutLength right, LayoutLength bottom)
468 foreach (var childLayout in LayoutChildren)
470 if (!childLayout.SetPositionByLayout)
472 LayoutLength childWidth = childLayout.MeasuredWidth.Size;
473 LayoutLength childHeight = childLayout.MeasuredHeight.Size;
475 LayoutLength childPositionX = new LayoutLength(childLayout.Owner.PositionX);
476 LayoutLength childPositionY = new LayoutLength(childLayout.Owner.PositionY);
478 childLayout.Layout(childPositionX, childPositionY, childPositionX + childWidth, childPositionY + childHeight, true);
484 /// Overridden method called when the layout is attached to an owner.<br />
486 /// <since_tizen> 6 </since_tizen>
487 protected override void OnAttachedToOwner()
489 // Layout takes ownership of it's owner's children.
490 foreach (View view in Owner.Children)
492 AddChildToLayoutGroup(view);
495 // Connect to owner ChildAdded signal.
496 Owner.ChildAdded += OnChildAddedToOwner;
498 // Removing Child from the owners View will directly call the LayoutGroup removal API.
502 /// Virtual method to allow derived classes to remove any children before it is removed from
505 [EditorBrowsable(EditorBrowsableState.Never)]
506 protected override void OnUnparent()
508 // Disconnect to owner ChildAdded signal.
509 Owner.ChildAdded -= OnChildAddedToOwner;
512 // Virtual Methods that can be overridden by derived classes.
515 /// Callback when child is added to container.<br />
516 /// Derived classes can use this to set their own child properties on the child layout's owner.<br />
518 /// <param name="child">The Layout child.</param>
519 /// <since_tizen> 6 </since_tizen>
520 protected virtual void OnChildAdd(LayoutItem child)
525 /// Callback when child is removed from container.<br />
527 /// <param name="child">The Layout child.</param>
528 /// <since_tizen> 6 </since_tizen>
529 protected virtual void OnChildRemove(LayoutItem child)
534 /// Ask all of the children of this view to measure themselves, taking into
535 /// account both the MeasureSpec requirements for this view and its padding.<br />
536 /// The heavy lifting is done in GetChildMeasureSpec.<br />
538 /// <param name="widthMeasureSpec">The width requirements for this view.</param>
539 /// <param name="heightMeasureSpec">The height requirements for this view.</param>
540 /// <since_tizen> 6 </since_tizen>
541 protected virtual void MeasureChildren(MeasureSpecification widthMeasureSpec, MeasureSpecification heightMeasureSpec)
543 foreach (LayoutItem childLayout in LayoutChildren)
545 MeasureChildWithMargins(childLayout, widthMeasureSpec, new LayoutLength(0), heightMeasureSpec, new LayoutLength(0));
550 /// Ask one of the children of this view to measure itself, taking into
551 /// account both the MeasureSpec requirements for this view and its padding.<br />
552 /// The heavy lifting is done in GetChildMeasureSpecification.<br />
554 /// <param name="child">The child to measure.</param>
555 /// <param name="parentWidthMeasureSpec">The width requirements for this view.</param>
556 /// <param name="parentHeightMeasureSpec">The height requirements for this view.</param>
557 /// <exception cref="ArgumentNullException"> Thrown when child is null. </exception>
558 /// <since_tizen> 6 </since_tizen>
559 protected virtual void MeasureChild(LayoutItem child, MeasureSpecification parentWidthMeasureSpec, MeasureSpecification parentHeightMeasureSpec)
563 throw new ArgumentNullException(nameof(child));
566 View childOwner = child.Owner;
568 MeasureSpecification childWidthMeasureSpec = GetChildMeasureSpecification(
569 new MeasureSpecification(new LayoutLength(parentWidthMeasureSpec.Size), parentWidthMeasureSpec.Mode),
570 new LayoutLength(Padding.Start + Padding.End),
571 new LayoutLength(childOwner.WidthSpecification));
573 MeasureSpecification childHeightMeasureSpec = GetChildMeasureSpecification(
574 new MeasureSpecification(new LayoutLength(parentHeightMeasureSpec.Size), parentHeightMeasureSpec.Mode),
575 new LayoutLength(Padding.Top + Padding.Bottom),
576 new LayoutLength(childOwner.HeightSpecification));
578 child.Measure(childWidthMeasureSpec, childHeightMeasureSpec);
582 /// Ask one of the children of this view to measure itself, taking into
583 /// account both the MeasureSpec requirements for this view and its padding.<br />
584 /// and margins. The heavy lifting is done in GetChildMeasureSpecification.<br />
586 /// <param name="child">The child to measure.</param>
587 /// <param name="parentWidthMeasureSpec">The width requirements for this view.</param>
588 /// <param name="widthUsed">Extra space that has been used up by the parent horizontally (possibly by other children of the parent).</param>
589 /// <param name="parentHeightMeasureSpec">The height requirements for this view.</param>
590 /// <param name="heightUsed">Extra space that has been used up by the parent vertically (possibly by other children of the parent).</param>
591 /// <exception cref="ArgumentNullException"> Thrown when child is null. </exception>
592 /// <since_tizen> 6 </since_tizen>
593 protected virtual void MeasureChildWithMargins(LayoutItem child, MeasureSpecification parentWidthMeasureSpec, LayoutLength widthUsed, MeasureSpecification parentHeightMeasureSpec, LayoutLength heightUsed)
597 throw new ArgumentNullException(nameof(child));
600 View childOwner = child.Owner;
601 Extents margin = childOwner.Margin;
603 MeasureSpecification childWidthMeasureSpec = GetChildMeasureSpecification(
604 new MeasureSpecification(
605 new LayoutLength(parentWidthMeasureSpec.Size + widthUsed - (margin.Start + margin.End)),
606 parentWidthMeasureSpec.Mode),
607 new LayoutLength(Padding.Start + Padding.End),
608 new LayoutLength(childOwner.WidthSpecification));
610 MeasureSpecification childHeightMeasureSpec = GetChildMeasureSpecification(
611 new MeasureSpecification(
612 new LayoutLength(parentHeightMeasureSpec.Size + heightUsed - (margin.Top + margin.Bottom)),
613 parentHeightMeasureSpec.Mode),
614 new LayoutLength(Padding.Top + Padding.Bottom),
615 new LayoutLength(childOwner.HeightSpecification));
616 child.Measure(childWidthMeasureSpec, childHeightMeasureSpec);
621 /// Ask one of the children of this view to measure itself, taking into
622 /// account both the MeasureSpec requirements for this view and without its padding.<br />
623 /// and margins. The heavy lifting is done in GetChildMeasureSpecification.<br />
625 /// <param name="child">The child to measure.</param>
626 /// <param name="parentWidthMeasureSpec">The width requirements for this view.</param>
627 /// <param name="parentHeightMeasureSpec">The height requirements for this view.</param>
628 [EditorBrowsable(EditorBrowsableState.Never)]
629 protected void MeasureChildWithoutPadding(LayoutItem child, MeasureSpecification parentWidthMeasureSpec, MeasureSpecification parentHeightMeasureSpec)
631 View childOwner = child.Owner;
633 MeasureSpecification childWidthMeasureSpec = GetChildMeasureSpecification(
634 new MeasureSpecification(new LayoutLength(parentWidthMeasureSpec.Size), parentWidthMeasureSpec.Mode),
636 new LayoutLength(childOwner.WidthSpecification));
638 MeasureSpecification childHeightMeasureSpec = GetChildMeasureSpecification(
639 new MeasureSpecification(new LayoutLength(parentHeightMeasureSpec.Size), parentHeightMeasureSpec.Mode),
641 new LayoutLength(childOwner.HeightSpecification));
643 child.Measure(childWidthMeasureSpec, childHeightMeasureSpec);
647 /// Gets the value that is contained in the attached BindableProperty.
649 /// <typeparam name="T">The return type of property</typeparam>
650 /// <param name="bindable">The bindable object.</param>
651 /// <param name="property">The BindableProperty for which to get the value.</param>
652 /// <returns>The value that is contained in the attached BindableProperty.</returns>
653 [EditorBrowsable(EditorBrowsableState.Never)]
654 public static T GetAttachedValue<T>(Binding.BindableObject bindable, Binding.BindableProperty property)
656 if (bindable == null)
657 throw new ArgumentNullException(nameof(bindable));
659 return (T)bindable.GetValue(property);
663 /// Sets the value of the attached property.
665 /// <param name="bindable">The bindable object.</param>
666 /// <param name="property">The BindableProperty on which to assign a value.</param>
667 /// <param name="value">The value to set.</param>
668 [EditorBrowsable(EditorBrowsableState.Never)]
669 public static void SetAttachedValue(Binding.BindableObject bindable, Binding.BindableProperty property, object value)
671 if (bindable == null)
672 throw new ArgumentNullException(nameof(bindable));
674 bindable.SetValueCore(property, value, SetValueFlags.None, SetValuePrivateFlags.ManuallySet, false);
676 internal static void OnChildPropertyChanged(Binding.BindableObject bindable, object oldValue, object newValue)
682 View view = bindable as View;
683 view?.Layout?.RequestLayout();
686 internal static Binding.BindableProperty.ValidateValueDelegate ValidateEnum(int enumMin, int enumMax)
688 return (Binding.BindableObject bindable, object value) =>
690 int @enum = (int)value;
691 return enumMin <= @enum && @enum <= enumMax;