337b1f9f09705464d0c7e6bf24aa11bc1a1b44b5
[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             /// <summary>
83             /// Left bound.
84             /// </summary>
85             [EditorBrowsable(EditorBrowsableState.Never)]
86             Left,
87
88             /// <summary>
89             /// Right bound.
90             /// </summary>
91             [EditorBrowsable(EditorBrowsableState.Never)]
92             Right,
93         }
94
95         /// <summary>
96         /// Constructor.
97         /// </summary>
98         /// <param name="direction">Current pan direction</param>
99         /// <param name="displacement">Current total displacement</param>
100         [EditorBrowsable(EditorBrowsableState.Never)]
101         public ScrollOutOfBoundEventArgs(Direction direction, float displacement)
102         {
103             PanDirection = direction;
104             Displacement = displacement;
105         }
106
107         /// <summary>
108         /// Current pan direction of ContentContainer.
109         /// </summary>
110         [EditorBrowsable(EditorBrowsableState.Never)]
111         public Direction PanDirection
112         {
113             get;
114         }
115
116         /// <summary>
117         /// Current total displacement of ContentContainer.
118         /// if its value is greater than 0, it is at the top/left;
119         /// if less than 0, it is at the bottom/right.
120         /// </summary>
121         [EditorBrowsable(EditorBrowsableState.Never)]
122         public float Displacement
123         {
124             get;
125         }
126     }
127
128     /// <summary>
129     /// This class provides a View that can scroll a single View with a layout. This View can be a nest of Views.
130     /// </summary>
131     /// <since_tizen> 8 </since_tizen>
132     public class ScrollableBase : Control
133     {
134         static bool LayoutDebugScrollableBase = false; // Debug flag
135         private Direction mScrollingDirection = Direction.Vertical;
136         private bool mScrollEnabled = true;
137         private int mScrollDuration = 125;
138         private int mPageWidth = 0;
139         private float mPageFlickThreshold = 0.4f;
140         private float mScrollingEventThreshold = 0.001f;
141
142         private class ScrollableBaseCustomLayout : AbsoluteLayout
143         {
144             protected override void OnMeasure(MeasureSpecification widthMeasureSpec, MeasureSpecification heightMeasureSpec)
145             {
146                 MeasuredSize.StateType childWidthState = MeasuredSize.StateType.MeasuredSizeOK;
147                 MeasuredSize.StateType childHeightState = MeasuredSize.StateType.MeasuredSizeOK;
148
149                 Direction scrollingDirection = Direction.Vertical;
150                 ScrollableBase scrollableBase = this.Owner as ScrollableBase;
151                 if (scrollableBase != null)
152                 {
153                     scrollingDirection = scrollableBase.ScrollingDirection;
154                 }
155
156                 float totalWidth = 0.0f;
157                 float totalHeight = 0.0f;
158
159                 // measure child, should be a single scrolling child
160                 foreach (LayoutItem childLayout in LayoutChildren)
161                 {
162                     if (childLayout != null && childLayout.Owner.Name == "ContentContainer")
163                     {
164                         // Get size of child
165                         // Use an Unspecified MeasureSpecification mode so scrolling child is not restricted to it's parents size in Height (for vertical scrolling)
166                         // or Width for horizontal scrolling
167                         if (scrollingDirection == Direction.Vertical)
168                         {
169                             MeasureSpecification unrestrictedMeasureSpec = new MeasureSpecification(heightMeasureSpec.Size, MeasureSpecification.ModeType.Unspecified);
170                             MeasureChildWithMargins(childLayout, widthMeasureSpec, new LayoutLength(0), unrestrictedMeasureSpec, new LayoutLength(0)); // Height unrestricted by parent
171                         }
172                         else
173                         {
174                             MeasureSpecification unrestrictedMeasureSpec = new MeasureSpecification(widthMeasureSpec.Size, MeasureSpecification.ModeType.Unspecified);
175                             MeasureChildWithMargins(childLayout, unrestrictedMeasureSpec, new LayoutLength(0), heightMeasureSpec, new LayoutLength(0)); // Width unrestricted by parent
176                         }
177
178                         totalWidth = (childLayout.MeasuredWidth.Size + (childLayout.Padding.Start + childLayout.Padding.End)).AsDecimal();
179                         totalHeight = (childLayout.MeasuredHeight.Size + (childLayout.Padding.Top + childLayout.Padding.Bottom)).AsDecimal();
180
181                         if (childLayout.MeasuredWidth.State == MeasuredSize.StateType.MeasuredSizeTooSmall)
182                         {
183                             childWidthState = MeasuredSize.StateType.MeasuredSizeTooSmall;
184                         }
185                         if (childLayout.MeasuredHeight.State == MeasuredSize.StateType.MeasuredSizeTooSmall)
186                         {
187                             childHeightState = MeasuredSize.StateType.MeasuredSizeTooSmall;
188                         }
189                     }
190                 }
191
192                 MeasuredSize widthSizeAndState = ResolveSizeAndState(new LayoutLength(totalWidth), widthMeasureSpec, MeasuredSize.StateType.MeasuredSizeOK);
193                 MeasuredSize heightSizeAndState = ResolveSizeAndState(new LayoutLength(totalHeight), heightMeasureSpec, MeasuredSize.StateType.MeasuredSizeOK);
194                 totalWidth = widthSizeAndState.Size.AsDecimal();
195                 totalHeight = heightSizeAndState.Size.AsDecimal();
196
197                 // Ensure layout respects it's given minimum size
198                 totalWidth = Math.Max(totalWidth, SuggestedMinimumWidth.AsDecimal());
199                 totalHeight = Math.Max(totalHeight, SuggestedMinimumHeight.AsDecimal());
200
201                 widthSizeAndState.State = childWidthState;
202                 heightSizeAndState.State = childHeightState;
203
204                 SetMeasuredDimensions(ResolveSizeAndState(new LayoutLength(totalWidth), widthMeasureSpec, childWidthState),
205                     ResolveSizeAndState(new LayoutLength(totalHeight), heightMeasureSpec, childHeightState));
206
207                 // Size of ScrollableBase is changed. Change Page width too.
208                 if (scrollableBase != null)
209                 {
210                     scrollableBase.mPageWidth = (int)MeasuredWidth.Size.AsRoundedValue();
211                     scrollableBase.OnScrollingChildRelayout(null, null);
212                 }
213             }
214         } //  ScrollableBaseCustomLayout
215
216         /// <summary>
217         /// The direction axis to scroll.
218         /// </summary>
219         /// <since_tizen> 8 </since_tizen>
220         public enum Direction
221         {
222             /// <summary>
223             /// Horizontal axis.
224             /// </summary>
225             /// <since_tizen> 8 </since_tizen>
226             Horizontal,
227
228             /// <summary>
229             /// Vertical axis.
230             /// </summary>
231             /// <since_tizen> 8 </since_tizen>
232             Vertical
233         }
234
235         /// <summary>
236         /// Scrolling direction mode.
237         /// Default is Vertical scrolling.
238         /// </summary>
239         /// <since_tizen> 8 </since_tizen>
240         public Direction ScrollingDirection
241         {
242             get
243             {
244                 return mScrollingDirection;
245             }
246             set
247             {
248                 if (value != mScrollingDirection)
249                 {
250                     mScrollingDirection = value;
251                     mPanGestureDetector.ClearAngles();
252                     mPanGestureDetector.AddDirection(value == Direction.Horizontal ?
253                         PanGestureDetector.DirectionHorizontal : PanGestureDetector.DirectionVertical);
254
255                     ContentContainer.WidthSpecification = ScrollingDirection == Direction.Vertical ? LayoutParamPolicies.MatchParent : LayoutParamPolicies.WrapContent;
256                     ContentContainer.HeightSpecification = ScrollingDirection == Direction.Vertical ? LayoutParamPolicies.WrapContent : LayoutParamPolicies.MatchParent;
257                 }
258             }
259         }
260
261         /// <summary>
262         /// Enable or disable scrolling.
263         /// </summary>
264         /// <since_tizen> 8 </since_tizen>
265         public bool ScrollEnabled
266         {
267             get
268             {
269                 return mScrollEnabled;
270             }
271             set
272             {
273                 if (value != mScrollEnabled)
274                 {
275                     mScrollEnabled = value;
276                     if (mScrollEnabled)
277                     {
278                         mPanGestureDetector.Detected += OnPanGestureDetected;
279                     }
280                     else
281                     {
282                         mPanGestureDetector.Detected -= OnPanGestureDetected;
283                     }
284                 }
285             }
286         }
287
288         /// <summary>
289         /// Pages mode, enables moving to the next or return to current page depending on pan displacement.
290         /// Default is false.
291         /// </summary>
292         /// <since_tizen> 8 </since_tizen>
293         public bool SnapToPage { set; get; } = false;
294
295         /// <summary>
296         /// Get current page.
297         /// Working property with SnapToPage property.
298         /// </summary>
299         /// <since_tizen> 8 </since_tizen>
300         public int CurrentPage { get; private set; } = 0;
301
302         /// <summary>
303         /// Duration of scroll animation.
304         /// Default value is 125ms.
305         /// </summary>
306         /// <since_tizen> 8 </since_tizen>
307         public int ScrollDuration
308         {
309             set
310             {
311                 mScrollDuration = value >= 0 ? value : mScrollDuration;
312             }
313             get
314             {
315                 return mScrollDuration;
316             }
317         }
318
319         /// <summary>
320         /// Scroll Available area.
321         /// </summary>
322         /// <since_tizen> 8 </since_tizen>
323         public Vector2 ScrollAvailableArea { set; get; }
324
325         /// <summary>
326         /// An event emitted when user starts dragging ScrollableBase, user can subscribe or unsubscribe to this event handler.<br />
327         /// </summary>
328         /// <since_tizen> 8 </since_tizen>
329         public event EventHandler<ScrollEventArgs> ScrollDragStarted;
330
331         /// <summary>
332         /// An event emitted when user stops dragging ScrollableBase, user can subscribe or unsubscribe to this event handler.<br />
333         /// </summary>
334         /// <since_tizen> 8 </since_tizen>
335         public event EventHandler<ScrollEventArgs> ScrollDragEnded;
336
337         /// <summary>
338         /// An event emitted when the scrolling slide animation starts, user can subscribe or unsubscribe to this event handler.<br />
339         /// </summary>
340         /// <since_tizen> 8 </since_tizen>
341         public event EventHandler<ScrollEventArgs> ScrollAnimationStarted;
342
343         /// <summary>
344         /// An event emitted when the scrolling slide animation ends, user can subscribe or unsubscribe to this event handler.<br />
345         /// </summary>
346         /// <since_tizen> 8 </since_tizen>
347         public event EventHandler<ScrollEventArgs> ScrollAnimationEnded;
348
349         /// <summary>
350         /// An event emitted when scrolling, user can subscribe or unsubscribe to this event handler.<br />
351         /// </summary>
352         /// <since_tizen> 8 </since_tizen>
353         public event EventHandler<ScrollEventArgs> Scrolling;
354
355         /// <summary>
356         /// An event emitted when scrolling out of bound, user can subscribe or unsubscribe to this event handler.<br />
357         /// </summary>
358         [EditorBrowsable(EditorBrowsableState.Never)]
359         public event EventHandler<ScrollOutOfBoundEventArgs> ScrollOutOfBound;
360
361         /// <summary>
362         /// Scrollbar for ScrollableBase.
363         /// </summary>
364         /// <since_tizen> 8 </since_tizen>
365         public ScrollbarBase Scrollbar
366         {
367             get
368             {
369                 return scrollBar;
370             }
371             set
372             {
373                 if (scrollBar)
374                 {
375                     base.Remove(scrollBar);
376                 }
377                 scrollBar = value;
378
379                 if (scrollBar != null)
380                 {
381                     scrollBar.Name = "ScrollBar";
382                     base.Add(scrollBar);
383
384                     if (hideScrollbar)
385                     {
386                         scrollBar.Hide();
387                     }
388                     else
389                     {
390                         scrollBar.Show();
391                     }
392
393                     SetScrollbar();
394                 }
395             }
396         }
397
398         /// <summary>
399         /// Always hide Scrollbar.
400         /// </summary>
401         /// <since_tizen> 8 </since_tizen>
402         public bool HideScrollbar
403         {
404             get
405             {
406                 return hideScrollbar;
407             }
408             set
409             {
410                 hideScrollbar = value;
411
412                 if (scrollBar)
413                 {
414                     if (value)
415                     {
416                         scrollBar.Hide();
417                     }
418                     else
419                     {
420                         scrollBar.Show();
421                     }
422                 }
423             }
424         }
425
426         /// <summary>
427         /// Container which has content of ScrollableBase.
428         /// </summary>
429         /// <since_tizen> 8 </since_tizen>
430         public View ContentContainer { get; private set; }
431
432         /// <summary>
433         /// Set the layout on this View. Replaces any existing Layout.
434         /// </summary>
435         /// <since_tizen> 8 </since_tizen>
436         public new LayoutItem Layout
437         {
438             get
439             {
440                 return ContentContainer.Layout;
441             }
442             set
443             {
444                 ContentContainer.Layout = value;
445             }
446         }
447
448         /// <summary>
449         /// List of children of Container.
450         /// </summary>
451         /// <since_tizen> 8 </since_tizen>
452         public new List<View> Children
453         {
454             get
455             {
456                 return ContentContainer.Children;
457             }
458         }
459
460         /// <summary>
461         /// Deceleration rate of scrolling by finger.
462         /// Rate should be bigger than 0 and smaller than 1.
463         /// Default value is 0.998f;
464         /// </summary>
465         /// <since_tizen> 8 </since_tizen>
466         public float DecelerationRate
467         {
468             get
469             {
470                 return decelerationRate;
471             }
472             set
473             {
474                 decelerationRate = (value < 1 && value > 0) ? value : decelerationRate;
475                 logValueOfDeceleration = (float)Math.Log(value);
476             }
477         }
478
479         /// <summary>
480         /// Threashold not to go infinit at the end of scrolling animation.
481         /// </summary>
482         [EditorBrowsable(EditorBrowsableState.Never)]
483         public float DecelerationThreshold { get; set; } = 0.1f;
484
485         /// <summary>
486         /// Scrolling event will be thrown when this amount of scroll position is changed.
487         /// If this threshold becomes smaller, the tracking detail increases but the scrolling range that can be tracked becomes smaller.
488         /// If large sized ContentContainer is required, please use larger threshold value.
489         /// Default ScrollingEventThreshold value is 0.001f.
490         /// </summary>
491         [EditorBrowsable(EditorBrowsableState.Never)]
492         public float ScrollingEventThreshold
493         {
494             get
495             {
496                 return mScrollingEventThreshold;
497             }
498             set
499             {
500                 if (mScrollingEventThreshold != value && value > 0)
501                 {
502                     ContentContainer.RemovePropertyNotification(propertyNotification);
503                     propertyNotification = ContentContainer.AddPropertyNotification("position", PropertyCondition.Step(value));
504                     propertyNotification.Notified += OnPropertyChanged;
505                     mScrollingEventThreshold = value;
506                 }
507             }
508         }
509
510         /// <summary>
511         /// Page will be changed when velocity of panning is over threshold.
512         /// The unit of threshold is pixel per milisec.
513         /// </summary>
514         /// <since_tizen> 8 </since_tizen>
515         public float PageFlickThreshold
516         {
517             get
518             {
519                 return mPageFlickThreshold;
520             }
521             set
522             {
523                 mPageFlickThreshold = value >= 0f ? value : mPageFlickThreshold;
524             }
525         }
526
527         /// <summary>
528         /// Padding for the ScrollableBase
529         /// </summary>
530         [EditorBrowsable(EditorBrowsableState.Never)]
531         public Extents Padding
532         {
533             get
534             {
535                 return ContentContainer.Padding;
536             }
537             set
538             {
539                 ContentContainer.Padding = value;
540             }
541         }
542
543         /// <summary>
544         /// Alphafunction for scroll animation.
545         /// </summary>
546         [EditorBrowsable(EditorBrowsableState.Never)]
547         public AlphaFunction ScrollAlphaFunction { get; set; } = new AlphaFunction(AlphaFunction.BuiltinFunctions.Linear);
548
549         private bool hideScrollbar = true;
550         private float maxScrollDistance;
551         private float childTargetPosition = 0.0f;
552         private PanGestureDetector mPanGestureDetector;
553         private View mInterruptTouchingChild;
554         private ScrollbarBase scrollBar;
555         private bool scrolling = false;
556         private float ratioOfScreenWidthToCompleteScroll = 0.5f;
557         private float totalDisplacementForPan = 0.0f;
558         private Size previousContainerSize = new Size();
559         private Size previousSize = new Size();
560         private PropertyNotification propertyNotification;
561         private float noticeAnimationEndBeforePosition = 0.0f;
562         private bool readyToNotice = false;
563
564         /// <summary>
565         /// Notice before animation is finished.
566         /// </summary>
567         [EditorBrowsable(EditorBrowsableState.Never)]
568         // Let's consider more whether this needs to be set as protected.
569         public float NoticeAnimationEndBeforePosition
570         {
571             get => noticeAnimationEndBeforePosition;
572             set => noticeAnimationEndBeforePosition = value;
573         }
574
575         // Let's consider more whether this needs to be set as protected.
576         private float finalTargetPosition;
577
578         private Animation scrollAnimation;
579         // Declare user alpha function delegate
580         [UnmanagedFunctionPointer(CallingConvention.StdCall)]
581         private delegate float UserAlphaFunctionDelegate(float progress);
582         private UserAlphaFunctionDelegate customScrollAlphaFunction;
583         private float velocityOfLastPan = 0.0f;
584         private float panAnimationDuration = 0.0f;
585         private float panAnimationDelta = 0.0f;
586         private float logValueOfDeceleration = 0.0f;
587         private float decelerationRate = 0.0f;
588
589         private View topOverShootingShadowView;
590         private View bottomOverShootingShadowView;
591         private View leftOverShootingShadowView;
592         private View rightOverShootingShadowView;
593         private const int overShootingShadowScaleHeightLimit = 64 * 3;
594         private const int overShootingShadowAnimationDuration = 300;
595         private Animation overShootingShadowAnimation;
596         private bool isOverShootingShadowShown = false;
597         private float startShowShadowDisplacement;
598
599         /// <summary>
600         /// Default Constructor
601         /// </summary>
602         /// <since_tizen> 8 </since_tizen>
603         public ScrollableBase() : base()
604         {
605             DecelerationRate = 0.998f;
606
607             base.Layout = new ScrollableBaseCustomLayout();
608             mPanGestureDetector = new PanGestureDetector();
609             mPanGestureDetector.Attach(this);
610             mPanGestureDetector.AddDirection(PanGestureDetector.DirectionVertical);
611             mPanGestureDetector.Detected += OnPanGestureDetected;
612
613             ClippingMode = ClippingModeType.ClipToBoundingBox;
614
615             //Default Scrolling child
616             ContentContainer = new View()
617             {
618                 Name = "ContentContainer",
619                 WidthSpecification = ScrollingDirection == Direction.Vertical ? LayoutParamPolicies.MatchParent : LayoutParamPolicies.WrapContent,
620                 HeightSpecification = ScrollingDirection == Direction.Vertical ? LayoutParamPolicies.WrapContent : LayoutParamPolicies.MatchParent,
621             };
622             ContentContainer.Relayout += OnScrollingChildRelayout;
623             propertyNotification = ContentContainer.AddPropertyNotification("position", PropertyCondition.Step(mScrollingEventThreshold));
624             propertyNotification.Notified += OnPropertyChanged;
625             base.Add(ContentContainer);
626
627             //Interrupt touching when panning is started
628             mInterruptTouchingChild = new View()
629             {
630                 Size = new Size(Window.Instance.WindowSize),
631                 BackgroundColor = Color.Transparent,
632             };
633             mInterruptTouchingChild.TouchEvent += OnIterruptTouchingChildTouched;
634             Scrollbar = new Scrollbar();
635
636             //Show vertical shadow on the top (or bottom) of the scrollable when panning down (or up).
637             topOverShootingShadowView = new View
638             {
639                 BackgroundImage = FrameworkInformation.ResourcePath + "nui_component_default_scroll_over_shooting_top.png",
640                 Opacity = 1.0f,
641                 SizeHeight = 0.0f,
642                 PositionUsesPivotPoint = true,
643                 ParentOrigin = NUI.ParentOrigin.TopCenter,
644                 PivotPoint = NUI.PivotPoint.TopCenter,
645             };
646             bottomOverShootingShadowView = new View
647             {
648                 BackgroundImage = FrameworkInformation.ResourcePath + "nui_component_default_scroll_over_shooting_bottom.png",
649                 Opacity = 1.0f,
650                 SizeHeight = 0.0f,
651                 PositionUsesPivotPoint = true,
652                 ParentOrigin = NUI.ParentOrigin.BottomCenter,
653                 PivotPoint = NUI.PivotPoint.BottomCenter,
654             };
655             //Show horizontal shadow on the left (or right) of the scrollable when panning down (or up).
656             leftOverShootingShadowView = new View
657             {
658                 BackgroundImage = FrameworkInformation.ResourcePath + "nui_component_default_scroll_over_shooting_left.png",
659                 Opacity = 1.0f,
660                 SizeWidth = 0.0f,
661                 PositionUsesPivotPoint = true,
662                 ParentOrigin = NUI.ParentOrigin.CenterLeft,
663                 PivotPoint = NUI.PivotPoint.CenterLeft,
664             };
665             rightOverShootingShadowView = new View
666             {
667                 BackgroundImage = FrameworkInformation.ResourcePath + "nui_component_default_scroll_over_shooting_right.png",
668                 Opacity = 1.0f,
669                 SizeWidth = 0.0f,
670                 PositionUsesPivotPoint = true,
671                 ParentOrigin = NUI.ParentOrigin.CenterRight,
672                 PivotPoint = NUI.PivotPoint.CenterRight,
673             };
674
675             AccessibilityManager.Instance.SetAccessibilityAttribute(this, AccessibilityManager.AccessibilityAttribute.Trait, "ScrollableBase");
676         }
677
678         private bool OnIterruptTouchingChildTouched(object source, View.TouchEventArgs args)
679         {
680             if (args.Touch.GetState(0) == PointStateType.Down)
681             {
682                 if (scrolling && !SnapToPage)
683                 {
684                     StopScroll();
685                 }
686             }
687             return true;
688         }
689
690         private void OnPropertyChanged(object source, PropertyNotification.NotifyEventArgs args)
691         {
692             OnScroll();
693         }
694
695         /// <summary>
696         /// Called after a child has been added to the owning view.
697         /// </summary>
698         /// <param name="view">The child which has been added.</param>
699         /// <since_tizen> 8 </since_tizen>
700         public override void Add(View view)
701         {
702             ContentContainer.Add(view);
703         }
704
705         /// <summary>
706         /// Called after a child has been removed from the owning view.
707         /// </summary>
708         /// <param name="view">The child which has been removed.</param>
709         /// <since_tizen> 8 </since_tizen>
710         public override void Remove(View view)
711         {
712             if (SnapToPage && CurrentPage == Children.IndexOf(view) && CurrentPage == Children.Count - 1)
713             {
714                 // Target View is current page and also last child.
715                 // CurrentPage should be changed to previous page.
716                 CurrentPage = Math.Max(0, CurrentPage - 1);
717                 ScrollToIndex(CurrentPage);
718             }
719
720             ContentContainer.Remove(view);
721         }
722
723         private void OnScrollingChildRelayout(object source, EventArgs args)
724         {
725             // Size is changed. Calculate maxScrollDistance.
726             bool isSizeChanged = previousContainerSize.Width != ContentContainer.Size.Width || previousContainerSize.Height != ContentContainer.Size.Height ||
727                 previousSize.Width != Size.Width || previousSize.Height != Size.Height;
728
729             if (isSizeChanged)
730             {
731                 maxScrollDistance = CalculateMaximumScrollDistance();
732                 SetScrollbar();
733             }
734
735             previousContainerSize = ContentContainer.Size;
736             previousSize = Size;
737         }
738
739         /// <summary>
740         /// The composition of a Scrollbar can vary depending on how you use ScrollableBase.
741         /// Set the composition that will go into the ScrollableBase according to your ScrollableBase.
742         /// </summary>
743         /// <since_tizen> 8 </since_tizen>
744         [EditorBrowsable(EditorBrowsableState.Never)]
745         protected virtual void SetScrollbar()
746         {
747             if (Scrollbar)
748             {
749                 bool isHorizontal = ScrollingDirection == Direction.Horizontal;
750                 float contentLength = isHorizontal ? ContentContainer.Size.Width : ContentContainer.Size.Height;
751                 float viewportLength = isHorizontal ? Size.Width : Size.Height;
752                 float currentPosition = isHorizontal ? ContentContainer.CurrentPosition.X : ContentContainer.CurrentPosition.Y;
753                 Scrollbar.Initialize(contentLength, viewportLength, currentPosition, isHorizontal);
754             }
755         }
756
757         /// <summary>
758         /// Scrolls to the item at the specified index.
759         /// </summary>
760         /// <param name="index">Index of item.</param>
761         /// <since_tizen> 8 </since_tizen>
762         public void ScrollToIndex(int index)
763         {
764             if (ContentContainer.ChildCount - 1 < index || index < 0)
765             {
766                 return;
767             }
768
769             if (SnapToPage)
770             {
771                 CurrentPage = index;
772             }
773
774             float targetPosition = Math.Min(ScrollingDirection == Direction.Vertical ? Children[index].Position.Y : Children[index].Position.X, maxScrollDistance);
775             AnimateChildTo(ScrollDuration, -targetPosition);
776         }
777
778         private void OnScrollDragStarted()
779         {
780             ScrollEventArgs eventArgs = new ScrollEventArgs(ContentContainer.CurrentPosition);
781             ScrollDragStarted?.Invoke(this, eventArgs);
782         }
783
784         private void OnScrollDragEnded()
785         {
786             ScrollEventArgs eventArgs = new ScrollEventArgs(ContentContainer.CurrentPosition);
787             ScrollDragEnded?.Invoke(this, eventArgs);
788         }
789
790         private void OnScrollAnimationStarted()
791         {
792             ScrollEventArgs eventArgs = new ScrollEventArgs(ContentContainer.CurrentPosition);
793             ScrollAnimationStarted?.Invoke(this, eventArgs);
794         }
795
796         private void OnScrollAnimationEnded()
797         {
798             scrolling = false;
799             base.Remove(mInterruptTouchingChild);
800
801             ScrollEventArgs eventArgs = new ScrollEventArgs(ContentContainer.CurrentPosition);
802             ScrollAnimationEnded?.Invoke(this, eventArgs);
803         }
804
805         private void OnScroll()
806         {
807             ScrollEventArgs eventArgs = new ScrollEventArgs(ContentContainer.CurrentPosition);
808             Scrolling?.Invoke(this, eventArgs);
809
810             bool isHorizontal = ScrollingDirection == Direction.Horizontal;
811             float contentLength = isHorizontal ? ContentContainer.Size.Width : ContentContainer.Size.Height;
812             float currentPosition = isHorizontal ? ContentContainer.CurrentPosition.X : ContentContainer.CurrentPosition.Y;
813
814             scrollBar?.Update(contentLength, Math.Abs(currentPosition));
815             CheckPreReachedTargetPosition();
816         }
817
818         private void CheckPreReachedTargetPosition()
819         {
820             // Check whether we reached pre-reached target position
821             if (readyToNotice &&
822                 ContentContainer.CurrentPosition.Y <= finalTargetPosition + NoticeAnimationEndBeforePosition &&
823                 ContentContainer.CurrentPosition.Y >= finalTargetPosition - NoticeAnimationEndBeforePosition)
824             {
825                 //Notice first
826                 readyToNotice = false;
827                 OnPreReachedTargetPosition(finalTargetPosition);
828             }
829         }
830
831         /// <summary>
832         /// This helps developer who wants to know before scroll is reaching target position.
833         /// </summary>
834         /// <param name="targetPosition">Index of item.</param>
835         /// This may be public opened in tizen_6.0 after ACR done. Before ACR, need to be hidden as inhouse API
836         [EditorBrowsable(EditorBrowsableState.Never)]
837         protected virtual void OnPreReachedTargetPosition(float targetPosition)
838         {
839
840         }
841
842         private void StopScroll()
843         {
844             if (scrollAnimation != null)
845             {
846                 if (scrollAnimation.State == Animation.States.Playing)
847                 {
848                     Debug.WriteLineIf(LayoutDebugScrollableBase, "StopScroll Animation Playing");
849                     scrollAnimation.Stop(Animation.EndActions.Cancel);
850                     OnScrollAnimationEnded();
851                 }
852                 scrollAnimation.Clear();
853             }
854         }
855
856         private void AnimateChildTo(int duration, float axisPosition)
857         {
858             Debug.WriteLineIf(LayoutDebugScrollableBase, "AnimationTo Animation Duration:" + duration + " Destination:" + axisPosition);
859             finalTargetPosition = axisPosition;
860
861             StopScroll(); // Will replace previous animation so will stop existing one.
862
863             if (scrollAnimation == null)
864             {
865                 scrollAnimation = new Animation();
866                 scrollAnimation.Finished += ScrollAnimationFinished;
867             }
868
869             scrollAnimation.Duration = duration;
870             scrollAnimation.DefaultAlphaFunction = new AlphaFunction(AlphaFunction.BuiltinFunctions.EaseOutSquare);
871             scrollAnimation.AnimateTo(ContentContainer, (ScrollingDirection == Direction.Horizontal) ? "PositionX" : "PositionY", axisPosition, ScrollAlphaFunction);
872             scrolling = true;
873             OnScrollAnimationStarted();
874             scrollAnimation.Play();
875         }
876
877         /// <summary>
878         /// Scroll to specific position with or without animation.
879         /// </summary>
880         /// <param name="position">Destination.</param>
881         /// <param name="animate">Scroll with or without animation</param>
882         /// <since_tizen> 8 </since_tizen>
883         public void ScrollTo(float position, bool animate)
884         {
885             float currentPositionX = ContentContainer.CurrentPosition.X != 0 ? ContentContainer.CurrentPosition.X : ContentContainer.Position.X;
886             float currentPositionY = ContentContainer.CurrentPosition.Y != 0 ? ContentContainer.CurrentPosition.Y : ContentContainer.Position.Y;
887             float delta = ScrollingDirection == Direction.Horizontal ? currentPositionX : currentPositionY;
888             // The argument position is the new pan position. So the new position of ScrollableBase becomes (-position).
889             // To move ScrollableBase's position to (-position), it moves by (-position - currentPosition).
890             delta = -position - delta;
891
892             ScrollBy(delta, animate);
893         }
894
895         private float BoundScrollPosition(float targetPosition)
896         {
897             if (ScrollAvailableArea != null)
898             {
899                 float minScrollPosition = ScrollAvailableArea.X;
900                 float maxScrollPosition = ScrollAvailableArea.Y;
901
902                 targetPosition = Math.Min(-minScrollPosition, targetPosition);
903                 targetPosition = Math.Max(-maxScrollPosition, targetPosition);
904             }
905             else
906             {
907                 targetPosition = Math.Min(0, targetPosition);
908                 targetPosition = Math.Max(-maxScrollDistance, targetPosition);
909             }
910
911             return targetPosition;
912         }
913
914         private void ScrollBy(float displacement, bool animate)
915         {
916             if (GetChildCount() == 0 || maxScrollDistance < 0)
917             {
918                 return;
919             }
920
921             float childCurrentPosition = (ScrollingDirection == Direction.Horizontal) ? ContentContainer.PositionX : ContentContainer.PositionY;
922
923             Debug.WriteLineIf(LayoutDebugScrollableBase, "ScrollBy childCurrentPosition:" + childCurrentPosition +
924                 " displacement:" + displacement,
925                 " maxScrollDistance:" + maxScrollDistance);
926
927             childTargetPosition = childCurrentPosition + displacement; // child current position + gesture displacement
928
929             Debug.WriteLineIf(LayoutDebugScrollableBase, "ScrollBy currentAxisPosition:" + childCurrentPosition + "childTargetPosition:" + childTargetPosition);
930
931             if (animate)
932             {
933                 // Calculate scroll animaton duration
934                 float scrollDistance = Math.Abs(displacement);
935                 readyToNotice = true;
936
937                 AnimateChildTo(ScrollDuration, BoundScrollPosition(AdjustTargetPositionOfScrollAnimation(BoundScrollPosition(childTargetPosition))));
938             }
939             else
940             {
941                 finalTargetPosition = BoundScrollPosition(childTargetPosition);
942
943                 // Set position of scrolling child without an animation
944                 if (ScrollingDirection == Direction.Horizontal)
945                 {
946                     ContentContainer.PositionX = finalTargetPosition;
947                 }
948                 else
949                 {
950                     ContentContainer.PositionY = finalTargetPosition;
951                 }
952             }
953         }
954
955         /// <summary>
956         /// you can override it to clean-up your own resources.
957         /// </summary>
958         /// <param name="type">DisposeTypes</param>
959         /// This will be public opened in tizen_5.5 after ACR done. Before ACR, need to be hidden as inhouse API.
960         [EditorBrowsable(EditorBrowsableState.Never)]
961         protected override void Dispose(DisposeTypes type)
962         {
963             if (disposed)
964             {
965                 return;
966             }
967
968             if (type == DisposeTypes.Explicit)
969             {
970                 StopOverShootingShadowAnimation();
971                 StopScroll();
972
973                 if (mPanGestureDetector != null)
974                 {
975                     mPanGestureDetector.Detected -= OnPanGestureDetected;
976                     mPanGestureDetector.Dispose();
977                     mPanGestureDetector = null;
978                 }
979
980                 propertyNotification.Dispose();
981             }
982             base.Dispose(type);
983         }
984
985         private float CalculateMaximumScrollDistance()
986         {
987             float scrollingChildLength = 0;
988             float scrollerLength = 0;
989             if (ScrollingDirection == Direction.Horizontal)
990             {
991                 Debug.WriteLineIf(LayoutDebugScrollableBase, "Horizontal");
992
993                 scrollingChildLength = ContentContainer.Size.Width;
994                 scrollerLength = Size.Width;
995             }
996             else
997             {
998                 Debug.WriteLineIf(LayoutDebugScrollableBase, "Vertical");
999                 scrollingChildLength = ContentContainer.Size.Height;
1000                 scrollerLength = Size.Height;
1001             }
1002
1003             Debug.WriteLineIf(LayoutDebugScrollableBase, "ScrollBy maxScrollDistance:" + (scrollingChildLength - scrollerLength) +
1004                 " parent length:" + scrollerLength +
1005                 " scrolling child length:" + scrollingChildLength);
1006
1007             return Math.Max(scrollingChildLength - scrollerLength, 0);
1008         }
1009
1010         private void PageSnap(float velocity)
1011         {
1012             Debug.WriteLineIf(LayoutDebugScrollableBase, "PageSnap with pan candidate totalDisplacement:" + totalDisplacementForPan +
1013                 " currentPage[" + CurrentPage + "]");
1014
1015             //Increment current page if total displacement enough to warrant a page change.
1016             if (Math.Abs(totalDisplacementForPan) > (mPageWidth * ratioOfScreenWidthToCompleteScroll))
1017             {
1018                 if (totalDisplacementForPan < 0)
1019                 {
1020                     CurrentPage = Math.Min(Math.Max(Children.Count - 1, 0), ++CurrentPage);
1021                 }
1022                 else
1023                 {
1024                     CurrentPage = Math.Max(0, --CurrentPage);
1025                 }
1026             }
1027             else if (Math.Abs(velocity) > PageFlickThreshold)
1028             {
1029                 if (velocity < 0)
1030                 {
1031                     CurrentPage = Math.Min(Math.Max(Children.Count - 1, 0), ++CurrentPage);
1032                 }
1033                 else
1034                 {
1035                     CurrentPage = Math.Max(0, --CurrentPage);
1036                 }
1037             }
1038
1039             // Animate to new page or reposition to current page
1040             float destinationX = -(Children[CurrentPage].Position.X + Children[CurrentPage].CurrentSize.Width / 2 - CurrentSize.Width / 2); // set to middle of current page
1041             Debug.WriteLineIf(LayoutDebugScrollableBase, "Snapping to page[" + CurrentPage + "] to:" + destinationX + " from:" + ContentContainer.PositionX);
1042             AnimateChildTo(ScrollDuration, destinationX);
1043         }
1044
1045         /// <summary>
1046         /// Enable/Disable overshooting effect. default is disabled.
1047         /// </summary>
1048         [EditorBrowsable(EditorBrowsableState.Never)]
1049         public bool EnableOverShootingEffect { get; set; } = false;
1050
1051         private void AttachOverShootingShadowView()
1052         {
1053             if (!EnableOverShootingEffect)
1054                 return;
1055
1056             // stop animation if necessary.
1057             StopOverShootingShadowAnimation();
1058
1059             if (ScrollingDirection == Direction.Horizontal)
1060             {
1061                 base.Add(leftOverShootingShadowView);
1062                 base.Add(rightOverShootingShadowView);
1063
1064                 leftOverShootingShadowView.Size = new Size(0.0f, SizeHeight);
1065                 leftOverShootingShadowView.Opacity = 1.0f;
1066                 leftOverShootingShadowView.RaiseToTop();
1067
1068                 rightOverShootingShadowView.Size = new Size(0.0f, SizeHeight);
1069                 rightOverShootingShadowView.Opacity = 1.0f;
1070                 rightOverShootingShadowView.RaiseToTop();
1071             }
1072             else
1073             {
1074                 base.Add(topOverShootingShadowView);
1075                 base.Add(bottomOverShootingShadowView);
1076
1077                 topOverShootingShadowView.Size = new Size(SizeWidth, 0.0f);
1078                 topOverShootingShadowView.Opacity = 1.0f;
1079                 topOverShootingShadowView.RaiseToTop();
1080
1081                 bottomOverShootingShadowView.Size = new Size(SizeWidth, 0.0f);
1082                 bottomOverShootingShadowView.Opacity = 1.0f;
1083                 bottomOverShootingShadowView.RaiseToTop();
1084             }
1085
1086             // at the beginning, height or width of overshooting shadow is 0, so it is invisible.
1087             isOverShootingShadowShown = false;
1088         }
1089
1090         private void DragOverShootingShadow(float totalPanDisplacement, float panDisplacement)
1091         {
1092             if (!EnableOverShootingEffect)
1093                 return;
1094
1095             if (totalPanDisplacement > 0) // downwards
1096             {
1097                 // check if reaching at the top / left.
1098                 if ((int)finalTargetPosition != 0)
1099                 {
1100                     isOverShootingShadowShown = false;
1101                     return;
1102                 }
1103
1104                 // save start displacement, and re-calculate displacement.
1105                 if (!isOverShootingShadowShown)
1106                 {
1107                     startShowShadowDisplacement = totalPanDisplacement;
1108                 }
1109                 isOverShootingShadowShown = true;
1110
1111                 float newDisplacement = (int)totalPanDisplacement < (int)startShowShadowDisplacement ? 0 : totalPanDisplacement - startShowShadowDisplacement;
1112
1113                 if (ScrollingDirection == Direction.Horizontal)
1114                 {
1115                     // scale limit of height is 60%.
1116                     float heightScale = newDisplacement / overShootingShadowScaleHeightLimit;
1117                     leftOverShootingShadowView.SizeHeight = heightScale > 0.6f ? SizeHeight * 0.4f : SizeHeight * (1.0f - heightScale);
1118
1119                     // scale limit of width is 300%.
1120                     leftOverShootingShadowView.SizeWidth = newDisplacement > overShootingShadowScaleHeightLimit ? overShootingShadowScaleHeightLimit : newDisplacement;
1121
1122                     // trigger event
1123                     ScrollOutOfBoundEventArgs.Direction scrollDirection = panDisplacement > 0 ?
1124                        ScrollOutOfBoundEventArgs.Direction.Right : ScrollOutOfBoundEventArgs.Direction.Left;
1125                     OnScrollOutOfBound(scrollDirection, totalPanDisplacement);
1126                 }
1127                 else
1128                 {
1129                     // scale limit of width is 60%.
1130                     float widthScale = newDisplacement / overShootingShadowScaleHeightLimit;
1131                     topOverShootingShadowView.SizeWidth = widthScale > 0.6f ? SizeWidth * 0.4f : SizeWidth * (1.0f - widthScale);
1132
1133                     // scale limit of height is 300%.
1134                     topOverShootingShadowView.SizeHeight = newDisplacement > overShootingShadowScaleHeightLimit ? overShootingShadowScaleHeightLimit : newDisplacement;
1135
1136                     // trigger event
1137                     ScrollOutOfBoundEventArgs.Direction scrollDirection = panDisplacement > 0 ?
1138                        ScrollOutOfBoundEventArgs.Direction.Down : ScrollOutOfBoundEventArgs.Direction.Up;
1139                     OnScrollOutOfBound(scrollDirection, totalPanDisplacement);
1140                 }
1141             }
1142             else if (totalPanDisplacement < 0) // upwards
1143             {
1144                 // check if reaching at the bottom.
1145                 if (-(int)finalTargetPosition != (int)maxScrollDistance)
1146                 {
1147                     isOverShootingShadowShown = false;
1148                     return;
1149                 }
1150
1151                 // save start displacement, and re-calculate displacement.
1152                 if (!isOverShootingShadowShown)
1153                 {
1154                     startShowShadowDisplacement = totalPanDisplacement;
1155                 }
1156                 isOverShootingShadowShown = true;
1157
1158                 float newDisplacement = (int)startShowShadowDisplacement < (int)totalPanDisplacement ? 0 : startShowShadowDisplacement - totalPanDisplacement;
1159
1160                 if (ScrollingDirection == Direction.Horizontal)
1161                 {
1162                     // scale limit of height is 60%.
1163                     float heightScale = newDisplacement / overShootingShadowScaleHeightLimit;
1164                     rightOverShootingShadowView.SizeHeight = heightScale > 0.6f ? SizeHeight * 0.4f : SizeHeight * (1.0f - heightScale);
1165
1166                     // scale limit of width is 300%.
1167                     rightOverShootingShadowView.SizeWidth = newDisplacement > overShootingShadowScaleHeightLimit ? overShootingShadowScaleHeightLimit : newDisplacement;
1168
1169                     // trigger event
1170                     ScrollOutOfBoundEventArgs.Direction scrollDirection = panDisplacement > 0 ?
1171                        ScrollOutOfBoundEventArgs.Direction.Right : ScrollOutOfBoundEventArgs.Direction.Left;
1172                     OnScrollOutOfBound(scrollDirection, totalPanDisplacement);
1173                 }
1174                 else
1175                 {
1176                     // scale limit of width is 60%.
1177                     float widthScale = newDisplacement / overShootingShadowScaleHeightLimit;
1178                     bottomOverShootingShadowView.SizeWidth = widthScale > 0.6f ? SizeWidth * 0.4f : SizeWidth * (1.0f - widthScale);
1179
1180                     // scale limit of height is 300%.
1181                     bottomOverShootingShadowView.SizeHeight = newDisplacement > overShootingShadowScaleHeightLimit ? overShootingShadowScaleHeightLimit : newDisplacement;
1182
1183                     // trigger event
1184                     ScrollOutOfBoundEventArgs.Direction scrollDirection = panDisplacement > 0 ?
1185                        ScrollOutOfBoundEventArgs.Direction.Down : ScrollOutOfBoundEventArgs.Direction.Up;
1186                     OnScrollOutOfBound(scrollDirection, totalPanDisplacement);
1187                 }
1188             }
1189             else
1190             {
1191                 // if total displacement is 0, shadow would become invisible.
1192                 isOverShootingShadowShown = false;
1193             }
1194         }
1195
1196         private void PlayOverShootingShadowAnimation()
1197         {
1198             if (!EnableOverShootingEffect)
1199                 return;
1200
1201             // stop animation if necessary.
1202             StopOverShootingShadowAnimation();
1203
1204             if (overShootingShadowAnimation == null)
1205             {
1206                 overShootingShadowAnimation = new Animation(overShootingShadowAnimationDuration);
1207                 overShootingShadowAnimation.Finished += OnOverShootingShadowAnimationFinished;
1208             }
1209
1210             if (ScrollingDirection == Direction.Horizontal)
1211             {
1212                 View targetView = totalDisplacementForPan < 0 ? rightOverShootingShadowView : leftOverShootingShadowView;
1213                 overShootingShadowAnimation.AnimateTo(targetView, "SizeHeight", SizeHeight);
1214                 overShootingShadowAnimation.AnimateTo(targetView, "SizeWidth", 0.0f);
1215                 overShootingShadowAnimation.AnimateTo(targetView, "Opacity", 0.0f);
1216             }
1217             else
1218             {
1219                 View targetView = totalDisplacementForPan < 0 ? bottomOverShootingShadowView : topOverShootingShadowView;
1220                 overShootingShadowAnimation.AnimateTo(targetView, "SizeWidth", SizeWidth);
1221                 overShootingShadowAnimation.AnimateTo(targetView, "SizeHeight", 0.0f);
1222                 overShootingShadowAnimation.AnimateTo(targetView, "Opacity", 0.0f);
1223             }
1224             overShootingShadowAnimation.Play();
1225         }
1226
1227         private void StopOverShootingShadowAnimation()
1228         {
1229             if (overShootingShadowAnimation == null || overShootingShadowAnimation.State != Animation.States.Playing)
1230                 return;
1231
1232             overShootingShadowAnimation.Stop(Animation.EndActions.Cancel);
1233             OnOverShootingShadowAnimationFinished(null, null);
1234             overShootingShadowAnimation.Clear();
1235         }
1236
1237         private void OnOverShootingShadowAnimationFinished(object sender, EventArgs e)
1238         {
1239             if (ScrollingDirection == Direction.Horizontal)
1240             {
1241                 base.Remove(leftOverShootingShadowView);
1242                 base.Remove(rightOverShootingShadowView);
1243
1244                 leftOverShootingShadowView.Size = new Size(0.0f, SizeHeight);
1245                 rightOverShootingShadowView.Size = new Size(0.0f, SizeHeight);
1246             }
1247             else
1248             {
1249                 base.Remove(topOverShootingShadowView);
1250                 base.Remove(bottomOverShootingShadowView);
1251
1252                 topOverShootingShadowView.Size = new Size(SizeWidth, 0.0f);
1253                 bottomOverShootingShadowView.Size = new Size(SizeWidth, 0.0f);
1254             }
1255
1256             // after animation finished, height/width & opacity of vertical shadow both are 0, so it is invisible.
1257             isOverShootingShadowShown = false;
1258         }
1259
1260         private void OnScrollOutOfBound(ScrollOutOfBoundEventArgs.Direction direction, float displacement)
1261         {
1262             ScrollOutOfBoundEventArgs args = new ScrollOutOfBoundEventArgs(direction, displacement);
1263             ScrollOutOfBound?.Invoke(this, args);
1264         }
1265
1266         private void OnPanGestureDetected(object source, PanGestureDetector.DetectedEventArgs e)
1267         {
1268             OnPanGesture(e.PanGesture);
1269         }
1270
1271         private void OnPanGesture(PanGesture panGesture)
1272         {
1273             if (SnapToPage && scrollAnimation != null && scrollAnimation.State == Animation.States.Playing)
1274             {
1275                 return;
1276             }
1277
1278             if (panGesture.State == Gesture.StateType.Started)
1279             {
1280                 readyToNotice = false;
1281                 base.Add(mInterruptTouchingChild);
1282                 AttachOverShootingShadowView();
1283                 Debug.WriteLineIf(LayoutDebugScrollableBase, "Gesture Start");
1284                 if (scrolling && !SnapToPage)
1285                 {
1286                     StopScroll();
1287                 }
1288                 totalDisplacementForPan = 0.0f;
1289                 OnScrollDragStarted();
1290             }
1291             else if (panGesture.State == Gesture.StateType.Continuing)
1292             {
1293                 if (ScrollingDirection == Direction.Horizontal)
1294                 {
1295                     // if vertical shadow is shown, does not scroll.
1296                     if (!isOverShootingShadowShown)
1297                     {
1298                         ScrollBy(panGesture.Displacement.X, false);
1299                     }
1300                     totalDisplacementForPan += panGesture.Displacement.X;
1301                     DragOverShootingShadow(totalDisplacementForPan, panGesture.Displacement.X);
1302                 }
1303                 else
1304                 {
1305                     // if vertical shadow is shown, does not scroll.
1306                     if (!isOverShootingShadowShown)
1307                     {
1308                         ScrollBy(panGesture.Displacement.Y, false);
1309                     }
1310                     totalDisplacementForPan += panGesture.Displacement.Y;
1311                     DragOverShootingShadow(totalDisplacementForPan, panGesture.Displacement.Y);
1312                 }
1313                 Debug.WriteLineIf(LayoutDebugScrollableBase, "OnPanGestureDetected Continue totalDisplacementForPan:" + totalDisplacementForPan);
1314             }
1315             else if (panGesture.State == Gesture.StateType.Finished || panGesture.State == Gesture.StateType.Cancelled)
1316             {
1317                 PlayOverShootingShadowAnimation();
1318                 OnScrollDragEnded();
1319                 StopScroll(); // Will replace previous animation so will stop existing one.
1320
1321                 if (scrollAnimation == null)
1322                 {
1323                     scrollAnimation = new Animation();
1324                     scrollAnimation.Finished += ScrollAnimationFinished;
1325                 }
1326
1327                 float panVelocity = (ScrollingDirection == Direction.Horizontal) ? panGesture.Velocity.X : panGesture.Velocity.Y;
1328
1329                 if (SnapToPage)
1330                 {
1331                     PageSnap(panVelocity);
1332                 }
1333                 else
1334                 {
1335                     if (panVelocity == 0)
1336                     {
1337                         float currentScrollPosition = (ScrollingDirection == Direction.Horizontal ? ContentContainer.CurrentPosition.X : ContentContainer.CurrentPosition.Y);
1338                         scrollAnimation.DefaultAlphaFunction = new AlphaFunction(AlphaFunction.BuiltinFunctions.Linear);
1339                         scrollAnimation.Duration = 0;
1340                         scrollAnimation.AnimateTo(ContentContainer, (ScrollingDirection == Direction.Horizontal) ? "PositionX" : "PositionY", currentScrollPosition);
1341                         scrollAnimation.Play();
1342                     }
1343                     else
1344                     {
1345                         Decelerating(panVelocity, scrollAnimation);
1346                     }
1347                 }
1348
1349                 totalDisplacementForPan = 0;
1350                 scrolling = true;
1351                 readyToNotice = true;
1352                 OnScrollAnimationStarted();
1353             }
1354         }
1355
1356         internal void BaseRemove(View view)
1357         {
1358             base.Remove(view);
1359         }
1360
1361         internal override bool OnAccessibilityPan(PanGesture gestures)
1362         {
1363             if (SnapToPage && scrollAnimation != null && scrollAnimation.State == Animation.States.Playing)
1364             {
1365                 return false;
1366             }
1367
1368             OnPanGesture(gestures);
1369             return true;
1370         }
1371
1372         private float CustomScrollAlphaFunction(float progress)
1373         {
1374             if (panAnimationDelta == 0)
1375             {
1376                 return 1.0f;
1377             }
1378             else
1379             {
1380                 // Parameter "progress" is normalized value. We need to multiply target duration to calculate distance.
1381                 // Can get real distance using equation of deceleration (check Decelerating function)
1382                 // After get real distance, normalize it
1383                 float realDuration = progress * panAnimationDuration;
1384                 float realDistance = velocityOfLastPan * ((float)Math.Pow(decelerationRate, realDuration) - 1) / logValueOfDeceleration;
1385                 float result = Math.Min(realDistance / Math.Abs(panAnimationDelta), 1.0f);
1386                 return result;
1387             }
1388         }
1389
1390         /// <summary>
1391         /// you can override it to custom your decelerating
1392         /// </summary>
1393         /// <param name="velocity">Velocity of current pan.</param>
1394         /// <param name="animation">Scroll animation.</param>
1395         [EditorBrowsable(EditorBrowsableState.Never)]
1396         protected virtual void Decelerating(float velocity, Animation animation)
1397         {
1398             // Decelerating using deceleration equation ===========
1399             //
1400             // V   : velocity (pixel per milisecond)
1401             // V0  : initial velocity
1402             // d   : deceleration rate,
1403             // t   : time
1404             // X   : final position after decelerating
1405             // log : natural logarithm
1406             //
1407             // V(t) = V0 * d pow t;
1408             // X(t) = V0 * (d pow t - 1) / log d;  <-- Integrate the velocity function
1409             // X(∞) = V0 * d / (1 - d); <-- Result using inifit T can be final position because T is tending to infinity.
1410             //
1411             // Because of final T is tending to inifity, we should use threshold value to finish.
1412             // Final T = log(-threshold * log d / |V0| ) / log d;
1413
1414             velocityOfLastPan = Math.Abs(velocity);
1415
1416             float currentScrollPosition = -(ScrollingDirection == Direction.Horizontal ? ContentContainer.CurrentPosition.X : ContentContainer.CurrentPosition.Y);
1417             panAnimationDelta = (velocityOfLastPan * decelerationRate) / (1 - decelerationRate);
1418             panAnimationDelta = velocity > 0 ? -panAnimationDelta : panAnimationDelta;
1419
1420             float destination = -(panAnimationDelta + currentScrollPosition);
1421             float adjustDestination = AdjustTargetPositionOfScrollAnimation(destination);
1422             float maxPosition = ScrollAvailableArea != null ? ScrollAvailableArea.Y : maxScrollDistance;
1423             float minPosition = ScrollAvailableArea != null ? ScrollAvailableArea.X : 0;
1424
1425             if (destination < -maxPosition || destination > minPosition)
1426             {
1427                 panAnimationDelta = velocity > 0 ? (currentScrollPosition - minPosition) : (maxPosition - currentScrollPosition);
1428                 destination = velocity > 0 ? minPosition : -maxPosition;
1429
1430                 if (panAnimationDelta == 0)
1431                 {
1432                     panAnimationDuration = 0.0f;
1433                 }
1434                 else
1435                 {
1436                     panAnimationDuration = (float)Math.Log((panAnimationDelta * logValueOfDeceleration / velocityOfLastPan + 1), decelerationRate);
1437                 }
1438
1439                 Debug.WriteLineIf(LayoutDebugScrollableBase, "\n" +
1440                     "OverRange======================= \n" +
1441                     "[decelerationRate] " + decelerationRate + "\n" +
1442                     "[logValueOfDeceleration] " + logValueOfDeceleration + "\n" +
1443                     "[Velocity] " + velocityOfLastPan + "\n" +
1444                     "[CurrentPosition] " + currentScrollPosition + "\n" +
1445                     "[CandidateDelta] " + panAnimationDelta + "\n" +
1446                     "[Destination] " + destination + "\n" +
1447                     "[Duration] " + panAnimationDuration + "\n" +
1448                     "================================ \n"
1449                 );
1450             }
1451             else
1452             {
1453                 panAnimationDuration = (float)Math.Log(-DecelerationThreshold * logValueOfDeceleration / velocityOfLastPan) / logValueOfDeceleration;
1454
1455                 if (adjustDestination != destination)
1456                 {
1457                     destination = adjustDestination;
1458                     panAnimationDelta = destination + currentScrollPosition;
1459                     velocityOfLastPan = Math.Abs(panAnimationDelta * logValueOfDeceleration / ((float)Math.Pow(decelerationRate, panAnimationDuration) - 1));
1460                     panAnimationDuration = (float)Math.Log(-DecelerationThreshold * logValueOfDeceleration / velocityOfLastPan) / logValueOfDeceleration;
1461                 }
1462
1463                 Debug.WriteLineIf(LayoutDebugScrollableBase, "\n" +
1464                     "================================ \n" +
1465                     "[decelerationRate] " + decelerationRate + "\n" +
1466                     "[logValueOfDeceleration] " + logValueOfDeceleration + "\n" +
1467                     "[Velocity] " + velocityOfLastPan + "\n" +
1468                     "[CurrentPosition] " + currentScrollPosition + "\n" +
1469                     "[CandidateDelta] " + panAnimationDelta + "\n" +
1470                     "[Destination] " + destination + "\n" +
1471                     "[Duration] " + panAnimationDuration + "\n" +
1472                     "================================ \n"
1473                 );
1474             }
1475
1476             finalTargetPosition = destination;
1477
1478             customScrollAlphaFunction = new UserAlphaFunctionDelegate(CustomScrollAlphaFunction);
1479             animation.DefaultAlphaFunction = new AlphaFunction(customScrollAlphaFunction);
1480             GC.KeepAlive(customScrollAlphaFunction);
1481             animation.Duration = (int)panAnimationDuration;
1482             animation.AnimateTo(ContentContainer, (ScrollingDirection == Direction.Horizontal) ? "PositionX" : "PositionY", destination);
1483             animation.Play();
1484         }
1485
1486         private void ScrollAnimationFinished(object sender, EventArgs e)
1487         {
1488             OnScrollAnimationEnded();
1489         }
1490
1491         /// <summary>
1492         /// Adjust scrolling position by own scrolling rules.
1493         /// Override this function when developer wants to change destination of flicking.(e.g. always snap to center of item)
1494         /// </summary>
1495         /// This may be public opened in tizen_6.0 after ACR done. Before ACR, need to be hidden as inhouse API
1496         [EditorBrowsable(EditorBrowsableState.Never)]
1497         protected virtual float AdjustTargetPositionOfScrollAnimation(float position)
1498         {
1499             return position;
1500         }
1501
1502         /// <summary>
1503         /// Scroll position given to ScrollTo.
1504         /// This is the position in the opposite direction to the position of ContentContainer.
1505         /// </summary>
1506         /// <since_tizen> 8 </since_tizen>
1507         public Position ScrollPosition
1508         {
1509             get
1510             {
1511                 return new Position(-ContentContainer.Position);
1512             }
1513         }
1514
1515         /// <summary>
1516         /// Current scroll position in the middle of ScrollTo animation.
1517         /// This is the position in the opposite direction to the current position of ContentContainer.
1518         /// </summary>
1519         /// <since_tizen> 8 </since_tizen>
1520         public Position ScrollCurrentPosition
1521         {
1522             get
1523             {
1524                 return new Position(-ContentContainer.CurrentPosition);
1525             }
1526         }
1527
1528         /// <summary>
1529         /// Remove all children in ContentContainer.
1530         /// </summary>
1531         /// <param name="dispose">If true, removed child is disposed.</param>
1532         [EditorBrowsable(EditorBrowsableState.Never)]
1533         public void RemoveAllChildren(bool dispose = false)
1534         {
1535             RecursiveRemoveChildren(ContentContainer, dispose);
1536         }
1537
1538         private void RecursiveRemoveChildren(View parent, bool dispose)
1539         {
1540             if (parent == null)
1541             {
1542                 return;
1543             }
1544             int maxChild = (int)parent.GetChildCount();
1545             for (int i = maxChild - 1; i >= 0; --i)
1546             {
1547                 View child = parent.GetChildAt((uint)i);
1548                 if (child == null)
1549                 {
1550                     continue;
1551                 }
1552                 RecursiveRemoveChildren(child, dispose);
1553                 parent.Remove(child);
1554                 if (dispose)
1555                 {
1556                     child.Dispose();
1557                 }
1558             }
1559         }
1560
1561     }
1562
1563 } // namespace