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