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::Internal::Control::CreateAccessibleObject()
424 DevelControl::ControlAccessible* CreateAccessibleObject() override;
427 * @copydoc Toolkit::Control::OnAccessibilityPan()
429 bool OnAccessibilityPan(PanGesture gesture) override;
432 * @copydoc Toolkit::Control::GetNextKeyboardFocusableActor()
434 Actor GetNextKeyboardFocusableActor(Actor actor, Toolkit::Control::KeyboardFocus::Direction direction, bool loopEnabled) override;
437 * @copydoc Toolkit::Control::OnKeyboardFocusChangeCommitted()
439 void OnKeyboardFocusChangeCommitted(Actor commitedFocusableActor) override;
442 class ItemViewAccessible : public Scrollable::ScrollableAccessible
445 using Scrollable::ScrollableAccessible::ScrollableAccessible;
448 * @copydoc Dali::Toolkit::DevelControl::ControlAccessible::ScrollToChild()
450 bool ScrollToChild(Actor child) override;
454 * Construct a new ItemView.
455 * @param[in] factory The factory which provides ItemView with items.
457 ItemView(ItemFactory& factory);
460 * A reference counted object may only be deleted by calling Unreference()
466 ItemView(const ItemView&);
469 ItemView& operator=(const ItemView& rhs);
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.
475 void ReapplyAllConstraints();
478 * Helper to relayout after item(s) are removed.
480 void OnItemsRemoved();
483 * Helper to remove items outside a given range.
484 * @param[in] range The range of required items.
486 void RemoveItems(ItemRange range);
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.
494 void AddItems(ItemLayout& layout, const Vector3& layoutSize, ItemRange range);
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.
502 ItemRange GetItemRange(ItemLayout& layout, const Vector3& layoutSize, float layoutPosition, bool reserveExtra);
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.
514 float ClampFirstItemPosition(float targetPosition, const Vector3& targetSize, ItemLayout& layout, bool updateOvershoot = true);
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.
522 bool OnTouch(Actor actor, const TouchEvent& touch);
525 * Called upon pan gesture event.
527 * @param[in] gesture The gesture event.
529 void OnPan(const PanGesture& pan);
532 * Helper to handle anchoring animations.
533 * @return The animation, or an uninitialized handle if not necessary.
535 Animation DoAnchoring();
538 * Callback from scroll animations
539 * @param[in] animation The scroll-animation which has finished.
541 void OnScrollFinished(Animation& animation);
544 * Callback from layout activation scroll animations
545 * @param[in] animation The scroll-animation which has finished.
547 void OnLayoutActivationScrollFinished(Animation& animation);
550 * Called by animation system when overshoot has finished animating to maximum (either -1.0f or 1.0f)
552 * @param[in] animation the animation that has finished
554 void OnOvershootOnFinished(Animation& animation);
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.
560 bool OnWheelEventFinished();
563 * Stops and removes animation if exists.
564 * @param[in,out] animation The animation handle to be removed.
566 void RemoveAnimation(Animation& animation);
569 * @copydoc Toolkit::Internal::Scrollable::EnableScrollOvershoot
571 void EnableScrollOvershoot(bool enable) override;
574 * Helper to calculate the scroll overshoot according to the pan gesture displacement.
575 * @return The scroll overshoot.
577 float CalculateScrollOvershoot();
580 * Helper to calculate the scroll overshoot according to the pan gesture displacement.
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.
586 void AnimateScrollOvershoot(float overshootAmount, bool animateBack = false);
589 * Gets the scroll position in pixels according to the logical layout position.
590 * @param[in] layoutSize The current size of the layout.
592 float GetScrollPosition(float layoutPosition, const Vector3& layoutSize) const;
595 * Calculates the minimum and maximum positions for each axis to scroll to.
596 * @param[in] layoutSize The current size of the layout.
598 void CalculateDomainSize(const Vector3& layoutSize);
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
605 bool IsLayoutScrollable(const Vector3& layoutSize);
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
613 void OnRefreshNotification(PropertyNotification& source);
616 Property::Array mlayoutArray;
618 ItemContainer mItemPool;
619 ItemFactory& mItemFactory;
620 std::vector<ItemLayoutPtr> mLayouts; ///< Container of Dali::Toolkit::ItemLayout objects
621 Actor mOvershootOverlay; ///< The overlay actor for overshoot effect
622 Animation mResizeAnimation;
623 Animation mScrollAnimation;
624 Animation mScrollOvershootAnimation;
625 Timer mWheelEventFinishedTimer; ///< The timer to determine whether there is no wheel event received for a certain period of time.
626 PropertyNotification mRefreshNotification; ///< Stores the property notification used for item view refresh
627 LayoutActivatedSignalType mLayoutActivatedSignal;
628 Vector3 mActiveLayoutTargetSize;
629 Vector3 mItemsParentOrigin;
630 Vector3 mItemsAnchorPoint;
631 Vector2 mTotalPanDisplacement;
632 ItemLayout* mActiveLayout;
634 float mAnchoringDuration;
635 float mRefreshIntervalLayoutPositions; ///< Refresh item view when the layout position changes by this interval in both positive and negative directions.
636 float mMinimumSwipeSpeed;
637 float mMinimumSwipeDistance;
638 float mWheelScrollDistanceStep; ///< The step of scroll distance in actor coordinates for each wheel event received.
639 float mScrollDistance;
641 float mScrollOvershoot;
643 GestureState mGestureState : 8;
644 bool mAnimatingOvershootOn : 1; ///< Whether we are currently animating overshoot to 1.0f/-1.0f (on) or to 0.0f (off)
645 bool mAnimateOvershootOff : 1; ///< Whether we are currently animating overshoot to 1.0f/-1.0f (on) or to 0.0f (off)
646 bool mAnchoringEnabled : 1;
647 bool mRefreshOrderHint : 1; ///< True if scrolling towards the last item
648 bool mIsFlicking : 1;
649 bool mAddingItems : 1;
650 bool mRefreshEnabled : 1; ///< Whether to refresh the cache automatically
651 bool mRefreshNotificationEnabled : 1; ///< Whether to disable refresh notifications or not.
652 bool mInAnimation : 1; ///< Keeps track of whether an animation is controlling the overshoot property.
655 } // namespace Internal
657 // Helpers for public-api forwarding methods
659 inline Toolkit::Internal::ItemView& GetImpl(Toolkit::ItemView& itemView)
661 DALI_ASSERT_ALWAYS(itemView);
663 Dali::RefObject& handle = itemView.GetImplementation();
665 return static_cast<Toolkit::Internal::ItemView&>(handle);
668 inline const Toolkit::Internal::ItemView& GetImpl(const Toolkit::ItemView& itemView)
670 DALI_ASSERT_ALWAYS(itemView);
672 const Dali::RefObject& handle = itemView.GetImplementation();
674 return static_cast<const Toolkit::Internal::ItemView&>(handle);
677 } // namespace Toolkit
681 #endif // DALI_TOOLKIT_INTERNAL_ITEM_VIEW_H