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/devel-api/layouting/layout-group-impl.h>
24 #include <dali-toolkit/internal/layouting/layout-item-data-impl.h>
29 #if defined(DEBUG_ENABLED)
30 Debug::Filter* gLayoutFilter = Debug::Filter::New( Debug::NoLogging, false, "LOG_LAYOUT" );
33 const char* WIDTH_SPECIFICATION_NAME( "widthSpecification" );
34 const char* HEIGHT_SPECIFICATION_NAME( "heightSpecification" );
36 const float DEFAULT_TRANSITION_DURATION( 0.5f );
46 LayoutItem::LayoutItem()
47 : mImpl( new LayoutItem::Impl() ),
52 LayoutItem::~LayoutItem()
54 // An object with a unique_ptr to an opaque structure must define it's destructor in the translation unit
55 // where the opaque structure is defined. It cannot use the default method in the header file.
58 LayoutItemPtr LayoutItem::New( Handle& owner )
60 LayoutItemPtr layoutPtr = new LayoutItem();
64 void LayoutItem::Initialize( Handle& owner, const std::string& containerType )
66 mImpl->mOwner = &(owner.GetBaseObject());
67 RegisterChildProperties( containerType );
68 OnInitialize(); // Ensure direct deriving class gets initialized
72 Handle LayoutItem::GetOwner() const
74 return Handle::DownCast(BaseHandle(mImpl->mOwner));
77 void LayoutItem::Unparent()
79 // Enable directly derived types to first remove children
82 // Remove myself from parent
83 LayoutParent* parent = GetParent();
86 parent->Remove( *this );
89 // Remove parent reference
96 void LayoutItem::SetAnimateLayout( bool animateLayout )
98 DALI_LOG_INFO( gLayoutFilter, Debug::Verbose, "LayoutItem::SetAnimateLayout animateLayout(%s)\n", (animateLayout)?"true":"false" );
100 auto owner = GetOwner();
101 auto actor = Actor::DownCast(owner);
105 DALI_LOG_INFO( gLayoutFilter, Debug::Verbose, "LayoutItem::SetAnimateLayout animateLayout(%s) owner(%s)\n", (animateLayout)?"true":"false", actor.GetName().c_str() );
108 mImpl->mAnimated = animateLayout;
110 OnAnimationStateChanged( animateLayout );
113 bool LayoutItem::IsLayoutAnimated() const
115 return mImpl->mAnimated;
118 void LayoutItem::RegisterChildProperties( const std::string& containerType )
120 // Call on derived types
121 auto typeInfo = TypeRegistry::Get().GetTypeInfo( containerType );
124 Property::IndexContainer indices;
125 typeInfo.GetChildPropertyIndices( indices );
127 if( std::find( indices.Begin(), indices.End(), Toolkit::LayoutItem::ChildProperty::WIDTH_SPECIFICATION ) ==
130 ChildPropertyRegistration( typeInfo.GetName(), WIDTH_SPECIFICATION_NAME,
131 Toolkit::LayoutItem::ChildProperty::WIDTH_SPECIFICATION, Property::INTEGER );
133 ChildPropertyRegistration( typeInfo.GetName(), HEIGHT_SPECIFICATION_NAME,
134 Toolkit::LayoutItem::ChildProperty::HEIGHT_SPECIFICATION, Property::INTEGER );
137 OnRegisterChildProperties( containerType );
141 void LayoutItem::OnRegisterChildProperties( const std::string& containerType )
146 void LayoutItem::Measure( MeasureSpec widthMeasureSpec, MeasureSpec heightMeasureSpec )
148 DALI_LOG_TRACE_METHOD( gLayoutFilter );
150 const bool forceLayout = mImpl->GetPrivateFlag( Impl::PRIVATE_FLAG_FORCE_LAYOUT );
152 const bool specChanged =
153 ( widthMeasureSpec != mImpl->mOldWidthMeasureSpec ) ||
154 ( heightMeasureSpec != mImpl->mOldHeightMeasureSpec );
156 const bool isSpecExactly =
157 ( widthMeasureSpec.GetMode() == MeasureSpec::Mode::EXACTLY ) &&
158 ( heightMeasureSpec.GetMode() == MeasureSpec::Mode::EXACTLY );
160 const bool matchesSpecSize =
161 ( GetMeasuredWidth() == widthMeasureSpec.GetSize() ) &&
162 ( GetMeasuredHeight() == heightMeasureSpec.GetSize() );
164 const bool needsLayout = specChanged && ( !isSpecExactly || !matchesSpecSize );
166 DALI_LOG_STREAM( gLayoutFilter, Debug::Verbose, "LayoutItem::Measure("<<widthMeasureSpec<<", "<<heightMeasureSpec<<") Owner:"<<Actor::DownCast(GetOwner()).GetName() <<" forceLayout="<<forceLayout<<", specChanged="<<specChanged<<", isSpecExactly="<<isSpecExactly<<", matchesSpecSize="<<matchesSpecSize<<", needsLayout="<<needsLayout <<std::endl <<(forceLayout||needsLayout?" Remeasuring":" NoChange"));
168 if( forceLayout || needsLayout )
170 mImpl->ClearPrivateFlag( Impl::PRIVATE_FLAG_MEASURED_DIMENSION_SET );
172 // measure ourselves, this should set the measured dimension flag back
173 #if defined(DEBUG_ENABLED)
174 std::ostringstream o;
175 o<<widthMeasureSpec<<","<<heightMeasureSpec;
176 DALI_LOG_INFO( gLayoutFilter, Debug::Concise, "LayoutItem::Measure Calling %s OnMeasure( %s )\n", Actor::DownCast(GetOwner()).GetName().c_str(), o.str().c_str());
178 OnMeasure( widthMeasureSpec, heightMeasureSpec );
179 mImpl->ClearPrivateFlag( Impl::PRIVATE_FLAG_MEASURE_NEEDED_BEFORE_LAYOUT );
181 // flag not set, setMeasuredDimension() was not invoked, we raise an exception to warn the developer
182 DALI_ASSERT_ALWAYS( mImpl->GetPrivateFlag( Impl::PRIVATE_FLAG_MEASURED_DIMENSION_SET ) &&
183 "Layout's OnMeasure() mension()" );
184 mImpl->SetPrivateFlag( Impl::PRIVATE_FLAG_LAYOUT_REQUIRED );
187 mImpl->mOldWidthMeasureSpec = widthMeasureSpec;
188 mImpl->mOldHeightMeasureSpec = heightMeasureSpec;
191 void LayoutItem::Layout( LayoutLength l, LayoutLength t, LayoutLength r, LayoutLength b )
193 DALI_LOG_TRACE_METHOD( gLayoutFilter );
195 if( mImpl->GetPrivateFlag( Impl::PRIVATE_FLAG_MEASURE_NEEDED_BEFORE_LAYOUT ) )
197 OnMeasure( mImpl->mOldWidthMeasureSpec, mImpl->mOldHeightMeasureSpec );
198 mImpl->ClearPrivateFlag( Impl::PRIVATE_FLAG_MEASURE_NEEDED_BEFORE_LAYOUT );
201 bool changed = SetFrame( l, t, r, b );
203 if( changed || mImpl->GetPrivateFlag( Impl::PRIVATE_FLAG_LAYOUT_REQUIRED ) )
205 OnLayout( changed, l, t, r, b );
206 mImpl->ClearPrivateFlag( Impl::PRIVATE_FLAG_LAYOUT_REQUIRED );
209 mImpl->ClearPrivateFlag( Impl::PRIVATE_FLAG_FORCE_LAYOUT );
210 mImpl->SetPrivateFlag( Impl::PRIVATE_FLAG_IS_LAID_OUT );
213 LayoutLength LayoutItem::GetMinimumWidth() const
215 return mImpl->mMinimumSize.GetWidth();
218 LayoutLength LayoutItem::GetMinimumHeight() const
220 return mImpl->mMinimumSize.GetHeight();
223 void LayoutItem::SetMinimumWidth( LayoutLength minimumWidth )
225 mImpl->mMinimumSize.SetWidth( minimumWidth );
229 void LayoutItem::SetMinimumHeight( LayoutLength minimumHeight )
231 mImpl->mMinimumSize.SetHeight( minimumHeight );
235 Extents LayoutItem::GetPadding() const
237 Toolkit::Control control = Toolkit::Control::DownCast( mImpl->mOwner );
240 Extents padding = control.GetProperty<Extents>( Toolkit::Control::Property::PADDING );
242 DALI_LOG_INFO( gLayoutFilter, Debug::Verbose, "LayoutItem::Padding for %s : (%d,%d,%d,%d) \n",
243 control.GetName().c_str(),
244 padding.start, padding.end, padding.top, padding.bottom
254 Extents LayoutItem::GetMargin() const
256 Toolkit::Control control = Toolkit::Control::DownCast( mImpl->mOwner );
259 return control.GetProperty<Extents>( Toolkit::Control::Property::MARGIN );
267 LayoutLength LayoutItem::GetDefaultSize( LayoutLength size, MeasureSpec measureSpec )
269 LayoutLength result = size;
270 auto specMode = measureSpec.GetMode();
271 auto specSize = measureSpec.GetSize();
275 case MeasureSpec::Mode::UNSPECIFIED:
280 case MeasureSpec::Mode::AT_MOST:
282 LayoutLength tmp = specSize;
293 case MeasureSpec::Mode::EXACTLY:
302 void LayoutItem::OnMeasure( MeasureSpec widthMeasureSpec, MeasureSpec heightMeasureSpec)
304 DALI_LOG_INFO( gLayoutFilter, Debug::Verbose, "LayoutItem::OnMeasure\n");
306 SetMeasuredDimensions( GetDefaultSize( GetSuggestedMinimumWidth(), widthMeasureSpec ),
307 GetDefaultSize( GetSuggestedMinimumHeight(), heightMeasureSpec ) );
310 void LayoutItem::OnLayout( bool changed, LayoutLength left, LayoutLength top, LayoutLength right, LayoutLength bottom )
314 void LayoutItem::SetParent( LayoutParent* parent )
316 mImpl->mLayoutParent = parent;
317 if ( parent == nullptr )
319 mImpl->ClearPrivateFlag( Impl::PRIVATE_FLAG_FORCE_SET_FRAME );
323 LayoutParent* LayoutItem::GetParent()
325 return mImpl->mLayoutParent;
328 void LayoutItem::RequestLayout()
330 Toolkit::Control control = Toolkit::Control::DownCast( mImpl->mOwner );
333 DALI_LOG_INFO( gLayoutFilter, Debug::Verbose, "LayoutItem::RequestLayout %s\n", control.GetName().c_str());
335 // @todo Enforce failure if called in Measure/Layout passes.
336 mImpl->SetPrivateFlag( Impl::PRIVATE_FLAG_FORCE_LAYOUT );
337 Toolkit::LayoutController layoutController = Toolkit::LayoutController::Get();
338 layoutController.RequestLayout( Toolkit::LayoutItem(this) );
341 bool LayoutItem::IsLayoutRequested() const
343 return mImpl->GetPrivateFlag( Impl::PRIVATE_FLAG_FORCE_LAYOUT );
346 void LayoutItem::SetLayoutRequested()
348 mImpl->SetPrivateFlag( Impl::PRIVATE_FLAG_FORCE_LAYOUT );
351 void LayoutItem::SetMeasuredDimensions( MeasuredSize measuredWidth, MeasuredSize measuredHeight )
353 DALI_LOG_INFO( gLayoutFilter, Debug::Verbose, "LayoutItem::SetMeasuredDimensions width(%d) height(%d) \n",
354 MeasureSpec::IntType( measuredWidth.GetSize() ),
355 MeasureSpec::IntType( measuredHeight.GetSize() )
358 mImpl->SetPrivateFlag( Impl::PRIVATE_FLAG_MEASURED_DIMENSION_SET );
359 mImpl->mMeasuredWidth = measuredWidth;
360 mImpl->mMeasuredHeight = measuredHeight;
363 LayoutLength LayoutItem::GetMeasuredWidth() const
365 // Get the size portion of the measured width
366 return mImpl->mMeasuredWidth.GetSize();
369 LayoutLength LayoutItem::GetMeasuredHeight() const
371 return mImpl->mMeasuredHeight.GetSize();
374 MeasuredSize LayoutItem::GetMeasuredWidthAndState() const
376 return mImpl->mMeasuredWidth;
379 MeasuredSize LayoutItem::GetMeasuredHeightAndState() const
381 return mImpl->mMeasuredHeight;
384 LayoutLength LayoutItem::GetSuggestedMinimumWidth() const
386 auto owner = GetOwner();
387 auto actor = Actor::DownCast(owner);
388 auto naturalSize = actor ? actor.GetNaturalSize() : Vector3::ZERO;
390 return std::max( mImpl->mMinimumSize.GetWidth(), LayoutLength::IntType( naturalSize.width ) );
393 LayoutLength LayoutItem::GetSuggestedMinimumHeight() const
395 auto owner = GetOwner();
396 auto actor = Actor::DownCast(owner);
397 auto naturalSize = actor ? actor.GetNaturalSize() : Vector3::ZERO;
399 return std::max( mImpl->mMinimumSize.GetHeight(), LayoutLength::IntType(naturalSize.height) );
402 MeasuredSize LayoutItem::ResolveSizeAndState( LayoutLength size, MeasureSpec measureSpec, MeasuredSize::State childMeasuredState )
404 auto specMode = measureSpec.GetMode();
405 LayoutLength specSize = measureSpec.GetSize();
410 case MeasureSpec::Mode::AT_MOST:
414 result = MeasuredSize( specSize, MeasuredSize::MEASURED_SIZE_TOO_SMALL );
418 result.SetSize( size );
423 case MeasureSpec::Mode::EXACTLY:
425 result.SetSize( specSize );
429 case MeasureSpec::Mode::UNSPECIFIED:
432 result.SetSize( size );
437 result.SetState( childMeasuredState );
442 bool LayoutItem::SetFrame( LayoutLength left, LayoutLength top, LayoutLength right, LayoutLength bottom )
444 bool changed = false;
446 DALI_LOG_INFO( gLayoutFilter, Debug::Verbose, "LayoutItem::SetFrame enter(%d, %d, %d, %d)\n", left.mValue, top.mValue, right.mValue, bottom.mValue );
448 if( mImpl->mLeft != left || mImpl->mRight != right || mImpl->mTop != top || mImpl->mBottom != bottom || !mImpl->GetPrivateFlag( Impl::PRIVATE_FLAG_FORCE_SET_FRAME ) )
452 auto oldWidth = mImpl->mRight - mImpl->mLeft;
453 auto oldHeight = mImpl->mBottom - mImpl->mTop;
454 auto newWidth = right - left;
455 auto newHeight = bottom - top;
456 bool sizeChanged = (newWidth != oldWidth) || (newHeight != oldHeight);
460 mImpl->mRight = right;
461 mImpl->mBottom = bottom;
463 mImpl->SetPrivateFlag( Impl::PRIVATE_FLAG_FORCE_SET_FRAME );
466 // Reflect up to parent control
467 auto owner = GetOwner();
468 auto actor = Actor::DownCast(owner);
471 DALI_LOG_INFO( gLayoutFilter, Debug::Verbose, "LayoutItem::SetFrame owner(%s) (%d, %d, %d, %d)\n", actor.GetName().c_str(),
472 left.mValue, top.mValue, right.mValue, bottom.mValue );
473 if( mImpl->mAnimated )
475 auto animation = Animation::New( 0.5f );
476 animation.AnimateTo( Property( actor, Actor::Property::POSITION_X ), float( left.mValue ) );
477 animation.AnimateTo( Property( actor, Actor::Property::POSITION_Y ), float( top.mValue ) );
479 animation.AnimateTo( Property( actor, Actor::Property::SIZE_WIDTH ), float( newWidth ) );
480 animation.AnimateTo( Property( actor, Actor::Property::SIZE_HEIGHT ), float( newHeight ) );
482 animation.FinishedSignal().Connect( mSlotDelegate, &LayoutItem::OnLayoutAnimationFinished );
487 // @todo Collate into list of Property & Property::Value pairs.
488 actor.SetX( float( left.mValue ) );
489 actor.SetY( float( top.mValue ) );
490 actor.SetProperty( Actor::Property::SIZE_WIDTH, float( newWidth ) );
491 actor.SetProperty( Actor::Property::SIZE_HEIGHT, float( newHeight ) );
497 SizeChange( LayoutSize( newWidth, newHeight ), LayoutSize( oldWidth, oldHeight ) );
501 DALI_LOG_INFO( gLayoutFilter, Debug::Verbose, "LayoutItem::SetFrame exit(%d, %d, %d, %d)\n", left.mValue, top.mValue, right.mValue, bottom.mValue );
506 void LayoutItem::OnLayoutAnimationFinished( Animation& animation )
508 auto owner = GetOwner();
509 auto actor = Actor::DownCast(owner);
512 actor.SetSize( Vector3( mImpl->mRight-mImpl->mLeft, mImpl->mBottom-mImpl->mTop, 0.0f ) );
516 void LayoutItem::SizeChange( LayoutSize newSize, LayoutSize oldSize)
518 OnSizeChanged( newSize, oldSize );
522 void LayoutItem::OnSizeChanged( LayoutSize newSize, LayoutSize oldSize )
526 void LayoutItem::OnInitialize()
531 } // namespace Internal
532 } // namespace Toolkit