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