08e55f0955bfc3394519fed4e5c885910d99cf80
[platform/core/csapi/tizenfx.git] / src / Tizen.NUI / src / public / Layouting / AbsoluteLayout.cs
1 /* Copyright (c) 2021 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 namespace Tizen.NUI
18 {
19     /// <summary>
20     /// [Draft] This class implements a absolute layout, allowing explicit positioning of children.
21     ///  Positions are from the top left of the layout and can be set using the View.Position and alike.
22     /// </summary>
23     public class AbsoluteLayout : LayoutGroup
24     {
25         /// <summary>
26         /// [Draft] Constructor
27         /// </summary>
28         /// <since_tizen> 6 </since_tizen>
29         public AbsoluteLayout()
30         {
31         }
32
33         /// <summary>
34         /// Measure the layout and its content to determine the measured width and the measured height.<br />
35         /// </summary>
36         /// <param name="widthMeasureSpec">horizontal space requirements as imposed by the parent.</param>
37         /// <param name="heightMeasureSpec">vertical space requirements as imposed by the parent.</param>
38         /// <since_tizen> 6 </since_tizen>
39         protected override void OnMeasure(MeasureSpecification widthMeasureSpec, MeasureSpecification heightMeasureSpec)
40         {
41             // Ensure layout respects it's given minimum size
42             float maxWidth = SuggestedMinimumWidth.AsDecimal();
43             float maxHeight = SuggestedMinimumHeight.AsDecimal();
44
45             MeasuredSize.StateType childWidthState = MeasuredSize.StateType.MeasuredSizeOK;
46             MeasuredSize.StateType childHeightState = MeasuredSize.StateType.MeasuredSizeOK;
47
48             foreach (LayoutItem childLayout in IterateLayoutChildren())
49             {
50                 // Get size of child with no padding, no margin. we won't support margin, padding for AbsolutLayout.
51                 MeasureChildWithoutPadding(childLayout, widthMeasureSpec, heightMeasureSpec);
52
53                 // Determine the width and height needed by the children using their given position and size.
54                 // Children could overlap so find the right most child.
55                 float childRight = childLayout.MeasuredWidth.Size.AsDecimal() + childLayout.Owner.PositionX;
56                 float childBottom = childLayout.MeasuredHeight.Size.AsDecimal() + childLayout.Owner.PositionY;
57
58                 if (maxWidth < childRight)
59                     maxWidth = childRight;
60
61                 if (maxHeight < childBottom)
62                     maxHeight = childBottom;
63
64                 if (childLayout.MeasuredWidth.State == MeasuredSize.StateType.MeasuredSizeTooSmall)
65                 {
66                     childWidthState = MeasuredSize.StateType.MeasuredSizeTooSmall;
67                 }
68                 if (childLayout.MeasuredHeight.State == MeasuredSize.StateType.MeasuredSizeTooSmall)
69                 {
70                     childHeightState = MeasuredSize.StateType.MeasuredSizeTooSmall;
71                 }
72             }
73
74             SetMeasuredDimensions(ResolveSizeAndState(new LayoutLength(maxWidth), widthMeasureSpec, childWidthState),
75                                   ResolveSizeAndState(new LayoutLength(maxHeight), heightMeasureSpec, childHeightState));
76         }
77
78         /// <summary>
79         /// Assign a size and position to each of its children.<br />
80         /// </summary>
81         /// <param name="changed">This is a new size or position for this layout.</param>
82         /// <param name="left">Left position, relative to parent.</param>
83         /// <param name="top"> Top position, relative to parent.</param>
84         /// <param name="right">Right position, relative to parent.</param>
85         /// <param name="bottom">Bottom position, relative to parent.</param>
86         /// <since_tizen> 6 </since_tizen>
87         protected override void OnLayout(bool changed, LayoutLength left, LayoutLength top, LayoutLength right, LayoutLength bottom)
88         {
89             // Absolute layout positions it's children at their Actor positions.
90             // Children could overlap or spill outside the parent, as is the nature of absolute positions.
91             foreach (LayoutItem childLayout in IterateLayoutChildren())
92             {
93                 LayoutLength childWidth = childLayout.MeasuredWidth.Size;
94                 LayoutLength childHeight = childLayout.MeasuredHeight.Size;
95
96                 LayoutLength childLeft = new LayoutLength(childLayout.Owner.PositionX);
97                 LayoutLength childTop = new LayoutLength(childLayout.Owner.PositionY);
98
99                 childLayout.Layout(childLeft, childTop, childLeft + childWidth, childTop + childHeight, true);
100             }
101         }
102     }
103 } // namespace