2 * Copyright (c) 2018 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 #include "custom-layout-impl.h"
21 #include <dali/public-api/actors/actor.h>
30 using Dali::Toolkit::MeasuredSize;
32 CustomLayoutPtr CustomLayout::New()
34 CustomLayoutPtr layout( new CustomLayout() );
38 void CustomLayout::OnMeasure( MeasureSpec widthMeasureSpec, MeasureSpec heightMeasureSpec )
40 auto accumulatedWidth = 0;
43 // In this layout we will:
44 // Measuring the layout with the children in a horizontal configuration, one after another
45 // Set the required width to be the accumulated width of our children
46 // Set the required height to be the maximum height of any of our children
48 for( unsigned int i=0; i<GetChildCount(); ++i )
50 auto childLayout = GetChildAt( i );
53 MeasureChild( childLayout, widthMeasureSpec, heightMeasureSpec );
54 accumulatedWidth += childLayout->GetMeasuredWidth();
55 std::max( childLayout->GetMeasuredHeight().mValue, maxHeight );
59 // Finally, call this method to set the dimensions we would like
60 SetMeasuredDimensions( MeasuredSize( accumulatedWidth ), MeasuredSize( maxHeight ) );
63 void CustomLayout::OnLayout( bool changed, LayoutLength left, LayoutLength top, LayoutLength right, LayoutLength bottom )
65 LayoutLength childTop( 0 );
66 LayoutLength childLeft( 0 );
68 // We want to vertically align the children to the middle
69 auto height = bottom - top;
70 auto middle = height / 2;
72 // Horizontally align the children to the middle of the space they are given too
73 auto width = right - left;
74 int count = GetChildCount();
75 auto childIncrement = width / count;
76 auto center = childIncrement / 2;
78 // Check layout direction
79 auto owner = GetOwner();
80 auto actor = Actor::DownCast(owner);
81 const bool isLayoutRtl = actor ? actor.GetProperty< bool >( Actor::Property::LAYOUT_DIRECTION ) : false;
83 for( int i = 0; i < count; i++)
85 auto itemIndex = isLayoutRtl ? count - 1 - i : i; // If RTL, then layout the last item first
87 Dali::Toolkit::Internal::LayoutItemPtr childLayout = GetChildAt( itemIndex );
88 if( childLayout != nullptr )
90 auto childWidth = childLayout->GetMeasuredWidth();
91 auto childHeight = childLayout->GetMeasuredHeight();
93 childTop = middle - (childHeight / 2);
95 auto left = childLeft + center - childWidth / 2;
97 childLayout->Layout( left, childTop, left + childWidth, childTop + childHeight );
98 childLeft += childIncrement;
103 } // namespace Internal