Merge "[AT-SPI] Use Accessible::IsHighlighted()" into devel/master
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / internal / controls / scroll-bar / scroll-bar-impl.cpp
1 /*
2  * Copyright (c) 2024 Samsung Electronics Co., Ltd.
3  *
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
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  *
16  */
17
18 // CLASS HEADER
19 #include <dali-toolkit/internal/controls/scroll-bar/scroll-bar-impl.h>
20
21 // EXTERNAL INCLUDES
22 #include <dali/devel-api/actors/actor-devel.h>
23 #include <dali/devel-api/object/property-helper-devel.h>
24 #include <dali/integration-api/debug.h>
25 #include <dali/public-api/animation/constraint.h>
26 #include <dali/public-api/animation/constraints.h>
27 #include <dali/public-api/math/math-utils.h>
28 #include <dali/public-api/object/property-array.h>
29 #include <dali/public-api/object/type-registry-helper.h>
30 #include <dali/public-api/object/type-registry.h>
31 #include <cstring> // for strcmp
32
33 // INTERNAL INCLUDES
34 #include <dali-toolkit/devel-api/asset-manager/asset-manager.h>
35 #include <dali-toolkit/internal/controls/control/control-data-impl.h>
36 #include <dali-toolkit/internal/controls/scrollable/item-view/item-view-impl.h>
37 #include <dali-toolkit/public-api/controls/image-view/image-view.h>
38
39 using namespace Dali;
40
41 namespace
42 {
43 const char* DEFAULT_INDICATOR_IMAGE_FILE_NAME = "popup_scroll.9.png";
44 const float DEFAULT_SLIDER_DEPTH(1.0f);
45 const float DEFAULT_INDICATOR_SHOW_DURATION(0.5f);
46 const float DEFAULT_INDICATOR_HIDE_DURATION(0.5f);
47 const float DEFAULT_PAN_GESTURE_PROCESS_TIME(16.7f); // 16.7 milliseconds, i.e. one frame
48 const float DEFAULT_INDICATOR_FIXED_HEIGHT(80.0f);
49 const float DEFAULT_INDICATOR_MINIMUM_HEIGHT(0.0f);
50 const float DEFAULT_INDICATOR_START_PADDING(0.0f);
51 const float DEFAULT_INDICATOR_END_PADDING(0.0f);
52 const float DEFAULT_INDICATOR_TRANSIENT_DURATION(1.0f);
53
54 /**
55  * Indicator size constraint
56  * Indicator size depends on both indicator's parent size and the scroll content size
57  */
58 struct IndicatorSizeConstraint
59 {
60   /**
61    * @param[in] minimumHeight The minimum height for the indicator
62    * @param[in] padding The sum of the padding at the start & end of the indicator
63    */
64   IndicatorSizeConstraint(float minimumHeight, float padding)
65   : mMinimumHeight(minimumHeight),
66     mPadding(padding)
67   {
68   }
69
70   /**
71    * Constraint operator
72    * @param[in] current The current indicator size
73    * @param[in] parentSizeProperty The parent size of scroll indicator.
74    * @return The new scroll indicator size.
75    */
76   void operator()(Vector3& current, const PropertyInputContainer& inputs)
77   {
78     const Vector3& parentSize  = inputs[0]->GetVector3();
79     const float    contentSize = inputs[1]->GetFloat();
80
81     // Take into account padding that may exist at the beginning and end of the indicator.
82     const float parentHeightMinusPadding = parentSize.height - mPadding;
83
84     float height = contentSize > parentHeightMinusPadding ? parentHeightMinusPadding * (parentHeightMinusPadding / contentSize) : parentHeightMinusPadding * ((parentHeightMinusPadding - contentSize * 0.5f) / parentHeightMinusPadding);
85
86     current.y = std::max(mMinimumHeight, height);
87   }
88
89   float mMinimumHeight;
90   float mPadding;
91 };
92
93 /**
94  * Indicator position constraint
95  * Positions the indicator to reflect the current scroll position within the scroll domain.
96  */
97 struct IndicatorPositionConstraint
98 {
99   /**
100    * @param[in] startPadding The padding at the start of the indicator
101    * @param[in] endPadding The padding at the end of the indicator
102    */
103   IndicatorPositionConstraint(float startPadding, float endPadding)
104   : mStartPadding(startPadding),
105     mEndPadding(endPadding)
106   {
107   }
108
109   /**
110    * Constraint operator
111    * @param[in,out] current The current indicator position
112    * @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)
113    * @return The new indicator position is returned.
114    */
115   void operator()(Vector3& current, const PropertyInputContainer& inputs)
116   {
117     const Vector3& indicatorSize         = inputs[0]->GetVector3();
118     const Vector3& parentSize            = inputs[1]->GetVector3();
119     const float    scrollPosition        = -inputs[2]->GetFloat();
120     const float    minimumScrollPosition = inputs[3]->GetFloat();
121     const float    maximumScrollPosition = inputs[4]->GetFloat();
122
123     // Take into account padding that may exist at the beginning and end of the indicator.
124     const float parentHeightMinusPadding = parentSize.height - (mStartPadding + mEndPadding);
125
126     float relativePosition = std::max(0.0f, std::min(1.0f, (scrollPosition - minimumScrollPosition) / (maximumScrollPosition - minimumScrollPosition)));
127     current.y              = mStartPadding + (parentHeightMinusPadding - indicatorSize.height) * relativePosition;
128     current.z              = DEFAULT_SLIDER_DEPTH;
129   }
130
131   float mStartPadding;
132   float mEndPadding;
133 };
134
135 } // unnamed namespace
136
137 namespace Dali
138 {
139 namespace Toolkit
140 {
141 namespace Internal
142 {
143 namespace
144 {
145 using namespace Dali;
146
147 BaseHandle Create()
148 {
149   return Toolkit::ScrollBar::New();
150 }
151
152 // clang-format off
153 // Setup properties, signals and actions using the type-registry.
154 DALI_TYPE_REGISTRATION_BEGIN( Toolkit::ScrollBar, Toolkit::Control, Create );
155
156 DALI_PROPERTY_REGISTRATION(Toolkit, ScrollBar, "scrollDirection",            STRING, SCROLL_DIRECTION            )
157 DALI_PROPERTY_REGISTRATION(Toolkit, ScrollBar, "indicatorHeightPolicy",      STRING, INDICATOR_HEIGHT_POLICY     )
158 DALI_PROPERTY_REGISTRATION(Toolkit, ScrollBar, "indicatorFixedHeight",       FLOAT,  INDICATOR_FIXED_HEIGHT      )
159 DALI_PROPERTY_REGISTRATION(Toolkit, ScrollBar, "indicatorShowDuration",      FLOAT,  INDICATOR_SHOW_DURATION     )
160 DALI_PROPERTY_REGISTRATION(Toolkit, ScrollBar, "indicatorHideDuration",      FLOAT,  INDICATOR_HIDE_DURATION     )
161 DALI_PROPERTY_REGISTRATION(Toolkit, ScrollBar, "scrollPositionIntervals",    ARRAY,  SCROLL_POSITION_INTERVALS   )
162 DALI_PROPERTY_REGISTRATION(Toolkit, ScrollBar, "indicatorMinimumHeight",     FLOAT,  INDICATOR_MINIMUM_HEIGHT    )
163 DALI_PROPERTY_REGISTRATION(Toolkit, ScrollBar, "indicatorStartPadding",      FLOAT,  INDICATOR_START_PADDING     )
164 DALI_PROPERTY_REGISTRATION(Toolkit, ScrollBar, "indicatorEndPadding",        FLOAT,  INDICATOR_END_PADDING       )
165 DALI_PROPERTY_REGISTRATION(Toolkit, ScrollBar, "indicatorTransientDuration", FLOAT,  INDICATOR_TRANSIENT_DURATION)
166
167 DALI_SIGNAL_REGISTRATION(Toolkit, ScrollBar, "panFinished",                   PAN_FINISHED_SIGNAL                    )
168 DALI_SIGNAL_REGISTRATION(Toolkit, ScrollBar, "scrollPositionIntervalReached", SCROLL_POSITION_INTERVAL_REACHED_SIGNAL)
169
170 DALI_ACTION_REGISTRATION(Toolkit, ScrollBar, "ShowIndicator",          ACTION_SHOW_INDICATOR          )
171 DALI_ACTION_REGISTRATION(Toolkit, ScrollBar, "HideIndicator",          ACTION_HIDE_INDICATOR          )
172 DALI_ACTION_REGISTRATION(Toolkit, ScrollBar, "ShowTransientIndicator", ACTION_SHOW_TRANSIENT_INDICATOR)
173
174 DALI_TYPE_REGISTRATION_END()
175 // clang-format on
176
177 const char* SCROLL_DIRECTION_NAME[]        = {"VERTICAL", "HORIZONTAL"};
178 const char* INDICATOR_HEIGHT_POLICY_NAME[] = {"VARIABLE", "FIXED"};
179
180 } // namespace
181
182 ScrollBar::ScrollBar(Toolkit::ScrollBar::Direction direction)
183 : Control(ControlBehaviour(CONTROL_BEHAVIOUR_DEFAULT)),
184   mIndicatorShowAlpha(1.0f),
185   mDirection(direction),
186   mScrollableObject(WeakHandle<Handle>()),
187   mPropertyScrollPosition(Property::INVALID_INDEX),
188   mPropertyMinScrollPosition(Property::INVALID_INDEX),
189   mPropertyMaxScrollPosition(Property::INVALID_INDEX),
190   mPropertyScrollContentSize(Property::INVALID_INDEX),
191   mIndicatorShowDuration(DEFAULT_INDICATOR_SHOW_DURATION),
192   mIndicatorHideDuration(DEFAULT_INDICATOR_HIDE_DURATION),
193   mTransientIndicatorDuration(DEFAULT_INDICATOR_TRANSIENT_DURATION),
194   mScrollStart(0.0f),
195   mGestureDisplacement(Vector2::ZERO),
196   mCurrentScrollPosition(0.0f),
197   mIndicatorHeightPolicy(Toolkit::ScrollBar::VARIABLE),
198   mIndicatorFixedHeight(DEFAULT_INDICATOR_FIXED_HEIGHT),
199   mIndicatorMinimumHeight(DEFAULT_INDICATOR_MINIMUM_HEIGHT),
200   mIndicatorStartPadding(DEFAULT_INDICATOR_START_PADDING),
201   mIndicatorEndPadding(DEFAULT_INDICATOR_END_PADDING),
202   mIsPanning(false),
203   mIndicatorFirstShow(true)
204 {
205 }
206
207 ScrollBar::~ScrollBar()
208 {
209 }
210
211 void ScrollBar::OnInitialize()
212 {
213   auto self = Self();
214
215   CreateDefaultIndicatorActor();
216   self.SetProperty(Actor::Property::DRAW_MODE, DrawMode::OVERLAY_2D);
217
218   self.SetProperty(DevelControl::Property::ACCESSIBILITY_ROLE, Dali::Accessibility::Role::SCROLL_BAR);
219 }
220
221 DevelControl::ControlAccessible* ScrollBar::CreateAccessibleObject()
222 {
223   return new ScrollBarAccessible(Self());
224 }
225
226 void ScrollBar::SetScrollPropertySource(Handle handle, Property::Index propertyScrollPosition, Property::Index propertyMinScrollPosition, Property::Index propertyMaxScrollPosition, Property::Index propertyScrollContentSize)
227 {
228   if(handle && propertyScrollPosition != Property::INVALID_INDEX && propertyMinScrollPosition != Property::INVALID_INDEX && propertyMaxScrollPosition != Property::INVALID_INDEX && propertyScrollContentSize != Property::INVALID_INDEX)
229   {
230     mScrollableObject          = WeakHandle<Handle>(handle);
231     mPropertyScrollPosition    = propertyScrollPosition;
232     mPropertyMinScrollPosition = propertyMinScrollPosition;
233     mPropertyMaxScrollPosition = propertyMaxScrollPosition;
234     mPropertyScrollContentSize = propertyScrollContentSize;
235
236     ApplyConstraints();
237   }
238   else
239   {
240     DALI_LOG_ERROR("Can not set empty handle of source object or invalid source property index\n");
241   }
242 }
243
244 void ScrollBar::CreateDefaultIndicatorActor()
245 {
246   const std::string  imageDirPath = AssetManager::GetDaliImagePath();
247   Toolkit::ImageView indicator    = Toolkit::ImageView::New(imageDirPath + DEFAULT_INDICATOR_IMAGE_FILE_NAME);
248   indicator.SetProperty(Actor::Property::PARENT_ORIGIN, ParentOrigin::TOP_LEFT);
249   indicator.SetProperty(Actor::Property::ANCHOR_POINT, AnchorPoint::TOP_LEFT);
250   indicator.SetStyleName("ScrollBarIndicator");
251   indicator.SetProperty(Actor::Property::COLOR_MODE, USE_OWN_MULTIPLY_PARENT_COLOR);
252   SetScrollIndicator(indicator);
253 }
254
255 void ScrollBar::SetScrollIndicator(Actor indicator)
256 {
257   // Don't allow empty handle
258   if(indicator)
259   {
260     // Remove current Indicator
261     if(mIndicator)
262     {
263       Self().Remove(mIndicator);
264     }
265     mIndicator = indicator;
266
267     mIndicatorFirstShow = true;
268     Self().Add(mIndicator);
269
270     EnableGestureDetection(GestureType::Value(GestureType::PAN));
271
272     PanGestureDetector detector(GetPanGestureDetector());
273     detector.DetachAll();
274     detector.Attach(mIndicator);
275
276     unsigned int childCount = mIndicator.GetChildCount();
277     for(unsigned int index = 0; index < childCount; index++)
278     {
279       Actor child = mIndicator.GetChildAt(index);
280       if(child)
281       {
282         detector.Attach(child);
283       }
284     }
285   }
286   else
287   {
288     DALI_LOG_ERROR("Empty handle of scroll indicator\n");
289   }
290 }
291
292 Actor ScrollBar::GetScrollIndicator()
293 {
294   return mIndicator;
295 }
296
297 void ScrollBar::ApplyConstraints()
298 {
299   Handle scrollableHandle = mScrollableObject.GetHandle();
300
301   if(scrollableHandle)
302   {
303     if(mIndicatorSizeConstraint)
304     {
305       mIndicatorSizeConstraint.Remove();
306     }
307
308     // Set indicator height according to the indicator's height policy
309     if(mIndicatorHeightPolicy == Toolkit::ScrollBar::FIXED)
310     {
311       mIndicator.SetProperty(Actor::Property::SIZE, Vector2(Self().GetCurrentProperty<Vector3>(Actor::Property::SIZE).width, mIndicatorFixedHeight));
312     }
313     else
314     {
315       mIndicatorSizeConstraint = Constraint::New<Vector3>(mIndicator, Actor::Property::SIZE, IndicatorSizeConstraint(mIndicatorMinimumHeight, mIndicatorStartPadding + mIndicatorEndPadding));
316       mIndicatorSizeConstraint.AddSource(ParentSource(Actor::Property::SIZE));
317       mIndicatorSizeConstraint.AddSource(Source(scrollableHandle, mPropertyScrollContentSize));
318       mIndicatorSizeConstraint.Apply();
319     }
320
321     if(mIndicatorPositionConstraint)
322     {
323       mIndicatorPositionConstraint.Remove();
324     }
325
326     mIndicatorPositionConstraint = Constraint::New<Vector3>(mIndicator, Actor::Property::POSITION, IndicatorPositionConstraint(mIndicatorStartPadding, mIndicatorEndPadding));
327     mIndicatorPositionConstraint.AddSource(LocalSource(Actor::Property::SIZE));
328     mIndicatorPositionConstraint.AddSource(ParentSource(Actor::Property::SIZE));
329     mIndicatorPositionConstraint.AddSource(Source(scrollableHandle, mPropertyScrollPosition));
330     mIndicatorPositionConstraint.AddSource(Source(scrollableHandle, mPropertyMinScrollPosition));
331     mIndicatorPositionConstraint.AddSource(Source(scrollableHandle, mPropertyMaxScrollPosition));
332     mIndicatorPositionConstraint.Apply();
333   }
334 }
335
336 void ScrollBar::SetScrollPositionIntervals(const Dali::Vector<float>& positions)
337 {
338   mScrollPositionIntervals = positions;
339
340   Handle scrollableHandle = mScrollableObject.GetHandle();
341
342   if(scrollableHandle)
343   {
344     if(mPositionNotification)
345     {
346       scrollableHandle.RemovePropertyNotification(mPositionNotification);
347     }
348
349     mPositionNotification = scrollableHandle.AddPropertyNotification(mPropertyScrollPosition, VariableStepCondition(mScrollPositionIntervals));
350     mPositionNotification.NotifySignal().Connect(this, &ScrollBar::OnScrollPositionIntervalReached);
351   }
352 }
353
354 Dali::Vector<float> ScrollBar::GetScrollPositionIntervals() const
355 {
356   return mScrollPositionIntervals;
357 }
358
359 void ScrollBar::OnScrollPositionIntervalReached(PropertyNotification& source)
360 {
361   // Emit the signal to notify the scroll position crossing
362   Handle scrollableHandle = mScrollableObject.GetHandle();
363   if(scrollableHandle)
364   {
365     mScrollPositionIntervalReachedSignal.Emit(scrollableHandle.GetCurrentProperty<float>(mPropertyScrollPosition));
366
367     auto accessible = GetAccessibleObject();
368     if(DALI_LIKELY(accessible) && accessible->IsHighlighted())
369     {
370       accessible->Emit(Dali::Accessibility::ObjectPropertyChangeEvent::VALUE);
371     }
372   }
373 }
374
375 void ScrollBar::ShowIndicator()
376 {
377   // Cancel any animation
378   if(mAnimation)
379   {
380     mAnimation.Clear();
381     mAnimation.Reset();
382   }
383
384   if(mIndicatorFirstShow)
385   {
386     // Preserve the alpha value from the stylesheet
387     mIndicatorShowAlpha = Self().GetCurrentProperty<Vector4>(Actor::Property::COLOR).a;
388     mIndicatorFirstShow = false;
389   }
390
391   if(mIndicatorShowDuration > 0.0f)
392   {
393     mAnimation = Animation::New(mIndicatorShowDuration);
394     mAnimation.AnimateTo(Property(mIndicator, Actor::Property::COLOR_ALPHA), mIndicatorShowAlpha, AlphaFunction::EASE_IN);
395     mAnimation.Play();
396   }
397   else
398   {
399     mIndicator.SetProperty(Actor::Property::OPACITY, mIndicatorShowAlpha);
400   }
401 }
402
403 void ScrollBar::HideIndicator()
404 {
405   // Cancel any animation
406   if(mAnimation)
407   {
408     mAnimation.Clear();
409     mAnimation.Reset();
410   }
411
412   if(mIndicatorHideDuration > 0.0f)
413   {
414     mAnimation = Animation::New(mIndicatorHideDuration);
415     mAnimation.AnimateTo(Property(mIndicator, Actor::Property::COLOR_ALPHA), 0.0f, AlphaFunction::EASE_IN);
416     mAnimation.Play();
417   }
418   else
419   {
420     mIndicator.SetProperty(Actor::Property::OPACITY, 0.0f);
421   }
422 }
423
424 void ScrollBar::ShowTransientIndicator()
425 {
426   // Cancel any animation
427   if(mAnimation)
428   {
429     mAnimation.Clear();
430     mAnimation.Reset();
431   }
432
433   mAnimation = Animation::New(mIndicatorShowDuration + mTransientIndicatorDuration + mIndicatorHideDuration);
434   if(mIndicatorShowDuration > 0.0f)
435   {
436     mAnimation.AnimateTo(Property(mIndicator, Actor::Property::COLOR_ALPHA),
437                          mIndicatorShowAlpha,
438                          AlphaFunction::EASE_IN,
439                          TimePeriod(0, mIndicatorShowDuration));
440   }
441   else
442   {
443     mIndicator.SetProperty(Actor::Property::OPACITY, mIndicatorShowAlpha);
444   }
445   mAnimation.AnimateTo(Property(mIndicator, Actor::Property::COLOR_ALPHA),
446                        0.0f,
447                        AlphaFunction::EASE_IN,
448                        TimePeriod((mIndicatorShowDuration + mTransientIndicatorDuration), mIndicatorHideDuration));
449   mAnimation.Play();
450 }
451
452 bool ScrollBar::OnPanGestureProcessTick()
453 {
454   // Update the scroll position property.
455   Handle scrollableHandle = mScrollableObject.GetHandle();
456   if(scrollableHandle)
457   {
458     scrollableHandle.SetProperty(mPropertyScrollPosition, mCurrentScrollPosition);
459   }
460
461   return true;
462 }
463
464 void ScrollBar::OnPan(const PanGesture& gesture)
465 {
466   Handle scrollableHandle = mScrollableObject.GetHandle();
467
468   if(scrollableHandle)
469   {
470     Dali::Toolkit::ItemView itemView = Dali::Toolkit::ItemView::DownCast(scrollableHandle);
471
472     switch(gesture.GetState())
473     {
474       case Dali::GestureState::STARTED:
475       {
476         if(!mPanProcessTimer)
477         {
478           // Make sure the pan gesture is only being processed once per frame.
479           mPanProcessTimer = Timer::New(DEFAULT_PAN_GESTURE_PROCESS_TIME);
480           mPanProcessTimer.TickSignal().Connect(this, &ScrollBar::OnPanGestureProcessTick);
481           mPanProcessTimer.Start();
482         }
483
484         ShowIndicator();
485         mScrollStart         = scrollableHandle.GetCurrentProperty<float>(mPropertyScrollPosition);
486         mGestureDisplacement = Vector2::ZERO;
487         mIsPanning           = true;
488
489         break;
490       }
491       case Dali::GestureState::CONTINUING:
492       {
493         mGestureDisplacement += gesture.GetDisplacement();
494
495         float minScrollPosition = scrollableHandle.GetCurrentProperty<float>(mPropertyMinScrollPosition);
496         float maxScrollPosition = scrollableHandle.GetCurrentProperty<float>(mPropertyMaxScrollPosition);
497
498         // The domain size is the internal range
499         float domainSize  = maxScrollPosition - minScrollPosition;
500         float logicalSize = Self().GetCurrentProperty<Vector3>(Actor::Property::SIZE).y - (mIndicator.GetCurrentProperty<Vector3>(Actor::Property::SIZE).y + mIndicatorStartPadding + mIndicatorEndPadding);
501
502         mCurrentScrollPosition = mScrollStart - ((mGestureDisplacement.y * domainSize) / logicalSize);
503         mCurrentScrollPosition = -std::min(maxScrollPosition, std::max(-mCurrentScrollPosition, minScrollPosition));
504
505         break;
506       }
507       default:
508       {
509         mIsPanning = false;
510
511         if(mPanProcessTimer)
512         {
513           // Destroy the timer when pan gesture is finished.
514           mPanProcessTimer.Stop();
515           mPanProcessTimer.TickSignal().Disconnect(this, &ScrollBar::OnPanGestureProcessTick);
516           mPanProcessTimer.Reset();
517         }
518
519         if(itemView)
520         {
521           // Refresh the ItemView cache with extra items
522           GetImpl(itemView).DoRefresh(mCurrentScrollPosition, true);
523         }
524
525         mPanFinishedSignal.Emit();
526
527         break;
528       }
529     }
530
531     if(itemView)
532     {
533       // Disable automatic refresh in ItemView during fast scrolling
534       GetImpl(itemView).SetRefreshEnabled(!mIsPanning);
535     }
536   }
537 }
538
539 void ScrollBar::OnSizeSet(const Vector3& size)
540 {
541   if(mIndicatorHeightPolicy == Toolkit::ScrollBar::FIXED)
542   {
543     mIndicator.SetProperty(Actor::Property::SIZE, Vector2(size.width, mIndicatorFixedHeight));
544   }
545
546   Control::OnSizeSet(size);
547 }
548
549 void ScrollBar::SetScrollDirection(Toolkit::ScrollBar::Direction direction)
550 {
551   mDirection = direction;
552 }
553
554 Toolkit::ScrollBar::Direction ScrollBar::GetScrollDirection() const
555 {
556   return mDirection;
557 }
558
559 void ScrollBar::SetIndicatorHeightPolicy(Toolkit::ScrollBar::IndicatorHeightPolicy policy)
560 {
561   if(policy != mIndicatorHeightPolicy)
562   {
563     mIndicatorHeightPolicy = policy;
564     ApplyConstraints();
565   }
566 }
567
568 Toolkit::ScrollBar::IndicatorHeightPolicy ScrollBar::GetIndicatorHeightPolicy() const
569 {
570   return mIndicatorHeightPolicy;
571 }
572
573 void ScrollBar::SetIndicatorFixedHeight(float height)
574 {
575   mIndicatorFixedHeight = height;
576
577   if(mIndicatorHeightPolicy == Toolkit::ScrollBar::FIXED)
578   {
579     mIndicator.SetProperty(Actor::Property::SIZE, Vector2(Self().GetCurrentProperty<Vector3>(Actor::Property::SIZE).width, mIndicatorFixedHeight));
580   }
581 }
582
583 float ScrollBar::GetIndicatorFixedHeight() const
584 {
585   return mIndicatorFixedHeight;
586 }
587
588 void ScrollBar::SetIndicatorShowDuration(float durationSeconds)
589 {
590   mIndicatorShowDuration = durationSeconds;
591 }
592
593 float ScrollBar::GetIndicatorShowDuration() const
594 {
595   return mIndicatorShowDuration;
596 }
597
598 void ScrollBar::SetIndicatorHideDuration(float durationSeconds)
599 {
600   mIndicatorHideDuration = durationSeconds;
601 }
602
603 float ScrollBar::GetIndicatorHideDuration() const
604 {
605   return mIndicatorHideDuration;
606 }
607
608 void ScrollBar::OnScrollDirectionPropertySet(Property::Value propertyValue)
609 {
610   std::string directionName(propertyValue.Get<std::string>());
611   if(directionName == "VERTICAL")
612   {
613     SetScrollDirection(Toolkit::ScrollBar::VERTICAL);
614   }
615   else if(directionName == "HORIZONTAL")
616   {
617     SetScrollDirection(Toolkit::ScrollBar::HORIZONTAL);
618   }
619   else
620   {
621     DALI_ASSERT_ALWAYS(!"ScrollBar::OnScrollDirectionPropertySet(). Invalid Property value.");
622   }
623 }
624
625 void ScrollBar::OnIndicatorHeightPolicyPropertySet(Property::Value propertyValue)
626 {
627   std::string policyName(propertyValue.Get<std::string>());
628   if(policyName == "VARIABLE")
629   {
630     SetIndicatorHeightPolicy(Toolkit::ScrollBar::VARIABLE);
631   }
632   else if(policyName == "FIXED")
633   {
634     SetIndicatorHeightPolicy(Toolkit::ScrollBar::FIXED);
635   }
636   else
637   {
638     DALI_ASSERT_ALWAYS(!"ScrollBar::OnIndicatorHeightPolicyPropertySet(). Invalid Property value.");
639   }
640 }
641
642 bool ScrollBar::DoConnectSignal(BaseObject* object, ConnectionTrackerInterface* tracker, const std::string& signalName, FunctorDelegate* functor)
643 {
644   Dali::BaseHandle handle(object);
645
646   bool               connected(true);
647   Toolkit::ScrollBar scrollBar = Toolkit::ScrollBar::DownCast(handle);
648
649   if(0 == strcmp(signalName.c_str(), PAN_FINISHED_SIGNAL))
650   {
651     scrollBar.PanFinishedSignal().Connect(tracker, functor);
652   }
653   else if(0 == strcmp(signalName.c_str(), SCROLL_POSITION_INTERVAL_REACHED_SIGNAL))
654   {
655     scrollBar.ScrollPositionIntervalReachedSignal().Connect(tracker, functor);
656   }
657   else
658   {
659     // signalName does not match any signal
660     connected = false;
661   }
662
663   return connected;
664 }
665
666 void ScrollBar::SetProperty(BaseObject* object, Property::Index index, const Property::Value& value)
667 {
668   Toolkit::ScrollBar scrollBar = Toolkit::ScrollBar::DownCast(Dali::BaseHandle(object));
669
670   if(scrollBar)
671   {
672     ScrollBar& scrollBarImpl(GetImpl(scrollBar));
673     switch(index)
674     {
675       case Toolkit::ScrollBar::Property::SCROLL_DIRECTION:
676       {
677         scrollBarImpl.OnScrollDirectionPropertySet(value);
678         break;
679       }
680       case Toolkit::ScrollBar::Property::INDICATOR_HEIGHT_POLICY:
681       {
682         scrollBarImpl.OnIndicatorHeightPolicyPropertySet(value);
683         break;
684       }
685       case Toolkit::ScrollBar::Property::INDICATOR_FIXED_HEIGHT:
686       {
687         scrollBarImpl.SetIndicatorFixedHeight(value.Get<float>());
688         break;
689       }
690       case Toolkit::ScrollBar::Property::INDICATOR_SHOW_DURATION:
691       {
692         scrollBarImpl.SetIndicatorShowDuration(value.Get<float>());
693         break;
694       }
695       case Toolkit::ScrollBar::Property::INDICATOR_HIDE_DURATION:
696       {
697         scrollBarImpl.SetIndicatorHideDuration(value.Get<float>());
698         break;
699       }
700       case Toolkit::ScrollBar::Property::SCROLL_POSITION_INTERVALS:
701       {
702         const Property::Array* array = value.GetArray();
703         if(array)
704         {
705           Dali::Vector<float> positions;
706           size_t              positionCount = array->Count();
707           positions.Resize(positionCount);
708
709           bool valid = true;
710           for(size_t i = 0; i != positionCount; ++i)
711           {
712             if(DALI_UNLIKELY(!array->GetElementAt(i).Get(positions[i])))
713             {
714               // Given array is invalid. Fast out.
715               valid = false;
716               break;
717             }
718           }
719
720           if(DALI_LIKELY(valid))
721           {
722             scrollBarImpl.SetScrollPositionIntervals(positions);
723           }
724         }
725         break;
726       }
727       case Toolkit::ScrollBar::Property::INDICATOR_MINIMUM_HEIGHT:
728       {
729         scrollBarImpl.mIndicatorMinimumHeight = value.Get<float>();
730         scrollBarImpl.ApplyConstraints();
731         break;
732       }
733       case Toolkit::ScrollBar::Property::INDICATOR_START_PADDING:
734       {
735         scrollBarImpl.mIndicatorStartPadding = value.Get<float>();
736         scrollBarImpl.ApplyConstraints();
737         break;
738       }
739       case Toolkit::ScrollBar::Property::INDICATOR_END_PADDING:
740       {
741         scrollBarImpl.mIndicatorEndPadding = value.Get<float>();
742         scrollBarImpl.ApplyConstraints();
743         break;
744       }
745       case Toolkit::ScrollBar::Property::INDICATOR_TRANSIENT_DURATION:
746       {
747         scrollBarImpl.mTransientIndicatorDuration = value.Get<float>();
748         break;
749       }
750     }
751   }
752 }
753
754 Property::Value ScrollBar::GetProperty(BaseObject* object, Property::Index index)
755 {
756   Property::Value value;
757
758   Toolkit::ScrollBar scrollBar = Toolkit::ScrollBar::DownCast(Dali::BaseHandle(object));
759
760   if(scrollBar)
761   {
762     ScrollBar& scrollBarImpl(GetImpl(scrollBar));
763     switch(index)
764     {
765       case Toolkit::ScrollBar::Property::SCROLL_DIRECTION:
766       {
767         value = SCROLL_DIRECTION_NAME[scrollBarImpl.GetScrollDirection()];
768         break;
769       }
770       case Toolkit::ScrollBar::Property::INDICATOR_HEIGHT_POLICY:
771       {
772         value = INDICATOR_HEIGHT_POLICY_NAME[scrollBarImpl.GetIndicatorHeightPolicy()];
773         break;
774       }
775       case Toolkit::ScrollBar::Property::INDICATOR_FIXED_HEIGHT:
776       {
777         value = scrollBarImpl.GetIndicatorFixedHeight();
778         break;
779       }
780       case Toolkit::ScrollBar::Property::INDICATOR_SHOW_DURATION:
781       {
782         value = scrollBarImpl.GetIndicatorShowDuration();
783         break;
784       }
785       case Toolkit::ScrollBar::Property::INDICATOR_HIDE_DURATION:
786       {
787         value = scrollBarImpl.GetIndicatorHideDuration();
788         break;
789       }
790       case Toolkit::ScrollBar::Property::SCROLL_POSITION_INTERVALS:
791       {
792         Property::Value  tempValue(Property::ARRAY);
793         Property::Array* array = tempValue.GetArray();
794
795         if(array)
796         {
797           Dali::Vector<float> positions = scrollBarImpl.GetScrollPositionIntervals();
798           size_t              positionCount(positions.Count());
799
800           for(size_t i(0); i != positionCount; ++i)
801           {
802             array->PushBack(positions[i]);
803           }
804
805           value = tempValue;
806         }
807         break;
808       }
809       case Toolkit::ScrollBar::Property::INDICATOR_MINIMUM_HEIGHT:
810       {
811         value = scrollBarImpl.mIndicatorMinimumHeight;
812         break;
813       }
814       case Toolkit::ScrollBar::Property::INDICATOR_START_PADDING:
815       {
816         value = scrollBarImpl.mIndicatorStartPadding;
817         break;
818       }
819       case Toolkit::ScrollBar::Property::INDICATOR_END_PADDING:
820       {
821         value = scrollBarImpl.mIndicatorEndPadding;
822         break;
823       }
824       case Toolkit::ScrollBar::Property::INDICATOR_TRANSIENT_DURATION:
825       {
826         value = scrollBarImpl.mTransientIndicatorDuration;
827         break;
828       }
829     }
830   }
831   return value;
832 }
833
834 bool ScrollBar::DoAction(BaseObject* object, const std::string& actionName, const Property::Map& attributes)
835 {
836   bool ret = false;
837
838   Dali::BaseHandle handle(object);
839
840   Toolkit::ScrollBar scrollBar = Toolkit::ScrollBar::DownCast(handle);
841
842   DALI_ASSERT_DEBUG(scrollBar);
843
844   if(scrollBar)
845   {
846     if(0 == strcmp(actionName.c_str(), ACTION_SHOW_INDICATOR))
847     {
848       GetImpl(scrollBar).ShowIndicator();
849       ret = true;
850     }
851     else if(0 == strcmp(actionName.c_str(), ACTION_HIDE_INDICATOR))
852     {
853       GetImpl(scrollBar).HideIndicator();
854       ret = true;
855     }
856     else if(0 == strcmp(actionName.c_str(), ACTION_SHOW_TRANSIENT_INDICATOR))
857     {
858       GetImpl(scrollBar).ShowTransientIndicator();
859       ret = true;
860     }
861   }
862
863   return ret;
864 }
865
866 Toolkit::ScrollBar ScrollBar::New(Toolkit::ScrollBar::Direction direction)
867 {
868   // Create the implementation, temporarily owned by this handle on stack
869   IntrusivePtr<ScrollBar> impl = new ScrollBar(direction);
870
871   // Pass ownership to CustomActor handle
872   Toolkit::ScrollBar handle(*impl);
873
874   // Second-phase init of the implementation
875   // This can only be done after the CustomActor connection has been made...
876   impl->Initialize();
877
878   return handle;
879 }
880
881 double ScrollBar::ScrollBarAccessible::GetMinimum() const
882 {
883   auto   self             = Toolkit::ScrollBar::DownCast(Self());
884   Handle scrollableHandle = GetImpl(self).mScrollableObject.GetHandle();
885   return scrollableHandle ? scrollableHandle.GetCurrentProperty<float>(GetImpl(self).mPropertyMinScrollPosition) : 0.0f;
886 }
887
888 double ScrollBar::ScrollBarAccessible::GetCurrent() const
889 {
890   auto   self             = Toolkit::ScrollBar::DownCast(Self());
891   Handle scrollableHandle = GetImpl(self).mScrollableObject.GetHandle();
892   return scrollableHandle ? scrollableHandle.GetCurrentProperty<float>(GetImpl(self).mPropertyScrollPosition) : 0.0f;
893 }
894
895 std::string ScrollBar::ScrollBarAccessible::GetValueText() const
896 {
897   return {}; // Text mode is not used at the moment
898 }
899
900 double ScrollBar::ScrollBarAccessible::GetMaximum() const
901 {
902   auto   self             = Toolkit::ScrollBar::DownCast(Self());
903   Handle scrollableHandle = GetImpl(self).mScrollableObject.GetHandle();
904   return scrollableHandle ? scrollableHandle.GetCurrentProperty<float>(GetImpl(self).mPropertyMaxScrollPosition) : 1.0f;
905 }
906
907 bool ScrollBar::ScrollBarAccessible::SetCurrent(double current)
908 {
909   if(current < GetMinimum() || current > GetMaximum())
910   {
911     return false;
912   }
913
914   auto valueBefore = GetCurrent();
915
916   auto   self             = Toolkit::ScrollBar::DownCast(Self());
917   Handle scrollableHandle = GetImpl(self).mScrollableObject.GetHandle();
918   if(!scrollableHandle)
919   {
920     return false;
921   }
922
923   scrollableHandle.SetProperty(GetImpl(self).mPropertyScrollPosition, static_cast<float>(current));
924
925   auto valueAfter = GetCurrent();
926
927   if(!Dali::Equals(current, valueBefore) && Dali::Equals(valueBefore, valueAfter))
928   {
929     return false;
930   }
931
932   return true;
933 }
934
935 double ScrollBar::ScrollBarAccessible::GetMinimumIncrement() const
936 {
937   return 1.0;
938 }
939
940 } // namespace Internal
941
942 } // namespace Toolkit
943
944 } // namespace Dali