Merge changes I2df640e0,Ia1188305,I7fae506e,I7967a7cc,Ib0fdcdf4, ... into devel/master
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / internal / controls / scrollable / item-view / item-view-impl.h
1 #ifndef DALI_TOOLKIT_INTERNAL_ITEM_VIEW_H
2 #define DALI_TOOLKIT_INTERNAL_ITEM_VIEW_H
3
4 /*
5  * Copyright (c) 2020 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/object/property-notification.h>
25 #include <dali/public-api/object/property-map.h>
26 #include <dali/public-api/object/property-array.h>
27
28 // INTERNAL INCLUDES
29 #include <dali-toolkit/public-api/controls/control-impl.h>
30 #include <dali-toolkit/public-api/controls/scrollable/item-view/item-view.h>
31 #include <dali-toolkit/public-api/controls/scrollable/item-view/item-layout.h>
32 #include <dali-toolkit/public-api/controls/image-view/image-view.h>
33 #include <dali-toolkit/internal/controls/scrollable/scrollable-impl.h>
34 #include <dali-toolkit/public-api/focus-manager/keyboard-focus-manager.h>
35
36 namespace Dali
37 {
38
39 namespace Toolkit
40 {
41
42 namespace Internal
43 {
44
45 class ItemView;
46
47 typedef IntrusivePtr<ItemView> ItemViewPtr;
48
49 /**
50  * ItemView is a scrollable layout container.
51  * Multiple ItemLayouts may be provided, to determine the logical position of each item a layout.
52  * Actor-ID pairs are provided from a shared ItemFactory, to display the currently visible items.
53  */
54 class ItemView : public Scrollable
55 {
56 public:
57
58   // Signals
59   typedef Toolkit::ItemView::LayoutActivatedSignalType LayoutActivatedSignalType;
60
61 public:
62
63   /**
64    * Create a new ItemView.
65    * @param[in] factory The factory which provides ItemView with items.
66    * @return A public handle to the newly allocated ItemView.
67    */
68   static Dali::Toolkit::ItemView New(ItemFactory& factory);
69
70   /**
71    * @copydoc Toolkit::ItemView::GetLayoutCount
72    */
73   unsigned int GetLayoutCount() const;
74
75   /**
76    * @copydoc Toolkit::ItemView::AddLayout
77    */
78   void AddLayout(ItemLayout& layout);
79
80   /**
81    * @copydoc Toolkit::ItemView::RemoveLayout
82    */
83   void RemoveLayout(unsigned int layoutIndex);
84
85   /**
86    * @copydoc Toolkit::ItemView::GetLayout
87    */
88   ItemLayoutPtr GetLayout(unsigned int layoutIndex) const;
89
90   /**
91    * @copydoc Toolkit::ItemView::GetActiveLayout
92    */
93   ItemLayoutPtr GetActiveLayout() const;
94
95   /**
96    * @copydoc Toolkit::ItemView::GetCurrentLayoutPosition
97    */
98   float GetCurrentLayoutPosition(unsigned int itemId) const;
99
100   /**
101    * @copydoc Toolkit::ItemView::ActivateLayout
102    */
103   void ActivateLayout(unsigned int layoutIndex, const Vector3& targetSize, float durationSeconds);
104
105   /**
106    * @copydoc Toolkit::ItemView::DeactivateCurrentLayout
107    */
108   void DeactivateCurrentLayout();
109
110   /**
111    * @copydoc Toolkit::ItemView::SetMinimumSwipeSpeed
112    */
113   void SetMinimumSwipeSpeed(float speed);
114
115   /**
116    * @copydoc Toolkit::ItemView::GetMinimumSwipeSpeed
117    */
118   float GetMinimumSwipeSpeed() const;
119
120   /**
121    * @copydoc Toolkit::ItemView::SetMinimumSwipeDistance
122    */
123   void SetMinimumSwipeDistance(float distance);
124
125   /**
126    * @copydoc Toolkit::ItemView::GetMinimumSwipeDistance
127    */
128   float GetMinimumSwipeDistance() const;
129
130   /**
131    * @copydoc Toolkit::ItemView::SetWheelScrollDistanceStep
132    */
133   void SetWheelScrollDistanceStep(float step);
134
135   /**
136    * @copydoc Toolkit::ItemView::GetWheelScrollDistanceStep
137    */
138   float GetWheelScrollDistanceStep() const;
139
140   /**
141    * @copydoc Toolkit::ItemView::SetAnchoring
142    */
143   void SetAnchoring(bool enabled);
144
145   /**
146    * @copydoc Toolkit::ItemView::GetAnchoring
147    */
148   bool GetAnchoring() const;
149
150   /**
151    * @copydoc Toolkit::ItemView::SetAnchoringDuration
152    */
153   void SetAnchoringDuration(float durationSeconds);
154
155   /**
156    * @copydoc Toolkit::ItemView::GetAnchoringDuration
157    */
158   float GetAnchoringDuration() const;
159
160   /**
161    * @copydoc Toolkit::ItemView::ScrollToItem
162    */
163   void ScrollToItem(unsigned int itemId, float durationSeconds);
164
165   /**
166    * @copydoc Toolkit::ItemView::SetRefreshInterval
167    */
168   void SetRefreshInterval(float intervalLayoutPositions);
169
170   /**
171    * @copydoc Toolkit::ItemView::GetRefreshInterval
172    */
173   float GetRefreshInterval() const;
174
175   /**
176    * @copydoc Toolkit::ItemView::Refresh
177    */
178   void Refresh();
179
180   /**
181    * @copydoc Toolkit::ItemView::GetItem
182    */
183   Actor GetItem(unsigned int itemId) const;
184
185   /**
186    * @copydoc Toolkit::ItemView::GetItemId
187    */
188   unsigned int GetItemId(Actor actor) const;
189
190   /**
191    * @copydoc Toolkit::ItemView::InsertItem
192    */
193   void InsertItem(Item newItem, float durationSeconds);
194
195   /**
196    * @copydoc Toolkit::ItemView::InsertItem
197    */
198   void InsertItems(const ItemContainer& newItems, float durationSeconds);
199
200   /**
201    * @copydoc Toolkit::ItemView::RemoveItem
202    */
203   void RemoveItem(ItemId itemId, float durationSeconds);
204
205   /**
206    * @copydoc Toolkit::ItemView::InsertItem
207    */
208   void RemoveItems(const ItemIdContainer& itemIds, float durationSeconds);
209
210   /**
211    * @copydoc Toolkit::ItemView::InsertItem
212    */
213   void ReplaceItem(Item replacementItem, float durationSeconds);
214
215   /**
216    * @copydoc Toolkit::ItemView::InsertItem
217    */
218   void ReplaceItems(const ItemContainer& replacementItems, float durationSeconds);
219
220   /**
221    * @copydoc Toolkit::Scrollable::GetCurrentScrollPosition
222    */
223   Vector2 GetCurrentScrollPosition() const;
224
225   /**
226    * @copydoc Toolkit::Scrollable::AddOverlay()
227    */
228   void AddOverlay(Actor actor);
229
230   /**
231    * @copydoc Toolkit::Scrollable::RemoveOverlay()
232    */
233   void RemoveOverlay(Actor actor);
234
235   /**
236    * @copydoc Toolkit::Scrollable::ScrollTo(const Vector2& position, float duration)
237    */
238   void ScrollTo(const Vector2& position, float duration);
239
240   /**
241    * @copydoc Toolkit::Internal::Scrollable::SetOvershootSize
242    */
243   void SetOvershootSize( const Vector2& size );
244
245   /**
246    * @copydoc Toolkit::Internal::Scrollable::SetOvershootEffectColor
247    */
248   void SetOvershootEffectColor( const Vector4& color );
249
250   /**
251    * @brief Set whether to enable automatic refresh or not. When refresh is disabled,
252    * ItemView will not automatically refresh the cache in the given interval when the
253    * layout position is changed. This is useful in some cases, for example, automatic
254    * refresh is not needed during fast scrolling, otherwise it will cache unneeded
255    * items when the layout position changes quickly.
256    *
257    * @param[in] enabled True to enable automatic refresh or false to disable it.
258    */
259   void SetRefreshEnabled(bool enabled);
260
261   /**
262    * @brief Helper to perform the refresh.
263    *
264    * @param[in] currentLayoutPosition The current layout position.
265    * @param[in] cacheExtra Whether to cache extra items during refresh.
266    */
267   void DoRefresh(float currentLayoutPosition, bool cacheExtra);
268
269   /**
270    * @copydoc Toolkit::ItemView::SetItemsParentOrigin
271    */
272   void SetItemsParentOrigin( const Vector3& parentOrigin );
273
274   /**
275    * @copydoc Toolkit::ItemView::GetItemsParentOrigin
276    */
277   Vector3 GetItemsParentOrigin() const;
278
279   /**
280    * @copydoc Toolkit::ItemView::SetItemsAnchorPoint
281    */
282   void SetItemsAnchorPoint( const Vector3& anchorPoint );
283
284   /**
285    * @copydoc Toolkit::ItemView::GetItemsAnchorPoint
286    */
287   Vector3 GetItemsAnchorPoint() const;
288
289   /**
290    * @copydoc Toolkit::ItemView::GetItemsRange
291    */
292   void GetItemsRange(ItemRange& range);
293
294   /**
295    * @copydoc Toolkit::ItemView::LayoutActivatedSignal()
296    */
297   LayoutActivatedSignalType& LayoutActivatedSignal()
298   {
299     return mLayoutActivatedSignal;
300   }
301
302   /**
303    * Connects a callback function with the object's signals.
304    * @param[in] object The object providing the signal.
305    * @param[in] tracker Used to disconnect the signal.
306    * @param[in] signalName The signal to connect to.
307    * @param[in] functor A newly allocated FunctorDelegate.
308    * @return True if the signal was connected.
309    * @post If a signal was connected, ownership of functor was passed to CallbackBase. Otherwise the caller is responsible for deleting the unused functor.
310    */
311   static bool DoConnectSignal( BaseObject* object, ConnectionTrackerInterface* tracker, const std::string& signalName, FunctorDelegate* functor );
312
313   //properties
314
315   /**
316    * Called when a property of an object of this type is set.
317    * @param[in] object The object whose property is set.
318    * @param[in] index The property index.
319    * @param[in] value The new property value.
320    */
321   static void SetProperty( BaseObject* object, Property::Index index, const Property::Value& value );
322
323   /**
324    * Called to retrieve a property of an object of this type.
325    * @param[in] object The object whose property is to be retrieved.
326    * @param[in] index The property index.
327    * @return The current value of the property.
328    */
329   static Property::Value GetProperty( BaseObject* object, Property::Index index );
330
331   /**
332    * Performs actions as requested using the action name.
333    * @param[in] object The object on which to perform the action.
334    * @param[in] actionName The action to perform.
335    * @param[in] attributes The attributes with which to perfrom this action.
336    * @return true if action has been accepted by this control
337    */
338   static bool DoAction( BaseObject* object, const std::string& actionName, const Property::Map& attributes );
339
340   /**
341    * Helper for DoAction( ACTION_STOP_SCROLLING ).
342    */
343   void DoStopScrolling();
344
345   /**
346    * Helper for DoAction( ACTION_ENABLE/DISABLE_REFRESH_NOTIFICATIONS ).
347    * @param[in] enabled Whether to disable refresh notifications or not.
348    */
349   void SetRefreshNotificationEnabled( bool enabled );
350
351 private:
352
353   /**
354    * Get all the layouts used in the ItemView.
355    * @return The layout array
356    */
357   Property::Array GetLayoutArray();
358
359   /**
360    * Set all the layouts. that will be used in the ItemView.
361    * @param[in] layouts The layouts used in the itemView.
362    */
363   void SetLayoutArray( const Property::Array& layouts );
364
365   /**
366    * Remove an Actor if found in the ItemPool.
367    * @param[in] itemId The item to remove.
368    * @return True if the remaining actors were reordered.
369    */
370   bool RemoveActor( unsigned int itemId );
371
372   /**
373    * Remove any Actors outside a given range.
374    * @param[in] @param[in] range The range of required items.
375    */
376   void RemoveActorsOutsideRange( ItemRange range );
377
378   /**
379    * Add a range of Actors, if they are not already in the ItemPool.
380    * @param[in] range The range of Item IDs to associate with the new actors.
381    * @param[in] layoutSize The layout-size.
382    */
383   void AddActorsWithinRange( ItemRange range, const Vector3& layoutSize );
384
385   /**
386    * Add a new Actor, if not already in the ItemPool.
387    * @param[in] item The ID for the new item.
388    * @param[in] layoutSize The layout-size.
389    */
390   void AddNewActor( ItemId item, const Vector3& layoutSize );
391
392   /**
393    * Apply the constraints etc. that are required for ItemView children.
394    * @param[in] item The item to setup.
395    * @param[in] layoutSize The layout-size.
396    */
397   void SetupActor( Item item, const Vector3& layoutSize );
398
399   /**
400    * Remove the Actor from the ItemPool and notify the ItemFactory the actor has been released by ItemView.
401    * @param[in] item The ID for the item to be released.
402    * @param[in] actor The actor to be removed from ItemView.
403    */
404   void ReleaseActor( ItemId item, Actor actor );
405
406 private: // From CustomActorImpl
407
408   /**
409    * From CustomActorImpl; called after a child has been added to the owning actor.
410    * @param[in] child The child which has been added.
411    */
412   void OnChildAdd(Actor& child) override;
413
414   /**
415    * Called after a wheel-event is received by the owning actor.
416    * @param[in] actor Actor associated with the wheel event.
417    * @param[in] event The wheel event.
418    * @return True if the event should be consumed.
419    */
420   bool OnWheelEvent(Actor actor, const WheelEvent& event);
421
422 private: // From Control
423
424   /**
425    * @copydoc Toolkit::Control::OnInitialize()
426    */
427   void OnInitialize() override;
428
429   /**
430    * @copydoc Toolkit::Control::OnAccessibilityPan()
431    */
432   bool OnAccessibilityPan(PanGesture gesture) override;
433
434   /**
435    * @copydoc Toolkit::Control::GetNextKeyboardFocusableActor()
436    */
437   Actor GetNextKeyboardFocusableActor(Actor actor, Toolkit::Control::KeyboardFocus::Direction direction, bool loopEnabled) override;
438
439   /**
440    * @copydoc Toolkit::Control::OnKeyboardFocusChangeCommitted()
441    */
442   void OnKeyboardFocusChangeCommitted(Actor commitedFocusableActor) override;
443
444 protected:
445   struct AccessibleImpl : public Scrollable::AccessibleImpl
446   {
447     using Scrollable::AccessibleImpl::AccessibleImpl;
448
449     void EnsureChildVisible(Actor child) override;
450   };
451
452   /**
453    * Construct a new ItemView.
454    * @param[in] factory The factory which provides ItemView with items.
455    */
456   ItemView(ItemFactory& factory);
457
458   /**
459    * A reference counted object may only be deleted by calling Unreference()
460    */
461   virtual ~ItemView();
462
463 private:
464
465   // Undefined
466   ItemView(const ItemView&);
467
468   // Undefined
469   ItemView& operator=(const ItemView& rhs);
470
471   /**
472    * Helper to re-apply all the constraints after items have been inserted, removed etc.
473    * @param[in] durationSeconds The time taken to fully constrain the actors.
474    */
475   void ReapplyAllConstraints();
476
477   /**
478    * Helper to relayout after item(s) are removed.
479    */
480   void OnItemsRemoved();
481
482   /**
483    * Helper to remove items outside a given range.
484    * @param[in] range The range of required items.
485    */
486   void RemoveItems(ItemRange range);
487
488   /**
489    * Helper to add a range of items, if not already in the ItemPool.
490    * @param[in] layout The layout used to position the new items.
491    * @param[in] layoutSize The current size of the layout.
492    * @param[in] range The range of required items.
493    */
494   void AddItems(ItemLayout& layout, const Vector3& layoutSize, ItemRange range);
495
496   /**
497    * Helper to find the range of items to populate with.
498    * @param[in] layout The current layout.
499    * @param[in] range The range of items.
500    * @param[in] reserveExtra True if reserve items should be included.
501    */
502   ItemRange GetItemRange(ItemLayout& layout, const Vector3& layoutSize, float layoutPosition, bool reserveExtra);
503
504   // Input Handling
505
506   /**
507    * Helper to clamp the first-item position when dragging/swiping.
508    * @param[in] targetPosition The target position of the drag etc.
509    * @param[in] targetSize The target ItemView & layout size.
510    * @param[in] layout The current layout.
511    * @param[in] updateOvershoot False stops the current overshoot value from being clamped also.
512    * @return The clamped first-item position.
513    */
514   float ClampFirstItemPosition(float targetPosition, const Vector3& targetSize, ItemLayout& layout, bool updateOvershoot = true);
515
516   /**
517    * Called after a touch-signal is received by the owning actor.
518    * @param[in] actor The touched actor.
519    * @param[in] touch The touch information.
520    * @return True if the event should be consumed.
521    */
522   bool OnTouch( Actor actor, const TouchEvent& touch );
523
524   /**
525    * Called upon pan gesture event.
526    *
527    * @param[in] gesture The gesture event.
528    */
529   void OnPan( const PanGesture& pan );
530
531   /**
532    * Helper to handle anchoring animations.
533    * @return The animation, or an uninitialized handle if not necessary.
534    */
535   Animation DoAnchoring();
536
537   /**
538    * Callback from scroll animations
539    * @param[in] animation The scroll-animation which has finished.
540    */
541   void OnScrollFinished(Animation& animation);
542
543   /**
544    * Callback from layout activation scroll animations
545    * @param[in] animation The scroll-animation which has finished.
546    */
547   void OnLayoutActivationScrollFinished(Animation& animation);
548
549   /**
550    * Called by animation system when overshoot has finished animating to maximum (either -1.0f or 1.0f)
551    *
552    * @param[in] animation the animation that has finished
553    */
554   void OnOvershootOnFinished(Animation& animation);
555
556   /**
557    * This is called after a timeout when no new wheel event is received for a certain period of time.
558    * @return will return false; one-shot timer.
559    */
560   bool OnWheelEventFinished();
561
562   /**
563    * Stops and removes animation if exists.
564    * @param[in,out] animation The animation handle to be removed.
565    */
566   void RemoveAnimation(Animation& animation);
567
568   /**
569    * @copydoc Toolkit::Internal::Scrollable::EnableScrollOvershoot
570    */
571   void EnableScrollOvershoot( bool enable ) override;
572
573   /**
574    * Helper to calculate the scroll overshoot according to the pan gesture displacement.
575    * @return The scroll overshoot.
576    */
577   float CalculateScrollOvershoot();
578
579   /**
580    * Helper to calculate the scroll overshoot according to the pan gesture displacement.
581    *
582    * @param[in] overshootAmount amount to animate to
583    * @param[in] animateBack whether to animate back to zero immediately after
584    * @return The scroll overshoot.
585    */
586   void AnimateScrollOvershoot(float overshootAmount, bool animateBack = false);
587
588   /**
589    * Gets the scroll position in pixels according to the logical layout position.
590    * @param[in] layoutSize The current size of the layout.
591    */
592   float GetScrollPosition(float layoutPosition, const Vector3& layoutSize) const;
593
594   /**
595    * Calculates the minimum and maximum positions for each axis to scroll to.
596    * @param[in] layoutSize The current size of the layout.
597    */
598   void CalculateDomainSize(const Vector3& layoutSize);
599
600   /**
601    * Calculates whether we want to allow current item view to scroll.
602    * @param[in] layoutSize The current size of the layout.
603    * @return    true if itemview is scrollable
604    */
605   bool IsLayoutScrollable(const Vector3& layoutSize);
606
607   /**
608    * Callback when the current layout position of ItemView changes in both positive
609    * and negative directions by the specified amount. Refresh the ItemView to create
610    * newly visible items.
611    * @param[in] source the property notification that triggered this callback
612    */
613   void OnRefreshNotification(PropertyNotification& source);
614
615 private:
616
617   Property::Array mlayoutArray;
618
619   ItemContainer mItemPool;
620   ItemFactory& mItemFactory;
621   std::vector< ItemLayoutPtr > mLayouts;            ///< Container of Dali::Toolkit::ItemLayout objects
622   Actor mOvershootOverlay;                          ///< The overlay actor for overshoot effect
623   Animation mResizeAnimation;
624   Animation mScrollAnimation;
625   Animation mScrollOvershootAnimation;
626   Timer mWheelEventFinishedTimer;                   ///< The timer to determine whether there is no wheel event received for a certain period of time.
627   PropertyNotification mRefreshNotification;        ///< Stores the property notification used for item view refresh
628   LayoutActivatedSignalType mLayoutActivatedSignal;
629   Vector3 mActiveLayoutTargetSize;
630   Vector3 mItemsParentOrigin;
631   Vector3 mItemsAnchorPoint;
632   Vector2 mTotalPanDisplacement;
633   ItemLayout* mActiveLayout;
634
635   float mAnchoringDuration;
636   float mRefreshIntervalLayoutPositions;            ///< Refresh item view when the layout position changes by this interval in both positive and negative directions.
637   float mMinimumSwipeSpeed;
638   float mMinimumSwipeDistance;
639   float mWheelScrollDistanceStep;                   ///< The step of scroll distance in actor coordinates for each wheel event received.
640   float mScrollDistance;
641   float mScrollSpeed;
642   float mScrollOvershoot;
643
644   GestureState mGestureState            : 8;
645   bool mAnimatingOvershootOn            : 1;        ///< Whether we are currently animating overshoot to 1.0f/-1.0f (on) or to 0.0f (off)
646   bool mAnimateOvershootOff             : 1;        ///< Whether we are currently animating overshoot to 1.0f/-1.0f (on) or to 0.0f (off)
647   bool mAnchoringEnabled                : 1;
648   bool mRefreshOrderHint                : 1;        ///< True if scrolling towards the last item
649   bool mIsFlicking                      : 1;
650   bool mAddingItems                     : 1;
651   bool mRefreshEnabled                  : 1;        ///< Whether to refresh the cache automatically
652   bool mRefreshNotificationEnabled      : 1;        ///< Whether to disable refresh notifications or not.
653   bool mInAnimation                     : 1;        ///< Keeps track of whether an animation is controlling the overshoot property.
654 };
655
656 } // namespace Internal
657
658 // Helpers for public-api forwarding methods
659
660 inline Toolkit::Internal::ItemView& GetImpl(Toolkit::ItemView& itemView)
661 {
662   DALI_ASSERT_ALWAYS(itemView);
663
664   Dali::RefObject& handle = itemView.GetImplementation();
665
666   return static_cast<Toolkit::Internal::ItemView&>(handle);
667 }
668
669 inline const Toolkit::Internal::ItemView& GetImpl(const Toolkit::ItemView& itemView)
670 {
671   DALI_ASSERT_ALWAYS(itemView);
672
673   const Dali::RefObject& handle = itemView.GetImplementation();
674
675   return static_cast<const Toolkit::Internal::ItemView&>(handle);
676 }
677
678 } // namespace Toolkit
679
680 } // namespace Dali
681
682 #endif // DALI_TOOLKIT_INTERNAL_ITEM_VIEW_H