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 // measure ourselves, this should set the measured dimension flag back
145 OnMeasure( widthMeasureSpec, heightMeasureSpec );
146 mImpl->ClearPrivateFlag( Impl::PRIVATE_FLAG_MEASURE_NEEDED_BEFORE_LAYOUT );
148 // flag not set, setMeasuredDimension() was not invoked, we raise an exception to warn the developer
149 DALI_ASSERT_ALWAYS( mImpl->GetPrivateFlag( Impl::PRIVATE_FLAG_MEASURED_DIMENSION_SET ) &&
150 "Layout's OnMeasure() did not set the measured dimension by calling setMeasuredDimension()" );
151 mImpl->SetPrivateFlag( Impl::PRIVATE_FLAG_LAYOUT_REQUIRED );
154 mImpl->mOldWidthMeasureSpec = widthMeasureSpec;
155 mImpl->mOldHeightMeasureSpec = heightMeasureSpec;
158 void LayoutItem::Layout( LayoutLength l, LayoutLength t, LayoutLength r, LayoutLength b )
160 if( mImpl->GetPrivateFlag( Impl::PRIVATE_FLAG_MEASURE_NEEDED_BEFORE_LAYOUT ) )
162 OnMeasure( mImpl->mOldWidthMeasureSpec, mImpl->mOldHeightMeasureSpec );
163 mImpl->ClearPrivateFlag( Impl::PRIVATE_FLAG_MEASURE_NEEDED_BEFORE_LAYOUT );
166 bool changed = SetFrame( l, t, r, b );
168 if( changed || mImpl->GetPrivateFlag( Impl::PRIVATE_FLAG_LAYOUT_REQUIRED ) )
170 OnLayout( changed, l, t, r, b );
171 mImpl->ClearPrivateFlag( Impl::PRIVATE_FLAG_LAYOUT_REQUIRED );
174 mImpl->ClearPrivateFlag( Impl::PRIVATE_FLAG_FORCE_LAYOUT );
175 mImpl->SetPrivateFlag( Impl::PRIVATE_FLAG_IS_LAID_OUT );
178 LayoutLength LayoutItem::GetMinimumWidth() const
180 return mImpl->mMinimumSize.GetWidth();
183 LayoutLength LayoutItem::GetMinimumHeight() const
185 return mImpl->mMinimumSize.GetHeight();
188 void LayoutItem::SetMinimumWidth( LayoutLength minimumWidth )
190 mImpl->mMinimumSize.SetWidth( minimumWidth );
194 void LayoutItem::SetMinimumHeight( LayoutLength minimumHeight )
196 mImpl->mMinimumSize.SetHeight( minimumHeight );
200 Extents LayoutItem::GetPadding() const
202 return mImpl->mPadding;
205 LayoutLength LayoutItem::GetDefaultSize( LayoutLength size, MeasureSpec measureSpec )
207 LayoutLength result = size;
208 auto specMode = measureSpec.GetMode();
209 auto specSize = measureSpec.GetSize();
213 case MeasureSpec::Mode::UNSPECIFIED:
218 case MeasureSpec::Mode::AT_MOST:
219 case MeasureSpec::Mode::EXACTLY:
228 void LayoutItem::OnMeasure( MeasureSpec widthMeasureSpec, MeasureSpec heightMeasureSpec)
230 SetMeasuredDimensions( GetDefaultSize( GetSuggestedMinimumWidth(), widthMeasureSpec ),
231 GetDefaultSize( GetSuggestedMinimumHeight(), heightMeasureSpec ) );
234 void LayoutItem::OnLayout( bool changed, LayoutLength left, LayoutLength top, LayoutLength right, LayoutLength bottom )
238 LayoutParent* LayoutItem::GetParent()
240 return mImpl->mLayoutParent;
243 void LayoutItem::RequestLayout()
245 // @todo Enforce failure if called in Measure/Layout passes.
246 mImpl->SetPrivateFlag( Impl::PRIVATE_FLAG_FORCE_LAYOUT );
247 Toolkit::LayoutController layoutController = Toolkit::LayoutController::Get();
248 layoutController.RequestLayout( Toolkit::LayoutItem(this) );
251 bool LayoutItem::IsLayoutRequested() const
253 return mImpl->GetPrivateFlag( Impl::PRIVATE_FLAG_FORCE_LAYOUT );
256 void LayoutItem::SetMeasuredDimensions( MeasuredSize measuredWidth, MeasuredSize measuredHeight )
258 mImpl->SetPrivateFlag( Impl::PRIVATE_FLAG_MEASURED_DIMENSION_SET );
259 mImpl->mMeasuredWidth = measuredWidth;
260 mImpl->mMeasuredHeight = measuredHeight;
263 LayoutLength LayoutItem::GetMeasuredWidth() const
265 // Get the size portion of the measured width
266 return mImpl->mMeasuredWidth.GetSize();
269 LayoutLength LayoutItem::GetMeasuredHeight() const
271 return mImpl->mMeasuredHeight.GetSize();
274 MeasuredSize LayoutItem::GetMeasuredWidthAndState() const
276 return mImpl->mMeasuredWidth;
279 MeasuredSize LayoutItem::GetMeasuredHeightAndState() const
281 return mImpl->mMeasuredHeight;
284 LayoutLength LayoutItem::GetSuggestedMinimumWidth() const
286 auto owner = GetOwner();
287 auto actor = Actor::DownCast(owner);
288 auto naturalSize = actor ? actor.GetNaturalSize() : Vector3::ZERO;
290 return std::max( mImpl->mMinimumSize.GetWidth(), LayoutLength::IntType( naturalSize.width ) );
293 LayoutLength LayoutItem::GetSuggestedMinimumHeight() const
295 auto owner = GetOwner();
296 auto actor = Actor::DownCast(owner);
297 auto naturalSize = actor ? actor.GetNaturalSize() : Vector3::ZERO;
299 return std::max( mImpl->mMinimumSize.GetHeight(), LayoutLength::IntType(naturalSize.height) );
302 MeasuredSize LayoutItem::ResolveSizeAndState( LayoutLength size, MeasureSpec measureSpec, MeasuredSize::State childMeasuredState )
304 auto specMode = measureSpec.GetMode();
305 LayoutLength specSize = measureSpec.GetSize();
310 case MeasureSpec::Mode::AT_MOST:
314 result = MeasuredSize( specSize, MeasuredSize::MEASURED_SIZE_TOO_SMALL );
318 result.SetSize( size );
323 case MeasureSpec::Mode::EXACTLY:
325 result.SetSize( specSize );
329 case MeasureSpec::Mode::UNSPECIFIED:
332 result.SetSize( size );
337 result.SetState( childMeasuredState );
342 bool LayoutItem::SetFrame( LayoutLength left, LayoutLength top, LayoutLength right, LayoutLength bottom )
344 bool changed = false;
346 DALI_LOG_INFO( gLayoutFilter, Debug::Verbose, "LayoutItem::SetFrame(%d, %d, %d, %d)\n", left.mValue, top.mValue, right.mValue, bottom.mValue );
348 if( mImpl->mLeft != left || mImpl->mRight != right || mImpl->mTop != top || mImpl->mBottom != bottom )
352 auto oldWidth = mImpl->mRight - mImpl->mLeft;
353 auto oldHeight = mImpl->mBottom - mImpl->mTop;
354 auto newWidth = right - left;
355 auto newHeight = bottom - top;
356 bool sizeChanged = (newWidth != oldWidth) || (newHeight != oldHeight);
360 mImpl->mRight = right;
361 mImpl->mBottom = bottom;
363 mImpl->SetPrivateFlag( Impl::PRIVATE_FLAG_HAS_BOUNDS );
366 // Reflect up to parent control
367 auto owner = GetOwner();
368 auto actor = Actor::DownCast(owner);
371 if( mImpl->mAnimated )
373 auto animation = Animation::New( 0.5f );
374 animation.AnimateTo( Property( actor, Actor::Property::POSITION ),
375 Vector3( float(left.mValue), float(top.mValue), 0.0f ) );
376 animation.AnimateTo( Property( actor, Actor::Property::SIZE ),
377 Vector3( right-left, bottom-top, 0.0f ) );
378 animation.FinishedSignal().Connect( mSlotDelegate, &LayoutItem::OnLayoutAnimationFinished );
383 // @todo Collate into list of Property & Property::Value pairs.
384 actor.SetPosition( Vector3( float(left.mValue), float(top.mValue), 0.0f ) );
385 actor.SetSize( Vector3( right-left, bottom-top, 0.0f ) );
391 SizeChange( LayoutSize( newWidth, newHeight ), LayoutSize( oldWidth, oldHeight ) );
397 void LayoutItem::OnLayoutAnimationFinished( Animation& animation )
399 auto owner = GetOwner();
400 auto actor = Actor::DownCast(owner);
403 actor.SetSize( Vector3( mImpl->mRight-mImpl->mLeft, mImpl->mBottom-mImpl->mTop, 0.0f ) );
407 void LayoutItem::SizeChange( LayoutSize newSize, LayoutSize oldSize)
409 OnSizeChanged( newSize, oldSize );
413 void LayoutItem::OnSizeChanged( LayoutSize newSize, LayoutSize oldSize )
417 void LayoutItem::OnInitialize()
422 } // namespace Internal
423 } // namespace Toolkit