2 * Copyright (c) 2014 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.
19 #include <dali-toolkit/internal/controls/scroll-bar/scroll-bar-impl.h>
22 #include <cstring> // for strcmp
23 #include <dali/public-api/animation/constraint.h>
24 #include <dali/public-api/animation/constraints.h>
25 #include <dali/public-api/images/resource-image.h>
26 #include <dali/public-api/object/type-registry.h>
27 #include <dali/public-api/object/property-array.h>
28 #include <dali/devel-api/object/type-registry-helper.h>
29 #include <dali/integration-api/debug.h>
33 #include <dali-toolkit/internal/controls/scrollable/item-view/item-view-impl.h>
40 const char* DEFAULT_INDICATOR_IMAGE_PATH = DALI_IMAGE_DIR "popup_scroll.png";
41 const Vector4 DEFAULT_INDICATOR_NINE_PATCH_BORDER(4.0f, 9.0f, 7.0f, 11.0f);
42 const float MINIMUM_INDICATOR_HEIGHT(20.0f); // The minimum indicator height for the nine patch border
43 const float DEFAULT_SLIDER_DEPTH(1.0f);
44 const float DEFAULT_INDICATOR_SHOW_DURATION(0.5f);
45 const float DEFAULT_INDICATOR_HIDE_DURATION(0.5f);
46 const float DEFAULT_PAN_GESTURE_PROCESS_TIME(16.7f); // 16.7 milliseconds, i.e. one frame
47 const float DEFAULT_INDICATOR_FIXED_HEIGHT(80.0f);
50 * Indicator size constraint
51 * Indicator size depends on both indicator's parent size and the scroll content size
53 struct IndicatorSizeConstraint
55 IndicatorSizeConstraint()
61 * @param[in] current The current indicator size
62 * @param[in] parentSizeProperty The parent size of scroll indicator.
63 * @return The new scroll indicator size.
65 void operator()(Vector3& current, const PropertyInputContainer& inputs )
67 const Vector3& parentSize = inputs[0]->GetVector3();
68 const float contentSize = inputs[1]->GetFloat();
70 float height = contentSize > parentSize.height ?
71 parentSize.height * ( parentSize.height / contentSize ) :
72 parentSize.height * ( (parentSize.height - contentSize * 0.5f) / parentSize.height);
74 current.y = std::max(MINIMUM_INDICATOR_HEIGHT, height);
79 * Indicator position constraint
80 * Positions the indicator to reflect the current scroll position within the scroll domain.
82 struct IndicatorPositionConstraint
85 * @param[in] minPosition The minimum limit of scroll position
86 * @param[in] maxPosition the maximum limit of scroll position
88 IndicatorPositionConstraint()
94 * @param[in,out] current The current indicator position
95 * @param[in] inputs Contains the size of indicator, the size of indicator's parent, and the scroll position of the scrollable container (from 0.0 -> 1.0 in each axis)
96 * @return The new indicator position is returned.
98 void operator()( Vector3& current, const PropertyInputContainer& inputs )
100 const Vector3& indicatorSize = inputs[0]->GetVector3();
101 const Vector3& parentSize = inputs[1]->GetVector3();
102 const float scrollPosition = -inputs[2]->GetFloat();
103 const float minScrollPosition = inputs[3]->GetFloat();
104 const float maxScrollPosition = inputs[4]->GetFloat();
106 float relativePosition = std::max( 0.0f, std::min( 1.0f, (scrollPosition - minScrollPosition) / (maxScrollPosition - minScrollPosition) ) );
107 current.y = ( parentSize.height - indicatorSize.height ) * relativePosition;
108 current.z = DEFAULT_SLIDER_DEPTH;
112 } // unnamed namespace
126 using namespace Dali;
130 return Toolkit::ScrollBar::New();
133 // Setup properties, signals and actions using the type-registry.
134 DALI_TYPE_REGISTRATION_BEGIN( Toolkit::ScrollBar, Toolkit::Control, Create );
136 DALI_PROPERTY_REGISTRATION( Toolkit, ScrollBar, "scroll-direction", STRING, SCROLL_DIRECTION )
137 DALI_PROPERTY_REGISTRATION( Toolkit, ScrollBar, "indicator-height-policy", STRING, INDICATOR_HEIGHT_POLICY )
138 DALI_PROPERTY_REGISTRATION( Toolkit, ScrollBar, "indicator-fixed-height", FLOAT, INDICATOR_FIXED_HEIGHT )
139 DALI_PROPERTY_REGISTRATION( Toolkit, ScrollBar, "indicator-show-duration", FLOAT, INDICATOR_SHOW_DURATION )
140 DALI_PROPERTY_REGISTRATION( Toolkit, ScrollBar, "indicator-hide-duration", FLOAT, INDICATOR_HIDE_DURATION )
141 DALI_PROPERTY_REGISTRATION( Toolkit, ScrollBar, "scroll-position-intervals", ARRAY, SCROLL_POSITION_INTERVALS )
143 DALI_SIGNAL_REGISTRATION( Toolkit, ScrollBar, "pan-finished", PAN_FINISHED_SIGNAL )
144 DALI_SIGNAL_REGISTRATION( Toolkit, ScrollBar, "scroll-position-interval-reached", SCROLL_POSITION_INTERVAL_REACHED_SIGNAL )
146 DALI_TYPE_REGISTRATION_END()
148 const char* SCROLL_DIRECTION_NAME[] = {"Vertical", "Horizontal"};
149 const char* INDICATOR_HEIGHT_POLICY_NAME[] = {"Variable", "Fixed"};
153 ScrollBar::ScrollBar(Toolkit::ScrollBar::Direction direction)
154 : Control( ControlBehaviour( REQUIRES_TOUCH_EVENTS | REQUIRES_STYLE_CHANGE_SIGNALS ) ),
155 mDirection(direction),
156 mScrollableObject(WeakHandleBase()),
157 mPropertyScrollPosition(Property::INVALID_INDEX),
158 mPropertyMinScrollPosition(Property::INVALID_INDEX),
159 mPropertyMaxScrollPosition(Property::INVALID_INDEX),
160 mPropertyScrollContentSize(Property::INVALID_INDEX),
161 mIndicatorShowDuration(DEFAULT_INDICATOR_SHOW_DURATION),
162 mIndicatorHideDuration(DEFAULT_INDICATOR_HIDE_DURATION),
165 mCurrentScrollPosition(0.0f),
166 mIndicatorHeightPolicy(Toolkit::ScrollBar::Variable),
167 mIndicatorFixedHeight(DEFAULT_INDICATOR_FIXED_HEIGHT)
171 ScrollBar::~ScrollBar()
175 void ScrollBar::OnInitialize()
177 CreateDefaultIndicatorActor();
178 Self().SetDrawMode(DrawMode::OVERLAY_2D);
181 void ScrollBar::SetScrollPropertySource( Handle handle, Property::Index propertyScrollPosition, Property::Index propertyMinScrollPosition, Property::Index propertyMaxScrollPosition, Property::Index propertyScrollContentSize )
184 && propertyScrollPosition != Property::INVALID_INDEX
185 && propertyMinScrollPosition != Property::INVALID_INDEX
186 && propertyMaxScrollPosition != Property::INVALID_INDEX
187 && propertyScrollContentSize != Property::INVALID_INDEX )
189 mScrollableObject = WeakHandleBase(handle);
190 mPropertyScrollPosition = propertyScrollPosition;
191 mPropertyMinScrollPosition = propertyMinScrollPosition;
192 mPropertyMaxScrollPosition = propertyMaxScrollPosition;
193 mPropertyScrollContentSize = propertyScrollContentSize;
199 DALI_LOG_ERROR("Can not set empty handle of source object or invalid source property index\n");
203 void ScrollBar::CreateDefaultIndicatorActor()
205 Image indicatorImage = ResourceImage::New( DEFAULT_INDICATOR_IMAGE_PATH );
206 ImageActor indicator = ImageActor::New( indicatorImage );
207 indicator.SetNinePatchBorder( DEFAULT_INDICATOR_NINE_PATCH_BORDER );
208 indicator.SetStyle( ImageActor::STYLE_NINE_PATCH );
209 indicator.SetParentOrigin( ParentOrigin::TOP_LEFT );
210 indicator.SetAnchorPoint( AnchorPoint::TOP_LEFT );
212 SetScrollIndicator(indicator);
215 void ScrollBar::SetScrollIndicator( Actor indicator )
217 // Don't allow empty handle
220 mIndicator = indicator;
221 Self().Add(mIndicator);
223 EnableGestureDetection(Gesture::Type(Gesture::Pan));
225 PanGestureDetector detector( GetPanGestureDetector() );
226 detector.DetachAll();
227 detector.Attach( mIndicator );
229 unsigned int childCount = mIndicator.GetChildCount();
230 for ( unsigned int index = 0; index < childCount; index++ )
232 Actor child = mIndicator.GetChildAt( index );
235 detector.Attach( child );
241 DALI_LOG_ERROR("Empty handle of scroll indicator\n");
245 Actor ScrollBar::GetScrollIndicator()
250 void ScrollBar::ApplyConstraints()
252 Handle scrollableHandle = mScrollableObject.GetBaseHandle();
254 if( scrollableHandle )
256 if(mIndicatorSizeConstraint)
258 mIndicatorSizeConstraint.Remove();
261 // Set indicator height according to the indicator's height policy
262 if(mIndicatorHeightPolicy == Toolkit::ScrollBar::Fixed)
264 mIndicator.SetSize(Self().GetCurrentSize().width, mIndicatorFixedHeight);
268 mIndicatorSizeConstraint = Constraint::New<Vector3>( mIndicator, Actor::Property::SIZE, IndicatorSizeConstraint() );
269 mIndicatorSizeConstraint.AddSource( ParentSource( Actor::Property::SIZE ) );
270 mIndicatorSizeConstraint.AddSource( Source( scrollableHandle, mPropertyScrollContentSize ) );
271 mIndicatorSizeConstraint.Apply();
274 if(mIndicatorPositionConstraint)
276 mIndicatorPositionConstraint.Remove();
279 mIndicatorPositionConstraint = Constraint::New<Vector3>( mIndicator, Actor::Property::POSITION, IndicatorPositionConstraint() );
280 mIndicatorPositionConstraint.AddSource( LocalSource( Actor::Property::SIZE ) );
281 mIndicatorPositionConstraint.AddSource( ParentSource( Actor::Property::SIZE ) );
282 mIndicatorPositionConstraint.AddSource( Source( scrollableHandle, mPropertyScrollPosition ) );
283 mIndicatorPositionConstraint.AddSource( Source( scrollableHandle, mPropertyMinScrollPosition ) );
284 mIndicatorPositionConstraint.AddSource( Source( scrollableHandle, mPropertyMaxScrollPosition ) );
285 mIndicatorPositionConstraint.Apply();
289 void ScrollBar::SetScrollPositionIntervals( const Dali::Vector<float>& positions )
291 mScrollPositionIntervals = positions;
293 Handle scrollableHandle = mScrollableObject.GetBaseHandle();
295 if( scrollableHandle )
297 if( mPositionNotification )
299 scrollableHandle.RemovePropertyNotification(mPositionNotification);
302 mPositionNotification = scrollableHandle.AddPropertyNotification( mPropertyScrollPosition, VariableStepCondition(mScrollPositionIntervals) );
303 mPositionNotification.NotifySignal().Connect( this, &ScrollBar::OnScrollPositionIntervalReached );
307 Dali::Vector<float> ScrollBar::GetScrollPositionIntervals() const
309 return mScrollPositionIntervals;
312 void ScrollBar::OnScrollPositionIntervalReached(PropertyNotification& source)
314 // Emit the signal to notify the scroll position crossing
315 Handle scrollableHandle = mScrollableObject.GetBaseHandle();
318 mScrollPositionIntervalReachedSignal.Emit(scrollableHandle.GetProperty<float>(mPropertyScrollPosition));
322 void ScrollBar::ShowIndicator()
324 // Cancel any animation
331 if(mIndicatorShowDuration > 0.0f)
333 mAnimation = Animation::New( mIndicatorShowDuration );
334 mAnimation.AnimateTo( Property( mIndicator, Actor::Property::COLOR_ALPHA ), 1.0f, AlphaFunction::EASE_IN );
339 mIndicator.SetOpacity(1.0f);
343 void ScrollBar::HideIndicator()
345 // Cancel any animation
352 if(mIndicatorHideDuration > 0.0f)
354 mAnimation = Animation::New( mIndicatorHideDuration );
355 mAnimation.AnimateTo( Property( mIndicator, Actor::Property::COLOR_ALPHA ), 0.0f, AlphaFunction::EASE_IN );
360 mIndicator.SetOpacity(0.0f);
364 bool ScrollBar::OnPanGestureProcessTick()
366 // Update the scroll position property.
367 Handle scrollableHandle = mScrollableObject.GetBaseHandle();
368 if( scrollableHandle )
370 scrollableHandle.SetProperty(mPropertyScrollPosition, mCurrentScrollPosition);
376 void ScrollBar::OnPan( const PanGesture& gesture )
378 Handle scrollableHandle = mScrollableObject.GetBaseHandle();
382 Dali::Toolkit::ItemView itemView = Dali::Toolkit::ItemView::DownCast(scrollableHandle);
384 switch(gesture.state)
386 case Gesture::Started:
388 if( !mPanProcessTimer )
390 // Make sure the pan gesture is only being processed once per frame.
391 mPanProcessTimer = Timer::New( DEFAULT_PAN_GESTURE_PROCESS_TIME );
392 mPanProcessTimer.TickSignal().Connect( this, &ScrollBar::OnPanGestureProcessTick );
393 mPanProcessTimer.Start();
397 mScrollStart = scrollableHandle.GetProperty<float>(mPropertyScrollPosition);
398 mGestureDisplacement = Vector3::ZERO;
403 case Gesture::Continuing:
405 Vector3 delta(gesture.displacement.x, gesture.displacement.y, 0.0f);
406 mGestureDisplacement+=delta;
408 Vector3 span = Self().GetCurrentSize() - mIndicator.GetCurrentSize();
409 float minScrollPosition = scrollableHandle.GetProperty<float>(mPropertyMinScrollPosition);
410 float maxScrollPosition = scrollableHandle.GetProperty<float>(mPropertyMaxScrollPosition);
411 float domainSize = maxScrollPosition - minScrollPosition;
413 mCurrentScrollPosition = mScrollStart - mGestureDisplacement.y * domainSize / span.y;
414 mCurrentScrollPosition = 0.0f - std::min(maxScrollPosition, std::max(-mCurrentScrollPosition, minScrollPosition));
422 if( mPanProcessTimer )
424 // Destroy the timer when pan gesture is finished.
425 mPanProcessTimer.Stop();
426 mPanProcessTimer.TickSignal().Disconnect( this, &ScrollBar::OnPanGestureProcessTick );
427 mPanProcessTimer.Reset();
432 // Refresh the ItemView cache with extra items
433 GetImpl(itemView).DoRefresh(mCurrentScrollPosition, true);
436 mPanFinishedSignal.Emit();
444 // Disable automatic refresh in ItemView during fast scrolling
445 GetImpl(itemView).SetRefreshEnabled(!mIsPanning);
450 void ScrollBar::OnSizeSet( const Vector3& size )
452 if(mIndicatorHeightPolicy == Toolkit::ScrollBar::Fixed)
454 mIndicator.SetSize(size.width, mIndicatorFixedHeight);
458 void ScrollBar::SetScrollDirection( Toolkit::ScrollBar::Direction direction )
460 mDirection = direction;
463 Toolkit::ScrollBar::Direction ScrollBar::GetScrollDirection() const
468 void ScrollBar::SetIndicatorHeightPolicy( Toolkit::ScrollBar::IndicatorHeightPolicy policy )
470 mIndicatorHeightPolicy = policy;
474 Toolkit::ScrollBar::IndicatorHeightPolicy ScrollBar::GetIndicatorHeightPolicy() const
476 return mIndicatorHeightPolicy;
479 void ScrollBar::SetIndicatorFixedHeight( float height )
481 mIndicatorFixedHeight = height;
483 if(mIndicatorHeightPolicy == Toolkit::ScrollBar::Fixed)
485 mIndicator.SetSize(Self().GetCurrentSize().width, mIndicatorFixedHeight);
489 float ScrollBar::GetIndicatorFixedHeight() const
491 return mIndicatorFixedHeight;
494 void ScrollBar::SetIndicatorShowDuration( float durationSeconds )
496 mIndicatorShowDuration = durationSeconds;
499 float ScrollBar::GetIndicatorShowDuration() const
501 return mIndicatorShowDuration;
504 void ScrollBar::SetIndicatorHideDuration( float durationSeconds )
506 mIndicatorHideDuration = durationSeconds;
509 float ScrollBar::GetIndicatorHideDuration() const
511 return mIndicatorHideDuration;
514 void ScrollBar::OnScrollDirectionPropertySet( Property::Value propertyValue )
516 std::string directionName( propertyValue.Get<std::string>() );
517 if(directionName == "Vertical")
519 SetScrollDirection(Toolkit::ScrollBar::Vertical);
521 else if(directionName == "Horizontal")
523 SetScrollDirection(Toolkit::ScrollBar::Horizontal);
527 DALI_ASSERT_ALWAYS( !"ScrollBar::OnScrollDirectionPropertySet(). Invalid Property value." );
531 void ScrollBar::OnIndicatorHeightPolicyPropertySet( Property::Value propertyValue )
533 std::string policyName( propertyValue.Get<std::string>() );
534 if(policyName == "Variable")
536 SetIndicatorHeightPolicy(Toolkit::ScrollBar::Variable);
538 else if(policyName == "Fixed")
540 SetIndicatorHeightPolicy(Toolkit::ScrollBar::Fixed);
544 DALI_ASSERT_ALWAYS( !"ScrollBar::OnIndicatorHeightPolicyPropertySet(). Invalid Property value." );
548 bool ScrollBar::DoConnectSignal( BaseObject* object, ConnectionTrackerInterface* tracker, const std::string& signalName, FunctorDelegate* functor )
550 Dali::BaseHandle handle( object );
552 bool connected( true );
553 Toolkit::ScrollBar scrollBar = Toolkit::ScrollBar::DownCast( handle );
555 if( 0 == strcmp( signalName.c_str(), PAN_FINISHED_SIGNAL ) )
557 scrollBar.PanFinishedSignal().Connect( tracker, functor );
559 else if( 0 == strcmp( signalName.c_str(), SCROLL_POSITION_INTERVAL_REACHED_SIGNAL ) )
561 scrollBar.ScrollPositionIntervalReachedSignal().Connect( tracker, functor );
565 // signalName does not match any signal
572 void ScrollBar::SetProperty( BaseObject* object, Property::Index index, const Property::Value& value )
574 Toolkit::ScrollBar scrollBar = Toolkit::ScrollBar::DownCast( Dali::BaseHandle( object ) );
578 ScrollBar& scrollBarImpl( GetImpl( scrollBar ) );
581 case Toolkit::ScrollBar::Property::SCROLL_DIRECTION:
583 scrollBarImpl.OnScrollDirectionPropertySet( value );
586 case Toolkit::ScrollBar::Property::INDICATOR_HEIGHT_POLICY:
588 scrollBarImpl.OnIndicatorHeightPolicyPropertySet( value );
591 case Toolkit::ScrollBar::Property::INDICATOR_FIXED_HEIGHT:
593 scrollBarImpl.SetIndicatorFixedHeight(value.Get<float>());
596 case Toolkit::ScrollBar::Property::INDICATOR_SHOW_DURATION:
598 scrollBarImpl.SetIndicatorShowDuration(value.Get<float>());
601 case Toolkit::ScrollBar::Property::INDICATOR_HIDE_DURATION:
603 scrollBarImpl.SetIndicatorHideDuration(value.Get<float>());
606 case Toolkit::ScrollBar::Property::SCROLL_POSITION_INTERVALS:
608 Property::Array* array = value.GetArray();
611 Dali::Vector<float> positions;
612 size_t positionCount = array->Count();
613 positions.Resize( positionCount );
614 for( size_t i = 0; i != positionCount; ++i )
616 array->GetElementAt( i ).Get( positions[i] );
619 scrollBarImpl.SetScrollPositionIntervals(positions);
627 Property::Value ScrollBar::GetProperty( BaseObject* object, Property::Index index )
629 Property::Value value;
631 Toolkit::ScrollBar scrollBar = Toolkit::ScrollBar::DownCast( Dali::BaseHandle( object ) );
635 ScrollBar& scrollBarImpl( GetImpl( scrollBar ) );
638 case Toolkit::ScrollBar::Property::SCROLL_DIRECTION:
640 value = SCROLL_DIRECTION_NAME[ scrollBarImpl.GetScrollDirection() ];
643 case Toolkit::ScrollBar::Property::INDICATOR_HEIGHT_POLICY:
645 value = INDICATOR_HEIGHT_POLICY_NAME[ scrollBarImpl.GetIndicatorHeightPolicy() ];
648 case Toolkit::ScrollBar::Property::INDICATOR_FIXED_HEIGHT:
650 value = scrollBarImpl.GetIndicatorFixedHeight();
653 case Toolkit::ScrollBar::Property::INDICATOR_SHOW_DURATION:
655 value = scrollBarImpl.GetIndicatorShowDuration();
658 case Toolkit::ScrollBar::Property::INDICATOR_HIDE_DURATION:
660 value = scrollBarImpl.GetIndicatorHideDuration();
663 case Toolkit::ScrollBar::Property::SCROLL_POSITION_INTERVALS:
665 Property::Value value( Property::ARRAY );
666 Property::Array* array = value.GetArray();
670 Dali::Vector<float> positions = scrollBarImpl.GetScrollPositionIntervals();
671 size_t positionCount( array->Count() );
672 for( size_t i( 0 ); i != positionCount; ++i )
674 array->PushBack( positions[i] );
684 Toolkit::ScrollBar ScrollBar::New(Toolkit::ScrollBar::Direction direction)
686 // Create the implementation, temporarily owned by this handle on stack
687 IntrusivePtr< ScrollBar > impl = new ScrollBar(direction);
689 // Pass ownership to CustomActor handle
690 Toolkit::ScrollBar handle( *impl );
692 // Second-phase init of the implementation
693 // This can only be done after the CustomActor connection has been made...
699 } // namespace Internal
701 } // namespace Toolkit