[NUI] Add DeleteAccessibilityAttribute
[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                     scrollBar.Unparent();
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                 AccessibilityManager.Instance.DeleteAccessibilityAttribute(this);
971                 StopOverShootingShadowAnimation();
972                 StopScroll();
973
974                 if (mPanGestureDetector != null)
975                 {
976                     mPanGestureDetector.Detected -= OnPanGestureDetected;
977                     mPanGestureDetector.Dispose();
978                     mPanGestureDetector = null;
979                 }
980
981                 propertyNotification.Dispose();
982             }
983             base.Dispose(type);
984         }
985
986         private float CalculateMaximumScrollDistance()
987         {
988             float scrollingChildLength = 0;
989             float scrollerLength = 0;
990             if (ScrollingDirection == Direction.Horizontal)
991             {
992                 Debug.WriteLineIf(LayoutDebugScrollableBase, "Horizontal");
993
994                 scrollingChildLength = ContentContainer.Size.Width;
995                 scrollerLength = Size.Width;
996             }
997             else
998             {
999                 Debug.WriteLineIf(LayoutDebugScrollableBase, "Vertical");
1000                 scrollingChildLength = ContentContainer.Size.Height;
1001                 scrollerLength = Size.Height;
1002             }
1003
1004             Debug.WriteLineIf(LayoutDebugScrollableBase, "ScrollBy maxScrollDistance:" + (scrollingChildLength - scrollerLength) +
1005                 " parent length:" + scrollerLength +
1006                 " scrolling child length:" + scrollingChildLength);
1007
1008             return Math.Max(scrollingChildLength - scrollerLength, 0);
1009         }
1010
1011         private void PageSnap(float velocity)
1012         {
1013             Debug.WriteLineIf(LayoutDebugScrollableBase, "PageSnap with pan candidate totalDisplacement:" + totalDisplacementForPan +
1014                 " currentPage[" + CurrentPage + "]");
1015
1016             //Increment current page if total displacement enough to warrant a page change.
1017             if (Math.Abs(totalDisplacementForPan) > (mPageWidth * ratioOfScreenWidthToCompleteScroll))
1018             {
1019                 if (totalDisplacementForPan < 0)
1020                 {
1021                     CurrentPage = Math.Min(Math.Max(Children.Count - 1, 0), ++CurrentPage);
1022                 }
1023                 else
1024                 {
1025                     CurrentPage = Math.Max(0, --CurrentPage);
1026                 }
1027             }
1028             else if (Math.Abs(velocity) > PageFlickThreshold)
1029             {
1030                 if (velocity < 0)
1031                 {
1032                     CurrentPage = Math.Min(Math.Max(Children.Count - 1, 0), ++CurrentPage);
1033                 }
1034                 else
1035                 {
1036                     CurrentPage = Math.Max(0, --CurrentPage);
1037                 }
1038             }
1039
1040             // Animate to new page or reposition to current page
1041             float destinationX = -(Children[CurrentPage].Position.X + Children[CurrentPage].CurrentSize.Width / 2 - CurrentSize.Width / 2); // set to middle of current page
1042             Debug.WriteLineIf(LayoutDebugScrollableBase, "Snapping to page[" + CurrentPage + "] to:" + destinationX + " from:" + ContentContainer.PositionX);
1043             AnimateChildTo(ScrollDuration, destinationX);
1044         }
1045
1046         /// <summary>
1047         /// Enable/Disable overshooting effect. default is disabled.
1048         /// </summary>
1049         [EditorBrowsable(EditorBrowsableState.Never)]
1050         public bool EnableOverShootingEffect { get; set; } = false;
1051
1052         private void AttachOverShootingShadowView()
1053         {
1054             if (!EnableOverShootingEffect)
1055                 return;
1056
1057             // stop animation if necessary.
1058             StopOverShootingShadowAnimation();
1059
1060             if (ScrollingDirection == Direction.Horizontal)
1061             {
1062                 base.Add(leftOverShootingShadowView);
1063                 base.Add(rightOverShootingShadowView);
1064
1065                 leftOverShootingShadowView.Size = new Size(0.0f, SizeHeight);
1066                 leftOverShootingShadowView.Opacity = 1.0f;
1067                 leftOverShootingShadowView.RaiseToTop();
1068
1069                 rightOverShootingShadowView.Size = new Size(0.0f, SizeHeight);
1070                 rightOverShootingShadowView.Opacity = 1.0f;
1071                 rightOverShootingShadowView.RaiseToTop();
1072             }
1073             else
1074             {
1075                 base.Add(topOverShootingShadowView);
1076                 base.Add(bottomOverShootingShadowView);
1077
1078                 topOverShootingShadowView.Size = new Size(SizeWidth, 0.0f);
1079                 topOverShootingShadowView.Opacity = 1.0f;
1080                 topOverShootingShadowView.RaiseToTop();
1081
1082                 bottomOverShootingShadowView.Size = new Size(SizeWidth, 0.0f);
1083                 bottomOverShootingShadowView.Opacity = 1.0f;
1084                 bottomOverShootingShadowView.RaiseToTop();
1085             }
1086
1087             // at the beginning, height or width of overshooting shadow is 0, so it is invisible.
1088             isOverShootingShadowShown = false;
1089         }
1090
1091         private void DragOverShootingShadow(float totalPanDisplacement, float panDisplacement)
1092         {
1093             if (!EnableOverShootingEffect)
1094                 return;
1095
1096             if (totalPanDisplacement > 0) // downwards
1097             {
1098                 // check if reaching at the top / left.
1099                 if ((int)finalTargetPosition != 0)
1100                 {
1101                     isOverShootingShadowShown = false;
1102                     return;
1103                 }
1104
1105                 // save start displacement, and re-calculate displacement.
1106                 if (!isOverShootingShadowShown)
1107                 {
1108                     startShowShadowDisplacement = totalPanDisplacement;
1109                 }
1110                 isOverShootingShadowShown = true;
1111
1112                 float newDisplacement = (int)totalPanDisplacement < (int)startShowShadowDisplacement ? 0 : totalPanDisplacement - startShowShadowDisplacement;
1113
1114                 if (ScrollingDirection == Direction.Horizontal)
1115                 {
1116                     // scale limit of height is 60%.
1117                     float heightScale = newDisplacement / overShootingShadowScaleHeightLimit;
1118                     leftOverShootingShadowView.SizeHeight = heightScale > 0.6f ? SizeHeight * 0.4f : SizeHeight * (1.0f - heightScale);
1119
1120                     // scale limit of width is 300%.
1121                     leftOverShootingShadowView.SizeWidth = newDisplacement > overShootingShadowScaleHeightLimit ? overShootingShadowScaleHeightLimit : newDisplacement;
1122
1123                     // trigger event
1124                     ScrollOutOfBoundEventArgs.Direction scrollDirection = panDisplacement > 0 ?
1125                        ScrollOutOfBoundEventArgs.Direction.Right : ScrollOutOfBoundEventArgs.Direction.Left;
1126                     OnScrollOutOfBound(scrollDirection, totalPanDisplacement);
1127                 }
1128                 else
1129                 {
1130                     // scale limit of width is 60%.
1131                     float widthScale = newDisplacement / overShootingShadowScaleHeightLimit;
1132                     topOverShootingShadowView.SizeWidth = widthScale > 0.6f ? SizeWidth * 0.4f : SizeWidth * (1.0f - widthScale);
1133
1134                     // scale limit of height is 300%.
1135                     topOverShootingShadowView.SizeHeight = newDisplacement > overShootingShadowScaleHeightLimit ? overShootingShadowScaleHeightLimit : newDisplacement;
1136
1137                     // trigger event
1138                     ScrollOutOfBoundEventArgs.Direction scrollDirection = panDisplacement > 0 ?
1139                        ScrollOutOfBoundEventArgs.Direction.Down : ScrollOutOfBoundEventArgs.Direction.Up;
1140                     OnScrollOutOfBound(scrollDirection, totalPanDisplacement);
1141                 }
1142             }
1143             else if (totalPanDisplacement < 0) // upwards
1144             {
1145                 // check if reaching at the bottom.
1146                 if (-(int)finalTargetPosition != (int)maxScrollDistance)
1147                 {
1148                     isOverShootingShadowShown = false;
1149                     return;
1150                 }
1151
1152                 // save start displacement, and re-calculate displacement.
1153                 if (!isOverShootingShadowShown)
1154                 {
1155                     startShowShadowDisplacement = totalPanDisplacement;
1156                 }
1157                 isOverShootingShadowShown = true;
1158
1159                 float newDisplacement = (int)startShowShadowDisplacement < (int)totalPanDisplacement ? 0 : startShowShadowDisplacement - totalPanDisplacement;
1160
1161                 if (ScrollingDirection == Direction.Horizontal)
1162                 {
1163                     // scale limit of height is 60%.
1164                     float heightScale = newDisplacement / overShootingShadowScaleHeightLimit;
1165                     rightOverShootingShadowView.SizeHeight = heightScale > 0.6f ? SizeHeight * 0.4f : SizeHeight * (1.0f - heightScale);
1166
1167                     // scale limit of width is 300%.
1168                     rightOverShootingShadowView.SizeWidth = newDisplacement > overShootingShadowScaleHeightLimit ? overShootingShadowScaleHeightLimit : newDisplacement;
1169
1170                     // trigger event
1171                     ScrollOutOfBoundEventArgs.Direction scrollDirection = panDisplacement > 0 ?
1172                        ScrollOutOfBoundEventArgs.Direction.Right : ScrollOutOfBoundEventArgs.Direction.Left;
1173                     OnScrollOutOfBound(scrollDirection, totalPanDisplacement);
1174                 }
1175                 else
1176                 {
1177                     // scale limit of width is 60%.
1178                     float widthScale = newDisplacement / overShootingShadowScaleHeightLimit;
1179                     bottomOverShootingShadowView.SizeWidth = widthScale > 0.6f ? SizeWidth * 0.4f : SizeWidth * (1.0f - widthScale);
1180
1181                     // scale limit of height is 300%.
1182                     bottomOverShootingShadowView.SizeHeight = newDisplacement > overShootingShadowScaleHeightLimit ? overShootingShadowScaleHeightLimit : newDisplacement;
1183
1184                     // trigger event
1185                     ScrollOutOfBoundEventArgs.Direction scrollDirection = panDisplacement > 0 ?
1186                        ScrollOutOfBoundEventArgs.Direction.Down : ScrollOutOfBoundEventArgs.Direction.Up;
1187                     OnScrollOutOfBound(scrollDirection, totalPanDisplacement);
1188                 }
1189             }
1190             else
1191             {
1192                 // if total displacement is 0, shadow would become invisible.
1193                 isOverShootingShadowShown = false;
1194             }
1195         }
1196
1197         private void PlayOverShootingShadowAnimation()
1198         {
1199             if (!EnableOverShootingEffect)
1200                 return;
1201
1202             // stop animation if necessary.
1203             StopOverShootingShadowAnimation();
1204
1205             if (overShootingShadowAnimation == null)
1206             {
1207                 overShootingShadowAnimation = new Animation(overShootingShadowAnimationDuration);
1208                 overShootingShadowAnimation.Finished += OnOverShootingShadowAnimationFinished;
1209             }
1210
1211             if (ScrollingDirection == Direction.Horizontal)
1212             {
1213                 View targetView = totalDisplacementForPan < 0 ? rightOverShootingShadowView : leftOverShootingShadowView;
1214                 overShootingShadowAnimation.AnimateTo(targetView, "SizeHeight", SizeHeight);
1215                 overShootingShadowAnimation.AnimateTo(targetView, "SizeWidth", 0.0f);
1216                 overShootingShadowAnimation.AnimateTo(targetView, "Opacity", 0.0f);
1217             }
1218             else
1219             {
1220                 View targetView = totalDisplacementForPan < 0 ? bottomOverShootingShadowView : topOverShootingShadowView;
1221                 overShootingShadowAnimation.AnimateTo(targetView, "SizeWidth", SizeWidth);
1222                 overShootingShadowAnimation.AnimateTo(targetView, "SizeHeight", 0.0f);
1223                 overShootingShadowAnimation.AnimateTo(targetView, "Opacity", 0.0f);
1224             }
1225             overShootingShadowAnimation.Play();
1226         }
1227
1228         private void StopOverShootingShadowAnimation()
1229         {
1230             if (overShootingShadowAnimation == null || overShootingShadowAnimation.State != Animation.States.Playing)
1231                 return;
1232
1233             overShootingShadowAnimation.Stop(Animation.EndActions.Cancel);
1234             OnOverShootingShadowAnimationFinished(null, null);
1235             overShootingShadowAnimation.Clear();
1236         }
1237
1238         private void OnOverShootingShadowAnimationFinished(object sender, EventArgs e)
1239         {
1240             if (ScrollingDirection == Direction.Horizontal)
1241             {
1242                 base.Remove(leftOverShootingShadowView);
1243                 base.Remove(rightOverShootingShadowView);
1244
1245                 leftOverShootingShadowView.Size = new Size(0.0f, SizeHeight);
1246                 rightOverShootingShadowView.Size = new Size(0.0f, SizeHeight);
1247             }
1248             else
1249             {
1250                 base.Remove(topOverShootingShadowView);
1251                 base.Remove(bottomOverShootingShadowView);
1252
1253                 topOverShootingShadowView.Size = new Size(SizeWidth, 0.0f);
1254                 bottomOverShootingShadowView.Size = new Size(SizeWidth, 0.0f);
1255             }
1256
1257             // after animation finished, height/width & opacity of vertical shadow both are 0, so it is invisible.
1258             isOverShootingShadowShown = false;
1259         }
1260
1261         private void OnScrollOutOfBound(ScrollOutOfBoundEventArgs.Direction direction, float displacement)
1262         {
1263             ScrollOutOfBoundEventArgs args = new ScrollOutOfBoundEventArgs(direction, displacement);
1264             ScrollOutOfBound?.Invoke(this, args);
1265         }
1266
1267         private void OnPanGestureDetected(object source, PanGestureDetector.DetectedEventArgs e)
1268         {
1269             OnPanGesture(e.PanGesture);
1270         }
1271
1272         private void OnPanGesture(PanGesture panGesture)
1273         {
1274             if (SnapToPage && scrollAnimation != null && scrollAnimation.State == Animation.States.Playing)
1275             {
1276                 return;
1277             }
1278
1279             if (panGesture.State == Gesture.StateType.Started)
1280             {
1281                 readyToNotice = false;
1282                 base.Add(mInterruptTouchingChild);
1283                 AttachOverShootingShadowView();
1284                 Debug.WriteLineIf(LayoutDebugScrollableBase, "Gesture Start");
1285                 if (scrolling && !SnapToPage)
1286                 {
1287                     StopScroll();
1288                 }
1289                 totalDisplacementForPan = 0.0f;
1290                 OnScrollDragStarted();
1291             }
1292             else if (panGesture.State == Gesture.StateType.Continuing)
1293             {
1294                 if (ScrollingDirection == Direction.Horizontal)
1295                 {
1296                     // if vertical shadow is shown, does not scroll.
1297                     if (!isOverShootingShadowShown)
1298                     {
1299                         ScrollBy(panGesture.Displacement.X, false);
1300                     }
1301                     totalDisplacementForPan += panGesture.Displacement.X;
1302                     DragOverShootingShadow(totalDisplacementForPan, panGesture.Displacement.X);
1303                 }
1304                 else
1305                 {
1306                     // if vertical shadow is shown, does not scroll.
1307                     if (!isOverShootingShadowShown)
1308                     {
1309                         ScrollBy(panGesture.Displacement.Y, false);
1310                     }
1311                     totalDisplacementForPan += panGesture.Displacement.Y;
1312                     DragOverShootingShadow(totalDisplacementForPan, panGesture.Displacement.Y);
1313                 }
1314                 Debug.WriteLineIf(LayoutDebugScrollableBase, "OnPanGestureDetected Continue totalDisplacementForPan:" + totalDisplacementForPan);
1315             }
1316             else if (panGesture.State == Gesture.StateType.Finished || panGesture.State == Gesture.StateType.Cancelled)
1317             {
1318                 PlayOverShootingShadowAnimation();
1319                 OnScrollDragEnded();
1320                 StopScroll(); // Will replace previous animation so will stop existing one.
1321
1322                 if (scrollAnimation == null)
1323                 {
1324                     scrollAnimation = new Animation();
1325                     scrollAnimation.Finished += ScrollAnimationFinished;
1326                 }
1327
1328                 float panVelocity = (ScrollingDirection == Direction.Horizontal) ? panGesture.Velocity.X : panGesture.Velocity.Y;
1329
1330                 if (SnapToPage)
1331                 {
1332                     PageSnap(panVelocity);
1333                 }
1334                 else
1335                 {
1336                     if (panVelocity == 0)
1337                     {
1338                         float currentScrollPosition = (ScrollingDirection == Direction.Horizontal ? ContentContainer.CurrentPosition.X : ContentContainer.CurrentPosition.Y);
1339                         scrollAnimation.DefaultAlphaFunction = new AlphaFunction(AlphaFunction.BuiltinFunctions.Linear);
1340                         scrollAnimation.Duration = 0;
1341                         scrollAnimation.AnimateTo(ContentContainer, (ScrollingDirection == Direction.Horizontal) ? "PositionX" : "PositionY", currentScrollPosition);
1342                         scrollAnimation.Play();
1343                     }
1344                     else
1345                     {
1346                         Decelerating(panVelocity, scrollAnimation);
1347                     }
1348                 }
1349
1350                 totalDisplacementForPan = 0;
1351                 scrolling = true;
1352                 readyToNotice = true;
1353                 OnScrollAnimationStarted();
1354             }
1355         }
1356
1357         internal void BaseRemove(View view)
1358         {
1359             base.Remove(view);
1360         }
1361
1362         internal override bool OnAccessibilityPan(PanGesture gestures)
1363         {
1364             if (SnapToPage && scrollAnimation != null && scrollAnimation.State == Animation.States.Playing)
1365             {
1366                 return false;
1367             }
1368
1369             OnPanGesture(gestures);
1370             return true;
1371         }
1372
1373         private float CustomScrollAlphaFunction(float progress)
1374         {
1375             if (panAnimationDelta == 0)
1376             {
1377                 return 1.0f;
1378             }
1379             else
1380             {
1381                 // Parameter "progress" is normalized value. We need to multiply target duration to calculate distance.
1382                 // Can get real distance using equation of deceleration (check Decelerating function)
1383                 // After get real distance, normalize it
1384                 float realDuration = progress * panAnimationDuration;
1385                 float realDistance = velocityOfLastPan * ((float)Math.Pow(decelerationRate, realDuration) - 1) / logValueOfDeceleration;
1386                 float result = Math.Min(realDistance / Math.Abs(panAnimationDelta), 1.0f);
1387                 return result;
1388             }
1389         }
1390
1391         /// <summary>
1392         /// you can override it to custom your decelerating
1393         /// </summary>
1394         /// <param name="velocity">Velocity of current pan.</param>
1395         /// <param name="animation">Scroll animation.</param>
1396         [EditorBrowsable(EditorBrowsableState.Never)]
1397         protected virtual void Decelerating(float velocity, Animation animation)
1398         {
1399             // Decelerating using deceleration equation ===========
1400             //
1401             // V   : velocity (pixel per milisecond)
1402             // V0  : initial velocity
1403             // d   : deceleration rate,
1404             // t   : time
1405             // X   : final position after decelerating
1406             // log : natural logarithm
1407             //
1408             // V(t) = V0 * d pow t;
1409             // X(t) = V0 * (d pow t - 1) / log d;  <-- Integrate the velocity function
1410             // X(∞) = V0 * d / (1 - d); <-- Result using inifit T can be final position because T is tending to infinity.
1411             //
1412             // Because of final T is tending to inifity, we should use threshold value to finish.
1413             // Final T = log(-threshold * log d / |V0| ) / log d;
1414
1415             velocityOfLastPan = Math.Abs(velocity);
1416
1417             float currentScrollPosition = -(ScrollingDirection == Direction.Horizontal ? ContentContainer.CurrentPosition.X : ContentContainer.CurrentPosition.Y);
1418             panAnimationDelta = (velocityOfLastPan * decelerationRate) / (1 - decelerationRate);
1419             panAnimationDelta = velocity > 0 ? -panAnimationDelta : panAnimationDelta;
1420
1421             float destination = -(panAnimationDelta + currentScrollPosition);
1422             float adjustDestination = AdjustTargetPositionOfScrollAnimation(destination);
1423             float maxPosition = ScrollAvailableArea != null ? ScrollAvailableArea.Y : maxScrollDistance;
1424             float minPosition = ScrollAvailableArea != null ? ScrollAvailableArea.X : 0;
1425
1426             if (destination < -maxPosition || destination > minPosition)
1427             {
1428                 panAnimationDelta = velocity > 0 ? (currentScrollPosition - minPosition) : (maxPosition - currentScrollPosition);
1429                 destination = velocity > 0 ? minPosition : -maxPosition;
1430
1431                 if (panAnimationDelta == 0)
1432                 {
1433                     panAnimationDuration = 0.0f;
1434                 }
1435                 else
1436                 {
1437                     panAnimationDuration = (float)Math.Log((panAnimationDelta * logValueOfDeceleration / velocityOfLastPan + 1), decelerationRate);
1438                 }
1439
1440                 Debug.WriteLineIf(LayoutDebugScrollableBase, "\n" +
1441                     "OverRange======================= \n" +
1442                     "[decelerationRate] " + decelerationRate + "\n" +
1443                     "[logValueOfDeceleration] " + logValueOfDeceleration + "\n" +
1444                     "[Velocity] " + velocityOfLastPan + "\n" +
1445                     "[CurrentPosition] " + currentScrollPosition + "\n" +
1446                     "[CandidateDelta] " + panAnimationDelta + "\n" +
1447                     "[Destination] " + destination + "\n" +
1448                     "[Duration] " + panAnimationDuration + "\n" +
1449                     "================================ \n"
1450                 );
1451             }
1452             else
1453             {
1454                 panAnimationDuration = (float)Math.Log(-DecelerationThreshold * logValueOfDeceleration / velocityOfLastPan) / logValueOfDeceleration;
1455
1456                 if (adjustDestination != destination)
1457                 {
1458                     destination = adjustDestination;
1459                     panAnimationDelta = destination + currentScrollPosition;
1460                     velocityOfLastPan = Math.Abs(panAnimationDelta * logValueOfDeceleration / ((float)Math.Pow(decelerationRate, panAnimationDuration) - 1));
1461                     panAnimationDuration = (float)Math.Log(-DecelerationThreshold * logValueOfDeceleration / velocityOfLastPan) / logValueOfDeceleration;
1462                 }
1463
1464                 Debug.WriteLineIf(LayoutDebugScrollableBase, "\n" +
1465                     "================================ \n" +
1466                     "[decelerationRate] " + decelerationRate + "\n" +
1467                     "[logValueOfDeceleration] " + logValueOfDeceleration + "\n" +
1468                     "[Velocity] " + velocityOfLastPan + "\n" +
1469                     "[CurrentPosition] " + currentScrollPosition + "\n" +
1470                     "[CandidateDelta] " + panAnimationDelta + "\n" +
1471                     "[Destination] " + destination + "\n" +
1472                     "[Duration] " + panAnimationDuration + "\n" +
1473                     "================================ \n"
1474                 );
1475             }
1476
1477             finalTargetPosition = destination;
1478
1479             customScrollAlphaFunction = new UserAlphaFunctionDelegate(CustomScrollAlphaFunction);
1480             animation.DefaultAlphaFunction = new AlphaFunction(customScrollAlphaFunction);
1481             GC.KeepAlive(customScrollAlphaFunction);
1482             animation.Duration = (int)panAnimationDuration;
1483             animation.AnimateTo(ContentContainer, (ScrollingDirection == Direction.Horizontal) ? "PositionX" : "PositionY", destination);
1484             animation.Play();
1485         }
1486
1487         private void ScrollAnimationFinished(object sender, EventArgs e)
1488         {
1489             OnScrollAnimationEnded();
1490         }
1491
1492         /// <summary>
1493         /// Adjust scrolling position by own scrolling rules.
1494         /// Override this function when developer wants to change destination of flicking.(e.g. always snap to center of item)
1495         /// </summary>
1496         /// This may be public opened in tizen_6.0 after ACR done. Before ACR, need to be hidden as inhouse API
1497         [EditorBrowsable(EditorBrowsableState.Never)]
1498         protected virtual float AdjustTargetPositionOfScrollAnimation(float position)
1499         {
1500             return position;
1501         }
1502
1503         /// <summary>
1504         /// Scroll position given to ScrollTo.
1505         /// This is the position in the opposite direction to the position of ContentContainer.
1506         /// </summary>
1507         /// <since_tizen> 8 </since_tizen>
1508         public Position ScrollPosition
1509         {
1510             get
1511             {
1512                 return new Position(-ContentContainer.Position);
1513             }
1514         }
1515
1516         /// <summary>
1517         /// Current scroll position in the middle of ScrollTo animation.
1518         /// This is the position in the opposite direction to the current position of ContentContainer.
1519         /// </summary>
1520         /// <since_tizen> 8 </since_tizen>
1521         public Position ScrollCurrentPosition
1522         {
1523             get
1524             {
1525                 return new Position(-ContentContainer.CurrentPosition);
1526             }
1527         }
1528
1529         /// <summary>
1530         /// Remove all children in ContentContainer.
1531         /// </summary>
1532         /// <param name="dispose">If true, removed child is disposed.</param>
1533         [EditorBrowsable(EditorBrowsableState.Never)]
1534         public void RemoveAllChildren(bool dispose = false)
1535         {
1536             RecursiveRemoveChildren(ContentContainer, dispose);
1537         }
1538
1539         private void RecursiveRemoveChildren(View parent, bool dispose)
1540         {
1541             if (parent == null)
1542             {
1543                 return;
1544             }
1545             int maxChild = (int)parent.GetChildCount();
1546             for (int i = maxChild - 1; i >= 0; --i)
1547             {
1548                 View child = parent.GetChildAt((uint)i);
1549                 if (child == null)
1550                 {
1551                     continue;
1552                 }
1553                 RecursiveRemoveChildren(child, dispose);
1554                 parent.Remove(child);
1555                 if (dispose)
1556                 {
1557                     child.Dispose();
1558                 }
1559             }
1560         }
1561
1562     }
1563
1564 } // namespace