1 /* Copyright (c) 2020 Samsung Electronics Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
7 * http://www.apache.org/licenses/LICENSE-2.0
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
18 using Tizen.NUI.BaseComponents;
19 using System.Collections.Generic;
20 using System.ComponentModel;
21 using System.Diagnostics;
22 using System.Runtime.InteropServices;
23 using Tizen.NUI.Accessibility;
25 namespace Tizen.NUI.Components
28 /// ScrollEventArgs is a class to record scroll event arguments which will sent to user.
30 /// <since_tizen> 8 </since_tizen>
31 public class ScrollEventArgs : EventArgs
33 private Position position;
36 /// Default constructor.
38 /// <param name="position">Current scroll position</param>
39 /// <since_tizen> 8 </since_tizen>
40 public ScrollEventArgs(Position position)
42 this.position = position;
46 /// Current position of ContentContainer.
48 /// <since_tizen> 8 </since_tizen>
49 public Position Position
59 /// ScrollOutofBoundEventArgs is to record scroll out-of-bound event arguments which will be sent to user.
61 [EditorBrowsable(EditorBrowsableState.Never)]
62 public class ScrollOutOfBoundEventArgs : EventArgs
65 /// The direction to be touched.
67 [EditorBrowsable(EditorBrowsableState.Never)]
73 [EditorBrowsable(EditorBrowsableState.Never)]
79 [EditorBrowsable(EditorBrowsableState.Never)]
86 /// <param name="direction">Current pan direction</param>
87 /// <param name="displacement">Current total displacement</param>
88 [EditorBrowsable(EditorBrowsableState.Never)]
89 public ScrollOutOfBoundEventArgs(Direction direction, float displacement)
91 PanDirection = direction;
92 Displacement = displacement;
96 /// Current pan direction of ContentContainer.
98 [EditorBrowsable(EditorBrowsableState.Never)]
99 public Direction PanDirection
105 /// Current total displacement of ContentContainer.
106 /// if its value is greater than 0, it is at the top/left;
107 /// if less than 0, it is at the bottom/right.
109 [EditorBrowsable(EditorBrowsableState.Never)]
110 public float Displacement
117 /// This class provides a View that can scroll a single View with a layout. This View can be a nest of Views.
119 /// <since_tizen> 8 </since_tizen>
120 public class ScrollableBase : Control
122 static bool LayoutDebugScrollableBase = false; // Debug flag
123 private Direction mScrollingDirection = Direction.Vertical;
124 private bool mScrollEnabled = true;
125 private int mScrollDuration = 125;
126 private int mPageWidth = 0;
127 private float mPageFlickThreshold = 0.4f;
128 private float mScrollingEventThreshold = 0.001f;
130 private class ScrollableBaseCustomLayout : AbsoluteLayout
132 protected override void OnMeasure(MeasureSpecification widthMeasureSpec, MeasureSpecification heightMeasureSpec)
134 MeasuredSize.StateType childWidthState = MeasuredSize.StateType.MeasuredSizeOK;
135 MeasuredSize.StateType childHeightState = MeasuredSize.StateType.MeasuredSizeOK;
137 Direction scrollingDirection = Direction.Vertical;
138 ScrollableBase scrollableBase = this.Owner as ScrollableBase;
139 if (scrollableBase != null)
141 scrollingDirection = scrollableBase.ScrollingDirection;
144 float totalWidth = 0.0f;
145 float totalHeight = 0.0f;
147 // measure child, should be a single scrolling child
148 foreach (LayoutItem childLayout in LayoutChildren)
150 if (childLayout != null && childLayout.Owner.Name == "ContentContainer")
153 // Use an Unspecified MeasureSpecification mode so scrolling child is not restricted to it's parents size in Height (for vertical scrolling)
154 // or Width for horizontal scrolling
155 if (scrollingDirection == Direction.Vertical)
157 MeasureSpecification unrestrictedMeasureSpec = new MeasureSpecification(heightMeasureSpec.Size, MeasureSpecification.ModeType.Unspecified);
158 MeasureChildWithMargins(childLayout, widthMeasureSpec, new LayoutLength(0), unrestrictedMeasureSpec, new LayoutLength(0)); // Height unrestricted by parent
162 MeasureSpecification unrestrictedMeasureSpec = new MeasureSpecification(widthMeasureSpec.Size, MeasureSpecification.ModeType.Unspecified);
163 MeasureChildWithMargins(childLayout, unrestrictedMeasureSpec, new LayoutLength(0), heightMeasureSpec, new LayoutLength(0)); // Width unrestricted by parent
166 totalWidth = (childLayout.MeasuredWidth.Size + (childLayout.Padding.Start + childLayout.Padding.End)).AsDecimal();
167 totalHeight = (childLayout.MeasuredHeight.Size + (childLayout.Padding.Top + childLayout.Padding.Bottom)).AsDecimal();
169 if (childLayout.MeasuredWidth.State == MeasuredSize.StateType.MeasuredSizeTooSmall)
171 childWidthState = MeasuredSize.StateType.MeasuredSizeTooSmall;
173 if (childLayout.MeasuredHeight.State == MeasuredSize.StateType.MeasuredSizeTooSmall)
175 childHeightState = MeasuredSize.StateType.MeasuredSizeTooSmall;
180 MeasuredSize widthSizeAndState = ResolveSizeAndState(new LayoutLength(totalWidth), widthMeasureSpec, MeasuredSize.StateType.MeasuredSizeOK);
181 MeasuredSize heightSizeAndState = ResolveSizeAndState(new LayoutLength(totalHeight), heightMeasureSpec, MeasuredSize.StateType.MeasuredSizeOK);
182 totalWidth = widthSizeAndState.Size.AsDecimal();
183 totalHeight = heightSizeAndState.Size.AsDecimal();
185 // Ensure layout respects it's given minimum size
186 totalWidth = Math.Max(totalWidth, SuggestedMinimumWidth.AsDecimal());
187 totalHeight = Math.Max(totalHeight, SuggestedMinimumHeight.AsDecimal());
189 widthSizeAndState.State = childWidthState;
190 heightSizeAndState.State = childHeightState;
192 SetMeasuredDimensions(ResolveSizeAndState(new LayoutLength(totalWidth), widthMeasureSpec, childWidthState),
193 ResolveSizeAndState(new LayoutLength(totalHeight), heightMeasureSpec, childHeightState));
195 // Size of ScrollableBase is changed. Change Page width too.
196 if (scrollableBase != null)
198 scrollableBase.mPageWidth = (int)MeasuredWidth.Size.AsRoundedValue();
199 scrollableBase.OnScrollingChildRelayout(null, null);
202 } // ScrollableBaseCustomLayout
205 /// The direction axis to scroll.
207 /// <since_tizen> 8 </since_tizen>
208 public enum Direction
213 /// <since_tizen> 8 </since_tizen>
219 /// <since_tizen> 8 </since_tizen>
224 /// Scrolling direction mode.
225 /// Default is Vertical scrolling.
227 /// <since_tizen> 8 </since_tizen>
228 public Direction ScrollingDirection
232 return mScrollingDirection;
236 if (value != mScrollingDirection)
238 mScrollingDirection = value;
239 mPanGestureDetector.ClearAngles();
240 mPanGestureDetector.AddDirection(value == Direction.Horizontal ?
241 PanGestureDetector.DirectionHorizontal : PanGestureDetector.DirectionVertical);
243 ContentContainer.WidthSpecification = ScrollingDirection == Direction.Vertical ? LayoutParamPolicies.MatchParent : LayoutParamPolicies.WrapContent;
244 ContentContainer.HeightSpecification = ScrollingDirection == Direction.Vertical ? LayoutParamPolicies.WrapContent : LayoutParamPolicies.MatchParent;
250 /// Enable or disable scrolling.
252 /// <since_tizen> 8 </since_tizen>
253 public bool ScrollEnabled
257 return mScrollEnabled;
261 if (value != mScrollEnabled)
263 mScrollEnabled = value;
266 mPanGestureDetector.Detected += OnPanGestureDetected;
270 mPanGestureDetector.Detected -= OnPanGestureDetected;
277 /// Pages mode, enables moving to the next or return to current page depending on pan displacement.
278 /// Default is false.
280 /// <since_tizen> 8 </since_tizen>
281 public bool SnapToPage { set; get; } = false;
284 /// Get current page.
285 /// Working property with SnapToPage property.
287 /// <since_tizen> 8 </since_tizen>
288 public int CurrentPage { get; private set; } = 0;
291 /// Duration of scroll animation.
292 /// Default value is 125ms.
294 /// <since_tizen> 8 </since_tizen>
295 public int ScrollDuration
299 mScrollDuration = value >= 0 ? value : mScrollDuration;
303 return mScrollDuration;
308 /// Scroll Available area.
310 /// <since_tizen> 8 </since_tizen>
311 public Vector2 ScrollAvailableArea { set; get; }
314 /// An event emitted when user starts dragging ScrollableBase, user can subscribe or unsubscribe to this event handler.<br />
316 /// <since_tizen> 8 </since_tizen>
317 public event EventHandler<ScrollEventArgs> ScrollDragStarted;
320 /// An event emitted when user stops dragging ScrollableBase, user can subscribe or unsubscribe to this event handler.<br />
322 /// <since_tizen> 8 </since_tizen>
323 public event EventHandler<ScrollEventArgs> ScrollDragEnded;
326 /// An event emitted when the scrolling slide animation starts, user can subscribe or unsubscribe to this event handler.<br />
328 /// <since_tizen> 8 </since_tizen>
329 public event EventHandler<ScrollEventArgs> ScrollAnimationStarted;
332 /// An event emitted when the scrolling slide animation ends, user can subscribe or unsubscribe to this event handler.<br />
334 /// <since_tizen> 8 </since_tizen>
335 public event EventHandler<ScrollEventArgs> ScrollAnimationEnded;
338 /// An event emitted when scrolling, user can subscribe or unsubscribe to this event handler.<br />
340 /// <since_tizen> 8 </since_tizen>
341 public event EventHandler<ScrollEventArgs> Scrolling;
344 /// An event emitted when scrolling out of bound, user can subscribe or unsubscribe to this event handler.<br />
346 [EditorBrowsable(EditorBrowsableState.Never)]
347 public event EventHandler<ScrollOutOfBoundEventArgs> ScrollOutOfBound;
350 /// Scrollbar for ScrollableBase.
352 /// <since_tizen> 8 </since_tizen>
353 public ScrollbarBase Scrollbar
363 scrollBar.Unparent();
367 if (scrollBar != null)
369 scrollBar.Name = "ScrollBar";
387 /// Always hide Scrollbar.
389 /// <since_tizen> 8 </since_tizen>
390 public bool HideScrollbar
394 return hideScrollbar;
398 hideScrollbar = value;
415 /// Container which has content of ScrollableBase.
417 /// <since_tizen> 8 </since_tizen>
418 public View ContentContainer { get; private set; }
421 /// Set the layout on this View. Replaces any existing Layout.
423 /// <since_tizen> 8 </since_tizen>
424 public new LayoutItem Layout
428 return ContentContainer.Layout;
432 ContentContainer.Layout = value;
437 /// List of children of Container.
439 /// <since_tizen> 8 </since_tizen>
440 public new List<View> Children
444 return ContentContainer.Children;
449 /// Deceleration rate of scrolling by finger.
450 /// Rate should be bigger than 0 and smaller than 1.
451 /// Default value is 0.998f;
453 /// <since_tizen> 8 </since_tizen>
454 public float DecelerationRate
458 return decelerationRate;
462 decelerationRate = (value < 1 && value > 0) ? value : decelerationRate;
463 logValueOfDeceleration = (float)Math.Log(value);
468 /// Threashold not to go infinit at the end of scrolling animation.
470 [EditorBrowsable(EditorBrowsableState.Never)]
471 public float DecelerationThreshold { get; set; } = 0.1f;
474 /// Scrolling event will be thrown when this amount of scroll position is changed.
475 /// If this threshold becomes smaller, the tracking detail increases but the scrolling range that can be tracked becomes smaller.
476 /// If large sized ContentContainer is required, please use larger threshold value.
477 /// Default ScrollingEventThreshold value is 0.001f.
479 [EditorBrowsable(EditorBrowsableState.Never)]
480 public float ScrollingEventThreshold
484 return mScrollingEventThreshold;
488 if (mScrollingEventThreshold != value && value > 0)
490 ContentContainer.RemovePropertyNotification(propertyNotification);
491 propertyNotification = ContentContainer.AddPropertyNotification("position", PropertyCondition.Step(value));
492 propertyNotification.Notified += OnPropertyChanged;
493 mScrollingEventThreshold = value;
499 /// Page will be changed when velocity of panning is over threshold.
500 /// The unit of threshold is pixel per milisec.
502 /// <since_tizen> 8 </since_tizen>
503 public float PageFlickThreshold
507 return mPageFlickThreshold;
511 mPageFlickThreshold = value >= 0f ? value : mPageFlickThreshold;
516 /// Padding for the ScrollableBase
518 [EditorBrowsable(EditorBrowsableState.Never)]
519 public Extents Padding
523 return ContentContainer.Padding;
527 ContentContainer.Padding = value;
532 /// Alphafunction for scroll animation.
534 [EditorBrowsable(EditorBrowsableState.Never)]
535 public AlphaFunction ScrollAlphaFunction { get; set; } = new AlphaFunction(AlphaFunction.BuiltinFunctions.Linear);
537 private bool hideScrollbar = true;
538 private float maxScrollDistance;
539 private float childTargetPosition = 0.0f;
540 private PanGestureDetector mPanGestureDetector;
541 private View mInterruptTouchingChild;
542 private ScrollbarBase scrollBar;
543 private bool scrolling = false;
544 private float ratioOfScreenWidthToCompleteScroll = 0.5f;
545 private float totalDisplacementForPan = 0.0f;
546 private Size previousContainerSize = new Size();
547 private Size previousSize = new Size();
548 private PropertyNotification propertyNotification;
549 private float noticeAnimationEndBeforePosition = 0.0f;
550 private bool readyToNotice = false;
553 /// Notice before animation is finished.
555 [EditorBrowsable(EditorBrowsableState.Never)]
556 // Let's consider more whether this needs to be set as protected.
557 public float NoticeAnimationEndBeforePosition
559 get => noticeAnimationEndBeforePosition;
560 set => noticeAnimationEndBeforePosition = value;
563 // Let's consider more whether this needs to be set as protected.
564 private float finalTargetPosition;
566 private Animation scrollAnimation;
567 // Declare user alpha function delegate
568 [UnmanagedFunctionPointer(CallingConvention.StdCall)]
569 private delegate float UserAlphaFunctionDelegate(float progress);
570 private UserAlphaFunctionDelegate customScrollAlphaFunction;
571 private float velocityOfLastPan = 0.0f;
572 private float panAnimationDuration = 0.0f;
573 private float panAnimationDelta = 0.0f;
574 private float logValueOfDeceleration = 0.0f;
575 private float decelerationRate = 0.0f;
577 private View verticalTopShadowView;
578 private View verticalBottomShadowView;
579 private const int verticalShadowScaleHeightLimit = 64 * 3;
580 private const int verticalShadowAnimationDuration = 300;
581 private Animation verticalShadowAnimation;
582 private bool isVerticalShadowShown = false;
583 private float startShowShadowDisplacement;
586 /// Default Constructor
588 /// <since_tizen> 8 </since_tizen>
589 public ScrollableBase() : base()
591 DecelerationRate = 0.998f;
593 base.Layout = new ScrollableBaseCustomLayout();
594 mPanGestureDetector = new PanGestureDetector();
595 mPanGestureDetector.Attach(this);
596 mPanGestureDetector.AddDirection(PanGestureDetector.DirectionVertical);
597 mPanGestureDetector.Detected += OnPanGestureDetected;
599 ClippingMode = ClippingModeType.ClipToBoundingBox;
601 //Default Scrolling child
602 ContentContainer = new View()
604 Name = "ContentContainer",
605 WidthSpecification = ScrollingDirection == Direction.Vertical ? LayoutParamPolicies.MatchParent : LayoutParamPolicies.WrapContent,
606 HeightSpecification = ScrollingDirection == Direction.Vertical ? LayoutParamPolicies.WrapContent : LayoutParamPolicies.MatchParent,
608 ContentContainer.Relayout += OnScrollingChildRelayout;
609 propertyNotification = ContentContainer.AddPropertyNotification("position", PropertyCondition.Step(mScrollingEventThreshold));
610 propertyNotification.Notified += OnPropertyChanged;
611 base.Add(ContentContainer);
613 //Interrupt touching when panning is started
614 mInterruptTouchingChild = new View()
616 Size = new Size(Window.Instance.WindowSize),
617 BackgroundColor = Color.Transparent,
619 mInterruptTouchingChild.TouchEvent += OnIterruptTouchingChildTouched;
620 Scrollbar = new Scrollbar();
622 //Show vertical shadow on the top (or bottom) of the scrollable when panning down (or up).
623 verticalTopShadowView = new View
625 BackgroundImage = Tizen.NUI.FrameworkInformation.ResourcePath + "nui_component_default_scroll_over_shooting_top.png",
628 PositionUsesPivotPoint = true,
629 ParentOrigin = NUI.ParentOrigin.TopCenter,
630 PivotPoint = NUI.PivotPoint.TopCenter,
632 verticalBottomShadowView = new View
634 BackgroundImage = Tizen.NUI.FrameworkInformation.ResourcePath + "nui_component_default_scroll_over_shooting_bottom.png",
637 PositionUsesPivotPoint = true,
638 ParentOrigin = NUI.ParentOrigin.BottomCenter,
639 PivotPoint = NUI.PivotPoint.BottomCenter,
642 AccessibilityManager.Instance.SetAccessibilityAttribute(this, AccessibilityManager.AccessibilityAttribute.Trait, "ScrollableBase");
645 private bool OnIterruptTouchingChildTouched(object source, View.TouchEventArgs args)
647 if (args.Touch.GetState(0) == PointStateType.Down)
649 if (scrolling && !SnapToPage)
657 private void OnPropertyChanged(object source, PropertyNotification.NotifyEventArgs args)
663 /// Called after a child has been added to the owning view.
665 /// <param name="view">The child which has been added.</param>
666 /// <since_tizen> 8 </since_tizen>
667 public override void Add(View view)
669 ContentContainer.Add(view);
673 /// Called after a child has been removed from the owning view.
675 /// <param name="view">The child which has been removed.</param>
676 /// <since_tizen> 8 </since_tizen>
677 public override void Remove(View view)
679 if (SnapToPage && CurrentPage == Children.IndexOf(view) && CurrentPage == Children.Count - 1)
681 // Target View is current page and also last child.
682 // CurrentPage should be changed to previous page.
683 CurrentPage = Math.Max(0, CurrentPage - 1);
684 ScrollToIndex(CurrentPage);
687 ContentContainer.Remove(view);
690 private void OnScrollingChildRelayout(object source, EventArgs args)
692 // Size is changed. Calculate maxScrollDistance.
693 bool isSizeChanged = previousContainerSize.Width != ContentContainer.Size.Width || previousContainerSize.Height != ContentContainer.Size.Height ||
694 previousSize.Width != Size.Width || previousSize.Height != Size.Height;
698 maxScrollDistance = CalculateMaximumScrollDistance();
702 previousContainerSize = ContentContainer.Size;
707 /// The composition of a Scrollbar can vary depending on how you use ScrollableBase.
708 /// Set the composition that will go into the ScrollableBase according to your ScrollableBase.
710 /// <since_tizen> 8 </since_tizen>
711 [EditorBrowsable(EditorBrowsableState.Never)]
712 protected virtual void SetScrollbar()
716 bool isHorizontal = ScrollingDirection == Direction.Horizontal;
717 float contentLength = isHorizontal ? ContentContainer.Size.Width : ContentContainer.Size.Height;
718 float viewportLength = isHorizontal ? Size.Width : Size.Height;
719 float currentPosition = isHorizontal ? ContentContainer.CurrentPosition.X : ContentContainer.CurrentPosition.Y;
720 Scrollbar.Initialize(contentLength, viewportLength, currentPosition, isHorizontal);
725 /// Scrolls to the item at the specified index.
727 /// <param name="index">Index of item.</param>
728 /// <since_tizen> 8 </since_tizen>
729 public void ScrollToIndex(int index)
731 if (ContentContainer.ChildCount - 1 < index || index < 0)
741 float targetPosition = Math.Min(ScrollingDirection == Direction.Vertical ? Children[index].Position.Y : Children[index].Position.X, maxScrollDistance);
742 AnimateChildTo(ScrollDuration, -targetPosition);
745 private void OnScrollDragStarted()
747 ScrollEventArgs eventArgs = new ScrollEventArgs(ContentContainer.CurrentPosition);
748 ScrollDragStarted?.Invoke(this, eventArgs);
751 private void OnScrollDragEnded()
753 ScrollEventArgs eventArgs = new ScrollEventArgs(ContentContainer.CurrentPosition);
754 ScrollDragEnded?.Invoke(this, eventArgs);
757 private void OnScrollAnimationStarted()
759 ScrollEventArgs eventArgs = new ScrollEventArgs(ContentContainer.CurrentPosition);
760 ScrollAnimationStarted?.Invoke(this, eventArgs);
763 private void OnScrollAnimationEnded()
766 base.Remove(mInterruptTouchingChild);
768 ScrollEventArgs eventArgs = new ScrollEventArgs(ContentContainer.CurrentPosition);
769 ScrollAnimationEnded?.Invoke(this, eventArgs);
772 private void OnScroll()
774 ScrollEventArgs eventArgs = new ScrollEventArgs(ContentContainer.CurrentPosition);
775 Scrolling?.Invoke(this, eventArgs);
777 bool isHorizontal = ScrollingDirection == Direction.Horizontal;
778 float contentLength = isHorizontal ? ContentContainer.Size.Width : ContentContainer.Size.Height;
779 float currentPosition = isHorizontal ? ContentContainer.CurrentPosition.X : ContentContainer.CurrentPosition.Y;
781 scrollBar?.Update(contentLength, Math.Abs(currentPosition));
782 CheckPreReachedTargetPosition();
785 private void CheckPreReachedTargetPosition()
787 // Check whether we reached pre-reached target position
789 ContentContainer.CurrentPosition.Y <= finalTargetPosition + NoticeAnimationEndBeforePosition &&
790 ContentContainer.CurrentPosition.Y >= finalTargetPosition - NoticeAnimationEndBeforePosition)
793 readyToNotice = false;
794 OnPreReachedTargetPosition(finalTargetPosition);
799 /// This helps developer who wants to know before scroll is reaching target position.
801 /// <param name="targetPosition">Index of item.</param>
802 /// This may be public opened in tizen_6.0 after ACR done. Before ACR, need to be hidden as inhouse API
803 [EditorBrowsable(EditorBrowsableState.Never)]
804 protected virtual void OnPreReachedTargetPosition(float targetPosition)
809 private void StopScroll()
811 if (scrollAnimation != null)
813 if (scrollAnimation.State == Animation.States.Playing)
815 Debug.WriteLineIf(LayoutDebugScrollableBase, "StopScroll Animation Playing");
816 scrollAnimation.Stop(Animation.EndActions.Cancel);
817 OnScrollAnimationEnded();
819 scrollAnimation.Clear();
823 private void AnimateChildTo(int duration, float axisPosition)
825 Debug.WriteLineIf(LayoutDebugScrollableBase, "AnimationTo Animation Duration:" + duration + " Destination:" + axisPosition);
826 finalTargetPosition = axisPosition;
828 StopScroll(); // Will replace previous animation so will stop existing one.
830 if (scrollAnimation == null)
832 scrollAnimation = new Animation();
833 scrollAnimation.Finished += ScrollAnimationFinished;
836 scrollAnimation.Duration = duration;
837 scrollAnimation.DefaultAlphaFunction = new AlphaFunction(AlphaFunction.BuiltinFunctions.EaseOutSquare);
838 scrollAnimation.AnimateTo(ContentContainer, (ScrollingDirection == Direction.Horizontal) ? "PositionX" : "PositionY", axisPosition, ScrollAlphaFunction);
840 OnScrollAnimationStarted();
841 scrollAnimation.Play();
845 /// Scroll to specific position with or without animation.
847 /// <param name="position">Destination.</param>
848 /// <param name="animate">Scroll with or without animation</param>
849 /// <since_tizen> 8 </since_tizen>
850 public void ScrollTo(float position, bool animate)
852 float currentPositionX = ContentContainer.CurrentPosition.X != 0 ? ContentContainer.CurrentPosition.X : ContentContainer.Position.X;
853 float currentPositionY = ContentContainer.CurrentPosition.Y != 0 ? ContentContainer.CurrentPosition.Y : ContentContainer.Position.Y;
854 float delta = ScrollingDirection == Direction.Horizontal ? currentPositionX : currentPositionY;
855 // The argument position is the new pan position. So the new position of ScrollableBase becomes (-position).
856 // To move ScrollableBase's position to (-position), it moves by (-position - currentPosition).
857 delta = -position - delta;
859 ScrollBy(delta, animate);
862 private float BoundScrollPosition(float targetPosition)
864 if (ScrollAvailableArea != null)
866 float minScrollPosition = ScrollAvailableArea.X;
867 float maxScrollPosition = ScrollAvailableArea.Y;
869 targetPosition = Math.Min(-minScrollPosition, targetPosition);
870 targetPosition = Math.Max(-maxScrollPosition, targetPosition);
874 targetPosition = Math.Min(0, targetPosition);
875 targetPosition = Math.Max(-maxScrollDistance, targetPosition);
878 return targetPosition;
881 private void ScrollBy(float displacement, bool animate)
883 if (GetChildCount() == 0 || maxScrollDistance < 0)
888 float childCurrentPosition = (ScrollingDirection == Direction.Horizontal) ? ContentContainer.PositionX : ContentContainer.PositionY;
890 Debug.WriteLineIf(LayoutDebugScrollableBase, "ScrollBy childCurrentPosition:" + childCurrentPosition +
891 " displacement:" + displacement,
892 " maxScrollDistance:" + maxScrollDistance);
894 childTargetPosition = childCurrentPosition + displacement; // child current position + gesture displacement
896 Debug.WriteLineIf(LayoutDebugScrollableBase, "ScrollBy currentAxisPosition:" + childCurrentPosition + "childTargetPosition:" + childTargetPosition);
900 // Calculate scroll animaton duration
901 float scrollDistance = Math.Abs(displacement);
902 readyToNotice = true;
904 AnimateChildTo(ScrollDuration, BoundScrollPosition(AdjustTargetPositionOfScrollAnimation(BoundScrollPosition(childTargetPosition))));
908 finalTargetPosition = BoundScrollPosition(childTargetPosition);
910 // Set position of scrolling child without an animation
911 if (ScrollingDirection == Direction.Horizontal)
913 ContentContainer.PositionX = finalTargetPosition;
917 ContentContainer.PositionY = finalTargetPosition;
923 /// you can override it to clean-up your own resources.
925 /// <param name="type">DisposeTypes</param>
926 /// This will be public opened in tizen_5.5 after ACR done. Before ACR, need to be hidden as inhouse API.
927 [EditorBrowsable(EditorBrowsableState.Never)]
928 protected override void Dispose(DisposeTypes type)
935 if (type == DisposeTypes.Explicit)
937 StopVerticalShadowAnimation();
940 if (mPanGestureDetector != null)
942 mPanGestureDetector.Detected -= OnPanGestureDetected;
943 mPanGestureDetector.Dispose();
944 mPanGestureDetector = null;
947 propertyNotification.Dispose();
952 private float CalculateMaximumScrollDistance()
954 float scrollingChildLength = 0;
955 float scrollerLength = 0;
956 if (ScrollingDirection == Direction.Horizontal)
958 Debug.WriteLineIf(LayoutDebugScrollableBase, "Horizontal");
960 scrollingChildLength = ContentContainer.Size.Width;
961 scrollerLength = Size.Width;
965 Debug.WriteLineIf(LayoutDebugScrollableBase, "Vertical");
966 scrollingChildLength = ContentContainer.Size.Height;
967 scrollerLength = Size.Height;
970 Debug.WriteLineIf(LayoutDebugScrollableBase, "ScrollBy maxScrollDistance:" + (scrollingChildLength - scrollerLength) +
971 " parent length:" + scrollerLength +
972 " scrolling child length:" + scrollingChildLength);
974 return Math.Max(scrollingChildLength - scrollerLength, 0);
977 private void PageSnap(float velocity)
979 Debug.WriteLineIf(LayoutDebugScrollableBase, "PageSnap with pan candidate totalDisplacement:" + totalDisplacementForPan +
980 " currentPage[" + CurrentPage + "]");
982 //Increment current page if total displacement enough to warrant a page change.
983 if (Math.Abs(totalDisplacementForPan) > (mPageWidth * ratioOfScreenWidthToCompleteScroll))
985 if (totalDisplacementForPan < 0)
987 CurrentPage = Math.Min(Math.Max(Children.Count - 1, 0), ++CurrentPage);
991 CurrentPage = Math.Max(0, --CurrentPage);
994 else if (Math.Abs(velocity) > PageFlickThreshold)
998 CurrentPage = Math.Min(Math.Max(Children.Count - 1, 0), ++CurrentPage);
1002 CurrentPage = Math.Max(0, --CurrentPage);
1006 // Animate to new page or reposition to current page
1007 float destinationX = -(Children[CurrentPage].Position.X + Children[CurrentPage].CurrentSize.Width / 2 - CurrentSize.Width / 2); // set to middle of current page
1008 Debug.WriteLineIf(LayoutDebugScrollableBase, "Snapping to page[" + CurrentPage + "] to:" + destinationX + " from:" + ContentContainer.PositionX);
1009 AnimateChildTo(ScrollDuration, destinationX);
1013 /// Enable/Disable overshooting effect. default is disabled.
1015 [EditorBrowsable(EditorBrowsableState.Never)]
1016 public bool EnableOverShootingEffect { get; set; } = false;
1018 private void AttachShadowView()
1020 if (!EnableOverShootingEffect)
1023 if (ScrollingDirection != Direction.Vertical)
1026 // stop animation if necessary.
1027 StopVerticalShadowAnimation();
1029 base.Add(verticalTopShadowView);
1030 base.Add(verticalBottomShadowView);
1032 verticalTopShadowView.Size = new Size(SizeWidth, 0.0f);
1033 verticalTopShadowView.Opacity = 1.0f;
1034 verticalTopShadowView.RaiseToTop();
1036 verticalBottomShadowView.Size = new Size(SizeWidth, 0.0f);
1037 verticalBottomShadowView.Opacity = 1.0f;
1038 verticalBottomShadowView.RaiseToTop();
1040 // at the beginning, height of vertical shadow is 0, so it is invisible.
1041 isVerticalShadowShown = false;
1044 private void DragVerticalShadow(float totalPanDisplacement, float panDisplacement)
1046 if (!EnableOverShootingEffect)
1049 if (ScrollingDirection != Direction.Vertical)
1052 if (totalPanDisplacement > 0) // downwards
1054 // check if reaching at the top.
1055 if ((int)finalTargetPosition != 0)
1057 isVerticalShadowShown = false;
1061 // save start displacement, and re-calculate displacement.
1062 if (!isVerticalShadowShown)
1064 startShowShadowDisplacement = totalPanDisplacement;
1066 isVerticalShadowShown = true;
1069 ScrollOutOfBoundEventArgs.Direction direction = panDisplacement > 0 ?
1070 ScrollOutOfBoundEventArgs.Direction.Down : ScrollOutOfBoundEventArgs.Direction.Up;
1071 OnScrollOutOfBound(direction, totalPanDisplacement);
1073 float newDisplacement = (int)totalPanDisplacement < (int)startShowShadowDisplacement ? 0 : totalPanDisplacement - startShowShadowDisplacement;
1075 // scale limit of width is 60%.
1076 float widthScale = newDisplacement / verticalShadowScaleHeightLimit;
1077 verticalTopShadowView.SizeWidth = widthScale > 0.6f ? SizeWidth * 0.4f : SizeWidth * (1.0f - widthScale);
1079 // scale limit of height is 300%.
1080 verticalTopShadowView.SizeHeight = newDisplacement > verticalShadowScaleHeightLimit ? verticalShadowScaleHeightLimit : newDisplacement;
1082 else if (totalPanDisplacement < 0) // upwards
1084 // check if reaching at the bottom.
1085 if (-(int)finalTargetPosition != (int)maxScrollDistance)
1087 isVerticalShadowShown = false;
1091 // save start displacement, and re-calculate displacement.
1092 if (!isVerticalShadowShown)
1094 startShowShadowDisplacement = totalPanDisplacement;
1096 isVerticalShadowShown = true;
1099 ScrollOutOfBoundEventArgs.Direction direction = panDisplacement > 0 ?
1100 ScrollOutOfBoundEventArgs.Direction.Down : ScrollOutOfBoundEventArgs.Direction.Up;
1101 OnScrollOutOfBound(direction, totalPanDisplacement);
1103 float newDisplacement = (int)startShowShadowDisplacement < (int)totalPanDisplacement ? 0 : startShowShadowDisplacement - totalPanDisplacement;
1105 // scale limit of width is 60%.
1106 float widthScale = newDisplacement / verticalShadowScaleHeightLimit;
1107 verticalBottomShadowView.SizeWidth = widthScale > 0.6f ? SizeWidth * 0.4f : SizeWidth * (1.0f - widthScale);
1109 // scale limit of height is 300%.
1110 verticalBottomShadowView.SizeHeight = newDisplacement > verticalShadowScaleHeightLimit ? verticalShadowScaleHeightLimit : newDisplacement;
1114 // if total displacement is 0, shadow would become invisible.
1115 isVerticalShadowShown = false;
1119 private void PlayVerticalShadowAnimation()
1121 if (!EnableOverShootingEffect)
1124 if (ScrollingDirection != Direction.Vertical)
1127 // stop animation if necessary.
1128 StopVerticalShadowAnimation();
1130 if (verticalShadowAnimation == null)
1132 verticalShadowAnimation = new Animation(verticalShadowAnimationDuration);
1133 verticalShadowAnimation.Finished += OnVerticalShadowAnimationFinished;
1136 View targetView = totalDisplacementForPan < 0 ? verticalBottomShadowView : verticalTopShadowView;
1137 verticalShadowAnimation.AnimateTo(targetView, "SizeWidth", SizeWidth);
1138 verticalShadowAnimation.AnimateTo(targetView, "SizeHeight", 0.0f);
1139 verticalShadowAnimation.AnimateTo(targetView, "Opacity", 0.0f);
1140 verticalShadowAnimation.Play();
1143 private void StopVerticalShadowAnimation()
1145 if (verticalShadowAnimation == null || verticalShadowAnimation.State != Animation.States.Playing)
1148 verticalShadowAnimation.Stop(Animation.EndActions.Cancel);
1149 OnVerticalShadowAnimationFinished(null, null);
1150 verticalShadowAnimation.Clear();
1153 private void OnVerticalShadowAnimationFinished(object sender, EventArgs e)
1155 base.Remove(verticalTopShadowView);
1156 base.Remove(verticalBottomShadowView);
1158 verticalTopShadowView.Size = new Size(SizeWidth, 0.0f);
1159 verticalBottomShadowView.Size = new Size(SizeWidth, 0.0f);
1161 // after animation finished, height & opacity of vertical shadow both are 0, so it is invisible.
1162 isVerticalShadowShown = false;
1165 private void OnScrollOutOfBound(ScrollOutOfBoundEventArgs.Direction direction, float displacement)
1167 ScrollOutOfBoundEventArgs args = new ScrollOutOfBoundEventArgs(direction, displacement);
1168 ScrollOutOfBound?.Invoke(this, args);
1171 private void OnPanGestureDetected(object source, PanGestureDetector.DetectedEventArgs e)
1173 OnPanGesture(e.PanGesture);
1176 private void OnPanGesture(PanGesture panGesture)
1178 if (SnapToPage && scrollAnimation != null && scrollAnimation.State == Animation.States.Playing)
1183 if (panGesture.State == Gesture.StateType.Started)
1185 readyToNotice = false;
1186 base.Add(mInterruptTouchingChild);
1188 Debug.WriteLineIf(LayoutDebugScrollableBase, "Gesture Start");
1189 if (scrolling && !SnapToPage)
1193 totalDisplacementForPan = 0.0f;
1194 OnScrollDragStarted();
1196 else if (panGesture.State == Gesture.StateType.Continuing)
1198 if (ScrollingDirection == Direction.Horizontal)
1200 ScrollBy(panGesture.Displacement.X, false);
1201 totalDisplacementForPan += panGesture.Displacement.X;
1205 // if vertical shadow is shown, does not scroll.
1206 if (!isVerticalShadowShown)
1208 ScrollBy(panGesture.Displacement.Y, false);
1210 totalDisplacementForPan += panGesture.Displacement.Y;
1211 DragVerticalShadow(totalDisplacementForPan, panGesture.Displacement.Y);
1213 Debug.WriteLineIf(LayoutDebugScrollableBase, "OnPanGestureDetected Continue totalDisplacementForPan:" + totalDisplacementForPan);
1215 else if (panGesture.State == Gesture.StateType.Finished || panGesture.State == Gesture.StateType.Cancelled)
1217 PlayVerticalShadowAnimation();
1218 OnScrollDragEnded();
1219 StopScroll(); // Will replace previous animation so will stop existing one.
1221 if (scrollAnimation == null)
1223 scrollAnimation = new Animation();
1224 scrollAnimation.Finished += ScrollAnimationFinished;
1227 float panVelocity = (ScrollingDirection == Direction.Horizontal) ? panGesture.Velocity.X : panGesture.Velocity.Y;
1231 PageSnap(panVelocity);
1235 if (panVelocity == 0)
1237 float currentScrollPosition = (ScrollingDirection == Direction.Horizontal ? ContentContainer.CurrentPosition.X : ContentContainer.CurrentPosition.Y);
1238 scrollAnimation.DefaultAlphaFunction = new AlphaFunction(AlphaFunction.BuiltinFunctions.Linear);
1239 scrollAnimation.Duration = 0;
1240 scrollAnimation.AnimateTo(ContentContainer, (ScrollingDirection == Direction.Horizontal) ? "PositionX" : "PositionY", currentScrollPosition);
1241 scrollAnimation.Play();
1245 Decelerating(panVelocity, scrollAnimation);
1249 totalDisplacementForPan = 0;
1251 readyToNotice = true;
1252 OnScrollAnimationStarted();
1256 internal override bool OnAccessibilityPan(PanGesture gestures)
1258 if (SnapToPage && scrollAnimation != null && scrollAnimation.State == Animation.States.Playing)
1263 OnPanGesture(gestures);
1267 private float CustomScrollAlphaFunction(float progress)
1269 if (panAnimationDelta == 0)
1275 // Parameter "progress" is normalized value. We need to multiply target duration to calculate distance.
1276 // Can get real distance using equation of deceleration (check Decelerating function)
1277 // After get real distance, normalize it
1278 float realDuration = progress * panAnimationDuration;
1279 float realDistance = velocityOfLastPan * ((float)Math.Pow(decelerationRate, realDuration) - 1) / logValueOfDeceleration;
1280 float result = Math.Min(realDistance / Math.Abs(panAnimationDelta), 1.0f);
1286 /// you can override it to custom your decelerating
1288 /// <param name="velocity">Velocity of current pan.</param>
1289 /// <param name="animation">Scroll animation.</param>
1290 [EditorBrowsable(EditorBrowsableState.Never)]
1291 protected virtual void Decelerating(float velocity, Animation animation)
1293 // Decelerating using deceleration equation ===========
1295 // V : velocity (pixel per milisecond)
1296 // V0 : initial velocity
1297 // d : deceleration rate,
1299 // X : final position after decelerating
1300 // log : natural logarithm
1302 // V(t) = V0 * d pow t;
1303 // X(t) = V0 * (d pow t - 1) / log d; <-- Integrate the velocity function
1304 // X(∞) = V0 * d / (1 - d); <-- Result using inifit T can be final position because T is tending to infinity.
1306 // Because of final T is tending to inifity, we should use threshold value to finish.
1307 // Final T = log(-threshold * log d / |V0| ) / log d;
1309 velocityOfLastPan = Math.Abs(velocity);
1311 float currentScrollPosition = -(ScrollingDirection == Direction.Horizontal ? ContentContainer.CurrentPosition.X : ContentContainer.CurrentPosition.Y);
1312 panAnimationDelta = (velocityOfLastPan * decelerationRate) / (1 - decelerationRate);
1313 panAnimationDelta = velocity > 0 ? -panAnimationDelta : panAnimationDelta;
1315 float destination = -(panAnimationDelta + currentScrollPosition);
1316 float adjustDestination = AdjustTargetPositionOfScrollAnimation(destination);
1317 float maxPosition = ScrollAvailableArea != null ? ScrollAvailableArea.Y : maxScrollDistance;
1318 float minPosition = ScrollAvailableArea != null ? ScrollAvailableArea.X : 0;
1320 if (destination < -maxPosition || destination > minPosition)
1322 panAnimationDelta = velocity > 0 ? (currentScrollPosition - minPosition) : (maxPosition - currentScrollPosition);
1323 destination = velocity > 0 ? minPosition : -maxPosition;
1325 if (panAnimationDelta == 0)
1327 panAnimationDuration = 0.0f;
1331 panAnimationDuration = (float)Math.Log((panAnimationDelta * logValueOfDeceleration / velocityOfLastPan + 1), decelerationRate);
1334 Debug.WriteLineIf(LayoutDebugScrollableBase, "\n" +
1335 "OverRange======================= \n" +
1336 "[decelerationRate] " + decelerationRate + "\n" +
1337 "[logValueOfDeceleration] " + logValueOfDeceleration + "\n" +
1338 "[Velocity] " + velocityOfLastPan + "\n" +
1339 "[CurrentPosition] " + currentScrollPosition + "\n" +
1340 "[CandidateDelta] " + panAnimationDelta + "\n" +
1341 "[Destination] " + destination + "\n" +
1342 "[Duration] " + panAnimationDuration + "\n" +
1343 "================================ \n"
1348 panAnimationDuration = (float)Math.Log(-DecelerationThreshold * logValueOfDeceleration / velocityOfLastPan) / logValueOfDeceleration;
1350 if (adjustDestination != destination)
1352 destination = adjustDestination;
1353 panAnimationDelta = destination + currentScrollPosition;
1354 velocityOfLastPan = Math.Abs(panAnimationDelta * logValueOfDeceleration / ((float)Math.Pow(decelerationRate, panAnimationDuration) - 1));
1355 panAnimationDuration = (float)Math.Log(-DecelerationThreshold * logValueOfDeceleration / velocityOfLastPan) / logValueOfDeceleration;
1358 Debug.WriteLineIf(LayoutDebugScrollableBase, "\n" +
1359 "================================ \n" +
1360 "[decelerationRate] " + decelerationRate + "\n" +
1361 "[logValueOfDeceleration] " + logValueOfDeceleration + "\n" +
1362 "[Velocity] " + velocityOfLastPan + "\n" +
1363 "[CurrentPosition] " + currentScrollPosition + "\n" +
1364 "[CandidateDelta] " + panAnimationDelta + "\n" +
1365 "[Destination] " + destination + "\n" +
1366 "[Duration] " + panAnimationDuration + "\n" +
1367 "================================ \n"
1371 finalTargetPosition = destination;
1373 customScrollAlphaFunction = new UserAlphaFunctionDelegate(CustomScrollAlphaFunction);
1374 animation.DefaultAlphaFunction = new AlphaFunction(customScrollAlphaFunction);
1375 GC.KeepAlive(customScrollAlphaFunction);
1376 animation.Duration = (int)panAnimationDuration;
1377 animation.AnimateTo(ContentContainer, (ScrollingDirection == Direction.Horizontal) ? "PositionX" : "PositionY", destination);
1381 private void ScrollAnimationFinished(object sender, EventArgs e)
1383 OnScrollAnimationEnded();
1387 /// Adjust scrolling position by own scrolling rules.
1388 /// Override this function when developer wants to change destination of flicking.(e.g. always snap to center of item)
1390 /// This may be public opened in tizen_6.0 after ACR done. Before ACR, need to be hidden as inhouse API
1391 [EditorBrowsable(EditorBrowsableState.Never)]
1392 protected virtual float AdjustTargetPositionOfScrollAnimation(float position)
1398 /// Scroll position given to ScrollTo.
1399 /// This is the position in the opposite direction to the position of ContentContainer.
1401 /// <since_tizen> 8 </since_tizen>
1402 public Position ScrollPosition
1406 return new Position(-ContentContainer.Position);
1411 /// Current scroll position in the middle of ScrollTo animation.
1412 /// This is the position in the opposite direction to the current position of ContentContainer.
1414 /// <since_tizen> 8 </since_tizen>
1415 public Position ScrollCurrentPosition
1419 return new Position(-ContentContainer.CurrentPosition);
1424 /// Remove all children in ContentContainer.
1426 /// <param name="dispose">If true, removed child is disposed.</param>
1427 [EditorBrowsable(EditorBrowsableState.Never)]
1428 public void RemoveAllChildren(bool dispose = false)
1430 RecursiveRemoveChildren(ContentContainer, dispose);
1433 private void RecursiveRemoveChildren(View parent, bool dispose)
1439 int maxChild = (int)parent.GetChildCount();
1440 for (int i = maxChild - 1; i >= 0; --i)
1442 View child = parent.GetChildAt((uint)i);
1447 RecursiveRemoveChildren(child, dispose);
1448 parent.Remove(child);