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;
89 OnAnimationStateChanged( animateLayout );
92 bool LayoutItem::IsLayoutAnimated() const
94 return mImpl->mAnimated;
97 void LayoutItem::RegisterChildProperties( const std::string& containerType )
99 // Call on derived types
100 auto typeInfo = TypeRegistry::Get().GetTypeInfo( containerType );
103 Property::IndexContainer indices;
104 typeInfo.GetChildPropertyIndices( indices );
106 if( std::find( indices.Begin(), indices.End(), Toolkit::LayoutItem::ChildProperty::WIDTH_SPECIFICATION ) ==
109 ChildPropertyRegistration( typeInfo.GetName(), WIDTH_SPECIFICATION_NAME,
110 Toolkit::LayoutItem::ChildProperty::WIDTH_SPECIFICATION, Property::INTEGER );
112 ChildPropertyRegistration( typeInfo.GetName(), HEIGHT_SPECIFICATION_NAME,
113 Toolkit::LayoutItem::ChildProperty::HEIGHT_SPECIFICATION, Property::INTEGER );
116 OnRegisterChildProperties( containerType );
120 void LayoutItem::OnRegisterChildProperties( const std::string& containerType )
125 void LayoutItem::Measure( MeasureSpec widthMeasureSpec, MeasureSpec heightMeasureSpec )
127 DALI_LOG_TRACE_METHOD( gLayoutFilter );
129 const bool forceLayout = mImpl->GetPrivateFlag( Impl::PRIVATE_FLAG_FORCE_LAYOUT );
131 const bool specChanged =
132 ( widthMeasureSpec != mImpl->mOldWidthMeasureSpec ) ||
133 ( heightMeasureSpec != mImpl->mOldHeightMeasureSpec );
135 const bool isSpecExactly =
136 ( widthMeasureSpec.GetMode() == MeasureSpec::Mode::EXACTLY ) &&
137 ( heightMeasureSpec.GetMode() == MeasureSpec::Mode::EXACTLY );
139 const bool matchesSpecSize =
140 ( GetMeasuredWidth() == widthMeasureSpec.GetSize() ) &&
141 ( GetMeasuredHeight() == heightMeasureSpec.GetSize() );
143 const bool needsLayout = specChanged && ( !isSpecExactly || !matchesSpecSize );
145 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"));
147 if( forceLayout || needsLayout )
149 mImpl->ClearPrivateFlag( Impl::PRIVATE_FLAG_MEASURED_DIMENSION_SET );
151 // measure ourselves, this should set the measured dimension flag back
152 #if defined(DEBUG_ENABLED)
153 std::ostringstream o;
154 o<<widthMeasureSpec<<","<<heightMeasureSpec;
155 DALI_LOG_INFO( gLayoutFilter, Debug::Concise, "Calling %s OnMeasure( %s )\n", Actor::DownCast(GetOwner()).GetName().c_str(), o.str().c_str());
157 OnMeasure( widthMeasureSpec, heightMeasureSpec );
158 mImpl->ClearPrivateFlag( Impl::PRIVATE_FLAG_MEASURE_NEEDED_BEFORE_LAYOUT );
160 // flag not set, setMeasuredDimension() was not invoked, we raise an exception to warn the developer
161 DALI_ASSERT_ALWAYS( mImpl->GetPrivateFlag( Impl::PRIVATE_FLAG_MEASURED_DIMENSION_SET ) &&
162 "Layout's OnMeasure() did not set the measured dimension by calling setMeasuredDimension()" );
163 mImpl->SetPrivateFlag( Impl::PRIVATE_FLAG_LAYOUT_REQUIRED );
166 mImpl->mOldWidthMeasureSpec = widthMeasureSpec;
167 mImpl->mOldHeightMeasureSpec = heightMeasureSpec;
170 void LayoutItem::Layout( LayoutLength l, LayoutLength t, LayoutLength r, LayoutLength b )
172 if( mImpl->GetPrivateFlag( Impl::PRIVATE_FLAG_MEASURE_NEEDED_BEFORE_LAYOUT ) )
174 OnMeasure( mImpl->mOldWidthMeasureSpec, mImpl->mOldHeightMeasureSpec );
175 mImpl->ClearPrivateFlag( Impl::PRIVATE_FLAG_MEASURE_NEEDED_BEFORE_LAYOUT );
178 bool changed = SetFrame( l, t, r, b );
180 if( changed || mImpl->GetPrivateFlag( Impl::PRIVATE_FLAG_LAYOUT_REQUIRED ) )
182 OnLayout( changed, l, t, r, b );
183 mImpl->ClearPrivateFlag( Impl::PRIVATE_FLAG_LAYOUT_REQUIRED );
186 mImpl->ClearPrivateFlag( Impl::PRIVATE_FLAG_FORCE_LAYOUT );
187 mImpl->SetPrivateFlag( Impl::PRIVATE_FLAG_IS_LAID_OUT );
190 LayoutLength LayoutItem::GetMinimumWidth() const
192 return mImpl->mMinimumSize.GetWidth();
195 LayoutLength LayoutItem::GetMinimumHeight() const
197 return mImpl->mMinimumSize.GetHeight();
200 void LayoutItem::SetMinimumWidth( LayoutLength minimumWidth )
202 mImpl->mMinimumSize.SetWidth( minimumWidth );
206 void LayoutItem::SetMinimumHeight( LayoutLength minimumHeight )
208 mImpl->mMinimumSize.SetHeight( minimumHeight );
212 Extents LayoutItem::GetPadding() const
214 Toolkit::Control control = Toolkit::Control::DownCast( mImpl->mOwner );
217 Extents padding = control.GetProperty<Extents>( Toolkit::Control::Property::PADDING );
219 DALI_LOG_INFO( gLayoutFilter, Debug::Verbose, "LayoutBase::Padding for %s : (%d,%d,%d,%d) \n",
220 control.GetName().c_str(),
221 padding.start, padding.end, padding.top, padding.bottom
231 Extents LayoutItem::GetMargin() const
233 Toolkit::Control control = Toolkit::Control::DownCast( mImpl->mOwner );
236 return control.GetProperty<Extents>( Toolkit::Control::Property::MARGIN );
244 LayoutLength LayoutItem::GetDefaultSize( LayoutLength size, MeasureSpec measureSpec )
246 LayoutLength result = size;
247 auto specMode = measureSpec.GetMode();
248 auto specSize = measureSpec.GetSize();
252 case MeasureSpec::Mode::UNSPECIFIED:
257 case MeasureSpec::Mode::AT_MOST:
259 LayoutLength tmp = specSize;
270 case MeasureSpec::Mode::EXACTLY:
279 void LayoutItem::OnMeasure( MeasureSpec widthMeasureSpec, MeasureSpec heightMeasureSpec)
281 SetMeasuredDimensions( GetDefaultSize( GetSuggestedMinimumWidth(), widthMeasureSpec ),
282 GetDefaultSize( GetSuggestedMinimumHeight(), heightMeasureSpec ) );
285 void LayoutItem::OnLayout( bool changed, LayoutLength left, LayoutLength top, LayoutLength right, LayoutLength bottom )
289 void LayoutItem::SetParent( LayoutParent* parent )
291 mImpl->mLayoutParent = parent;
294 LayoutParent* LayoutItem::GetParent()
296 return mImpl->mLayoutParent;
299 void LayoutItem::RequestLayout()
301 Toolkit::Control control = Toolkit::Control::DownCast( mImpl->mOwner );
304 DALI_LOG_INFO( gLayoutFilter, Debug::Verbose, "LayoutItem::RequestLayout %s\n", control.GetName().c_str());
306 // @todo Enforce failure if called in Measure/Layout passes.
307 mImpl->SetPrivateFlag( Impl::PRIVATE_FLAG_FORCE_LAYOUT );
308 Toolkit::LayoutController layoutController = Toolkit::LayoutController::Get();
309 layoutController.RequestLayout( Toolkit::LayoutItem(this) );
312 bool LayoutItem::IsLayoutRequested() const
314 return mImpl->GetPrivateFlag( Impl::PRIVATE_FLAG_FORCE_LAYOUT );
317 void LayoutItem::SetLayoutRequested()
319 return mImpl->SetPrivateFlag( Impl::PRIVATE_FLAG_FORCE_LAYOUT );
322 void LayoutItem::SetMeasuredDimensions( MeasuredSize measuredWidth, MeasuredSize measuredHeight )
324 DALI_LOG_INFO( gLayoutFilter, Debug::Verbose, "LayoutBase::SetMeasuredDimensions width(%d) height(%d) \n",
325 MeasureSpec::IntType( measuredWidth.GetSize() ),
326 MeasureSpec::IntType( measuredHeight.GetSize() )
329 mImpl->SetPrivateFlag( Impl::PRIVATE_FLAG_MEASURED_DIMENSION_SET );
330 mImpl->mMeasuredWidth = measuredWidth;
331 mImpl->mMeasuredHeight = measuredHeight;
334 LayoutLength LayoutItem::GetMeasuredWidth() const
336 // Get the size portion of the measured width
337 return mImpl->mMeasuredWidth.GetSize();
340 LayoutLength LayoutItem::GetMeasuredHeight() const
342 return mImpl->mMeasuredHeight.GetSize();
345 MeasuredSize LayoutItem::GetMeasuredWidthAndState() const
347 return mImpl->mMeasuredWidth;
350 MeasuredSize LayoutItem::GetMeasuredHeightAndState() const
352 return mImpl->mMeasuredHeight;
355 LayoutLength LayoutItem::GetSuggestedMinimumWidth() const
357 auto owner = GetOwner();
358 auto actor = Actor::DownCast(owner);
359 auto naturalSize = actor ? actor.GetNaturalSize() : Vector3::ZERO;
361 return std::max( mImpl->mMinimumSize.GetWidth(), LayoutLength::IntType( naturalSize.width ) );
364 LayoutLength LayoutItem::GetSuggestedMinimumHeight() const
366 auto owner = GetOwner();
367 auto actor = Actor::DownCast(owner);
368 auto naturalSize = actor ? actor.GetNaturalSize() : Vector3::ZERO;
370 return std::max( mImpl->mMinimumSize.GetHeight(), LayoutLength::IntType(naturalSize.height) );
373 MeasuredSize LayoutItem::ResolveSizeAndState( LayoutLength size, MeasureSpec measureSpec, MeasuredSize::State childMeasuredState )
375 auto specMode = measureSpec.GetMode();
376 LayoutLength specSize = measureSpec.GetSize();
381 case MeasureSpec::Mode::AT_MOST:
385 result = MeasuredSize( specSize, MeasuredSize::MEASURED_SIZE_TOO_SMALL );
389 result.SetSize( size );
394 case MeasureSpec::Mode::EXACTLY:
396 result.SetSize( specSize );
400 case MeasureSpec::Mode::UNSPECIFIED:
403 result.SetSize( size );
408 result.SetState( childMeasuredState );
413 bool LayoutItem::SetFrame( LayoutLength left, LayoutLength top, LayoutLength right, LayoutLength bottom )
415 bool changed = false;
417 DALI_LOG_INFO( gLayoutFilter, Debug::Verbose, "LayoutItem::SetFrame(%d, %d, %d, %d)\n", left.mValue, top.mValue, right.mValue, bottom.mValue );
419 if( mImpl->mLeft != left || mImpl->mRight != right || mImpl->mTop != top || mImpl->mBottom != bottom )
423 auto oldWidth = mImpl->mRight - mImpl->mLeft;
424 auto oldHeight = mImpl->mBottom - mImpl->mTop;
425 auto newWidth = right - left;
426 auto newHeight = bottom - top;
427 bool sizeChanged = (newWidth != oldWidth) || (newHeight != oldHeight);
431 mImpl->mRight = right;
432 mImpl->mBottom = bottom;
434 mImpl->SetPrivateFlag( Impl::PRIVATE_FLAG_HAS_BOUNDS );
437 // Reflect up to parent control
438 auto owner = GetOwner();
439 auto actor = Actor::DownCast(owner);
442 if( mImpl->mAnimated )
444 auto animation = Animation::New( 0.5f );
445 animation.AnimateTo( Property( actor, Actor::Property::POSITION ),
446 Vector3( float(left.mValue), float(top.mValue), 0.0f ) );
447 animation.AnimateTo( Property( actor, Actor::Property::SIZE ),
448 Vector3( right-left, bottom-top, 0.0f ) );
449 animation.FinishedSignal().Connect( mSlotDelegate, &LayoutItem::OnLayoutAnimationFinished );
454 // @todo Collate into list of Property & Property::Value pairs.
455 actor.SetPosition( Vector3( float(left.mValue), float(top.mValue), 0.0f ) );
456 actor.SetSize( Vector3( right-left, bottom-top, 0.0f ) );
462 SizeChange( LayoutSize( newWidth, newHeight ), LayoutSize( oldWidth, oldHeight ) );
468 void LayoutItem::OnLayoutAnimationFinished( Animation& animation )
470 auto owner = GetOwner();
471 auto actor = Actor::DownCast(owner);
474 actor.SetSize( Vector3( mImpl->mRight-mImpl->mLeft, mImpl->mBottom-mImpl->mTop, 0.0f ) );
478 void LayoutItem::SizeChange( LayoutSize newSize, LayoutSize oldSize)
480 OnSizeChanged( newSize, oldSize );
484 void LayoutItem::OnSizeChanged( LayoutSize newSize, LayoutSize oldSize )
488 void LayoutItem::OnInitialize()
493 } // namespace Internal
494 } // namespace Toolkit