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 mImpl->mAnimated = animateLayout;
100 OnAnimationStateChanged( animateLayout );
103 bool LayoutItem::IsLayoutAnimated() const
105 return mImpl->mAnimated;
108 void LayoutItem::RegisterChildProperties( const std::string& containerType )
110 // Call on derived types
111 auto typeInfo = TypeRegistry::Get().GetTypeInfo( containerType );
114 Property::IndexContainer indices;
115 typeInfo.GetChildPropertyIndices( indices );
117 if( std::find( indices.Begin(), indices.End(), Toolkit::LayoutItem::ChildProperty::WIDTH_SPECIFICATION ) ==
120 ChildPropertyRegistration( typeInfo.GetName(), WIDTH_SPECIFICATION_NAME,
121 Toolkit::LayoutItem::ChildProperty::WIDTH_SPECIFICATION, Property::INTEGER );
123 ChildPropertyRegistration( typeInfo.GetName(), HEIGHT_SPECIFICATION_NAME,
124 Toolkit::LayoutItem::ChildProperty::HEIGHT_SPECIFICATION, Property::INTEGER );
127 OnRegisterChildProperties( containerType );
131 void LayoutItem::OnRegisterChildProperties( const std::string& containerType )
136 void LayoutItem::Measure( MeasureSpec widthMeasureSpec, MeasureSpec heightMeasureSpec )
138 DALI_LOG_TRACE_METHOD( gLayoutFilter );
140 const bool forceLayout = mImpl->GetPrivateFlag( Impl::PRIVATE_FLAG_FORCE_LAYOUT );
142 const bool specChanged =
143 ( widthMeasureSpec != mImpl->mOldWidthMeasureSpec ) ||
144 ( heightMeasureSpec != mImpl->mOldHeightMeasureSpec );
146 const bool isSpecExactly =
147 ( widthMeasureSpec.GetMode() == MeasureSpec::Mode::EXACTLY ) &&
148 ( heightMeasureSpec.GetMode() == MeasureSpec::Mode::EXACTLY );
150 const bool matchesSpecSize =
151 ( GetMeasuredWidth() == widthMeasureSpec.GetSize() ) &&
152 ( GetMeasuredHeight() == heightMeasureSpec.GetSize() );
154 const bool needsLayout = specChanged && ( !isSpecExactly || !matchesSpecSize );
156 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"));
158 if( forceLayout || needsLayout )
160 mImpl->ClearPrivateFlag( Impl::PRIVATE_FLAG_MEASURED_DIMENSION_SET );
162 // measure ourselves, this should set the measured dimension flag back
163 #if defined(DEBUG_ENABLED)
164 std::ostringstream o;
165 o<<widthMeasureSpec<<","<<heightMeasureSpec;
166 DALI_LOG_INFO( gLayoutFilter, Debug::Concise, "Calling %s OnMeasure( %s )\n", Actor::DownCast(GetOwner()).GetName().c_str(), o.str().c_str());
168 OnMeasure( widthMeasureSpec, heightMeasureSpec );
169 mImpl->ClearPrivateFlag( Impl::PRIVATE_FLAG_MEASURE_NEEDED_BEFORE_LAYOUT );
171 // flag not set, setMeasuredDimension() was not invoked, we raise an exception to warn the developer
172 DALI_ASSERT_ALWAYS( mImpl->GetPrivateFlag( Impl::PRIVATE_FLAG_MEASURED_DIMENSION_SET ) &&
173 "Layout's OnMeasure() did not set the measured dimension by calling setMeasuredDimension()" );
174 mImpl->SetPrivateFlag( Impl::PRIVATE_FLAG_LAYOUT_REQUIRED );
177 mImpl->mOldWidthMeasureSpec = widthMeasureSpec;
178 mImpl->mOldHeightMeasureSpec = heightMeasureSpec;
181 void LayoutItem::Layout( LayoutLength l, LayoutLength t, LayoutLength r, LayoutLength b )
183 if( mImpl->GetPrivateFlag( Impl::PRIVATE_FLAG_MEASURE_NEEDED_BEFORE_LAYOUT ) )
185 OnMeasure( mImpl->mOldWidthMeasureSpec, mImpl->mOldHeightMeasureSpec );
186 mImpl->ClearPrivateFlag( Impl::PRIVATE_FLAG_MEASURE_NEEDED_BEFORE_LAYOUT );
189 bool changed = SetFrame( l, t, r, b );
191 if( changed || mImpl->GetPrivateFlag( Impl::PRIVATE_FLAG_LAYOUT_REQUIRED ) )
193 OnLayout( changed, l, t, r, b );
194 mImpl->ClearPrivateFlag( Impl::PRIVATE_FLAG_LAYOUT_REQUIRED );
197 mImpl->ClearPrivateFlag( Impl::PRIVATE_FLAG_FORCE_LAYOUT );
198 mImpl->SetPrivateFlag( Impl::PRIVATE_FLAG_IS_LAID_OUT );
201 LayoutLength LayoutItem::GetMinimumWidth() const
203 return mImpl->mMinimumSize.GetWidth();
206 LayoutLength LayoutItem::GetMinimumHeight() const
208 return mImpl->mMinimumSize.GetHeight();
211 void LayoutItem::SetMinimumWidth( LayoutLength minimumWidth )
213 mImpl->mMinimumSize.SetWidth( minimumWidth );
217 void LayoutItem::SetMinimumHeight( LayoutLength minimumHeight )
219 mImpl->mMinimumSize.SetHeight( minimumHeight );
223 Extents LayoutItem::GetPadding() const
225 Toolkit::Control control = Toolkit::Control::DownCast( mImpl->mOwner );
228 Extents padding = control.GetProperty<Extents>( Toolkit::Control::Property::PADDING );
230 DALI_LOG_INFO( gLayoutFilter, Debug::Verbose, "LayoutBase::Padding for %s : (%d,%d,%d,%d) \n",
231 control.GetName().c_str(),
232 padding.start, padding.end, padding.top, padding.bottom
242 Extents LayoutItem::GetMargin() const
244 Toolkit::Control control = Toolkit::Control::DownCast( mImpl->mOwner );
247 return control.GetProperty<Extents>( Toolkit::Control::Property::MARGIN );
255 LayoutLength LayoutItem::GetDefaultSize( LayoutLength size, MeasureSpec measureSpec )
257 LayoutLength result = size;
258 auto specMode = measureSpec.GetMode();
259 auto specSize = measureSpec.GetSize();
263 case MeasureSpec::Mode::UNSPECIFIED:
268 case MeasureSpec::Mode::AT_MOST:
270 LayoutLength tmp = specSize;
281 case MeasureSpec::Mode::EXACTLY:
290 void LayoutItem::OnMeasure( MeasureSpec widthMeasureSpec, MeasureSpec heightMeasureSpec)
292 SetMeasuredDimensions( GetDefaultSize( GetSuggestedMinimumWidth(), widthMeasureSpec ),
293 GetDefaultSize( GetSuggestedMinimumHeight(), heightMeasureSpec ) );
296 void LayoutItem::OnLayout( bool changed, LayoutLength left, LayoutLength top, LayoutLength right, LayoutLength bottom )
300 void LayoutItem::SetParent( LayoutParent* parent )
302 mImpl->mLayoutParent = parent;
305 LayoutParent* LayoutItem::GetParent()
307 return mImpl->mLayoutParent;
310 void LayoutItem::RequestLayout()
312 Toolkit::Control control = Toolkit::Control::DownCast( mImpl->mOwner );
315 DALI_LOG_INFO( gLayoutFilter, Debug::Verbose, "LayoutItem::RequestLayout %s\n", control.GetName().c_str());
317 // @todo Enforce failure if called in Measure/Layout passes.
318 mImpl->SetPrivateFlag( Impl::PRIVATE_FLAG_FORCE_LAYOUT );
319 Toolkit::LayoutController layoutController = Toolkit::LayoutController::Get();
320 layoutController.RequestLayout( Toolkit::LayoutItem(this) );
323 bool LayoutItem::IsLayoutRequested() const
325 return mImpl->GetPrivateFlag( Impl::PRIVATE_FLAG_FORCE_LAYOUT );
328 void LayoutItem::SetLayoutRequested()
330 mImpl->SetPrivateFlag( Impl::PRIVATE_FLAG_FORCE_LAYOUT );
333 void LayoutItem::SetMeasuredDimensions( MeasuredSize measuredWidth, MeasuredSize measuredHeight )
335 DALI_LOG_INFO( gLayoutFilter, Debug::Verbose, "LayoutBase::SetMeasuredDimensions width(%d) height(%d) \n",
336 MeasureSpec::IntType( measuredWidth.GetSize() ),
337 MeasureSpec::IntType( measuredHeight.GetSize() )
340 mImpl->SetPrivateFlag( Impl::PRIVATE_FLAG_MEASURED_DIMENSION_SET );
341 mImpl->mMeasuredWidth = measuredWidth;
342 mImpl->mMeasuredHeight = measuredHeight;
345 LayoutLength LayoutItem::GetMeasuredWidth() const
347 // Get the size portion of the measured width
348 return mImpl->mMeasuredWidth.GetSize();
351 LayoutLength LayoutItem::GetMeasuredHeight() const
353 return mImpl->mMeasuredHeight.GetSize();
356 MeasuredSize LayoutItem::GetMeasuredWidthAndState() const
358 return mImpl->mMeasuredWidth;
361 MeasuredSize LayoutItem::GetMeasuredHeightAndState() const
363 return mImpl->mMeasuredHeight;
366 LayoutLength LayoutItem::GetSuggestedMinimumWidth() const
368 auto owner = GetOwner();
369 auto actor = Actor::DownCast(owner);
370 auto naturalSize = actor ? actor.GetNaturalSize() : Vector3::ZERO;
372 return std::max( mImpl->mMinimumSize.GetWidth(), LayoutLength::IntType( naturalSize.width ) );
375 LayoutLength LayoutItem::GetSuggestedMinimumHeight() const
377 auto owner = GetOwner();
378 auto actor = Actor::DownCast(owner);
379 auto naturalSize = actor ? actor.GetNaturalSize() : Vector3::ZERO;
381 return std::max( mImpl->mMinimumSize.GetHeight(), LayoutLength::IntType(naturalSize.height) );
384 MeasuredSize LayoutItem::ResolveSizeAndState( LayoutLength size, MeasureSpec measureSpec, MeasuredSize::State childMeasuredState )
386 auto specMode = measureSpec.GetMode();
387 LayoutLength specSize = measureSpec.GetSize();
392 case MeasureSpec::Mode::AT_MOST:
396 result = MeasuredSize( specSize, MeasuredSize::MEASURED_SIZE_TOO_SMALL );
400 result.SetSize( size );
405 case MeasureSpec::Mode::EXACTLY:
407 result.SetSize( specSize );
411 case MeasureSpec::Mode::UNSPECIFIED:
414 result.SetSize( size );
419 result.SetState( childMeasuredState );
424 bool LayoutItem::SetFrame( LayoutLength left, LayoutLength top, LayoutLength right, LayoutLength bottom )
426 bool changed = false;
428 DALI_LOG_INFO( gLayoutFilter, Debug::Verbose, "LayoutItem::SetFrame enter(%d, %d, %d, %d)\n", left.mValue, top.mValue, right.mValue, bottom.mValue );
430 if( mImpl->mLeft != left || mImpl->mRight != right || mImpl->mTop != top || mImpl->mBottom != bottom )
434 auto oldWidth = mImpl->mRight - mImpl->mLeft;
435 auto oldHeight = mImpl->mBottom - mImpl->mTop;
436 auto newWidth = right - left;
437 auto newHeight = bottom - top;
438 bool sizeChanged = (newWidth != oldWidth) || (newHeight != oldHeight);
442 mImpl->mRight = right;
443 mImpl->mBottom = bottom;
445 mImpl->SetPrivateFlag( Impl::PRIVATE_FLAG_HAS_BOUNDS );
448 // Reflect up to parent control
449 auto owner = GetOwner();
450 auto actor = Actor::DownCast(owner);
453 if( mImpl->mAnimated )
455 auto animation = Animation::New( 0.5f );
456 animation.AnimateTo( Property( actor, Actor::Property::POSITION ),
457 Vector3( float(left.mValue), float(top.mValue), 0.0f ) );
458 animation.AnimateTo( Property( actor, Actor::Property::SIZE ),
459 Vector3( right-left, bottom-top, 0.0f ) );
460 animation.FinishedSignal().Connect( mSlotDelegate, &LayoutItem::OnLayoutAnimationFinished );
465 // @todo Collate into list of Property & Property::Value pairs.
466 actor.SetPosition( Vector3( float(left.mValue), float(top.mValue), 0.0f ) );
467 actor.SetSize( Vector3( right-left, bottom-top, 0.0f ) );
473 SizeChange( LayoutSize( newWidth, newHeight ), LayoutSize( oldWidth, oldHeight ) );
477 DALI_LOG_INFO( gLayoutFilter, Debug::Verbose, "LayoutItem::SetFrame exit(%d, %d, %d, %d)\n", left.mValue, top.mValue, right.mValue, bottom.mValue );
482 void LayoutItem::OnLayoutAnimationFinished( Animation& animation )
484 auto owner = GetOwner();
485 auto actor = Actor::DownCast(owner);
488 actor.SetSize( Vector3( mImpl->mRight-mImpl->mLeft, mImpl->mBottom-mImpl->mTop, 0.0f ) );
492 void LayoutItem::SizeChange( LayoutSize newSize, LayoutSize oldSize)
494 OnSizeChanged( newSize, oldSize );
498 void LayoutItem::OnSizeChanged( LayoutSize newSize, LayoutSize oldSize )
502 void LayoutItem::OnInitialize()
507 } // namespace Internal
508 } // namespace Toolkit