568e82353a9482113f784300338e80e4ed4cce17
[platform/core/csapi/tizenfx.git] / src / Tizen.NUI.Components / Controls / ScrollableBase.cs
1 /* Copyright (c) 2020 Samsung Electronics Co., Ltd.
2  *
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
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
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.
14  *
15  */
16 using System;
17 using Tizen.NUI;
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;
24
25 namespace Tizen.NUI.Components
26 {
27     /// <summary>
28     /// ScrollEventArgs is a class to record scroll event arguments which will sent to user.
29     /// </summary>
30     /// <since_tizen> 8 </since_tizen>
31     public class ScrollEventArgs : EventArgs
32     {
33         private Position position;
34
35         /// <summary>
36         /// Default constructor.
37         /// </summary>
38         /// <param name="position">Current scroll position</param>
39         /// <since_tizen> 8 </since_tizen>
40         public ScrollEventArgs(Position position)
41         {
42             this.position = position;
43         }
44
45         /// <summary>
46         /// Current position of ContentContainer.
47         /// </summary>
48         /// <since_tizen> 8 </since_tizen>
49         public Position Position
50         {
51             get
52             {
53                 return position;
54             }
55         }
56     }
57
58     /// <summary>
59     /// ScrollOutofBoundEventArgs is to record scroll out-of-bound event arguments which will be sent to user.
60     /// </summary>
61     [EditorBrowsable(EditorBrowsableState.Never)]
62     public class ScrollOutOfBoundEventArgs : EventArgs
63     {
64         /// <summary>
65         /// The direction to be touched.
66         /// </summary>
67         [EditorBrowsable(EditorBrowsableState.Never)]
68         public enum Direction
69         {
70             /// <summary>
71             /// Upwards.
72             /// </summary>
73             [EditorBrowsable(EditorBrowsableState.Never)]
74             Up,
75
76             /// <summary>
77             /// Downwards.
78             /// </summary>
79             [EditorBrowsable(EditorBrowsableState.Never)]
80             Down,
81         }
82
83         /// <summary>
84         /// Constructor.
85         /// </summary>
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)
90         {
91             PanDirection = direction;
92             Displacement = displacement;
93         }
94
95         /// <summary>
96         /// Current pan direction of ContentContainer.
97         /// </summary>
98         [EditorBrowsable(EditorBrowsableState.Never)]
99         public Direction PanDirection
100         {
101             get;
102         }
103
104         /// <summary>
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.
108         /// </summary>
109         [EditorBrowsable(EditorBrowsableState.Never)]
110         public float Displacement
111         {
112             get;
113         }
114     }
115
116     /// <summary>
117     /// This class provides a View that can scroll a single View with a layout. This View can be a nest of Views.
118     /// </summary>
119     /// <since_tizen> 8 </since_tizen>
120     public class ScrollableBase : Control
121     {
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;
129
130         private class ScrollableBaseCustomLayout : AbsoluteLayout
131         {
132             protected override void OnMeasure(MeasureSpecification widthMeasureSpec, MeasureSpecification heightMeasureSpec)
133             {
134                 MeasuredSize.StateType childWidthState = MeasuredSize.StateType.MeasuredSizeOK;
135                 MeasuredSize.StateType childHeightState = MeasuredSize.StateType.MeasuredSizeOK;
136
137                 Direction scrollingDirection = Direction.Vertical;
138                 ScrollableBase scrollableBase = this.Owner as ScrollableBase;
139                 if (scrollableBase != null)
140                 {
141                     scrollingDirection = scrollableBase.ScrollingDirection;
142                 }
143
144                 float totalWidth = 0.0f;
145                 float totalHeight = 0.0f;
146
147                 // measure child, should be a single scrolling child
148                 foreach (LayoutItem childLayout in LayoutChildren)
149                 {
150                     if (childLayout != null && childLayout.Owner.Name == "ContentContainer")
151                     {
152                         // Get size of child
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)
156                         {
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
159                         }
160                         else
161                         {
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
164                         }
165
166                         totalWidth = (childLayout.MeasuredWidth.Size + (childLayout.Padding.Start + childLayout.Padding.End)).AsDecimal();
167                         totalHeight = (childLayout.MeasuredHeight.Size + (childLayout.Padding.Top + childLayout.Padding.Bottom)).AsDecimal();
168
169                         if (childLayout.MeasuredWidth.State == MeasuredSize.StateType.MeasuredSizeTooSmall)
170                         {
171                             childWidthState = MeasuredSize.StateType.MeasuredSizeTooSmall;
172                         }
173                         if (childLayout.MeasuredHeight.State == MeasuredSize.StateType.MeasuredSizeTooSmall)
174                         {
175                             childHeightState = MeasuredSize.StateType.MeasuredSizeTooSmall;
176                         }
177                     }
178                 }
179
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();
184
185                 // Ensure layout respects it's given minimum size
186                 totalWidth = Math.Max(totalWidth, SuggestedMinimumWidth.AsDecimal());
187                 totalHeight = Math.Max(totalHeight, SuggestedMinimumHeight.AsDecimal());
188
189                 widthSizeAndState.State = childWidthState;
190                 heightSizeAndState.State = childHeightState;
191
192                 SetMeasuredDimensions(ResolveSizeAndState(new LayoutLength(totalWidth), widthMeasureSpec, childWidthState),
193                     ResolveSizeAndState(new LayoutLength(totalHeight), heightMeasureSpec, childHeightState));
194
195                 // Size of ScrollableBase is changed. Change Page width too.
196                 if (scrollableBase != null)
197                 {
198                     scrollableBase.mPageWidth = (int)MeasuredWidth.Size.AsRoundedValue();
199                     scrollableBase.OnScrollingChildRelayout(null, null);
200                 }
201             }
202         } //  ScrollableBaseCustomLayout
203
204         /// <summary>
205         /// The direction axis to scroll.
206         /// </summary>
207         /// <since_tizen> 8 </since_tizen>
208         public enum Direction
209         {
210             /// <summary>
211             /// Horizontal axis.
212             /// </summary>
213             /// <since_tizen> 8 </since_tizen>
214             Horizontal,
215
216             /// <summary>
217             /// Vertical axis.
218             /// </summary>
219             /// <since_tizen> 8 </since_tizen>
220             Vertical
221         }
222
223         /// <summary>
224         /// Scrolling direction mode.
225         /// Default is Vertical scrolling.
226         /// </summary>
227         /// <since_tizen> 8 </since_tizen>
228         public Direction ScrollingDirection
229         {
230             get
231             {
232                 return mScrollingDirection;
233             }
234             set
235             {
236                 if (value != mScrollingDirection)
237                 {
238                     mScrollingDirection = value;
239                     mPanGestureDetector.ClearAngles();
240                     mPanGestureDetector.AddDirection(value == Direction.Horizontal ?
241                         PanGestureDetector.DirectionHorizontal : PanGestureDetector.DirectionVertical);
242
243                     ContentContainer.WidthSpecification = ScrollingDirection == Direction.Vertical ? LayoutParamPolicies.MatchParent : LayoutParamPolicies.WrapContent;
244                     ContentContainer.HeightSpecification = ScrollingDirection == Direction.Vertical ? LayoutParamPolicies.WrapContent : LayoutParamPolicies.MatchParent;
245                 }
246             }
247         }
248
249         /// <summary>
250         /// Enable or disable scrolling.
251         /// </summary>
252         /// <since_tizen> 8 </since_tizen>
253         public bool ScrollEnabled
254         {
255             get
256             {
257                 return mScrollEnabled;
258             }
259             set
260             {
261                 if (value != mScrollEnabled)
262                 {
263                     mScrollEnabled = value;
264                     if (mScrollEnabled)
265                     {
266                         mPanGestureDetector.Detected += OnPanGestureDetected;
267                     }
268                     else
269                     {
270                         mPanGestureDetector.Detected -= OnPanGestureDetected;
271                     }
272                 }
273             }
274         }
275
276         /// <summary>
277         /// Pages mode, enables moving to the next or return to current page depending on pan displacement.
278         /// Default is false.
279         /// </summary>
280         /// <since_tizen> 8 </since_tizen>
281         public bool SnapToPage { set; get; } = false;
282
283         /// <summary>
284         /// Get current page.
285         /// Working property with SnapToPage property.
286         /// </summary>
287         /// <since_tizen> 8 </since_tizen>
288         public int CurrentPage { get; private set; } = 0;
289
290         /// <summary>
291         /// Duration of scroll animation.
292         /// Default value is 125ms.
293         /// </summary>
294         /// <since_tizen> 8 </since_tizen>
295         public int ScrollDuration
296         {
297             set
298             {
299                 mScrollDuration = value >= 0 ? value : mScrollDuration;
300             }
301             get
302             {
303                 return mScrollDuration;
304             }
305         }
306
307         /// <summary>
308         /// Scroll Available area.
309         /// </summary>
310         /// <since_tizen> 8 </since_tizen>
311         public Vector2 ScrollAvailableArea { set; get; }
312
313         /// <summary>
314         /// An event emitted when user starts dragging ScrollableBase, user can subscribe or unsubscribe to this event handler.<br />
315         /// </summary>
316         /// <since_tizen> 8 </since_tizen>
317         public event EventHandler<ScrollEventArgs> ScrollDragStarted;
318
319         /// <summary>
320         /// An event emitted when user stops dragging ScrollableBase, user can subscribe or unsubscribe to this event handler.<br />
321         /// </summary>
322         /// <since_tizen> 8 </since_tizen>
323         public event EventHandler<ScrollEventArgs> ScrollDragEnded;
324
325         /// <summary>
326         /// An event emitted when the scrolling slide animation starts, user can subscribe or unsubscribe to this event handler.<br />
327         /// </summary>
328         /// <since_tizen> 8 </since_tizen>
329         public event EventHandler<ScrollEventArgs> ScrollAnimationStarted;
330
331         /// <summary>
332         /// An event emitted when the scrolling slide animation ends, user can subscribe or unsubscribe to this event handler.<br />
333         /// </summary>
334         /// <since_tizen> 8 </since_tizen>
335         public event EventHandler<ScrollEventArgs> ScrollAnimationEnded;
336
337         /// <summary>
338         /// An event emitted when scrolling, user can subscribe or unsubscribe to this event handler.<br />
339         /// </summary>
340         /// <since_tizen> 8 </since_tizen>
341         public event EventHandler<ScrollEventArgs> Scrolling;
342
343         /// <summary>
344         /// An event emitted when scrolling out of bound, user can subscribe or unsubscribe to this event handler.<br />
345         /// </summary>
346         [EditorBrowsable(EditorBrowsableState.Never)]
347         public event EventHandler<ScrollOutOfBoundEventArgs> ScrollOutOfBound;
348
349         /// <summary>
350         /// Scrollbar for ScrollableBase.
351         /// </summary>
352         /// <since_tizen> 8 </since_tizen>
353         public ScrollbarBase Scrollbar
354         {
355             get
356             {
357                 return scrollBar;
358             }
359             set
360             {
361                 if (scrollBar)
362                 {
363                     scrollBar.Unparent();
364                 }
365                 scrollBar = value;
366
367                 if (scrollBar != null)
368                 {
369                     scrollBar.Name = "ScrollBar";
370                     base.Add(scrollBar);
371
372                     if (hideScrollbar)
373                     {
374                         scrollBar.Hide();
375                     }
376                     else
377                     {
378                         scrollBar.Show();
379                     }
380
381                     SetScrollbar();
382                 }
383             }
384         }
385
386         /// <summary>
387         /// Always hide Scrollbar.
388         /// </summary>
389         /// <since_tizen> 8 </since_tizen>
390         public bool HideScrollbar
391         {
392             get
393             {
394                 return hideScrollbar;
395             }
396             set
397             {
398                 hideScrollbar = value;
399
400                 if (scrollBar)
401                 {
402                     if (value)
403                     {
404                         scrollBar.Hide();
405                     }
406                     else
407                     {
408                         scrollBar.Show();
409                     }
410                 }
411             }
412         }
413
414         /// <summary>
415         /// Container which has content of ScrollableBase.
416         /// </summary>
417         /// <since_tizen> 8 </since_tizen>
418         public View ContentContainer { get; private set; }
419
420         /// <summary>
421         /// Set the layout on this View. Replaces any existing Layout.
422         /// </summary>
423         /// <since_tizen> 8 </since_tizen>
424         public new LayoutItem Layout
425         {
426             get
427             {
428                 return ContentContainer.Layout;
429             }
430             set
431             {
432                 ContentContainer.Layout = value;
433             }
434         }
435
436         /// <summary>
437         /// List of children of Container.
438         /// </summary>
439         /// <since_tizen> 8 </since_tizen>
440         public new List<View> Children
441         {
442             get
443             {
444                 return ContentContainer.Children;
445             }
446         }
447
448         /// <summary>
449         /// Deceleration rate of scrolling by finger.
450         /// Rate should be bigger than 0 and smaller than 1.
451         /// Default value is 0.998f;
452         /// </summary>
453         /// <since_tizen> 8 </since_tizen>
454         public float DecelerationRate
455         {
456             get
457             {
458                 return decelerationRate;
459             }
460             set
461             {
462                 decelerationRate = (value < 1 && value > 0) ? value : decelerationRate;
463                 logValueOfDeceleration = (float)Math.Log(value);
464             }
465         }
466
467         /// <summary>
468         /// Threashold not to go infinit at the end of scrolling animation.
469         /// </summary>
470         [EditorBrowsable(EditorBrowsableState.Never)]
471         public float DecelerationThreshold { get; set; } = 0.1f;
472
473         /// <summary>
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.
478         /// </summary>
479         [EditorBrowsable(EditorBrowsableState.Never)]
480         public float ScrollingEventThreshold
481         {
482             get
483             {
484                 return mScrollingEventThreshold;
485             }
486             set
487             {
488                 if (mScrollingEventThreshold != value && value > 0)
489                 {
490                     ContentContainer.RemovePropertyNotification(propertyNotification);
491                     propertyNotification = ContentContainer.AddPropertyNotification("position", PropertyCondition.Step(value));
492                     propertyNotification.Notified += OnPropertyChanged;
493                     mScrollingEventThreshold = value;
494                 }
495             }
496         }
497
498         /// <summary>
499         /// Page will be changed when velocity of panning is over threshold.
500         /// The unit of threshold is pixel per milisec.
501         /// </summary>
502         /// <since_tizen> 8 </since_tizen>
503         public float PageFlickThreshold
504         {
505             get
506             {
507                 return mPageFlickThreshold;
508             }
509             set
510             {
511                 mPageFlickThreshold = value >= 0f ? value : mPageFlickThreshold;
512             }
513         }
514
515         /// <summary>
516         /// Padding for the ScrollableBase
517         /// </summary>
518         [EditorBrowsable(EditorBrowsableState.Never)]
519         public Extents Padding
520         {
521             get
522             {
523                 return ContentContainer.Padding;
524             }
525             set
526             {
527                 ContentContainer.Padding = value;
528             }
529         }
530
531         /// <summary>
532         /// Alphafunction for scroll animation.
533         /// </summary>
534         [EditorBrowsable(EditorBrowsableState.Never)]
535         public AlphaFunction ScrollAlphaFunction { get; set; } = new AlphaFunction(AlphaFunction.BuiltinFunctions.Linear);
536
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;
551
552         /// <summary>
553         /// Notice before animation is finished.
554         /// </summary>
555         [EditorBrowsable(EditorBrowsableState.Never)]
556         // Let's consider more whether this needs to be set as protected.
557         public float NoticeAnimationEndBeforePosition
558         {
559             get => noticeAnimationEndBeforePosition;
560             set => noticeAnimationEndBeforePosition = value;
561         }
562
563         // Let's consider more whether this needs to be set as protected.
564         private float finalTargetPosition;
565
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;
576
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;
584
585         /// <summary>
586         /// Default Constructor
587         /// </summary>
588         /// <since_tizen> 8 </since_tizen>
589         public ScrollableBase() : base()
590         {
591             DecelerationRate = 0.998f;
592
593             base.Layout = new ScrollableBaseCustomLayout();
594             mPanGestureDetector = new PanGestureDetector();
595             mPanGestureDetector.Attach(this);
596             mPanGestureDetector.AddDirection(PanGestureDetector.DirectionVertical);
597             mPanGestureDetector.Detected += OnPanGestureDetected;
598
599             ClippingMode = ClippingModeType.ClipToBoundingBox;
600
601             //Default Scrolling child
602             ContentContainer = new View()
603             {
604                 Name = "ContentContainer",
605                 WidthSpecification = ScrollingDirection == Direction.Vertical ? LayoutParamPolicies.MatchParent : LayoutParamPolicies.WrapContent,
606                 HeightSpecification = ScrollingDirection == Direction.Vertical ? LayoutParamPolicies.WrapContent : LayoutParamPolicies.MatchParent,
607             };
608             ContentContainer.Relayout += OnScrollingChildRelayout;
609             propertyNotification = ContentContainer.AddPropertyNotification("position", PropertyCondition.Step(mScrollingEventThreshold));
610             propertyNotification.Notified += OnPropertyChanged;
611             base.Add(ContentContainer);
612
613             //Interrupt touching when panning is started
614             mInterruptTouchingChild = new View()
615             {
616                 Size = new Size(Window.Instance.WindowSize),
617                 BackgroundColor = Color.Transparent,
618             };
619             mInterruptTouchingChild.TouchEvent += OnIterruptTouchingChildTouched;
620             Scrollbar = new Scrollbar();
621
622             //Show vertical shadow on the top (or bottom) of the scrollable when panning down (or up).
623             verticalTopShadowView = new View
624             {
625                 BackgroundImage = Tizen.NUI.FrameworkInformation.ResourcePath + "nui_component_default_scroll_over_shooting_top.png",
626                 Opacity = 1.0f,
627                 SizeHeight = 0.0f,
628                 PositionUsesPivotPoint = true,
629                 ParentOrigin = NUI.ParentOrigin.TopCenter,
630                 PivotPoint = NUI.PivotPoint.TopCenter,
631             };
632             verticalBottomShadowView = new View
633             {
634                 BackgroundImage = Tizen.NUI.FrameworkInformation.ResourcePath + "nui_component_default_scroll_over_shooting_bottom.png",
635                 Opacity = 1.0f,
636                 SizeHeight = 0.0f,
637                 PositionUsesPivotPoint = true,
638                 ParentOrigin = NUI.ParentOrigin.BottomCenter,
639                 PivotPoint = NUI.PivotPoint.BottomCenter,
640             };
641
642             AccessibilityManager.Instance.SetAccessibilityAttribute(this, AccessibilityManager.AccessibilityAttribute.Trait, "ScrollableBase");
643         }
644
645         private bool OnIterruptTouchingChildTouched(object source, View.TouchEventArgs args)
646         {
647             if (args.Touch.GetState(0) == PointStateType.Down)
648             {
649                 if (scrolling && !SnapToPage)
650                 {
651                     StopScroll();
652                 }
653             }
654             return true;
655         }
656
657         private void OnPropertyChanged(object source, PropertyNotification.NotifyEventArgs args)
658         {
659             OnScroll();
660         }
661
662         /// <summary>
663         /// Called after a child has been added to the owning view.
664         /// </summary>
665         /// <param name="view">The child which has been added.</param>
666         /// <since_tizen> 8 </since_tizen>
667         public override void Add(View view)
668         {
669             ContentContainer.Add(view);
670         }
671
672         /// <summary>
673         /// Called after a child has been removed from the owning view.
674         /// </summary>
675         /// <param name="view">The child which has been removed.</param>
676         /// <since_tizen> 8 </since_tizen>
677         public override void Remove(View view)
678         {
679             if (SnapToPage && CurrentPage == Children.IndexOf(view) && CurrentPage == Children.Count - 1)
680             {
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);
685             }
686
687             ContentContainer.Remove(view);
688         }
689
690         private void OnScrollingChildRelayout(object source, EventArgs args)
691         {
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;
695
696             if (isSizeChanged)
697             {
698                 maxScrollDistance = CalculateMaximumScrollDistance();
699                 SetScrollbar();
700             }
701
702             previousContainerSize = ContentContainer.Size;
703             previousSize = Size;
704         }
705
706         /// <summary>
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.
709         /// </summary>
710         /// <since_tizen> 8 </since_tizen>
711         [EditorBrowsable(EditorBrowsableState.Never)]
712         protected virtual void SetScrollbar()
713         {
714             if (Scrollbar)
715             {
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);
721             }
722         }
723
724         /// <summary>
725         /// Scrolls to the item at the specified index.
726         /// </summary>
727         /// <param name="index">Index of item.</param>
728         /// <since_tizen> 8 </since_tizen>
729         public void ScrollToIndex(int index)
730         {
731             if (ContentContainer.ChildCount - 1 < index || index < 0)
732             {
733                 return;
734             }
735
736             if (SnapToPage)
737             {
738                 CurrentPage = index;
739             }
740
741             float targetPosition = Math.Min(ScrollingDirection == Direction.Vertical ? Children[index].Position.Y : Children[index].Position.X, maxScrollDistance);
742             AnimateChildTo(ScrollDuration, -targetPosition);
743         }
744
745         private void OnScrollDragStarted()
746         {
747             ScrollEventArgs eventArgs = new ScrollEventArgs(ContentContainer.CurrentPosition);
748             ScrollDragStarted?.Invoke(this, eventArgs);
749         }
750
751         private void OnScrollDragEnded()
752         {
753             ScrollEventArgs eventArgs = new ScrollEventArgs(ContentContainer.CurrentPosition);
754             ScrollDragEnded?.Invoke(this, eventArgs);
755         }
756
757         private void OnScrollAnimationStarted()
758         {
759             ScrollEventArgs eventArgs = new ScrollEventArgs(ContentContainer.CurrentPosition);
760             ScrollAnimationStarted?.Invoke(this, eventArgs);
761         }
762
763         private void OnScrollAnimationEnded()
764         {
765             scrolling = false;
766             base.Remove(mInterruptTouchingChild);
767
768             ScrollEventArgs eventArgs = new ScrollEventArgs(ContentContainer.CurrentPosition);
769             ScrollAnimationEnded?.Invoke(this, eventArgs);
770         }
771
772         private void OnScroll()
773         {
774             ScrollEventArgs eventArgs = new ScrollEventArgs(ContentContainer.CurrentPosition);
775             Scrolling?.Invoke(this, eventArgs);
776
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;
780
781             scrollBar?.Update(contentLength, Math.Abs(currentPosition));
782             CheckPreReachedTargetPosition();
783         }
784
785         private void CheckPreReachedTargetPosition()
786         {
787             // Check whether we reached pre-reached target position
788             if (readyToNotice &&
789                 ContentContainer.CurrentPosition.Y <= finalTargetPosition + NoticeAnimationEndBeforePosition &&
790                 ContentContainer.CurrentPosition.Y >= finalTargetPosition - NoticeAnimationEndBeforePosition)
791             {
792                 //Notice first
793                 readyToNotice = false;
794                 OnPreReachedTargetPosition(finalTargetPosition);
795             }
796         }
797
798         /// <summary>
799         /// This helps developer who wants to know before scroll is reaching target position.
800         /// </summary>
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)
805         {
806
807         }
808
809         private void StopScroll()
810         {
811             if (scrollAnimation != null)
812             {
813                 if (scrollAnimation.State == Animation.States.Playing)
814                 {
815                     Debug.WriteLineIf(LayoutDebugScrollableBase, "StopScroll Animation Playing");
816                     scrollAnimation.Stop(Animation.EndActions.Cancel);
817                     OnScrollAnimationEnded();
818                 }
819                 scrollAnimation.Clear();
820             }
821         }
822
823         private void AnimateChildTo(int duration, float axisPosition)
824         {
825             Debug.WriteLineIf(LayoutDebugScrollableBase, "AnimationTo Animation Duration:" + duration + " Destination:" + axisPosition);
826             finalTargetPosition = axisPosition;
827
828             StopScroll(); // Will replace previous animation so will stop existing one.
829
830             if (scrollAnimation == null)
831             {
832                 scrollAnimation = new Animation();
833                 scrollAnimation.Finished += ScrollAnimationFinished;
834             }
835
836             scrollAnimation.Duration = duration;
837             scrollAnimation.DefaultAlphaFunction = new AlphaFunction(AlphaFunction.BuiltinFunctions.EaseOutSquare);
838             scrollAnimation.AnimateTo(ContentContainer, (ScrollingDirection == Direction.Horizontal) ? "PositionX" : "PositionY", axisPosition, ScrollAlphaFunction);
839             scrolling = true;
840             OnScrollAnimationStarted();
841             scrollAnimation.Play();
842         }
843
844         /// <summary>
845         /// Scroll to specific position with or without animation.
846         /// </summary>
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)
851         {
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;
858
859             ScrollBy(delta, animate);
860         }
861
862         private float BoundScrollPosition(float targetPosition)
863         {
864             if (ScrollAvailableArea != null)
865             {
866                 float minScrollPosition = ScrollAvailableArea.X;
867                 float maxScrollPosition = ScrollAvailableArea.Y;
868
869                 targetPosition = Math.Min(-minScrollPosition, targetPosition);
870                 targetPosition = Math.Max(-maxScrollPosition, targetPosition);
871             }
872             else
873             {
874                 targetPosition = Math.Min(0, targetPosition);
875                 targetPosition = Math.Max(-maxScrollDistance, targetPosition);
876             }
877
878             return targetPosition;
879         }
880
881         private void ScrollBy(float displacement, bool animate)
882         {
883             if (GetChildCount() == 0 || maxScrollDistance < 0)
884             {
885                 return;
886             }
887
888             float childCurrentPosition = (ScrollingDirection == Direction.Horizontal) ? ContentContainer.PositionX : ContentContainer.PositionY;
889
890             Debug.WriteLineIf(LayoutDebugScrollableBase, "ScrollBy childCurrentPosition:" + childCurrentPosition +
891                 " displacement:" + displacement,
892                 " maxScrollDistance:" + maxScrollDistance);
893
894             childTargetPosition = childCurrentPosition + displacement; // child current position + gesture displacement
895
896             Debug.WriteLineIf(LayoutDebugScrollableBase, "ScrollBy currentAxisPosition:" + childCurrentPosition + "childTargetPosition:" + childTargetPosition);
897
898             if (animate)
899             {
900                 // Calculate scroll animaton duration
901                 float scrollDistance = Math.Abs(displacement);
902                 readyToNotice = true;
903
904                 AnimateChildTo(ScrollDuration, BoundScrollPosition(AdjustTargetPositionOfScrollAnimation(BoundScrollPosition(childTargetPosition))));
905             }
906             else
907             {
908                 finalTargetPosition = BoundScrollPosition(childTargetPosition);
909
910                 // Set position of scrolling child without an animation
911                 if (ScrollingDirection == Direction.Horizontal)
912                 {
913                     ContentContainer.PositionX = finalTargetPosition;
914                 }
915                 else
916                 {
917                     ContentContainer.PositionY = finalTargetPosition;
918                 }
919             }
920         }
921
922         /// <summary>
923         /// you can override it to clean-up your own resources.
924         /// </summary>
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)
929         {
930             if (disposed)
931             {
932                 return;
933             }
934
935             if (type == DisposeTypes.Explicit)
936             {
937                 StopVerticalShadowAnimation();
938                 StopScroll();
939
940                 if (mPanGestureDetector != null)
941                 {
942                     mPanGestureDetector.Detected -= OnPanGestureDetected;
943                     mPanGestureDetector.Dispose();
944                     mPanGestureDetector = null;
945                 }
946
947                 propertyNotification.Dispose();
948             }
949             base.Dispose(type);
950         }
951
952         private float CalculateMaximumScrollDistance()
953         {
954             float scrollingChildLength = 0;
955             float scrollerLength = 0;
956             if (ScrollingDirection == Direction.Horizontal)
957             {
958                 Debug.WriteLineIf(LayoutDebugScrollableBase, "Horizontal");
959
960                 scrollingChildLength = ContentContainer.Size.Width;
961                 scrollerLength = Size.Width;
962             }
963             else
964             {
965                 Debug.WriteLineIf(LayoutDebugScrollableBase, "Vertical");
966                 scrollingChildLength = ContentContainer.Size.Height;
967                 scrollerLength = Size.Height;
968             }
969
970             Debug.WriteLineIf(LayoutDebugScrollableBase, "ScrollBy maxScrollDistance:" + (scrollingChildLength - scrollerLength) +
971                 " parent length:" + scrollerLength +
972                 " scrolling child length:" + scrollingChildLength);
973
974             return Math.Max(scrollingChildLength - scrollerLength, 0);
975         }
976
977         private void PageSnap(float velocity)
978         {
979             Debug.WriteLineIf(LayoutDebugScrollableBase, "PageSnap with pan candidate totalDisplacement:" + totalDisplacementForPan +
980                 " currentPage[" + CurrentPage + "]");
981
982             //Increment current page if total displacement enough to warrant a page change.
983             if (Math.Abs(totalDisplacementForPan) > (mPageWidth * ratioOfScreenWidthToCompleteScroll))
984             {
985                 if (totalDisplacementForPan < 0)
986                 {
987                     CurrentPage = Math.Min(Math.Max(Children.Count - 1, 0), ++CurrentPage);
988                 }
989                 else
990                 {
991                     CurrentPage = Math.Max(0, --CurrentPage);
992                 }
993             }
994             else if (Math.Abs(velocity) > PageFlickThreshold)
995             {
996                 if (velocity < 0)
997                 {
998                     CurrentPage = Math.Min(Math.Max(Children.Count - 1, 0), ++CurrentPage);
999                 }
1000                 else
1001                 {
1002                     CurrentPage = Math.Max(0, --CurrentPage);
1003                 }
1004             }
1005
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);
1010         }
1011
1012         /// <summary>
1013         /// Enable/Disable overshooting effect. default is disabled.
1014         /// </summary>
1015         [EditorBrowsable(EditorBrowsableState.Never)]
1016         public bool EnableOverShootingEffect { get; set; } = false;
1017
1018         private void AttachShadowView()
1019         {
1020             if (!EnableOverShootingEffect)
1021                 return;
1022
1023             if (ScrollingDirection != Direction.Vertical)
1024                 return;
1025
1026             // stop animation if necessary.
1027             StopVerticalShadowAnimation();
1028
1029             base.Add(verticalTopShadowView);
1030             base.Add(verticalBottomShadowView);
1031
1032             verticalTopShadowView.Size = new Size(SizeWidth, 0.0f);
1033             verticalTopShadowView.Opacity = 1.0f;
1034             verticalTopShadowView.RaiseToTop();
1035
1036             verticalBottomShadowView.Size = new Size(SizeWidth, 0.0f);
1037             verticalBottomShadowView.Opacity = 1.0f;
1038             verticalBottomShadowView.RaiseToTop();
1039
1040             // at the beginning, height of vertical shadow is 0, so it is invisible.
1041             isVerticalShadowShown = false;
1042         }
1043
1044         private void DragVerticalShadow(float totalPanDisplacement, float panDisplacement)
1045         {
1046             if (!EnableOverShootingEffect)
1047                 return;
1048
1049             if (ScrollingDirection != Direction.Vertical)
1050                 return;
1051
1052             if (totalPanDisplacement > 0) // downwards
1053             {
1054                 // check if reaching at the top.
1055                 if ((int)finalTargetPosition != 0)
1056                 {
1057                     isVerticalShadowShown = false;
1058                     return;
1059                 }
1060
1061                 // save start displacement, and re-calculate displacement.
1062                 if (!isVerticalShadowShown)
1063                 {
1064                     startShowShadowDisplacement = totalPanDisplacement;
1065                 }
1066                 isVerticalShadowShown = true;
1067
1068                 // trigger event
1069                 ScrollOutOfBoundEventArgs.Direction direction = panDisplacement > 0 ?
1070                     ScrollOutOfBoundEventArgs.Direction.Down : ScrollOutOfBoundEventArgs.Direction.Up;
1071                 OnScrollOutOfBound(direction, totalPanDisplacement);
1072
1073                 float newDisplacement = (int)totalPanDisplacement < (int)startShowShadowDisplacement ? 0 : totalPanDisplacement - startShowShadowDisplacement;
1074
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);
1078
1079                 // scale limit of height is 300%.
1080                 verticalTopShadowView.SizeHeight = newDisplacement > verticalShadowScaleHeightLimit ? verticalShadowScaleHeightLimit : newDisplacement;
1081             }
1082             else if (totalPanDisplacement < 0) // upwards
1083             {
1084                 // check if reaching at the bottom.
1085                 if (-(int)finalTargetPosition != (int)maxScrollDistance)
1086                 {
1087                     isVerticalShadowShown = false;
1088                     return;
1089                 }
1090
1091                 // save start displacement, and re-calculate displacement.
1092                 if (!isVerticalShadowShown)
1093                 {
1094                     startShowShadowDisplacement = totalPanDisplacement;
1095                 }
1096                 isVerticalShadowShown = true;
1097
1098                 // trigger event
1099                 ScrollOutOfBoundEventArgs.Direction direction = panDisplacement > 0 ?
1100                     ScrollOutOfBoundEventArgs.Direction.Down : ScrollOutOfBoundEventArgs.Direction.Up;
1101                 OnScrollOutOfBound(direction, totalPanDisplacement);
1102
1103                 float newDisplacement = (int)startShowShadowDisplacement < (int)totalPanDisplacement ? 0 : startShowShadowDisplacement - totalPanDisplacement;
1104
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);
1108
1109                 // scale limit of height is 300%.
1110                 verticalBottomShadowView.SizeHeight = newDisplacement > verticalShadowScaleHeightLimit ? verticalShadowScaleHeightLimit : newDisplacement;
1111             }
1112             else
1113             {
1114                 // if total displacement is 0, shadow would become invisible.
1115                 isVerticalShadowShown = false;
1116             }
1117         }
1118
1119         private void PlayVerticalShadowAnimation()
1120         {
1121             if (!EnableOverShootingEffect)
1122                 return;
1123
1124             if (ScrollingDirection != Direction.Vertical)
1125                 return;
1126
1127             // stop animation if necessary.
1128             StopVerticalShadowAnimation();
1129
1130             if (verticalShadowAnimation == null)
1131             {
1132                 verticalShadowAnimation = new Animation(verticalShadowAnimationDuration);
1133                 verticalShadowAnimation.Finished += OnVerticalShadowAnimationFinished;
1134             }
1135
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();
1141         }
1142
1143         private void StopVerticalShadowAnimation()
1144         {
1145             if (verticalShadowAnimation == null || verticalShadowAnimation.State != Animation.States.Playing)
1146                 return;
1147
1148             verticalShadowAnimation.Stop(Animation.EndActions.Cancel);
1149             OnVerticalShadowAnimationFinished(null, null);
1150             verticalShadowAnimation.Clear();
1151         }
1152
1153         private void OnVerticalShadowAnimationFinished(object sender, EventArgs e)
1154         {
1155             base.Remove(verticalTopShadowView);
1156             base.Remove(verticalBottomShadowView);
1157
1158             verticalTopShadowView.Size = new Size(SizeWidth, 0.0f);
1159             verticalBottomShadowView.Size = new Size(SizeWidth, 0.0f);
1160
1161             // after animation finished, height & opacity of vertical shadow both are 0, so it is invisible.
1162             isVerticalShadowShown = false;
1163         }
1164
1165         private void OnScrollOutOfBound(ScrollOutOfBoundEventArgs.Direction direction, float displacement)
1166         {
1167             ScrollOutOfBoundEventArgs args = new ScrollOutOfBoundEventArgs(direction, displacement);
1168             ScrollOutOfBound?.Invoke(this, args);
1169         }
1170
1171         private void OnPanGestureDetected(object source, PanGestureDetector.DetectedEventArgs e)
1172         {
1173             OnPanGesture(e.PanGesture);
1174         }
1175
1176         private void OnPanGesture(PanGesture panGesture)
1177         {
1178             if (SnapToPage && scrollAnimation != null && scrollAnimation.State == Animation.States.Playing)
1179             {
1180                 return;
1181             }
1182
1183             if (panGesture.State == Gesture.StateType.Started)
1184             {
1185                 readyToNotice = false;
1186                 base.Add(mInterruptTouchingChild);
1187                 AttachShadowView();
1188                 Debug.WriteLineIf(LayoutDebugScrollableBase, "Gesture Start");
1189                 if (scrolling && !SnapToPage)
1190                 {
1191                     StopScroll();
1192                 }
1193                 totalDisplacementForPan = 0.0f;
1194                 OnScrollDragStarted();
1195             }
1196             else if (panGesture.State == Gesture.StateType.Continuing)
1197             {
1198                 if (ScrollingDirection == Direction.Horizontal)
1199                 {
1200                     ScrollBy(panGesture.Displacement.X, false);
1201                     totalDisplacementForPan += panGesture.Displacement.X;
1202                 }
1203                 else
1204                 {
1205                     // if vertical shadow is shown, does not scroll.
1206                     if (!isVerticalShadowShown)
1207                     {
1208                         ScrollBy(panGesture.Displacement.Y, false);
1209                     }
1210                     totalDisplacementForPan += panGesture.Displacement.Y;
1211                     DragVerticalShadow(totalDisplacementForPan, panGesture.Displacement.Y);
1212                 }
1213                 Debug.WriteLineIf(LayoutDebugScrollableBase, "OnPanGestureDetected Continue totalDisplacementForPan:" + totalDisplacementForPan);
1214             }
1215             else if (panGesture.State == Gesture.StateType.Finished || panGesture.State == Gesture.StateType.Cancelled)
1216             {
1217                 PlayVerticalShadowAnimation();
1218                 OnScrollDragEnded();
1219                 StopScroll(); // Will replace previous animation so will stop existing one.
1220
1221                 if (scrollAnimation == null)
1222                 {
1223                     scrollAnimation = new Animation();
1224                     scrollAnimation.Finished += ScrollAnimationFinished;
1225                 }
1226
1227                 float panVelocity = (ScrollingDirection == Direction.Horizontal) ? panGesture.Velocity.X : panGesture.Velocity.Y;
1228
1229                 if (SnapToPage)
1230                 {
1231                     PageSnap(panVelocity);
1232                 }
1233                 else
1234                 {
1235                     if (panVelocity == 0)
1236                     {
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();
1242                     }
1243                     else
1244                     {
1245                         Decelerating(panVelocity, scrollAnimation);
1246                     }
1247                 }
1248
1249                 totalDisplacementForPan = 0;
1250                 scrolling = true;
1251                 readyToNotice = true;
1252                 OnScrollAnimationStarted();
1253             }
1254         }
1255
1256         internal override bool OnAccessibilityPan(PanGesture gestures)
1257         {
1258             if (SnapToPage && scrollAnimation != null && scrollAnimation.State == Animation.States.Playing)
1259             {
1260                 return false;
1261             }
1262
1263             OnPanGesture(gestures);
1264             return true;
1265         }
1266
1267         private float CustomScrollAlphaFunction(float progress)
1268         {
1269             if (panAnimationDelta == 0)
1270             {
1271                 return 1.0f;
1272             }
1273             else
1274             {
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);
1281                 return result;
1282             }
1283         }
1284
1285         /// <summary>
1286         /// you can override it to custom your decelerating
1287         /// </summary>
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)
1292         {
1293             // Decelerating using deceleration equation ===========
1294             //
1295             // V   : velocity (pixel per milisecond)
1296             // V0  : initial velocity
1297             // d   : deceleration rate,
1298             // t   : time
1299             // X   : final position after decelerating
1300             // log : natural logarithm
1301             //
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.
1305             //
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;
1308
1309             velocityOfLastPan = Math.Abs(velocity);
1310
1311             float currentScrollPosition = -(ScrollingDirection == Direction.Horizontal ? ContentContainer.CurrentPosition.X : ContentContainer.CurrentPosition.Y);
1312             panAnimationDelta = (velocityOfLastPan * decelerationRate) / (1 - decelerationRate);
1313             panAnimationDelta = velocity > 0 ? -panAnimationDelta : panAnimationDelta;
1314
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;
1319
1320             if (destination < -maxPosition || destination > minPosition)
1321             {
1322                 panAnimationDelta = velocity > 0 ? (currentScrollPosition - minPosition) : (maxPosition - currentScrollPosition);
1323                 destination = velocity > 0 ? minPosition : -maxPosition;
1324
1325                 if (panAnimationDelta == 0)
1326                 {
1327                     panAnimationDuration = 0.0f;
1328                 }
1329                 else
1330                 {
1331                     panAnimationDuration = (float)Math.Log((panAnimationDelta * logValueOfDeceleration / velocityOfLastPan + 1), decelerationRate);
1332                 }
1333
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"
1344                 );
1345             }
1346             else
1347             {
1348                 panAnimationDuration = (float)Math.Log(-DecelerationThreshold * logValueOfDeceleration / velocityOfLastPan) / logValueOfDeceleration;
1349
1350                 if (adjustDestination != destination)
1351                 {
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;
1356                 }
1357
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"
1368                 );
1369             }
1370
1371             finalTargetPosition = destination;
1372
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);
1378             animation.Play();
1379         }
1380
1381         private void ScrollAnimationFinished(object sender, EventArgs e)
1382         {
1383             OnScrollAnimationEnded();
1384         }
1385
1386         /// <summary>
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)
1389         /// </summary>
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)
1393         {
1394             return position;
1395         }
1396
1397         /// <summary>
1398         /// Scroll position given to ScrollTo.
1399         /// This is the position in the opposite direction to the position of ContentContainer.
1400         /// </summary>
1401         /// <since_tizen> 8 </since_tizen>
1402         public Position ScrollPosition
1403         {
1404             get
1405             {
1406                 return new Position(-ContentContainer.Position);
1407             }
1408         }
1409
1410         /// <summary>
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.
1413         /// </summary>
1414         /// <since_tizen> 8 </since_tizen>
1415         public Position ScrollCurrentPosition
1416         {
1417             get
1418             {
1419                 return new Position(-ContentContainer.CurrentPosition);
1420             }
1421         }
1422
1423         /// <summary>
1424         /// Remove all children in ContentContainer.
1425         /// </summary>
1426         /// <param name="dispose">If true, removed child is disposed.</param>
1427         [EditorBrowsable(EditorBrowsableState.Never)]
1428         public void RemoveAllChildren(bool dispose = false)
1429         {
1430             RecursiveRemoveChildren(ContentContainer, dispose);
1431         }
1432
1433         private void RecursiveRemoveChildren(View parent, bool dispose)
1434         {
1435             if (parent == null)
1436             {
1437                 return;
1438             }
1439             int maxChild = (int)parent.GetChildCount();
1440             for (int i = maxChild - 1; i >= 0; --i)
1441             {
1442                 View child = parent.GetChildAt((uint)i);
1443                 if (child == null)
1444                 {
1445                     continue;
1446                 }
1447                 RecursiveRemoveChildren(child, dispose);
1448                 parent.Remove(child);
1449                 if (dispose)
1450                 {
1451                     child.Dispose();
1452                 }
1453             }
1454         }
1455
1456     }
1457
1458 } // namespace