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 <dali/public-api/animation/constraints.h>
23 #include <dali/public-api/object/type-registry.h>
26 #include <dali-toolkit/internal/controls/scrollable/item-view/item-view-impl.h>
33 const char* DEFAULT_INDICATOR_IMAGE_PATH = DALI_IMAGE_DIR "popup_scroll.png";
34 const Vector4 DEFAULT_INDICATOR_NINE_PATCH_BORDER(4.0f, 9.0f, 7.0f, 11.0f);
35 const float MINIMUM_INDICATOR_HEIGHT(20.0f); // The minimum indicator height for the nine patch border
36 const float DEFAULT_SLIDER_DEPTH(1.0f);
37 const float DEFAULT_INDICATOR_SHOW_DURATION(0.5f);
38 const float DEFAULT_INDICATOR_HIDE_DURATION(0.5f);
39 const float DEFAULT_PAN_GESTURE_PROCESS_TIME(16.7f); // 16.7 milliseconds, i.e. one frame
40 const float DEFAULT_INDICATOR_FIXED_HEIGHT(80.0f);
43 * Indicator size constraint
44 * Indicator size depends on both indicator's parent size and the scroll content size
46 struct IndicatorSizeConstraint
49 * @param[in] contentSize The size of scrollable content
51 IndicatorSizeConstraint(float contentSize)
52 : mContentSize(contentSize)
58 * @param[in] current The current indicator size
59 * @param[in] parentSizeProperty The parent size of scroll indicator.
60 * @return The new scroll indicator size.
62 Vector3 operator()(const Vector3& current,
63 const PropertyInput& parentSizeProperty)
65 const Vector3& parentSize = parentSizeProperty.GetVector3();
66 float height = mContentSize > parentSize.height ? parentSize.height * ( parentSize.height / mContentSize ) : parentSize.height * ( (parentSize.height - mContentSize * 0.5f) / parentSize.height);
67 return Vector3( parentSize.width, std::max(MINIMUM_INDICATOR_HEIGHT, height), parentSize.depth );
70 float mContentSize; ///< The size of scrollable content
74 * Indicator position constraint
75 * Positions the indicator to reflect the current scroll position within the scroll domain.
77 struct IndicatorPositionConstraint
80 * @param[in] minPosition The minimum limit of scroll position
81 * @param[in] maxPosition the maximum limit of scroll position
83 IndicatorPositionConstraint(float minPosition, float maxPosition)
84 : mMinPosition(minPosition),
85 mMaxPosition(maxPosition)
91 * @param[in] current The current indicator position
92 * @param[in] indicatorSizeProperty The size of indicator.
93 * @param[in] parentSizeProperty The parent size of indicator.
94 * @param[in] scrollPositionProperty The scroll position of the scrollable container // (from 0.0 -> 1.0 in each axis)
95 * @return The new indicator position is returned.
97 Vector3 operator()(const Vector3& current,
98 const PropertyInput& indicatorSizeProperty,
99 const PropertyInput& parentSizeProperty,
100 const PropertyInput& scrollPositionProperty)
102 Vector3 indicatorSize = indicatorSizeProperty.GetVector3();
103 Vector3 parentSize = parentSizeProperty.GetVector3();
104 float scrollPosition = scrollPositionProperty.GetFloat();
106 const float domainSize = fabs(mMaxPosition - mMinPosition);
107 float relativePosition = (mMaxPosition - scrollPosition) / domainSize;
108 return Vector3(current.x, relativePosition * (parentSize.height - indicatorSize.height), DEFAULT_SLIDER_DEPTH);
111 float mMinPosition; ///< The minimum scroll position
112 float mMaxPosition; ///< The maximum scroll position
115 } // unnamed namespace
123 const Property::Index ScrollBar::PROPERTY_INDICATOR_HEIGHT_POLICY( Internal::ScrollBar::SCROLLBAR_PROPERTY_START_INDEX );
124 const Property::Index ScrollBar::PROPERTY_INDICATOR_FIXED_HEIGHT( Internal::ScrollBar::SCROLLBAR_PROPERTY_START_INDEX + 1 );
125 const Property::Index ScrollBar::PROPERTY_INDICATOR_SHOW_DURATION( Internal::ScrollBar::SCROLLBAR_PROPERTY_START_INDEX + 2 );
126 const Property::Index ScrollBar::PROPERTY_INDICATOR_HIDE_DURATION( Internal::ScrollBar::SCROLLBAR_PROPERTY_START_INDEX + 3 );
134 using namespace Dali;
136 const char* INDICATOR_HEIGHT_POLICY_NAME[] = {"Variable", "Fixed"};
140 return Toolkit::ScrollBar::New();
143 TypeRegistration typeRegistration( typeid(Toolkit::ScrollBar), typeid(Toolkit::ScrollComponent), Create );
145 PropertyRegistration property1( typeRegistration, "indicator-height-policy", Toolkit::ScrollBar::PROPERTY_INDICATOR_HEIGHT_POLICY, Property::STRING, &ScrollBar::SetProperty, &ScrollBar::GetProperty );
146 PropertyRegistration property2( typeRegistration, "indicator-fixed-height", Toolkit::ScrollBar::PROPERTY_INDICATOR_FIXED_HEIGHT, Property::FLOAT, &ScrollBar::SetProperty, &ScrollBar::GetProperty );
147 PropertyRegistration property3( typeRegistration, "indicator-show-duration", Toolkit::ScrollBar::PROPERTY_INDICATOR_SHOW_DURATION, Property::FLOAT, &ScrollBar::SetProperty, &ScrollBar::GetProperty );
148 PropertyRegistration property4( typeRegistration, "indicator-hide-duration", Toolkit::ScrollBar::PROPERTY_INDICATOR_HIDE_DURATION, Property::FLOAT, &ScrollBar::SetProperty, &ScrollBar::GetProperty );
151 ScrollBar::ScrollBar()
152 : mIndicatorShowDuration(DEFAULT_INDICATOR_SHOW_DURATION),
153 mIndicatorHideDuration(DEFAULT_INDICATOR_HIDE_DURATION),
156 mCurrentScrollPosition(0.0f),
157 mIndicatorHeightPolicy(Toolkit::ScrollBar::Variable),
158 mIndicatorFixedHeight(DEFAULT_INDICATOR_FIXED_HEIGHT)
162 ScrollBar::~ScrollBar()
166 void ScrollBar::OnInitialize()
170 Image indicatorImage = Image::New( DEFAULT_INDICATOR_IMAGE_PATH );
171 mIndicator = ImageActor::New( indicatorImage );
172 mIndicator.SetNinePatchBorder( DEFAULT_INDICATOR_NINE_PATCH_BORDER );
173 mIndicator.SetStyle( ImageActor::STYLE_NINE_PATCH );
174 mIndicator.SetParentOrigin( ParentOrigin::TOP_LEFT );
175 mIndicator.SetAnchorPoint( AnchorPoint::TOP_LEFT );
176 self.Add(mIndicator);
178 self.SetDrawMode(DrawMode::OVERLAY);
180 // Enable the pan gesture which is attached to the control
181 EnableGestureDetection(Gesture::Type(Gesture::Pan));
184 void ScrollBar::OnScrollConnectorSet( Toolkit::ScrollConnector oldConnector )
188 oldConnector.DomainChangedSignal().Disconnect(this, &ScrollBar::OnScrollDomainChanged);
190 mScrollPositionObject.Reset();
193 if( mScrollConnector )
195 mScrollPositionObject = mScrollConnector.GetScrollPositionObject();
198 mScrollConnector.DomainChangedSignal().Connect(this, &ScrollBar::OnScrollDomainChanged);
202 void ScrollBar::SetIndicatorImage( Image image )
204 mIndicator.SetImage(image);
207 Actor ScrollBar::GetScrollIndicator()
212 void ScrollBar::ApplyConstraints()
214 if( mScrollConnector )
216 Constraint constraint;
218 if(mIndicatorSizeConstraint)
220 mIndicator.RemoveConstraint(mIndicatorSizeConstraint);
223 // Set indicator height according to the indicator's height policy
224 if(mIndicatorHeightPolicy == Toolkit::ScrollBar::Fixed)
226 mIndicator.SetSize(Self().GetCurrentSize().width, mIndicatorFixedHeight);
230 constraint = Constraint::New<Vector3>( Actor::SIZE,
231 ParentSource( Actor::SIZE ),
232 IndicatorSizeConstraint( mScrollConnector.GetContentLength() ) );
233 mIndicatorSizeConstraint = mIndicator.ApplyConstraint( constraint );
236 if(mIndicatorPositionConstraint)
238 mIndicator.RemoveConstraint(mIndicatorPositionConstraint);
241 constraint = Constraint::New<Vector3>( Actor::POSITION,
242 LocalSource( Actor::SIZE ),
243 ParentSource( Actor::SIZE ),
244 Source( mScrollPositionObject, Toolkit::ScrollConnector::SCROLL_POSITION ),
245 IndicatorPositionConstraint( mScrollConnector.GetMinLimit(), mScrollConnector.GetMaxLimit() ) );
246 mIndicatorPositionConstraint = mIndicator.ApplyConstraint( constraint );
250 mBackground.RemoveConstraints();
252 constraint = Constraint::New<Vector3>(Actor::SIZE,
253 ParentSource(Actor::SIZE),
254 EqualToConstraint());
255 mBackground.ApplyConstraint(constraint);
260 void ScrollBar::SetPositionNotifications( const std::vector<float>& positions )
262 if(mScrollPositionObject)
264 if(mPositionNotification)
266 mScrollPositionObject.RemovePropertyNotification(mPositionNotification);
268 mPositionNotification = mScrollPositionObject.AddPropertyNotification( Toolkit::ScrollConnector::SCROLL_POSITION, VariableStepCondition(positions) );
269 mPositionNotification.NotifySignal().Connect( this, &ScrollBar::OnScrollPositionNotified );
273 void ScrollBar::OnScrollPositionNotified(PropertyNotification& source)
275 // Emit the signal to notify the scroll position crossing
276 mScrollPositionNotifiedSignal.Emit(mScrollConnector.GetScrollPosition());
279 void ScrollBar::Show()
281 // Cancel any animation
288 if(mIndicatorShowDuration > 0.0f)
290 mAnimation = Animation::New( mIndicatorShowDuration );
291 mAnimation.OpacityTo( Self(), 1.0f, AlphaFunctions::EaseIn );
296 Self().SetOpacity(1.0f);
300 void ScrollBar::Hide()
302 // Cancel any animation
309 if(mIndicatorHideDuration > 0.0f)
311 mAnimation = Animation::New( mIndicatorHideDuration );
312 mAnimation.OpacityTo( Self(), 0.0f, AlphaFunctions::EaseIn );
317 Self().SetOpacity(0.0f);
321 bool ScrollBar::OnPanGestureProcessTick()
323 // Update the scroll position property.
324 if( mScrollConnector )
326 mScrollConnector.SetScrollPosition(mCurrentScrollPosition);
332 void ScrollBar::OnPan( PanGesture gesture )
336 Dali::Toolkit::ItemView itemView = Dali::Toolkit::ItemView::DownCast(Self().GetParent());
338 switch(gesture.state)
340 case Gesture::Started:
344 // Make sure the pan gesture is only being processed once per frame.
345 mTimer = Timer::New( DEFAULT_PAN_GESTURE_PROCESS_TIME );
346 mTimer.TickSignal().Connect( this, &ScrollBar::OnPanGestureProcessTick );
351 mScrollStart = mScrollConnector.GetScrollPosition();
352 mGestureDisplacement = Vector3::ZERO;
357 case Gesture::Continuing:
359 Vector3 delta(gesture.displacement.x, gesture.displacement.y, 0.0f);
360 mGestureDisplacement+=delta;
362 Vector3 span = Self().GetCurrentSize() - mIndicator.GetCurrentSize();
363 const float domainSize = fabs(mScrollConnector.GetMaxLimit() - mScrollConnector.GetMinLimit());
364 mCurrentScrollPosition = mScrollStart - mGestureDisplacement.y * domainSize / span.y;
365 mCurrentScrollPosition = std::min(mScrollConnector.GetMaxLimit(), std::max(mCurrentScrollPosition, mScrollConnector.GetMinLimit()));
375 // Destroy the timer when pan gesture is finished.
377 mTimer.TickSignal().Disconnect( this, &ScrollBar::OnPanGestureProcessTick );
383 // Refresh the ItemView cache with extra items
384 GetImpl(itemView).DoRefresh(mCurrentScrollPosition, true);
393 // Disable automatic refresh in ItemView during fast scrolling
394 GetImpl(itemView).SetRefreshEnabled(!mIsPanning);
399 void ScrollBar::OnScrollDomainChanged(float minPosition, float maxPosition, float contentSize)
401 // Reapply constraints when the scroll domain is changed
405 void ScrollBar::SetIndicatorHeightPolicy( Toolkit::ScrollBar::IndicatorHeightPolicy policy )
407 mIndicatorHeightPolicy = policy;
411 Toolkit::ScrollBar::IndicatorHeightPolicy ScrollBar::GetIndicatorHeightPolicy()
413 return mIndicatorHeightPolicy;
416 void ScrollBar::SetIndicatorFixedHeight( float height )
418 mIndicatorFixedHeight = height;
422 float ScrollBar::GetIndicatorFixedHeight()
424 return mIndicatorFixedHeight;
427 void ScrollBar::SetIndicatorShowDuration( float durationSeconds )
429 mIndicatorShowDuration = durationSeconds;
432 float ScrollBar::GetIndicatorShowDuration()
434 return mIndicatorShowDuration;
437 void ScrollBar::SetIndicatorHideDuration( float durationSeconds )
439 mIndicatorHideDuration = durationSeconds;
442 float ScrollBar::GetIndicatorHideDuration()
444 return mIndicatorHideDuration;
447 void ScrollBar::OnIndicatorHeightPolicyPropertySet( Property::Value propertyValue )
449 std::string policyName( propertyValue.Get<std::string>() );
450 if(policyName == "Variable")
452 SetIndicatorHeightPolicy(Toolkit::ScrollBar::Variable);
454 else if(policyName == "Fixed")
456 SetIndicatorHeightPolicy(Toolkit::ScrollBar::Fixed);
460 DALI_ASSERT_ALWAYS( !"ScrollBar::OnIndicatorHeightPolicyPropertySet(). Invalid Property value." );
464 void ScrollBar::SetProperty( BaseObject* object, Property::Index index, const Property::Value& value )
466 Toolkit::ScrollBar scrollBar = Toolkit::ScrollBar::DownCast( Dali::BaseHandle( object ) );
470 ScrollBar& scrollBarImpl( GetImpl( scrollBar ) );
473 case Toolkit::ScrollBar::PROPERTY_INDICATOR_HEIGHT_POLICY:
475 scrollBarImpl.OnIndicatorHeightPolicyPropertySet( value );
478 case Toolkit::ScrollBar::PROPERTY_INDICATOR_FIXED_HEIGHT:
480 scrollBarImpl.SetIndicatorFixedHeight(value.Get<float>());
483 case Toolkit::ScrollBar::PROPERTY_INDICATOR_SHOW_DURATION:
485 scrollBarImpl.SetIndicatorShowDuration(value.Get<float>());
488 case Toolkit::ScrollBar::PROPERTY_INDICATOR_HIDE_DURATION:
490 scrollBarImpl.SetIndicatorHideDuration(value.Get<float>());
497 Property::Value ScrollBar::GetProperty( BaseObject* object, Property::Index index )
499 Property::Value value;
501 Toolkit::ScrollBar scrollBar = Toolkit::ScrollBar::DownCast( Dali::BaseHandle( object ) );
505 ScrollBar& scrollBarImpl( GetImpl( scrollBar ) );
508 case Toolkit::ScrollBar::PROPERTY_INDICATOR_HEIGHT_POLICY:
510 value = INDICATOR_HEIGHT_POLICY_NAME[ scrollBarImpl.GetIndicatorHeightPolicy() ];
513 case Toolkit::ScrollBar::PROPERTY_INDICATOR_FIXED_HEIGHT:
515 value = scrollBarImpl.GetIndicatorFixedHeight();
518 case Toolkit::ScrollBar::PROPERTY_INDICATOR_SHOW_DURATION:
520 value = scrollBarImpl.GetIndicatorShowDuration();
523 case Toolkit::ScrollBar::PROPERTY_INDICATOR_HIDE_DURATION:
525 value = scrollBarImpl.GetIndicatorHideDuration();
533 Toolkit::ScrollBar ScrollBar::New()
535 // Create the implementation, temporarily owned by this handle on stack
536 IntrusivePtr< ScrollBar > impl = new ScrollBar();
538 // Pass ownership to CustomActor handle
539 Toolkit::ScrollBar handle( *impl );
541 // Second-phase init of the implementation
542 // This can only be done after the CustomActor connection has been made...
548 } // namespace Internal
550 } // namespace Toolkit