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