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.
17 #include <dali/integration-api/debug.h>
19 #include <dali/public-api/animation/animation.h>
20 #include <dali/public-api/object/type-registry-helper.h>
21 #include <dali-toolkit/public-api/controls/control.h>
22 #include <dali-toolkit/devel-api/layouting/layout-item-impl.h>
23 #include <dali-toolkit/internal/layouting/layout-item-data-impl.h>
25 #if defined(DEBUG_ENABLED)
26 Debug::Filter* gLayoutFilter = Debug::Filter::New( Debug::Verbose, false, "LOG_LAYOUT" );
31 const char* WIDTH_SPECIFICATION_NAME( "widthSpecification" );
32 const char* HEIGHT_SPECIFICATION_NAME( "heightSpecification" );
34 const float DEFAULT_TRANSITION_DURATION( 0.5f );
44 LayoutItem::LayoutItem()
45 : mImpl( new LayoutItem::Impl() ),
50 LayoutItem::~LayoutItem()
52 // An object with a unique_ptr to an opaque structure must define it's destructor in the translation unit
53 // where the opaque structure is defined. It cannot use the default method in the header file.
56 LayoutItemPtr LayoutItem::New( Handle& owner )
58 LayoutItemPtr layoutPtr = new LayoutItem();
62 void LayoutItem::Initialize( Handle& owner, const std::string& containerType )
64 mImpl->mOwner = &(owner.GetBaseObject());
65 RegisterChildProperties( containerType );
66 OnInitialize(); // Ensure direct deriving class gets initialized
70 Handle LayoutItem::GetOwner() const
72 return Handle::DownCast(BaseHandle(mImpl->mOwner));
75 void LayoutItem::Unparent()
77 // Enable directly derived types to first remove children
84 void LayoutItem::SetAnimateLayout( bool animateLayout )
86 mImpl->mAnimated = animateLayout;
89 bool LayoutItem::IsLayoutAnimated() const
91 return mImpl->mAnimated;
94 void LayoutItem::RegisterChildProperties( const std::string& containerType )
96 // Call on derived types
97 auto typeInfo = TypeRegistry::Get().GetTypeInfo( containerType );
100 Property::IndexContainer indices;
101 typeInfo.GetChildPropertyIndices( indices );
103 if( std::find( indices.Begin(), indices.End(), Toolkit::LayoutItem::ChildProperty::WIDTH_SPECIFICATION ) ==
106 ChildPropertyRegistration( typeInfo.GetName(), WIDTH_SPECIFICATION_NAME,
107 Toolkit::LayoutItem::ChildProperty::WIDTH_SPECIFICATION, Property::INTEGER );
109 ChildPropertyRegistration( typeInfo.GetName(), HEIGHT_SPECIFICATION_NAME,
110 Toolkit::LayoutItem::ChildProperty::HEIGHT_SPECIFICATION, Property::INTEGER );
113 OnRegisterChildProperties( containerType );
117 void LayoutItem::OnRegisterChildProperties( const std::string& containerType )
122 void LayoutItem::Measure( MeasureSpec widthMeasureSpec, MeasureSpec heightMeasureSpec )
124 const bool forceLayout = mImpl->GetPrivateFlag( Impl::PRIVATE_FLAG_FORCE_LAYOUT );
126 const bool specChanged =
127 ( widthMeasureSpec != mImpl->mOldWidthMeasureSpec ) ||
128 ( heightMeasureSpec != mImpl->mOldHeightMeasureSpec );
130 const bool isSpecExactly =
131 ( widthMeasureSpec.GetMode() == MeasureSpec::Mode::EXACTLY ) &&
132 ( heightMeasureSpec.GetMode() == MeasureSpec::Mode::EXACTLY );
134 const bool matchesSpecSize =
135 ( GetMeasuredWidth() == widthMeasureSpec.GetSize() ) &&
136 ( GetMeasuredHeight() == heightMeasureSpec.GetSize() );
138 const bool needsLayout = specChanged && ( !isSpecExactly || !matchesSpecSize );
140 if( forceLayout || needsLayout)
142 mImpl->ClearPrivateFlag( Impl::PRIVATE_FLAG_MEASURED_DIMENSION_SET );
144 //resolveRtlPropertiesIfNeeded();
146 int cacheIndex = -1; // = forceLayout ? -1 : mMeasureCache.indexOfKey(key);
147 if( cacheIndex < 0 ) //|| sIgnoreMeasureCache )
149 // measure ourselves, this should set the measured dimension flag back
150 OnMeasure( widthMeasureSpec, heightMeasureSpec );
151 mImpl->ClearPrivateFlag( Impl::PRIVATE_FLAG_MEASURE_NEEDED_BEFORE_LAYOUT );
155 mImpl->SetPrivateFlag( Impl::PRIVATE_FLAG_MEASURE_NEEDED_BEFORE_LAYOUT );
158 // flag not set, setMeasuredDimension() was not invoked, we raise an exception to warn the developer
159 DALI_ASSERT_ALWAYS( mImpl->GetPrivateFlag( Impl::PRIVATE_FLAG_MEASURED_DIMENSION_SET ) &&
160 "Layout's OnMeasure() did not set the measured dimension by calling setMeasuredDimension()" );
161 mImpl->SetPrivateFlag( Impl::PRIVATE_FLAG_LAYOUT_REQUIRED );
164 mImpl->mOldWidthMeasureSpec = widthMeasureSpec;
165 mImpl->mOldHeightMeasureSpec = heightMeasureSpec;
167 //mMeasureCache.put(key, ((long) mMeasuredWidth) << 32 | (long) mMeasuredHeight & 0xffffffffL); // suppress sign extension
170 void LayoutItem::Layout( LayoutLength l, LayoutLength t, LayoutLength r, LayoutLength b )
172 if( mImpl->GetPrivateFlag( Impl::PRIVATE_FLAG_MEASURE_NEEDED_BEFORE_LAYOUT ) )
174 OnMeasure( mImpl->mOldWidthMeasureSpec, mImpl->mOldHeightMeasureSpec );
175 mImpl->ClearPrivateFlag( Impl::PRIVATE_FLAG_MEASURE_NEEDED_BEFORE_LAYOUT );
178 bool changed = SetFrame( l, t, r, b );
180 if( changed || mImpl->GetPrivateFlag( Impl::PRIVATE_FLAG_LAYOUT_REQUIRED ) )
182 OnLayout( changed, l, t, r, b );
183 mImpl->ClearPrivateFlag( Impl::PRIVATE_FLAG_LAYOUT_REQUIRED );
186 mImpl->ClearPrivateFlag( Impl::PRIVATE_FLAG_FORCE_LAYOUT );
187 mImpl->SetPrivateFlag( Impl::PRIVATE_FLAG_IS_LAID_OUT );
190 LayoutLength LayoutItem::GetMinimumWidth() const
192 return mImpl->mMinimumSize.GetWidth();
195 LayoutLength LayoutItem::GetMinimumHeight() const
197 return mImpl->mMinimumSize.GetHeight();
200 void LayoutItem::SetMinimumWidth( LayoutLength minimumWidth )
202 mImpl->mMinimumSize.SetWidth( minimumWidth );
206 void LayoutItem::SetMinimumHeight( LayoutLength minimumHeight )
208 mImpl->mMinimumSize.SetHeight( minimumHeight );
212 Extents LayoutItem::GetPadding() const
214 return mImpl->mPadding;
217 LayoutLength LayoutItem::GetDefaultSize( LayoutLength size, MeasureSpec measureSpec )
219 LayoutLength result = size;
220 auto specMode = measureSpec.GetMode();
221 auto specSize = measureSpec.GetSize();
225 case MeasureSpec::Mode::UNSPECIFIED:
230 case MeasureSpec::Mode::AT_MOST:
231 case MeasureSpec::Mode::EXACTLY:
240 void LayoutItem::OnMeasure( MeasureSpec widthMeasureSpec, MeasureSpec heightMeasureSpec)
242 SetMeasuredDimensions( GetDefaultSize( GetSuggestedMinimumWidth(), widthMeasureSpec ),
243 GetDefaultSize( GetSuggestedMinimumHeight(), heightMeasureSpec ) );
246 void LayoutItem::OnLayout( bool changed, LayoutLength left, LayoutLength top, LayoutLength right, LayoutLength bottom )
250 LayoutParent* LayoutItem::GetParent()
252 return mImpl->mLayoutParent;
255 void LayoutItem::RequestLayout()
257 // @todo Enforce failure if called in Measure/Layout passes.
258 mImpl->SetPrivateFlag( Impl::PRIVATE_FLAG_FORCE_LAYOUT );
259 Toolkit::LayoutController layoutController = Toolkit::LayoutController::Get();
260 layoutController.RequestLayout( Toolkit::LayoutItem(this) );
263 bool LayoutItem::IsLayoutRequested() const
265 return mImpl->GetPrivateFlag( Impl::PRIVATE_FLAG_FORCE_LAYOUT );
268 void LayoutItem::SetMeasuredDimensions( MeasuredSize measuredWidth, MeasuredSize measuredHeight )
270 mImpl->SetPrivateFlag( Impl::PRIVATE_FLAG_MEASURED_DIMENSION_SET );
271 mImpl->mMeasuredWidth = measuredWidth;
272 mImpl->mMeasuredHeight = measuredHeight;
275 LayoutLength LayoutItem::GetMeasuredWidth() const
277 // Get the size portion of the measured width
278 return mImpl->mMeasuredWidth.GetSize();
281 LayoutLength LayoutItem::GetMeasuredHeight() const
283 return mImpl->mMeasuredHeight.GetSize();
286 MeasuredSize LayoutItem::GetMeasuredWidthAndState() const
288 return mImpl->mMeasuredWidth;
291 MeasuredSize LayoutItem::GetMeasuredHeightAndState() const
293 return mImpl->mMeasuredHeight;
296 LayoutLength LayoutItem::GetSuggestedMinimumWidth() const
298 auto owner = GetOwner();
299 auto actor = Actor::DownCast(owner);
300 auto naturalSize = actor ? actor.GetNaturalSize() : Vector3::ZERO;
302 return std::max( mImpl->mMinimumSize.GetWidth(), LayoutLength::IntType( naturalSize.width ) );
305 LayoutLength LayoutItem::GetSuggestedMinimumHeight() const
307 auto owner = GetOwner();
308 auto actor = Actor::DownCast(owner);
309 auto naturalSize = actor ? actor.GetNaturalSize() : Vector3::ZERO;
311 return std::max( mImpl->mMinimumSize.GetHeight(), LayoutLength::IntType(naturalSize.height) );
314 MeasuredSize LayoutItem::ResolveSizeAndState( LayoutLength size, MeasureSpec measureSpec, MeasuredSize::State childMeasuredState )
316 auto specMode = measureSpec.GetMode();
317 LayoutLength specSize = measureSpec.GetSize();
322 case MeasureSpec::Mode::AT_MOST:
326 result = MeasuredSize( specSize, MeasuredSize::MEASURED_SIZE_TOO_SMALL );
330 result.SetSize( size );
335 case MeasureSpec::Mode::EXACTLY:
337 result.SetSize( specSize );
341 case MeasureSpec::Mode::UNSPECIFIED:
344 result.SetSize( size );
349 result.SetState( childMeasuredState );
354 bool LayoutItem::SetFrame( LayoutLength left, LayoutLength top, LayoutLength right, LayoutLength bottom )
356 bool changed = false;
358 DALI_LOG_INFO( gLayoutFilter, Debug::Verbose, "LayoutItem::SetFrame(%d, %d, %d, %d)\n", left.mValue, top.mValue, right.mValue, bottom.mValue );
360 if( mImpl->mLeft != left || mImpl->mRight != right || mImpl->mTop != top || mImpl->mBottom != bottom )
364 auto oldWidth = mImpl->mRight - mImpl->mLeft;
365 auto oldHeight = mImpl->mBottom - mImpl->mTop;
366 auto newWidth = right - left;
367 auto newHeight = bottom - top;
368 bool sizeChanged = (newWidth != oldWidth) || (newHeight != oldHeight);
372 mImpl->mRight = right;
373 mImpl->mBottom = bottom;
375 mImpl->SetPrivateFlag( Impl::PRIVATE_FLAG_HAS_BOUNDS );
378 // Reflect up to parent control
379 auto owner = GetOwner();
380 auto actor = Actor::DownCast(owner);
383 if( mImpl->mAnimated )
385 auto animation = Animation::New( 0.5f );
386 animation.AnimateTo( Property( actor, Actor::Property::POSITION ),
387 Vector3( float(left.mValue), float(top.mValue), 0.0f ) );
388 animation.AnimateTo( Property( actor, Actor::Property::SIZE ),
389 Vector3( right-left, bottom-top, 0.0f ) );
390 animation.FinishedSignal().Connect( mSlotDelegate, &LayoutItem::OnLayoutAnimationFinished );
395 // @todo Collate into list of Property & Property::Value pairs.
396 actor.SetPosition( Vector3( float(left.mValue), float(top.mValue), 0.0f ) );
397 actor.SetSize( Vector3( right-left, bottom-top, 0.0f ) );
403 SizeChange( LayoutSize( newWidth, newHeight ), LayoutSize( oldWidth, oldHeight ) );
409 void LayoutItem::OnLayoutAnimationFinished( Animation& animation )
411 auto owner = GetOwner();
412 auto actor = Actor::DownCast(owner);
415 actor.SetSize( Vector3( mImpl->mRight-mImpl->mLeft, mImpl->mBottom-mImpl->mTop, 0.0f ) );
419 void LayoutItem::SizeChange( LayoutSize newSize, LayoutSize oldSize)
421 OnSizeChanged( newSize, oldSize );
425 void LayoutItem::OnSizeChanged( LayoutSize newSize, LayoutSize oldSize )
429 void LayoutItem::OnInitialize()
434 } // namespace Internal
435 } // namespace Toolkit