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 auto owner = GetOwner();
99 auto actor = Actor::DownCast(owner);
101 DALI_LOG_INFO( gLayoutFilter, Debug::Verbose, "LayoutItem::SetAnimateLayout animateLayout(%s) owner(%s)\n", (animateLayout)?"true":"false",
102 ( ( Actor::DownCast( owner) ) ? Actor::DownCast(owner).GetName().c_str() : "Invalid Actor" ) );
104 mImpl->mAnimated = animateLayout;
106 OnAnimationStateChanged( animateLayout );
109 bool LayoutItem::IsLayoutAnimated() const
111 return mImpl->mAnimated;
114 void LayoutItem::RegisterChildProperties( const std::string& containerType )
116 // Call on derived types
117 auto typeInfo = TypeRegistry::Get().GetTypeInfo( containerType );
120 Property::IndexContainer indices;
121 typeInfo.GetChildPropertyIndices( indices );
123 if( std::find( indices.Begin(), indices.End(), Toolkit::LayoutItem::ChildProperty::WIDTH_SPECIFICATION ) ==
126 ChildPropertyRegistration( typeInfo.GetName(), WIDTH_SPECIFICATION_NAME,
127 Toolkit::LayoutItem::ChildProperty::WIDTH_SPECIFICATION, Property::INTEGER );
129 ChildPropertyRegistration( typeInfo.GetName(), HEIGHT_SPECIFICATION_NAME,
130 Toolkit::LayoutItem::ChildProperty::HEIGHT_SPECIFICATION, Property::INTEGER );
133 OnRegisterChildProperties( containerType );
137 void LayoutItem::OnRegisterChildProperties( const std::string& containerType )
142 void LayoutItem::Measure( MeasureSpec widthMeasureSpec, MeasureSpec heightMeasureSpec )
144 DALI_LOG_TRACE_METHOD( gLayoutFilter );
146 const bool forceLayout = mImpl->GetPrivateFlag( Impl::PRIVATE_FLAG_FORCE_LAYOUT );
148 const bool specChanged =
149 ( widthMeasureSpec != mImpl->mOldWidthMeasureSpec ) ||
150 ( heightMeasureSpec != mImpl->mOldHeightMeasureSpec );
152 const bool isSpecExactly =
153 ( widthMeasureSpec.GetMode() == MeasureSpec::Mode::EXACTLY ) &&
154 ( heightMeasureSpec.GetMode() == MeasureSpec::Mode::EXACTLY );
156 const bool matchesSpecSize =
157 ( GetMeasuredWidth() == widthMeasureSpec.GetSize() ) &&
158 ( GetMeasuredHeight() == heightMeasureSpec.GetSize() );
160 const bool needsLayout = specChanged && ( !isSpecExactly || !matchesSpecSize );
162 DALI_LOG_STREAM( gLayoutFilter, Debug::General, "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"));
164 if( forceLayout || needsLayout )
166 mImpl->ClearPrivateFlag( Impl::PRIVATE_FLAG_MEASURED_DIMENSION_SET );
168 // measure ourselves, this should set the measured dimension flag back
169 #if defined(DEBUG_ENABLED)
170 std::ostringstream o;
171 o<<widthMeasureSpec<<","<<heightMeasureSpec;
172 DALI_LOG_INFO( gLayoutFilter, Debug::General, "LayoutItem::Measure Calling %s OnMeasure( %s )\n", Actor::DownCast(GetOwner()).GetName().c_str(), o.str().c_str());
174 OnMeasure( widthMeasureSpec, heightMeasureSpec );
175 mImpl->ClearPrivateFlag( Impl::PRIVATE_FLAG_MEASURE_NEEDED_BEFORE_LAYOUT );
177 // flag not set, setMeasuredDimension() was not invoked, we raise an exception to warn the developer
178 DALI_ASSERT_ALWAYS( mImpl->GetPrivateFlag( Impl::PRIVATE_FLAG_MEASURED_DIMENSION_SET ) &&
179 "Layout's OnMeasure() Measured dimension flag not set" );
180 mImpl->SetPrivateFlag( Impl::PRIVATE_FLAG_LAYOUT_REQUIRED );
183 mImpl->mOldWidthMeasureSpec = widthMeasureSpec;
184 mImpl->mOldHeightMeasureSpec = heightMeasureSpec;
187 void LayoutItem::Layout( LayoutLength l, LayoutLength t, LayoutLength r, LayoutLength b )
189 DALI_LOG_TRACE_METHOD( gLayoutFilter );
191 if( mImpl->GetPrivateFlag( Impl::PRIVATE_FLAG_MEASURE_NEEDED_BEFORE_LAYOUT ) )
193 OnMeasure( mImpl->mOldWidthMeasureSpec, mImpl->mOldHeightMeasureSpec );
194 mImpl->ClearPrivateFlag( Impl::PRIVATE_FLAG_MEASURE_NEEDED_BEFORE_LAYOUT );
197 bool changed = SetFrame( l, t, r, b );
199 if( changed || mImpl->GetPrivateFlag( Impl::PRIVATE_FLAG_LAYOUT_REQUIRED ) )
201 OnLayout( changed, l, t, r, b );
202 mImpl->ClearPrivateFlag( Impl::PRIVATE_FLAG_LAYOUT_REQUIRED );
205 mImpl->ClearPrivateFlag( Impl::PRIVATE_FLAG_FORCE_LAYOUT );
206 mImpl->SetPrivateFlag( Impl::PRIVATE_FLAG_IS_LAID_OUT );
209 LayoutLength LayoutItem::GetMinimumWidth() const
211 return mImpl->mMinimumSize.GetWidth();
214 LayoutLength LayoutItem::GetMinimumHeight() const
216 return mImpl->mMinimumSize.GetHeight();
219 void LayoutItem::SetMinimumWidth( LayoutLength minimumWidth )
221 mImpl->mMinimumSize.SetWidth( minimumWidth );
225 void LayoutItem::SetMinimumHeight( LayoutLength minimumHeight )
227 mImpl->mMinimumSize.SetHeight( minimumHeight );
231 Extents LayoutItem::GetPadding() const
233 Toolkit::Control control = Toolkit::Control::DownCast( mImpl->mOwner );
236 Extents padding = control.GetProperty<Extents>( Toolkit::Control::Property::PADDING );
238 DALI_LOG_INFO( gLayoutFilter, Debug::Verbose, "LayoutItem::Padding for %s : (%d,%d,%d,%d) \n",
239 control.GetName().c_str(),
240 padding.start, padding.end, padding.top, padding.bottom
250 Extents LayoutItem::GetMargin() const
252 Toolkit::Control control = Toolkit::Control::DownCast( mImpl->mOwner );
255 return control.GetProperty<Extents>( Toolkit::Control::Property::MARGIN );
263 LayoutLength LayoutItem::GetDefaultSize( LayoutLength size, MeasureSpec measureSpec )
265 LayoutLength result = size;
266 auto specMode = measureSpec.GetMode();
267 auto specSize = measureSpec.GetSize();
271 case MeasureSpec::Mode::UNSPECIFIED:
276 case MeasureSpec::Mode::AT_MOST:
278 LayoutLength tmp = specSize;
289 case MeasureSpec::Mode::EXACTLY:
298 void LayoutItem::OnMeasure( MeasureSpec widthMeasureSpec, MeasureSpec heightMeasureSpec)
300 DALI_LOG_INFO( gLayoutFilter, Debug::Verbose, "LayoutItem::OnMeasure\n");
302 SetMeasuredDimensions( GetDefaultSize( GetSuggestedMinimumWidth(), widthMeasureSpec ),
303 GetDefaultSize( GetSuggestedMinimumHeight(), heightMeasureSpec ) );
306 void LayoutItem::OnLayout( bool changed, LayoutLength left, LayoutLength top, LayoutLength right, LayoutLength bottom )
310 void LayoutItem::SetParent( LayoutParent* parent )
312 mImpl->mLayoutParent = parent;
313 if ( parent == nullptr )
315 mImpl->ClearPrivateFlag( Impl::PRIVATE_FLAG_FORCE_SET_FRAME );
319 LayoutParent* LayoutItem::GetParent()
321 return mImpl->mLayoutParent;
324 void LayoutItem::RequestLayout()
326 Toolkit::Control control = Toolkit::Control::DownCast( mImpl->mOwner );
329 DALI_LOG_INFO( gLayoutFilter, Debug::Verbose, "LayoutItem::RequestLayout %s\n", control.GetName().c_str());
331 // @todo Enforce failure if called in Measure/Layout passes.
332 mImpl->SetPrivateFlag( Impl::PRIVATE_FLAG_FORCE_LAYOUT );
333 Toolkit::LayoutController layoutController = Toolkit::LayoutController::Get();
334 layoutController.RequestLayout( Toolkit::LayoutItem(this) );
337 bool LayoutItem::IsLayoutRequested() const
339 return mImpl->GetPrivateFlag( Impl::PRIVATE_FLAG_FORCE_LAYOUT );
342 void LayoutItem::SetLayoutRequested()
344 mImpl->SetPrivateFlag( Impl::PRIVATE_FLAG_FORCE_LAYOUT );
347 void LayoutItem::SetMeasuredDimensions( MeasuredSize measuredWidth, MeasuredSize measuredHeight )
349 DALI_LOG_INFO( gLayoutFilter, Debug::Verbose, "LayoutItem::SetMeasuredDimensions width(%d) height(%d) \n",
350 measuredWidth.GetSize().mValue, measuredHeight.GetSize().mValue );
352 mImpl->SetPrivateFlag( Impl::PRIVATE_FLAG_MEASURED_DIMENSION_SET );
353 mImpl->mMeasuredWidth = measuredWidth;
354 mImpl->mMeasuredHeight = measuredHeight;
357 LayoutLength LayoutItem::GetMeasuredWidth() const
359 // Get the size portion of the measured width
360 return mImpl->mMeasuredWidth.GetSize();
363 LayoutLength LayoutItem::GetMeasuredHeight() const
365 return mImpl->mMeasuredHeight.GetSize();
368 MeasuredSize LayoutItem::GetMeasuredWidthAndState() const
370 return mImpl->mMeasuredWidth;
373 MeasuredSize LayoutItem::GetMeasuredHeightAndState() const
375 return mImpl->mMeasuredHeight;
378 LayoutLength LayoutItem::GetSuggestedMinimumWidth() const
380 auto owner = GetOwner();
381 auto actor = Actor::DownCast(owner);
382 auto naturalSize = actor ? actor.GetNaturalSize() : Vector3::ZERO;
384 return std::max( mImpl->mMinimumSize.GetWidth(), LayoutLength::IntType( naturalSize.width ) );
387 LayoutLength LayoutItem::GetSuggestedMinimumHeight() const
389 auto owner = GetOwner();
390 auto actor = Actor::DownCast(owner);
391 auto naturalSize = actor ? actor.GetNaturalSize() : Vector3::ZERO;
393 return std::max( mImpl->mMinimumSize.GetHeight(), LayoutLength::IntType(naturalSize.height) );
396 MeasuredSize LayoutItem::ResolveSizeAndState( LayoutLength size, MeasureSpec measureSpec, MeasuredSize::State childMeasuredState )
398 auto specMode = measureSpec.GetMode();
399 LayoutLength specSize = measureSpec.GetSize();
404 case MeasureSpec::Mode::AT_MOST:
408 result = MeasuredSize( specSize, MeasuredSize::MEASURED_SIZE_TOO_SMALL );
412 result.SetSize( size );
417 case MeasureSpec::Mode::EXACTLY:
419 result.SetSize( specSize );
423 case MeasureSpec::Mode::UNSPECIFIED:
426 result.SetSize( size );
431 result.SetState( childMeasuredState );
436 bool LayoutItem::SetFrame( LayoutLength left, LayoutLength top, LayoutLength right, LayoutLength bottom )
438 bool changed = false;
440 DALI_LOG_INFO( gLayoutFilter, Debug::Verbose, "LayoutItem::SetFrame enter(%d, %d, %d, %d)\n", left.mValue, top.mValue, right.mValue, bottom.mValue );
442 if( mImpl->mLeft != left || mImpl->mRight != right || mImpl->mTop != top || mImpl->mBottom != bottom || !mImpl->GetPrivateFlag( Impl::PRIVATE_FLAG_FORCE_SET_FRAME ) )
446 LayoutLength oldWidth = mImpl->mRight - mImpl->mLeft;
447 LayoutLength oldHeight = mImpl->mBottom - mImpl->mTop;
448 LayoutLength newWidth = right - left;
449 LayoutLength newHeight = bottom - top;
450 bool sizeChanged = (newWidth != oldWidth) || (newHeight != oldHeight);
454 mImpl->mRight = right;
455 mImpl->mBottom = bottom;
457 mImpl->SetPrivateFlag( Impl::PRIVATE_FLAG_FORCE_SET_FRAME );
460 // Reflect up to parent control
461 auto owner = GetOwner();
462 auto actor = Actor::DownCast(owner);
465 DALI_LOG_INFO( gLayoutFilter, Debug::Verbose, "LayoutItem::SetFrame owner(%s) (%d, %d, %d, %d)\n", actor.GetName().c_str(),
466 left.mValue, top.mValue, right.mValue, bottom.mValue );
467 if( mImpl->mAnimated )
469 auto animation = Animation::New( 0.5f );
470 animation.AnimateTo( Property( actor, Actor::Property::POSITION_X ), left.AsFloat() );
471 animation.AnimateTo( Property( actor, Actor::Property::POSITION_Y ), top.AsFloat() );
473 animation.AnimateTo( Property( actor, Actor::Property::SIZE_WIDTH ), newWidth.AsFloat() );
474 animation.AnimateTo( Property( actor, Actor::Property::SIZE_HEIGHT ), newHeight.AsFloat() );
476 animation.FinishedSignal().Connect( mSlotDelegate, &LayoutItem::OnLayoutAnimationFinished );
481 // @todo Collate into list of Property & Property::Value pairs.
482 actor.SetX( left.AsFloat() );
483 actor.SetY( top.AsFloat() );
484 actor.SetProperty( Actor::Property::SIZE_WIDTH, newWidth.AsFloat() );
485 actor.SetProperty( Actor::Property::SIZE_HEIGHT, newHeight.AsFloat() );
491 SizeChange( LayoutSize( newWidth, newHeight ), LayoutSize( oldWidth, oldHeight ) );
495 DALI_LOG_INFO( gLayoutFilter, Debug::Verbose, "LayoutItem::SetFrame exit(%d, %d, %d, %d)\n", left.mValue, top.mValue, right.mValue, bottom.mValue );
500 void LayoutItem::OnLayoutAnimationFinished( Animation& animation )
502 auto owner = GetOwner();
503 auto actor = Actor::DownCast(owner);
506 actor.SetSize( Vector3( mImpl->mRight.AsFloat() - mImpl->mLeft.AsFloat(), mImpl->mBottom.AsFloat() - mImpl->mTop.AsFloat(), 0.0f ) );
510 void LayoutItem::SizeChange( LayoutSize newSize, LayoutSize oldSize)
512 OnSizeChanged( newSize, oldSize );
516 void LayoutItem::OnSizeChanged( LayoutSize newSize, LayoutSize oldSize )
520 void LayoutItem::OnInitialize()
525 } // namespace Internal
526 } // namespace Toolkit