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