abb520086438cb05cd9510664b6daf44e184de80
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / internal / controls / scroll-bar / scroll-bar-impl.h
1 #ifndef DALI_TOOLKIT_INTERNAL_SCROLL_BAR_H
2 #define DALI_TOOLKIT_INTERNAL_SCROLL_BAR_H
3
4 /*
5  * Copyright (c) 2021 Samsung Electronics Co., Ltd.
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  * http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  *
19  */
20
21 // EXTERNAL INCLUDES
22 #include <dali/public-api/adaptor-framework/timer.h>
23 #include <dali/public-api/animation/animation.h>
24 #include <dali/public-api/animation/constraint.h>
25 #include <dali/public-api/common/dali-vector.h>
26 #include <dali/public-api/object/property-notification.h>
27 #include <dali/public-api/object/weak-handle.h>
28
29 // INTERNAL INCLUDES
30 #include <dali-toolkit/devel-api/controls/control-devel.h>
31 #include <dali-toolkit/devel-api/controls/scroll-bar/scroll-bar.h>
32 #include <dali-toolkit/internal/controls/control/control-data-impl.h>
33 #include <dali-toolkit/public-api/controls/control-impl.h>
34
35 namespace Dali
36 {
37 namespace Toolkit
38 {
39 namespace Internal
40 {
41 class ScrollBar;
42
43 typedef IntrusivePtr<ScrollBar> ScrollBarPtr;
44
45 /**
46  * ScrollBar is a UI component that can be added to the scrollable controls
47  * indicating the current scroll position of the scrollable content.
48  */
49 class ScrollBar : public Control
50 {
51 public:
52   // Signals
53   typedef Toolkit::ScrollBar::PanFinishedSignalType                   PanFinishedSignalType;
54   typedef Toolkit::ScrollBar::ScrollPositionIntervalReachedSignalType ScrollPositionIntervalReachedSignalType;
55
56 public:
57   /**
58    * @copydoc Toolkit::ScrollBar::New()
59    */
60   static Toolkit::ScrollBar New(Toolkit::ScrollBar::Direction direction);
61
62   /**
63    * @copydoc Toolkit::ScrollBar::SetScrollPropertySource()
64    */
65   void SetScrollPropertySource(Handle handle, Property::Index propertyScrollPosition, Property::Index propertyMinScrollPosition, Property::Index propertyMaxScrollPosition, Property::Index propertyScrollContentSize);
66
67   /**
68    * @copydoc Toolkit::ScrollBar::SetScrollIndicator()
69    */
70   void SetScrollIndicator(Actor indicator);
71
72   /**
73    * @copydoc Toolkit::ScrollBar::GetScrollIndicator()
74    */
75   Actor GetScrollIndicator();
76
77   /**
78    * @copydoc Toolkit::ScrollBar::SetScrollPositionIntervals()
79    */
80   void SetScrollPositionIntervals(const Dali::Vector<float>& positions);
81
82   /**
83    * @copydoc Toolkit::ScrollBar::GetScrollPositionIntervals()
84    */
85   Dali::Vector<float> GetScrollPositionIntervals() const;
86
87   /**
88    * @copydoc Toolkit::ScrollBar::SetScrollDirection()
89    */
90   void SetScrollDirection(Toolkit::ScrollBar::Direction direction);
91
92   /**
93    * @copydoc Toolkit::ScrollBar::GetScrollDirection()
94    */
95   Toolkit::ScrollBar::Direction GetScrollDirection() const;
96
97   /**
98    * @copydoc Toolkit::ScrollBar::SetIndicatorHeightPolicy()
99    */
100   void SetIndicatorHeightPolicy(Toolkit::ScrollBar::IndicatorHeightPolicy policy);
101
102   /**
103    * @copydoc Toolkit::ScrollBar::GetIndicatorHeightPolicy()
104    */
105   Toolkit::ScrollBar::IndicatorHeightPolicy GetIndicatorHeightPolicy() const;
106
107   /**
108    * @copydoc Toolkit::ScrollBar::SetIndicatorFixedHeight()
109    */
110   void SetIndicatorFixedHeight(float height);
111
112   /**
113    * @copydoc Toolkit::ScrollBar::GetIndicatorFixedHeight()
114    */
115   float GetIndicatorFixedHeight() const;
116
117   /**
118    * @copydoc Toolkit::ScrollBar::SetIndicatorShowDuration()
119    */
120   void SetIndicatorShowDuration(float durationSeconds);
121
122   /**
123    * @copydoc Toolkit::ScrollBar::GetIndicatorShowDuration()
124    */
125   float GetIndicatorShowDuration() const;
126
127   /**
128    * @copydoc Toolkit::ScrollBar::SetIndicatorHideDuration()
129    */
130   void SetIndicatorHideDuration(float durationSeconds);
131
132   /**
133    * @copydoc Toolkit::ScrollBar::GetIndicatorHideDuration()
134    */
135   float GetIndicatorHideDuration() const;
136
137   /**
138    * @copydoc Toolkit::ScrollBar::ShowIndicator()
139    */
140   void ShowIndicator();
141
142   /**
143    * @copydoc Toolkit::ScrollBar::HideIndicator()
144    */
145   void HideIndicator();
146
147   /**
148    * @brief Shows indicator until the transient duration has expired
149    */
150   void ShowTransientIndicator();
151
152   /**
153   * @copydoc Toolkit::ScrollBar::PanFinishedSignal()
154   */
155   PanFinishedSignalType& PanFinishedSignal()
156   {
157     return mPanFinishedSignal;
158   }
159
160   /**
161   * @copydoc Toolkit::ScrollBar::ScrollPositionIntervalReachedSignal()
162   */
163   ScrollPositionIntervalReachedSignalType& ScrollPositionIntervalReachedSignal()
164   {
165     return mScrollPositionIntervalReachedSignal;
166   }
167
168   /**
169   * Connects a callback function with the object's signals.
170   * @param[in] object The object providing the signal.
171   * @param[in] tracker Used to disconnect the signal.
172   * @param[in] signalName The signal to connect to.
173   * @param[in] functor A newly allocated FunctorDelegate.
174   * @return True if the signal was connected.
175   * @post If a signal was connected, ownership of functor was passed to CallbackBase. Otherwise the caller is responsible for deleting the unused functor.
176   */
177   static bool DoConnectSignal(BaseObject* object, ConnectionTrackerInterface* tracker, const std::string& signalName, FunctorDelegate* functor);
178
179   // Properties
180
181   /**
182   * Called when a property of an object of this type is set.
183   * @param[in] object The object whose property is set.
184   * @param[in] index The property index.
185   * @param[in] value The new property value.
186   */
187   static void SetProperty(BaseObject* object, Property::Index index, const Property::Value& value);
188
189   /**
190   * Called to retrieve a property of an object of this type.
191   * @param[in] object The object whose property is to be retrieved.
192   * @param[in] index The property index.
193   * @return The current value of the property.
194   */
195   static Property::Value GetProperty(BaseObject* object, Property::Index index);
196
197   /**
198   * Performs actions as requested using the action name.
199   * @param[in] object The object on which to perform the action.
200   * @param[in] actionName The action to perform.
201   * @param[in] attributes The attributes with which to perfrom this action.
202   * @return true if action has been accepted by this control
203   */
204   static bool DoAction(BaseObject* object, const std::string& actionName, const Property::Map& attributes);
205
206 private: // from Control
207   /**
208    * @copydoc Toolkit::Control::OnInitialize
209    */
210   void OnInitialize() override;
211
212   /**
213    * @copydoc Toolkit::Control::OnPan
214    */
215   void OnPan(const PanGesture& gesture) override;
216
217   /**
218    * @copydoc CustomActorImpl::OnSizeSet( const Vector3& size )
219    */
220   void OnSizeSet(const Vector3& size) override;
221
222 private:
223   /**
224    * Create the default indicator actor.
225    */
226   void CreateDefaultIndicatorActor();
227
228   /**
229    * Apply constraints for background and indicator.
230    * These constraints are based on values from the scroll connector.
231    */
232   void ApplyConstraints();
233
234   /**
235    * Callback when the current scroll position of the scrollable content goes above or
236    * below the values specified by SetScrollPositionIntervals().
237    * @param[in] source the property notification that triggered this callback
238    */
239   void OnScrollPositionIntervalReached(PropertyNotification& source);
240
241   /**
242    * Process the pan gesture per predefined timeout until the gesture is finished.
243    * @return True if the timer should be kept running.
244    */
245   bool OnPanGestureProcessTick();
246
247   /**
248    * Handle SetProperty for scroll direction.
249    * @param[in] propertyValue The new property value.
250    */
251   void OnScrollDirectionPropertySet(Property::Value propertyValue);
252
253   /**
254    * Handle SetProperty for scroll indicator height policy.
255    * @param[in] propertyValue The new property value.
256    */
257   void OnIndicatorHeightPolicyPropertySet(Property::Value propertyValue);
258
259 private:
260   /**
261    * Constructor.
262    * It initializes ScrollBar members.
263    */
264   ScrollBar(Toolkit::ScrollBar::Direction direction);
265
266   /**
267    * A reference counted object may only be deleted by calling Unreference()
268    */
269   virtual ~ScrollBar();
270
271 private:
272   Actor     mIndicator;          ///< Image of scroll indicator.
273   float     mIndicatorShowAlpha; ///< The alpha value when the indicator is fully shown
274   Animation mAnimation;          ///< Scroll indicator Show/Hide Animation.
275
276   Toolkit::ScrollBar::Direction mDirection; ///< The direction of scroll bar (vertical or horizontal)
277
278   WeakHandle<Handle> mScrollableObject; ///< Object to be scrolled
279
280   Property::Index mPropertyScrollPosition    = 0; ///< Index of scroll position property owned by the object to be scrolled
281   Property::Index mPropertyMinScrollPosition = 0; ///< Index of minimum scroll position property owned by the object to be scrolled
282   Property::Index mPropertyMaxScrollPosition = 1; ///< Index of maximum scroll position property owned by the object to be scrolled
283   Property::Index mPropertyScrollContentSize;     ///< Index of scroll content size property owned by the object to be scrolled
284
285   float mIndicatorShowDuration;      ///< The duration of scroll indicator show animation
286   float mIndicatorHideDuration;      ///< The duration of scroll indicator hide animation
287   float mTransientIndicatorDuration; ///< The duration before hiding transient indicator
288
289   float   mScrollStart;         ///< Scroll Start position (start of drag)
290   Vector2 mGestureDisplacement; ///< Gesture Displacement.
291
292   float mCurrentScrollPosition; ///< The current scroll position updated by the pan gesture
293
294   Toolkit::ScrollBar::IndicatorHeightPolicy mIndicatorHeightPolicy;  ///< The height policy of scroll indicator (variable or fixed)
295   float                                     mIndicatorFixedHeight;   ///< The fixed height of scroll indicator
296   float                                     mIndicatorMinimumHeight; ///< The minimum height for a variable size indicator
297   float                                     mIndicatorStartPadding;  ///< The padding at the start of the indicator
298   float                                     mIndicatorEndPadding;    ///< The padding at the end of the indicator
299
300   Timer mContractDelayTimer; ///< Timer guarantee contract delay time.
301   Timer mPanProcessTimer;    ///< The timer to process the pan gesture after the gesture is started.
302
303   Dali::Vector<float>  mScrollPositionIntervals; ///< List of values to receive notification for when the current scroll position goes above or below them
304   PropertyNotification mPositionNotification;    ///< Stores the property notification used for scroll position changes
305
306   PanFinishedSignalType                   mPanFinishedSignal;
307   ScrollPositionIntervalReachedSignalType mScrollPositionIntervalReachedSignal;
308
309   Constraint mIndicatorPositionConstraint;
310   Constraint mIndicatorSizeConstraint;
311
312   bool mIsPanning : 1;          ///< Whether the scroll bar is being panned.
313   bool mIndicatorFirstShow : 1; ///< True if the indicator has never been shown
314
315 protected:
316   struct AccessibleImpl : public DevelControl::AccessibleImpl,
317                           public virtual Dali::Accessibility::Value
318   {
319     using DevelControl::AccessibleImpl::AccessibleImpl;
320     double GetMinimum() override;
321     double GetCurrent() override;
322     double GetMaximum() override;
323     bool   SetCurrent(double) override;
324     double GetMinimumIncrement() override;
325   };
326 };
327
328 } // namespace Internal
329
330 // Helpers for public-api forwarding methods
331
332 inline Toolkit::Internal::ScrollBar& GetImpl(Toolkit::ScrollBar& scrollBar)
333 {
334   DALI_ASSERT_ALWAYS(scrollBar);
335
336   Dali::RefObject& handle = scrollBar.GetImplementation();
337
338   return static_cast<Toolkit::Internal::ScrollBar&>(handle);
339 }
340
341 inline const Toolkit::Internal::ScrollBar& GetImpl(const Toolkit::ScrollBar& scrollBar)
342 {
343   DALI_ASSERT_ALWAYS(scrollBar);
344
345   const Dali::RefObject& handle = scrollBar.GetImplementation();
346
347   return static_cast<const Toolkit::Internal::ScrollBar&>(handle);
348 }
349
350 } // namespace Toolkit
351
352 } // namespace Dali
353
354 #endif // DALI_TOOLKIT_INTERNAL_SCROLL_BAR_H