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 var win = Window.Get(Owner);
115 if (!childLayout.IsReplaceFlag())
119 NUIApplication.GetDefaultWindow().LayoutController.AddToRemovalStack(childLayout);
123 win.LayoutController.AddToRemovalStack(childLayout);
127 childLayout.ConditionForAnimation = childLayout.ConditionForAnimation | TransitionCondition.Remove;
128 // Add LayoutItem to the transition stack so can animate it out.
131 NUIApplication.GetDefaultWindow().LayoutController.AddTransitionDataEntry(new LayoutData(layoutItem, ConditionForAnimation, 0, 0, 0, 0));
135 win.LayoutController.AddTransitionDataEntry(new LayoutData(layoutItem, ConditionForAnimation, 0, 0, 0, 0));
139 // Reset condition for animation ready for next transition when required.
140 // SetFrame usually would do this but this LayoutItem is being removed.
141 childLayout.ConditionForAnimation = TransitionCondition.Unspecified;
150 // If child removed then set all siblings not being added to a ChangeOnRemove transition.
151 SetConditionsForAnimationOnLayoutGroup(TransitionCondition.ChangeOnRemove);
153 OnChildRemove(layoutItem);
158 /// Sets the order of the child layout in the layout group.
160 /// <param name="child">the child layout in the layout group</param>
161 /// <param name="order">the order of the child layout in the layout group</param>
162 [EditorBrowsable(EditorBrowsableState.Never)]
163 public void ChangeLayoutChildOrder(LayoutItem child, int order)
165 if ((child != null) && (LayoutChildren.Count > order))
167 LayoutChildren.Remove(child);
168 LayoutChildren.Insert(order, child);
173 // Attaches to View ChildAdded signal so called when a View is added to a view.
174 private void AddChildToLayoutGroup(View child)
176 if (View.LayoutingDisabled)
180 // Only give children a layout if their parent is an explicit container or a pure View.
181 // Pure View meaning not derived from a View, e.g a Legacy container.
182 // layoutSet flag is true when the View became a layout using the set Layout API opposed to automatically due to it's parent.
183 // First time the set Layout API is used by any View the Window no longer has layoutingDisabled.
185 // If child already has a Layout then don't change it.
186 if (null == child.Layout)
188 // If child view does not have Layout, then default layout is automatically set to the child view.
189 // The Layout is automatically added to this LayoutGroup when child view sets Layout.
190 child.Layout = child.CreateDefaultLayout();
196 // Parent transitions are not attached to children.
200 /// If the child has a layout then it is removed from the parent layout.
202 /// <param name="child">Child View to remove.</param>
203 internal void RemoveChildFromLayoutGroup(View child)
205 if (child.Layout != null)
207 Remove(child.Layout);
212 /// Set all children in a LayoutGroup to the supplied condition.
213 /// Children with Add or Remove conditions should not be changed.
215 private void SetConditionsForAnimationOnLayoutGroup(TransitionCondition conditionToSet)
217 foreach (LayoutItem childLayout in LayoutChildren)
219 switch (conditionToSet)
221 case TransitionCondition.ChangeOnAdd:
223 // If other children also being added (TransitionCondition.Add) then do not change their
224 // conditions, Continue to use their Add transitions.
225 if (childLayout.ConditionForAnimation.HasFlag(TransitionCondition.Add))
227 break; // Child being Added so don't update it's condition
231 // Set siblings for the child being added to use the ChangeOnAdd transition.
232 childLayout.ConditionForAnimation = TransitionCondition.ChangeOnAdd;
236 case TransitionCondition.ChangeOnRemove:
238 if (childLayout.ConditionForAnimation.HasFlag(TransitionCondition.Remove))
240 break; // Child being Removed so don't update it's condition
244 childLayout.ConditionForAnimation = TransitionCondition.ChangeOnRemove;
248 case TransitionCondition.LayoutChanged:
250 childLayout.ConditionForAnimation = TransitionCondition.LayoutChanged;
259 /// Callback for View.ChildAdded event
261 /// <param name="sender">The object triggering the event.</param>
262 /// <param name="childAddedEvent">Arguments from the event.</param>
263 void OnChildAddedToOwner(object sender, View.ChildAddedEventArgs childAddedEvent)
265 AddChildToLayoutGroup(childAddedEvent.Added);
269 /// Callback for View.ChildRemoved event
271 /// <param name="sender">The object triggering the event.</param>
272 /// <param name="childRemovedEvent">Arguments from the event.</param>
273 void OnChildRemovedFromOwner(object sender, View.ChildRemovedEventArgs childRemovedEvent)
275 RemoveChildFromLayoutGroup(childRemovedEvent.Removed);
279 /// Calculate the right measure spec for this child.
280 /// Does the hard part of MeasureChildren: figuring out the MeasureSpec to
281 /// pass to a particular child. This method figures out the right MeasureSpec
282 /// for one dimension (height or width) of one child view.<br />
284 /// <param name="parentMeasureSpec">The requirements for this view. MeasureSpecification.</param>
285 /// <param name="padding">The padding of this view for the current dimension and margins, if applicable. LayoutLength.</param>
286 /// <param name="childDimension"> How big the child wants to be in the current dimension. LayoutLength.</param>
287 /// <returns>a MeasureSpec for the child.</returns>
288 public static MeasureSpecification GetChildMeasureSpecification(MeasureSpecification parentMeasureSpec, LayoutLength padding, LayoutLength childDimension)
290 MeasureSpecification.ModeType specMode = parentMeasureSpec.Mode;
291 MeasureSpecification.ModeType resultMode = MeasureSpecification.ModeType.Unspecified;
293 // Child only can use parent's size without parent's padding and own margin.
294 LayoutLength resultSize = new LayoutLength(Math.Max(0.0f, (parentMeasureSpec.Size - padding).AsDecimal()));
297 // Parent has imposed an exact size on us
298 case MeasureSpecification.ModeType.Exactly:
300 if ((int)childDimension.AsRoundedValue() == LayoutParamPolicies.MatchParent)
302 resultMode = MeasureSpecification.ModeType.Exactly;
304 else if ((int)childDimension.AsRoundedValue() == LayoutParamPolicies.WrapContent)
306 resultMode = MeasureSpecification.ModeType.AtMost;
310 resultSize = childDimension;
311 resultMode = MeasureSpecification.ModeType.Exactly;
317 // Parent has imposed a maximum size on us
318 case MeasureSpecification.ModeType.AtMost:
320 if (childDimension.AsRoundedValue() == LayoutParamPolicies.MatchParent)
322 // Crashed. Cannot calculate.
324 // Child wants to be our size, but our size is not fixed.
325 // Constrain child to not be bigger than us.
326 resultMode = MeasureSpecification.ModeType.AtMost;
328 else if (childDimension.AsRoundedValue() == LayoutParamPolicies.WrapContent)
330 // Child wants to determine its own size. It can't be
333 // Don't need parent's size. Size of this child will be determined by its children.
334 resultMode = MeasureSpecification.ModeType.AtMost;
338 // Child wants a specific size... so be it
339 resultSize = childDimension;
340 resultMode = MeasureSpecification.ModeType.Exactly;
346 // Parent asked to see how big we want to be
347 case MeasureSpecification.ModeType.Unspecified:
349 if ((childDimension.AsRoundedValue() == LayoutParamPolicies.MatchParent))
351 // Child wants to be our size... find out how big it should be
353 // There is no one who has exact size in parent hierarchy.
355 resultMode = MeasureSpecification.ModeType.Unspecified;
357 else if (childDimension.AsRoundedValue() == (LayoutParamPolicies.WrapContent))
359 // Child wants to determine its own size.... find out how big
361 resultMode = MeasureSpecification.ModeType.Unspecified;
365 // Child wants a specific size... let him have it
366 resultSize = childDimension;
367 resultMode = MeasureSpecification.ModeType.Exactly;
373 return new MeasureSpecification(resultSize, resultMode);
377 /// Measure the layout and its content to determine the measured width and the measured height.<br />
378 /// If this method is overridden, it is the subclass's responsibility to make
379 /// sure the measured height and width are at least the layout's minimum height
380 /// and width. <br />
382 /// <param name="widthMeasureSpec">horizontal space requirements as imposed by the parent.</param>
383 /// <param name="heightMeasureSpec">vertical space requirements as imposed by the parent.</param>
384 /// <since_tizen> 6 </since_tizen>
385 protected override void OnMeasure(MeasureSpecification widthMeasureSpec, MeasureSpecification heightMeasureSpec)
387 LayoutLength measuredWidth = new LayoutLength(0.0f);
388 LayoutLength measuredHeight = new LayoutLength(0.0f);
390 // Layout takes size of largest child width and largest child height dimensions
391 foreach (LayoutItem childLayout in LayoutChildren)
393 if (childLayout != null)
395 MeasureChildWithMargins(childLayout, widthMeasureSpec, new LayoutLength(0), heightMeasureSpec, new LayoutLength(0));
396 LayoutLength childWidth = new LayoutLength(childLayout.MeasuredWidth.Size);
397 LayoutLength childHeight = new LayoutLength(childLayout.MeasuredHeight.Size);
399 Extents childMargin = childLayout.Margin;
400 measuredWidth = new LayoutLength(Math.Max(measuredWidth.AsDecimal(), childWidth.AsDecimal() + childMargin.Start + childMargin.End));
401 measuredHeight = new LayoutLength(Math.Max(measuredHeight.AsDecimal(), childHeight.AsDecimal() + childMargin.Top + childMargin.Bottom));
405 if (0 == LayoutChildren.Count)
407 // Must be a leaf as has no children
408 measuredWidth = GetDefaultSize(SuggestedMinimumWidth, widthMeasureSpec);
409 measuredHeight = GetDefaultSize(SuggestedMinimumHeight, heightMeasureSpec);
412 SetMeasuredDimensions(new MeasuredSize(measuredWidth, MeasuredSize.StateType.MeasuredSizeOK),
413 new MeasuredSize(measuredHeight, MeasuredSize.StateType.MeasuredSizeOK));
416 internal override void OnMeasureIndependentChildren(MeasureSpecification widthMeasureSpec, MeasureSpecification heightMeasureSpec)
418 foreach (var childLayout in LayoutChildren)
420 if (!childLayout.SetPositionByLayout)
422 MeasureChildWithoutPadding(childLayout, widthMeasureSpec, heightMeasureSpec);
428 /// Called from Layout() when this layout should assign a size and position to each of its children.<br />
429 /// Derived classes with children should override this method and call Layout() on each of their children.<br />
431 /// <param name="changed">This is a new size or position for this layout.</param>
432 /// <param name="left">Left position, relative to parent.</param>
433 /// <param name="top"> Top position, relative to parent.</param>
434 /// <param name="right">Right position, relative to parent.</param>
435 /// <param name="bottom">Bottom position, relative to parent.</param>
436 /// <since_tizen> 6 </since_tizen>
437 protected override void OnLayout(bool changed, LayoutLength left, LayoutLength top, LayoutLength right, LayoutLength bottom)
439 foreach (LayoutItem childLayout in LayoutChildren)
441 if (childLayout != null)
443 // Use position if explicitly set to child otherwise will be top left.
444 var childLeft = new LayoutLength(childLayout.Owner.PositionX);
445 var childTop = new LayoutLength(childLayout.Owner.PositionY);
451 // Margin and Padding only supported when child anchor point is TOP_LEFT.
452 if (owner.PivotPoint == PivotPoint.TopLeft || (owner.PositionUsesPivotPoint == false))
454 childLeft = childLeft + owner.Padding.Start + childLayout.Margin.Start;
455 childTop = childTop + owner.Padding.Top + childLayout.Margin.Top;
458 childLayout.Layout(childLeft, childTop, childLeft + childLayout.MeasuredWidth.Size, childTop + childLayout.MeasuredHeight.Size);
464 /// Layout independent children those Owners have true ExcludeLayouting. <br />
465 /// These children are required not to be affected by this layout. <br />
467 internal override void OnLayoutIndependentChildren(bool changed, LayoutLength left, LayoutLength top, LayoutLength right, LayoutLength bottom)
469 foreach (var childLayout in LayoutChildren)
471 if (!childLayout.SetPositionByLayout)
473 LayoutLength childWidth = childLayout.MeasuredWidth.Size;
474 LayoutLength childHeight = childLayout.MeasuredHeight.Size;
476 LayoutLength childPositionX = new LayoutLength(childLayout.Owner.PositionX);
477 LayoutLength childPositionY = new LayoutLength(childLayout.Owner.PositionY);
479 childLayout.Layout(childPositionX, childPositionY, childPositionX + childWidth, childPositionY + childHeight, true);
485 /// Overridden method called when the layout is attached to an owner.<br />
487 /// <since_tizen> 6 </since_tizen>
488 protected override void OnAttachedToOwner()
490 // Layout takes ownership of it's owner's children.
491 foreach (View view in Owner.Children)
493 AddChildToLayoutGroup(view);
496 // Connect to owner ChildAdded signal.
497 Owner.ChildAdded += OnChildAddedToOwner;
499 // Removing Child from the owners View will directly call the LayoutGroup removal API.
503 /// Virtual method to allow derived classes to remove any children before it is removed from
506 [EditorBrowsable(EditorBrowsableState.Never)]
507 protected override void OnUnparent()
509 // Disconnect to owner ChildAdded signal.
510 Owner.ChildAdded -= OnChildAddedToOwner;
513 // Virtual Methods that can be overridden by derived classes.
516 /// Callback when child is added to container.<br />
517 /// Derived classes can use this to set their own child properties on the child layout's owner.<br />
519 /// <param name="child">The Layout child.</param>
520 /// <since_tizen> 6 </since_tizen>
521 protected virtual void OnChildAdd(LayoutItem child)
526 /// Callback when child is removed from container.<br />
528 /// <param name="child">The Layout child.</param>
529 /// <since_tizen> 6 </since_tizen>
530 protected virtual void OnChildRemove(LayoutItem child)
535 /// Ask all of the children of this view to measure themselves, taking into
536 /// account both the MeasureSpec requirements for this view and its padding.<br />
537 /// The heavy lifting is done in GetChildMeasureSpec.<br />
539 /// <param name="widthMeasureSpec">The width requirements for this view.</param>
540 /// <param name="heightMeasureSpec">The height requirements for this view.</param>
541 /// <since_tizen> 6 </since_tizen>
542 protected virtual void MeasureChildren(MeasureSpecification widthMeasureSpec, MeasureSpecification heightMeasureSpec)
544 foreach (LayoutItem childLayout in LayoutChildren)
546 MeasureChildWithMargins(childLayout, widthMeasureSpec, new LayoutLength(0), heightMeasureSpec, new LayoutLength(0));
551 /// Ask one of the children of this view to measure itself, taking into
552 /// account both the MeasureSpec requirements for this view and its padding.<br />
553 /// The heavy lifting is done in GetChildMeasureSpecification.<br />
555 /// <param name="child">The child to measure.</param>
556 /// <param name="parentWidthMeasureSpec">The width requirements for this view.</param>
557 /// <param name="parentHeightMeasureSpec">The height requirements for this view.</param>
558 /// <exception cref="ArgumentNullException"> Thrown when child is null. </exception>
559 /// <since_tizen> 6 </since_tizen>
560 protected virtual void MeasureChild(LayoutItem child, MeasureSpecification parentWidthMeasureSpec, MeasureSpecification parentHeightMeasureSpec)
564 throw new ArgumentNullException(nameof(child));
567 View childOwner = child.Owner;
569 MeasureSpecification childWidthMeasureSpec = GetChildMeasureSpecification(
570 new MeasureSpecification(new LayoutLength(parentWidthMeasureSpec.Size), parentWidthMeasureSpec.Mode),
571 new LayoutLength(Padding.Start + Padding.End),
572 new LayoutLength(childOwner.WidthSpecification));
574 MeasureSpecification childHeightMeasureSpec = GetChildMeasureSpecification(
575 new MeasureSpecification(new LayoutLength(parentHeightMeasureSpec.Size), parentHeightMeasureSpec.Mode),
576 new LayoutLength(Padding.Top + Padding.Bottom),
577 new LayoutLength(childOwner.HeightSpecification));
579 child.Measure(childWidthMeasureSpec, childHeightMeasureSpec);
583 /// Ask one of the children of this view to measure itself, taking into
584 /// account both the MeasureSpec requirements for this view and its padding.<br />
585 /// and margins. The heavy lifting is done in GetChildMeasureSpecification.<br />
587 /// <param name="child">The child to measure.</param>
588 /// <param name="parentWidthMeasureSpec">The width requirements for this view.</param>
589 /// <param name="widthUsed">Extra space that has been used up by the parent horizontally (possibly by other children of the parent).</param>
590 /// <param name="parentHeightMeasureSpec">The height requirements for this view.</param>
591 /// <param name="heightUsed">Extra space that has been used up by the parent vertically (possibly by other children of the parent).</param>
592 /// <exception cref="ArgumentNullException"> Thrown when child is null. </exception>
593 /// <since_tizen> 6 </since_tizen>
594 protected virtual void MeasureChildWithMargins(LayoutItem child, MeasureSpecification parentWidthMeasureSpec, LayoutLength widthUsed, MeasureSpecification parentHeightMeasureSpec, LayoutLength heightUsed)
598 throw new ArgumentNullException(nameof(child));
601 View childOwner = child.Owner;
602 Extents margin = childOwner.Margin;
604 MeasureSpecification childWidthMeasureSpec = GetChildMeasureSpecification(
605 new MeasureSpecification(
606 new LayoutLength(parentWidthMeasureSpec.Size + widthUsed - (margin.Start + margin.End)),
607 parentWidthMeasureSpec.Mode),
608 new LayoutLength(Padding.Start + Padding.End),
609 new LayoutLength(childOwner.WidthSpecification));
611 MeasureSpecification childHeightMeasureSpec = GetChildMeasureSpecification(
612 new MeasureSpecification(
613 new LayoutLength(parentHeightMeasureSpec.Size + heightUsed - (margin.Top + margin.Bottom)),
614 parentHeightMeasureSpec.Mode),
615 new LayoutLength(Padding.Top + Padding.Bottom),
616 new LayoutLength(childOwner.HeightSpecification));
617 child.Measure(childWidthMeasureSpec, childHeightMeasureSpec);
622 /// Ask one of the children of this view to measure itself, taking into
623 /// account both the MeasureSpec requirements for this view and without its padding.<br />
624 /// and margins. The heavy lifting is done in GetChildMeasureSpecification.<br />
626 /// <param name="child">The child to measure.</param>
627 /// <param name="parentWidthMeasureSpec">The width requirements for this view.</param>
628 /// <param name="parentHeightMeasureSpec">The height requirements for this view.</param>
629 [EditorBrowsable(EditorBrowsableState.Never)]
630 protected void MeasureChildWithoutPadding(LayoutItem child, MeasureSpecification parentWidthMeasureSpec, MeasureSpecification parentHeightMeasureSpec)
632 View childOwner = child.Owner;
634 MeasureSpecification childWidthMeasureSpec = GetChildMeasureSpecification(
635 new MeasureSpecification(new LayoutLength(parentWidthMeasureSpec.Size), parentWidthMeasureSpec.Mode),
637 new LayoutLength(childOwner.WidthSpecification));
639 MeasureSpecification childHeightMeasureSpec = GetChildMeasureSpecification(
640 new MeasureSpecification(new LayoutLength(parentHeightMeasureSpec.Size), parentHeightMeasureSpec.Mode),
642 new LayoutLength(childOwner.HeightSpecification));
644 child.Measure(childWidthMeasureSpec, childHeightMeasureSpec);
648 /// Gets the value that is contained in the attached BindableProperty.
650 /// <typeparam name="T">The return type of property</typeparam>
651 /// <param name="bindable">The bindable object.</param>
652 /// <param name="property">The BindableProperty for which to get the value.</param>
653 /// <returns>The value that is contained in the attached BindableProperty.</returns>
654 [EditorBrowsable(EditorBrowsableState.Never)]
655 public static T GetAttachedValue<T>(Binding.BindableObject bindable, Binding.BindableProperty property)
657 if (bindable == null)
658 throw new ArgumentNullException(nameof(bindable));
660 return (T)bindable.GetValue(property);
664 /// Sets the value of the attached property.
666 /// <param name="bindable">The bindable object.</param>
667 /// <param name="property">The BindableProperty on which to assign a value.</param>
668 /// <param name="value">The value to set.</param>
669 [EditorBrowsable(EditorBrowsableState.Never)]
670 public static void SetAttachedValue(Binding.BindableObject bindable, Binding.BindableProperty property, object value)
672 if (bindable == null)
673 throw new ArgumentNullException(nameof(bindable));
675 bindable.SetValueCore(property, value, SetValueFlags.None, SetValuePrivateFlags.ManuallySet, false);
677 internal static void OnChildPropertyChanged(Binding.BindableObject bindable, object oldValue, object newValue)
683 View view = bindable as View;
684 view?.Layout?.RequestLayout();
687 internal static Binding.BindableProperty.ValidateValueDelegate ValidateEnum(int enumMin, int enumMax)
689 return (Binding.BindableObject bindable, object value) =>
691 int @enum = (int)value;
692 return enumMin <= @enum && @enum <= enumMax;