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>
28 #if defined(DEBUG_ENABLED)
29 Debug::Filter* gLayoutFilter = Debug::Filter::New( Debug::NoLogging, false, "LOG_LAYOUT" );
32 const char* WIDTH_SPECIFICATION_NAME( "widthSpecification" );
33 const char* HEIGHT_SPECIFICATION_NAME( "heightSpecification" );
35 const float DEFAULT_TRANSITION_DURATION( 0.5f );
45 LayoutItem::LayoutItem()
46 : mImpl( new LayoutItem::Impl() ),
51 LayoutItem::~LayoutItem()
53 // An object with a unique_ptr to an opaque structure must define it's destructor in the translation unit
54 // where the opaque structure is defined. It cannot use the default method in the header file.
57 LayoutItemPtr LayoutItem::New( Handle& owner )
59 LayoutItemPtr layoutPtr = new LayoutItem();
63 void LayoutItem::Initialize( Handle& owner, const std::string& containerType )
65 mImpl->mOwner = &(owner.GetBaseObject());
66 RegisterChildProperties( containerType );
67 OnInitialize(); // Ensure direct deriving class gets initialized
71 Handle LayoutItem::GetOwner() const
73 return Handle::DownCast(BaseHandle(mImpl->mOwner));
76 void LayoutItem::Unparent()
78 // Enable directly derived types to first remove children
85 void LayoutItem::SetAnimateLayout( bool animateLayout )
87 mImpl->mAnimated = animateLayout;
90 bool LayoutItem::IsLayoutAnimated() const
92 return mImpl->mAnimated;
95 void LayoutItem::RegisterChildProperties( const std::string& containerType )
97 // Call on derived types
98 auto typeInfo = TypeRegistry::Get().GetTypeInfo( containerType );
101 Property::IndexContainer indices;
102 typeInfo.GetChildPropertyIndices( indices );
104 if( std::find( indices.Begin(), indices.End(), Toolkit::LayoutItem::ChildProperty::WIDTH_SPECIFICATION ) ==
107 ChildPropertyRegistration( typeInfo.GetName(), WIDTH_SPECIFICATION_NAME,
108 Toolkit::LayoutItem::ChildProperty::WIDTH_SPECIFICATION, Property::INTEGER );
110 ChildPropertyRegistration( typeInfo.GetName(), HEIGHT_SPECIFICATION_NAME,
111 Toolkit::LayoutItem::ChildProperty::HEIGHT_SPECIFICATION, Property::INTEGER );
114 OnRegisterChildProperties( containerType );
118 void LayoutItem::OnRegisterChildProperties( const std::string& containerType )
123 void LayoutItem::Measure( MeasureSpec widthMeasureSpec, MeasureSpec heightMeasureSpec )
125 DALI_LOG_TRACE_METHOD( gLayoutFilter );
127 const bool forceLayout = mImpl->GetPrivateFlag( Impl::PRIVATE_FLAG_FORCE_LAYOUT );
129 const bool specChanged =
130 ( widthMeasureSpec != mImpl->mOldWidthMeasureSpec ) ||
131 ( heightMeasureSpec != mImpl->mOldHeightMeasureSpec );
133 const bool isSpecExactly =
134 ( widthMeasureSpec.GetMode() == MeasureSpec::Mode::EXACTLY ) &&
135 ( heightMeasureSpec.GetMode() == MeasureSpec::Mode::EXACTLY );
137 const bool matchesSpecSize =
138 ( GetMeasuredWidth() == widthMeasureSpec.GetSize() ) &&
139 ( GetMeasuredHeight() == heightMeasureSpec.GetSize() );
141 const bool needsLayout = specChanged && ( !isSpecExactly || !matchesSpecSize );
143 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"));
145 if( forceLayout || needsLayout )
147 mImpl->ClearPrivateFlag( Impl::PRIVATE_FLAG_MEASURED_DIMENSION_SET );
149 // measure ourselves, this should set the measured dimension flag back
150 #if defined(DEBUG_ENABLED)
151 std::ostringstream o;
152 o<<widthMeasureSpec<<","<<heightMeasureSpec;
153 DALI_LOG_INFO( gLayoutFilter, Debug::Concise, "Calling %s OnMeasure( %s )\n", Actor::DownCast(GetOwner()).GetName().c_str(), o.str().c_str());
155 OnMeasure( widthMeasureSpec, heightMeasureSpec );
156 mImpl->ClearPrivateFlag( 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;
168 void LayoutItem::Layout( LayoutLength l, LayoutLength t, LayoutLength r, LayoutLength b )
170 if( mImpl->GetPrivateFlag( Impl::PRIVATE_FLAG_MEASURE_NEEDED_BEFORE_LAYOUT ) )
172 OnMeasure( mImpl->mOldWidthMeasureSpec, mImpl->mOldHeightMeasureSpec );
173 mImpl->ClearPrivateFlag( Impl::PRIVATE_FLAG_MEASURE_NEEDED_BEFORE_LAYOUT );
176 bool changed = SetFrame( l, t, r, b );
178 if( changed || mImpl->GetPrivateFlag( Impl::PRIVATE_FLAG_LAYOUT_REQUIRED ) )
180 OnLayout( changed, l, t, r, b );
181 mImpl->ClearPrivateFlag( Impl::PRIVATE_FLAG_LAYOUT_REQUIRED );
184 mImpl->ClearPrivateFlag( Impl::PRIVATE_FLAG_FORCE_LAYOUT );
185 mImpl->SetPrivateFlag( Impl::PRIVATE_FLAG_IS_LAID_OUT );
188 LayoutLength LayoutItem::GetMinimumWidth() const
190 return mImpl->mMinimumSize.GetWidth();
193 LayoutLength LayoutItem::GetMinimumHeight() const
195 return mImpl->mMinimumSize.GetHeight();
198 void LayoutItem::SetMinimumWidth( LayoutLength minimumWidth )
200 mImpl->mMinimumSize.SetWidth( minimumWidth );
204 void LayoutItem::SetMinimumHeight( LayoutLength minimumHeight )
206 mImpl->mMinimumSize.SetHeight( minimumHeight );
210 Extents LayoutItem::GetPadding() const
212 Toolkit::Control control = Toolkit::Control::DownCast( mImpl->mOwner );
215 Extents padding = control.GetProperty<Extents>( Toolkit::Control::Property::PADDING );
217 DALI_LOG_INFO( gLayoutFilter, Debug::Verbose, "LayoutBase::Padding for %s : (%d,%d,%d,%d) \n",
218 control.GetName().c_str(),
219 padding.start, padding.end, padding.top, padding.bottom
229 Extents LayoutItem::GetMargin() const
231 Toolkit::Control control = Toolkit::Control::DownCast( mImpl->mOwner );
234 return control.GetProperty<Extents>( Toolkit::Control::Property::MARGIN );
242 LayoutLength LayoutItem::GetDefaultSize( LayoutLength size, MeasureSpec measureSpec )
244 LayoutLength result = size;
245 auto specMode = measureSpec.GetMode();
246 auto specSize = measureSpec.GetSize();
250 case MeasureSpec::Mode::UNSPECIFIED:
255 case MeasureSpec::Mode::AT_MOST:
257 LayoutLength tmp = specSize;
268 case MeasureSpec::Mode::EXACTLY:
277 void LayoutItem::OnMeasure( MeasureSpec widthMeasureSpec, MeasureSpec heightMeasureSpec)
279 SetMeasuredDimensions( GetDefaultSize( GetSuggestedMinimumWidth(), widthMeasureSpec ),
280 GetDefaultSize( GetSuggestedMinimumHeight(), heightMeasureSpec ) );
283 void LayoutItem::OnLayout( bool changed, LayoutLength left, LayoutLength top, LayoutLength right, LayoutLength bottom )
287 void LayoutItem::SetParent( LayoutParent* parent )
289 mImpl->mLayoutParent = parent;
292 LayoutParent* LayoutItem::GetParent()
294 return mImpl->mLayoutParent;
297 void LayoutItem::RequestLayout()
299 Toolkit::Control control = Toolkit::Control::DownCast( mImpl->mOwner );
302 DALI_LOG_INFO( gLayoutFilter, Debug::Verbose, "LayoutItem::RequestLayout %s\n", control.GetName().c_str());
304 // @todo Enforce failure if called in Measure/Layout passes.
305 mImpl->SetPrivateFlag( Impl::PRIVATE_FLAG_FORCE_LAYOUT );
306 Toolkit::LayoutController layoutController = Toolkit::LayoutController::Get();
307 layoutController.RequestLayout( Toolkit::LayoutItem(this) );
310 bool LayoutItem::IsLayoutRequested() const
312 return mImpl->GetPrivateFlag( Impl::PRIVATE_FLAG_FORCE_LAYOUT );
315 void LayoutItem::SetLayoutRequested()
317 return mImpl->SetPrivateFlag( Impl::PRIVATE_FLAG_FORCE_LAYOUT );
320 void LayoutItem::SetMeasuredDimensions( MeasuredSize measuredWidth, MeasuredSize measuredHeight )
322 DALI_LOG_INFO( gLayoutFilter, Debug::Verbose, "LayoutBase::SetMeasuredDimensions width(%d) height(%d) \n",
323 MeasureSpec::IntType( measuredWidth.GetSize() ),
324 MeasureSpec::IntType( measuredHeight.GetSize() )
327 mImpl->SetPrivateFlag( Impl::PRIVATE_FLAG_MEASURED_DIMENSION_SET );
328 mImpl->mMeasuredWidth = measuredWidth;
329 mImpl->mMeasuredHeight = measuredHeight;
332 LayoutLength LayoutItem::GetMeasuredWidth() const
334 // Get the size portion of the measured width
335 return mImpl->mMeasuredWidth.GetSize();
338 LayoutLength LayoutItem::GetMeasuredHeight() const
340 return mImpl->mMeasuredHeight.GetSize();
343 MeasuredSize LayoutItem::GetMeasuredWidthAndState() const
345 return mImpl->mMeasuredWidth;
348 MeasuredSize LayoutItem::GetMeasuredHeightAndState() const
350 return mImpl->mMeasuredHeight;
353 LayoutLength LayoutItem::GetSuggestedMinimumWidth() const
355 auto owner = GetOwner();
356 auto actor = Actor::DownCast(owner);
357 auto naturalSize = actor ? actor.GetNaturalSize() : Vector3::ZERO;
359 return std::max( mImpl->mMinimumSize.GetWidth(), LayoutLength::IntType( naturalSize.width ) );
362 LayoutLength LayoutItem::GetSuggestedMinimumHeight() const
364 auto owner = GetOwner();
365 auto actor = Actor::DownCast(owner);
366 auto naturalSize = actor ? actor.GetNaturalSize() : Vector3::ZERO;
368 return std::max( mImpl->mMinimumSize.GetHeight(), LayoutLength::IntType(naturalSize.height) );
371 MeasuredSize LayoutItem::ResolveSizeAndState( LayoutLength size, MeasureSpec measureSpec, MeasuredSize::State childMeasuredState )
373 auto specMode = measureSpec.GetMode();
374 LayoutLength specSize = measureSpec.GetSize();
379 case MeasureSpec::Mode::AT_MOST:
383 result = MeasuredSize( specSize, MeasuredSize::MEASURED_SIZE_TOO_SMALL );
387 result.SetSize( size );
392 case MeasureSpec::Mode::EXACTLY:
394 result.SetSize( specSize );
398 case MeasureSpec::Mode::UNSPECIFIED:
401 result.SetSize( size );
406 result.SetState( childMeasuredState );
411 bool LayoutItem::SetFrame( LayoutLength left, LayoutLength top, LayoutLength right, LayoutLength bottom )
413 bool changed = false;
415 DALI_LOG_INFO( gLayoutFilter, Debug::Verbose, "LayoutItem::SetFrame(%d, %d, %d, %d)\n", left.mValue, top.mValue, right.mValue, bottom.mValue );
417 if( mImpl->mLeft != left || mImpl->mRight != right || mImpl->mTop != top || mImpl->mBottom != bottom )
421 auto oldWidth = mImpl->mRight - mImpl->mLeft;
422 auto oldHeight = mImpl->mBottom - mImpl->mTop;
423 auto newWidth = right - left;
424 auto newHeight = bottom - top;
425 bool sizeChanged = (newWidth != oldWidth) || (newHeight != oldHeight);
429 mImpl->mRight = right;
430 mImpl->mBottom = bottom;
432 mImpl->SetPrivateFlag( Impl::PRIVATE_FLAG_HAS_BOUNDS );
435 // Reflect up to parent control
436 auto owner = GetOwner();
437 auto actor = Actor::DownCast(owner);
440 if( mImpl->mAnimated )
442 auto animation = Animation::New( 0.5f );
443 animation.AnimateTo( Property( actor, Actor::Property::POSITION ),
444 Vector3( float(left.mValue), float(top.mValue), 0.0f ) );
445 animation.AnimateTo( Property( actor, Actor::Property::SIZE ),
446 Vector3( right-left, bottom-top, 0.0f ) );
447 animation.FinishedSignal().Connect( mSlotDelegate, &LayoutItem::OnLayoutAnimationFinished );
452 // @todo Collate into list of Property & Property::Value pairs.
453 actor.SetPosition( Vector3( float(left.mValue), float(top.mValue), 0.0f ) );
454 actor.SetSize( Vector3( right-left, bottom-top, 0.0f ) );
460 SizeChange( LayoutSize( newWidth, newHeight ), LayoutSize( oldWidth, oldHeight ) );
466 void LayoutItem::OnLayoutAnimationFinished( Animation& animation )
468 auto owner = GetOwner();
469 auto actor = Actor::DownCast(owner);
472 actor.SetSize( Vector3( mImpl->mRight-mImpl->mLeft, mImpl->mBottom-mImpl->mTop, 0.0f ) );
476 void LayoutItem::SizeChange( LayoutSize newSize, LayoutSize oldSize)
478 OnSizeChanged( newSize, oldSize );
482 void LayoutItem::OnSizeChanged( LayoutSize newSize, LayoutSize oldSize )
486 void LayoutItem::OnInitialize()
491 } // namespace Internal
492 } // namespace Toolkit