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 LayoutLength accumulatedWidth = 0;
41 LayoutLength maxHeight = 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(), 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 LayoutLength middle = (bottom - top) / 2;
71 // Horizontally align the children to the middle of the space they are given too
72 LayoutLength width = right - left;
73 int count = GetChildCount();
74 LayoutLength childIncrement = width / count;
75 LayoutLength center = childIncrement / 2;
77 // Check layout direction
78 auto owner = GetOwner();
79 auto actor = Actor::DownCast(owner);
80 const bool isLayoutRtl = actor ? actor.GetProperty< bool >( Actor::Property::LAYOUT_DIRECTION ) : false;
82 for( int i = 0; i < count; i++)
84 auto itemIndex = isLayoutRtl ? count - 1 - i : i; // If RTL, then layout the last item first
86 Dali::Toolkit::Internal::LayoutItemPtr childLayout = GetChildAt( itemIndex );
87 if( childLayout != nullptr )
89 LayoutLength childWidth = childLayout->GetMeasuredWidth();
90 LayoutLength childHeight = childLayout->GetMeasuredHeight();
92 childTop = middle - childHeight / 2;
94 LayoutLength left = childLeft + center - childWidth / 2;
96 childLayout->Layout( left, childTop, left + childWidth, childTop + childHeight );
97 childLeft += childIncrement;
102 } // namespace Internal