1 #ifndef DALI_TOOLKIT_INTERNAL_ITEM_VIEW_H
2 #define DALI_TOOLKIT_INTERNAL_ITEM_VIEW_H
5 * Copyright (c) 2021 Samsung Electronics Co., Ltd.
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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.
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>
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>
44 typedef IntrusivePtr<ItemView> ItemViewPtr;
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.
51 class ItemView : public Scrollable
55 typedef Toolkit::ItemView::LayoutActivatedSignalType LayoutActivatedSignalType;
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.
63 static Dali::Toolkit::ItemView New(ItemFactory& factory);
66 * @copydoc Toolkit::ItemView::GetLayoutCount
68 unsigned int GetLayoutCount() const;
71 * @copydoc Toolkit::ItemView::AddLayout
73 void AddLayout(ItemLayout& layout);
76 * @copydoc Toolkit::ItemView::RemoveLayout
78 void RemoveLayout(unsigned int layoutIndex);
81 * @copydoc Toolkit::ItemView::GetLayout
83 ItemLayoutPtr GetLayout(unsigned int layoutIndex) const;
86 * @copydoc Toolkit::ItemView::GetActiveLayout
88 ItemLayoutPtr GetActiveLayout() const;
91 * @copydoc Toolkit::ItemView::GetCurrentLayoutPosition
93 float GetCurrentLayoutPosition(unsigned int itemId) const;
96 * @copydoc Toolkit::ItemView::ActivateLayout
98 void ActivateLayout(unsigned int layoutIndex, const Vector3& targetSize, float durationSeconds);
101 * @copydoc Toolkit::ItemView::DeactivateCurrentLayout
103 void DeactivateCurrentLayout();
106 * @copydoc Toolkit::ItemView::SetMinimumSwipeSpeed
108 void SetMinimumSwipeSpeed(float speed);
111 * @copydoc Toolkit::ItemView::GetMinimumSwipeSpeed
113 float GetMinimumSwipeSpeed() const;
116 * @copydoc Toolkit::ItemView::SetMinimumSwipeDistance
118 void SetMinimumSwipeDistance(float distance);
121 * @copydoc Toolkit::ItemView::GetMinimumSwipeDistance
123 float GetMinimumSwipeDistance() const;
126 * @copydoc Toolkit::ItemView::SetWheelScrollDistanceStep
128 void SetWheelScrollDistanceStep(float step);
131 * @copydoc Toolkit::ItemView::GetWheelScrollDistanceStep
133 float GetWheelScrollDistanceStep() const;
136 * @copydoc Toolkit::ItemView::SetAnchoring
138 void SetAnchoring(bool enabled);
141 * @copydoc Toolkit::ItemView::GetAnchoring
143 bool GetAnchoring() const;
146 * @copydoc Toolkit::ItemView::SetAnchoringDuration
148 void SetAnchoringDuration(float durationSeconds);
151 * @copydoc Toolkit::ItemView::GetAnchoringDuration
153 float GetAnchoringDuration() const;
156 * @copydoc Toolkit::ItemView::ScrollToItem
158 void ScrollToItem(unsigned int itemId, float durationSeconds);
161 * @copydoc Toolkit::ItemView::SetRefreshInterval
163 void SetRefreshInterval(float intervalLayoutPositions);
166 * @copydoc Toolkit::ItemView::GetRefreshInterval
168 float GetRefreshInterval() const;
171 * @copydoc Toolkit::ItemView::Refresh
176 * @copydoc Toolkit::ItemView::GetItem
178 Actor GetItem(unsigned int itemId) const;
181 * @copydoc Toolkit::ItemView::GetItemId
183 unsigned int GetItemId(Actor actor) const;
186 * @copydoc Toolkit::ItemView::InsertItem
188 void InsertItem(Item newItem, float durationSeconds);
191 * @copydoc Toolkit::ItemView::InsertItem
193 void InsertItems(const ItemContainer& newItems, float durationSeconds);
196 * @copydoc Toolkit::ItemView::RemoveItem
198 void RemoveItem(ItemId itemId, float durationSeconds);
201 * @copydoc Toolkit::ItemView::InsertItem
203 void RemoveItems(const ItemIdContainer& itemIds, float durationSeconds);
206 * @copydoc Toolkit::ItemView::InsertItem
208 void ReplaceItem(Item replacementItem, float durationSeconds);
211 * @copydoc Toolkit::ItemView::InsertItem
213 void ReplaceItems(const ItemContainer& replacementItems, float durationSeconds);
216 * @copydoc Toolkit::Scrollable::GetCurrentScrollPosition
218 Vector2 GetCurrentScrollPosition() const;
221 * @copydoc Toolkit::Scrollable::AddOverlay()
223 void AddOverlay(Actor actor);
226 * @copydoc Toolkit::Scrollable::RemoveOverlay()
228 void RemoveOverlay(Actor actor);
231 * @copydoc Toolkit::Scrollable::ScrollTo(const Vector2& position, float duration)
233 void ScrollTo(const Vector2& position, float duration);
236 * @copydoc Toolkit::Internal::Scrollable::SetOvershootSize
238 void SetOvershootSize(const Vector2& size);
241 * @copydoc Toolkit::Internal::Scrollable::SetOvershootEffectColor
243 void SetOvershootEffectColor(const Vector4& color);
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.
252 * @param[in] enabled True to enable automatic refresh or false to disable it.
254 void SetRefreshEnabled(bool enabled);
257 * @brief Helper to perform the refresh.
259 * @param[in] currentLayoutPosition The current layout position.
260 * @param[in] cacheExtra Whether to cache extra items during refresh.
262 void DoRefresh(float currentLayoutPosition, bool cacheExtra);
265 * @copydoc Toolkit::ItemView::SetItemsParentOrigin
267 void SetItemsParentOrigin(const Vector3& parentOrigin);
270 * @copydoc Toolkit::ItemView::GetItemsParentOrigin
272 Vector3 GetItemsParentOrigin() const;
275 * @copydoc Toolkit::ItemView::SetItemsAnchorPoint
277 void SetItemsAnchorPoint(const Vector3& anchorPoint);
280 * @copydoc Toolkit::ItemView::GetItemsAnchorPoint
282 Vector3 GetItemsAnchorPoint() const;
285 * @copydoc Toolkit::ItemView::GetItemsRange
287 void GetItemsRange(ItemRange& range);
290 * @copydoc Toolkit::ItemView::LayoutActivatedSignal()
292 LayoutActivatedSignalType& LayoutActivatedSignal()
294 return mLayoutActivatedSignal;
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.
306 static bool DoConnectSignal(BaseObject* object, ConnectionTrackerInterface* tracker, const std::string& signalName, FunctorDelegate* functor);
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.
316 static void SetProperty(BaseObject* object, Property::Index index, const Property::Value& value);
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.
324 static Property::Value GetProperty(BaseObject* object, Property::Index index);
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
333 static bool DoAction(BaseObject* object, const std::string& actionName, const Property::Map& attributes);
336 * Helper for DoAction( ACTION_STOP_SCROLLING ).
338 void DoStopScrolling();
341 * Helper for DoAction( ACTION_ENABLE/DISABLE_REFRESH_NOTIFICATIONS ).
342 * @param[in] enabled Whether to disable refresh notifications or not.
344 void SetRefreshNotificationEnabled(bool enabled);
348 * Get all the layouts used in the ItemView.
349 * @return The layout array
351 Property::Array GetLayoutArray();
354 * Set all the layouts. that will be used in the ItemView.
355 * @param[in] layouts The layouts used in the itemView.
357 void SetLayoutArray(const Property::Array& layouts);
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.
364 bool RemoveActor(unsigned int itemId);
367 * Remove any Actors outside a given range.
368 * @param[in] @param[in] range The range of required items.
370 void RemoveActorsOutsideRange(ItemRange range);
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.
377 void AddActorsWithinRange(ItemRange range, const Vector3& layoutSize);
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.
384 void AddNewActor(ItemId item, const Vector3& layoutSize);
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.
391 void SetupActor(Item item, const Vector3& layoutSize);
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.
398 void ReleaseActor(ItemId item, Actor actor);
400 private: // From CustomActorImpl
402 * From CustomActorImpl; called after a child has been added to the owning actor.
403 * @param[in] child The child which has been added.
405 void OnChildAdd(Actor& child) override;
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.
413 bool OnWheelEvent(Actor actor, const WheelEvent& event);
415 private: // From Control
417 * @copydoc Toolkit::Control::OnInitialize()
419 void OnInitialize() override;
422 * @copydoc Toolkit::Control::OnAccessibilityPan()
424 bool OnAccessibilityPan(PanGesture gesture) override;
427 * @copydoc Toolkit::Control::GetNextKeyboardFocusableActor()
429 Actor GetNextKeyboardFocusableActor(Actor actor, Toolkit::Control::KeyboardFocus::Direction direction, bool loopEnabled) override;
432 * @copydoc Toolkit::Control::OnKeyboardFocusChangeCommitted()
434 void OnKeyboardFocusChangeCommitted(Actor commitedFocusableActor) override;
437 struct AccessibleImpl : public Scrollable::AccessibleImpl
439 using Scrollable::AccessibleImpl::AccessibleImpl;
441 void EnsureChildVisible(Actor child) override;
445 * Construct a new ItemView.
446 * @param[in] factory The factory which provides ItemView with items.
448 ItemView(ItemFactory& factory);
451 * A reference counted object may only be deleted by calling Unreference()
457 ItemView(const ItemView&);
460 ItemView& operator=(const ItemView& rhs);
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.
466 void ReapplyAllConstraints();
469 * Helper to relayout after item(s) are removed.
471 void OnItemsRemoved();
474 * Helper to remove items outside a given range.
475 * @param[in] range The range of required items.
477 void RemoveItems(ItemRange range);
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.
485 void AddItems(ItemLayout& layout, const Vector3& layoutSize, ItemRange range);
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.
493 ItemRange GetItemRange(ItemLayout& layout, const Vector3& layoutSize, float layoutPosition, bool reserveExtra);
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.
505 float ClampFirstItemPosition(float targetPosition, const Vector3& targetSize, ItemLayout& layout, bool updateOvershoot = true);
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.
513 bool OnTouch(Actor actor, const TouchEvent& touch);
516 * Called upon pan gesture event.
518 * @param[in] gesture The gesture event.
520 void OnPan(const PanGesture& pan);
523 * Helper to handle anchoring animations.
524 * @return The animation, or an uninitialized handle if not necessary.
526 Animation DoAnchoring();
529 * Callback from scroll animations
530 * @param[in] animation The scroll-animation which has finished.
532 void OnScrollFinished(Animation& animation);
535 * Callback from layout activation scroll animations
536 * @param[in] animation The scroll-animation which has finished.
538 void OnLayoutActivationScrollFinished(Animation& animation);
541 * Called by animation system when overshoot has finished animating to maximum (either -1.0f or 1.0f)
543 * @param[in] animation the animation that has finished
545 void OnOvershootOnFinished(Animation& animation);
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.
551 bool OnWheelEventFinished();
554 * Stops and removes animation if exists.
555 * @param[in,out] animation The animation handle to be removed.
557 void RemoveAnimation(Animation& animation);
560 * @copydoc Toolkit::Internal::Scrollable::EnableScrollOvershoot
562 void EnableScrollOvershoot(bool enable) override;
565 * Helper to calculate the scroll overshoot according to the pan gesture displacement.
566 * @return The scroll overshoot.
568 float CalculateScrollOvershoot();
571 * Helper to calculate the scroll overshoot according to the pan gesture displacement.
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.
577 void AnimateScrollOvershoot(float overshootAmount, bool animateBack = false);
580 * Gets the scroll position in pixels according to the logical layout position.
581 * @param[in] layoutSize The current size of the layout.
583 float GetScrollPosition(float layoutPosition, const Vector3& layoutSize) const;
586 * Calculates the minimum and maximum positions for each axis to scroll to.
587 * @param[in] layoutSize The current size of the layout.
589 void CalculateDomainSize(const Vector3& layoutSize);
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
596 bool IsLayoutScrollable(const Vector3& layoutSize);
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
604 void OnRefreshNotification(PropertyNotification& source);
607 Property::Array mlayoutArray;
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;
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;
632 float mScrollOvershoot;
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.
646 } // namespace Internal
648 // Helpers for public-api forwarding methods
650 inline Toolkit::Internal::ItemView& GetImpl(Toolkit::ItemView& itemView)
652 DALI_ASSERT_ALWAYS(itemView);
654 Dali::RefObject& handle = itemView.GetImplementation();
656 return static_cast<Toolkit::Internal::ItemView&>(handle);
659 inline const Toolkit::Internal::ItemView& GetImpl(const Toolkit::ItemView& itemView)
661 DALI_ASSERT_ALWAYS(itemView);
663 const Dali::RefObject& handle = itemView.GetImplementation();
665 return static_cast<const Toolkit::Internal::ItemView&>(handle);
668 } // namespace Toolkit
672 #endif // DALI_TOOLKIT_INTERNAL_ITEM_VIEW_H