[NUIComponents] Roll back error modification
[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     [global::System.Diagnostics.CodeAnalysis.SuppressMessage("Design", "CA1001: Types that own disposable fields should be disposable.", Justification = "Scroll event is temporarily used for notifying scroll position update, so position will not be disposed during the event processing.")]
31     public class ScrollEventArgs : EventArgs
32     {
33         // Position class is derived class of Disposable class and they will be implicitly disposed by DisposeQueue,
34         // so that there will be no memory leak.
35         private Position position;
36         private Position scrollPosition;
37
38         /// <summary>
39         /// Default constructor.
40         /// </summary>
41         /// <param name="position">Current container position</param>
42         /// <since_tizen> 8 </since_tizen>
43         public ScrollEventArgs(Position position)
44         {
45             this.position = position;
46             this.scrollPosition = new Position(-position);
47         }
48
49         /// <summary>
50         /// Current position of ContentContainer.
51         /// </summary>
52         /// <since_tizen> 8 </since_tizen>
53         public Position Position
54         {
55             get
56             {
57                 return position;
58             }
59         }
60         /// <summary>
61         /// Current scroll position of scrollableBase pan.
62         /// This is the position in the opposite direction to the current position of ContentContainer.
63         /// </summary>
64         [EditorBrowsable(EditorBrowsableState.Never)]
65         public Position ScrollPosition
66         {
67             get
68             {
69                 return scrollPosition;
70             }
71         }
72     }
73
74     /// <summary>
75     /// ScrollOutofBoundEventArgs is to record scroll out-of-bound event arguments which will be sent to user.
76     /// </summary>
77     [EditorBrowsable(EditorBrowsableState.Never)]
78     public class ScrollOutOfBoundEventArgs : EventArgs
79     {
80         /// <summary>
81         /// The direction to be touched.
82         /// </summary>
83         [EditorBrowsable(EditorBrowsableState.Never)]
84         public enum Direction
85         {
86             /// <summary>
87             /// Upwards.
88             /// </summary>
89             [EditorBrowsable(EditorBrowsableState.Never)]
90             Up,
91
92             /// <summary>
93             /// Downwards.
94             /// </summary>
95             [EditorBrowsable(EditorBrowsableState.Never)]
96             Down,
97
98             /// <summary>
99             /// Left bound.
100             /// </summary>
101             [EditorBrowsable(EditorBrowsableState.Never)]
102             Left,
103
104             /// <summary>
105             /// Right bound.
106             /// </summary>
107             [EditorBrowsable(EditorBrowsableState.Never)]
108             Right,
109         }
110
111         /// <summary>
112         /// Constructor.
113         /// </summary>
114         /// <param name="direction">Current pan direction</param>
115         /// <param name="displacement">Current total displacement</param>
116         [EditorBrowsable(EditorBrowsableState.Never)]
117         public ScrollOutOfBoundEventArgs(Direction direction, float displacement)
118         {
119             PanDirection = direction;
120             Displacement = displacement;
121         }
122
123         /// <summary>
124         /// Current pan direction of ContentContainer.
125         /// </summary>
126         [EditorBrowsable(EditorBrowsableState.Never)]
127         public Direction PanDirection
128         {
129             get;
130         }
131
132         /// <summary>
133         /// Current total displacement of ContentContainer.
134         /// if its value is greater than 0, it is at the top/left;
135         /// if less than 0, it is at the bottom/right.
136         /// </summary>
137         [EditorBrowsable(EditorBrowsableState.Never)]
138         public float Displacement
139         {
140             get;
141         }
142     }
143
144     /// <summary>
145     /// This class provides a View that can scroll a single View with a layout. This View can be a nest of Views.
146     /// </summary>
147     /// <since_tizen> 8 </since_tizen>
148     public partial class ScrollableBase : Control
149     {
150         static bool LayoutDebugScrollableBase = false; // Debug flag
151         static bool focusDebugScrollableBase = false; // Debug flag
152         private Direction mScrollingDirection = Direction.Vertical;
153         private bool mScrollEnabled = true;
154         private int mScrollDuration = 125;
155         private int mPageWidth = 0;
156         private float mPageFlickThreshold = 0.4f;
157         private float mScrollingEventThreshold = 0.001f;
158         private bool fadeScrollbar = true;
159
160         private class ScrollableBaseCustomLayout : AbsoluteLayout
161         {
162             protected override void OnMeasure(MeasureSpecification widthMeasureSpec, MeasureSpecification heightMeasureSpec)
163             {
164                 MeasuredSize.StateType childWidthState = MeasuredSize.StateType.MeasuredSizeOK;
165                 MeasuredSize.StateType childHeightState = MeasuredSize.StateType.MeasuredSizeOK;
166
167                 Direction scrollingDirection = Direction.Vertical;
168                 ScrollableBase scrollableBase = this.Owner as ScrollableBase;
169                 if (scrollableBase != null)
170                 {
171                     scrollingDirection = scrollableBase.ScrollingDirection;
172                 }
173
174                 float totalWidth = 0.0f;
175                 float totalHeight = 0.0f;
176
177                 // measure child, should be a single scrolling child
178                 foreach (LayoutItem childLayout in LayoutChildren)
179                 {
180                     if (childLayout != null && childLayout.Owner.Name == "ContentContainer")
181                     {
182                         // Get size of child
183                         // Use an Unspecified MeasureSpecification mode so scrolling child is not restricted to it's parents size in Height (for vertical scrolling)
184                         // or Width for horizontal scrolling
185                         if (scrollingDirection == Direction.Vertical)
186                         {
187                             MeasureSpecification unrestrictedMeasureSpec = new MeasureSpecification(heightMeasureSpec.Size, MeasureSpecification.ModeType.Unspecified);
188                             MeasureChildWithMargins(childLayout, widthMeasureSpec, new LayoutLength(0), unrestrictedMeasureSpec, new LayoutLength(0)); // Height unrestricted by parent
189                         }
190                         else
191                         {
192                             MeasureSpecification unrestrictedMeasureSpec = new MeasureSpecification(widthMeasureSpec.Size, MeasureSpecification.ModeType.Unspecified);
193                             MeasureChildWithMargins(childLayout, unrestrictedMeasureSpec, new LayoutLength(0), heightMeasureSpec, new LayoutLength(0)); // Width unrestricted by parent
194                         }
195
196                         totalWidth = (childLayout.MeasuredWidth.Size + (childLayout.Padding.Start + childLayout.Padding.End)).AsDecimal();
197                         totalHeight = (childLayout.MeasuredHeight.Size + (childLayout.Padding.Top + childLayout.Padding.Bottom)).AsDecimal();
198
199                         if (childLayout.MeasuredWidth.State == MeasuredSize.StateType.MeasuredSizeTooSmall)
200                         {
201                             childWidthState = MeasuredSize.StateType.MeasuredSizeTooSmall;
202                         }
203                         if (childLayout.MeasuredHeight.State == MeasuredSize.StateType.MeasuredSizeTooSmall)
204                         {
205                             childHeightState = MeasuredSize.StateType.MeasuredSizeTooSmall;
206                         }
207                     }
208                 }
209
210                 MeasuredSize widthSizeAndState = ResolveSizeAndState(new LayoutLength(totalWidth), widthMeasureSpec, MeasuredSize.StateType.MeasuredSizeOK);
211                 MeasuredSize heightSizeAndState = ResolveSizeAndState(new LayoutLength(totalHeight), heightMeasureSpec, MeasuredSize.StateType.MeasuredSizeOK);
212                 totalWidth = widthSizeAndState.Size.AsDecimal();
213                 totalHeight = heightSizeAndState.Size.AsDecimal();
214
215                 // Ensure layout respects it's given minimum size
216                 totalWidth = Math.Max(totalWidth, SuggestedMinimumWidth.AsDecimal());
217                 totalHeight = Math.Max(totalHeight, SuggestedMinimumHeight.AsDecimal());
218
219                 widthSizeAndState.State = childWidthState;
220                 heightSizeAndState.State = childHeightState;
221
222                 SetMeasuredDimensions(ResolveSizeAndState(new LayoutLength(totalWidth), widthMeasureSpec, childWidthState),
223                     ResolveSizeAndState(new LayoutLength(totalHeight), heightMeasureSpec, childHeightState));
224
225                 // Size of ScrollableBase is changed. Change Page width too.
226                 if (scrollableBase != null)
227                 {
228                     scrollableBase.mPageWidth = (int)MeasuredWidth.Size.AsRoundedValue();
229                     scrollableBase.OnScrollingChildRelayout(null, null);
230                 }
231             }
232         } //  ScrollableBaseCustomLayout
233
234         /// <summary>
235         /// The direction axis to scroll.
236         /// </summary>
237         /// <since_tizen> 8 </since_tizen>
238         public enum Direction
239         {
240             /// <summary>
241             /// Horizontal axis.
242             /// </summary>
243             /// <since_tizen> 8 </since_tizen>
244             Horizontal,
245
246             /// <summary>
247             /// Vertical axis.
248             /// </summary>
249             /// <since_tizen> 8 </since_tizen>
250             Vertical
251         }
252
253         /// <summary>
254         /// Scrolling direction mode.
255         /// Default is Vertical scrolling.
256         /// </summary>
257         /// <since_tizen> 8 </since_tizen>
258         public Direction ScrollingDirection
259         {
260             get
261             {
262                 return (Direction)GetValue(ScrollingDirectionProperty);
263             }
264             set
265             {
266                 SetValue(ScrollingDirectionProperty, value);
267                 NotifyPropertyChanged();
268             }
269         }
270         private Direction InternalScrollingDirection
271         {
272             get
273             {
274                 return mScrollingDirection;
275             }
276             set
277             {
278                 if (value != mScrollingDirection)
279                 {
280                     //Reset scroll position and stop scroll animation
281                     ScrollTo(0, false);
282
283                     mScrollingDirection = value;
284                     mPanGestureDetector.ClearAngles();
285                     mPanGestureDetector.AddDirection(value == Direction.Horizontal ?
286                         PanGestureDetector.DirectionHorizontal : PanGestureDetector.DirectionVertical);
287
288                     ContentContainer.WidthSpecification = ScrollingDirection == Direction.Vertical ? LayoutParamPolicies.MatchParent : LayoutParamPolicies.WrapContent;
289                     ContentContainer.HeightSpecification = ScrollingDirection == Direction.Vertical ? LayoutParamPolicies.WrapContent : LayoutParamPolicies.MatchParent;
290                     SetScrollbar();
291                 }
292             }
293         }
294
295         /// <summary>
296         /// Enable or disable scrolling.
297         /// </summary>
298         /// <since_tizen> 8 </since_tizen>
299         public bool ScrollEnabled
300         {
301             get
302             {
303                 return (bool)GetValue(ScrollEnabledProperty);
304             }
305             set
306             {
307                 SetValue(ScrollEnabledProperty, value);
308                 NotifyPropertyChanged();
309             }
310         }
311         private bool InternalScrollEnabled
312         {
313             get
314             {
315                 return mScrollEnabled;
316             }
317             set
318             {
319                 if (value != mScrollEnabled)
320                 {
321                     mScrollEnabled = value;
322                     if (mScrollEnabled)
323                     {
324                         mPanGestureDetector.Detected += OnPanGestureDetected;
325                     }
326                     else
327                     {
328                         mPanGestureDetector.Detected -= OnPanGestureDetected;
329                     }
330                 }
331             }
332         }
333
334         /// <summary>
335         /// Gets scrollable status.
336         /// </summary>
337         [EditorBrowsable(EditorBrowsableState.Never)]
338         protected override bool AccessibilityIsScrollable()
339         {
340             return true;
341         }
342
343         /// <summary>
344         /// Pages mode, enables moving to the next or return to current page depending on pan displacement.
345         /// Default is false.
346         /// </summary>
347         /// <since_tizen> 8 </since_tizen>
348         public bool SnapToPage
349         {
350             get
351             {
352                 return (bool)GetValue(SnapToPageProperty);
353             }
354             set
355             {
356                 SetValue(SnapToPageProperty, value);
357                 NotifyPropertyChanged();
358             }
359         }
360         private bool InternalSnapToPage { set; get; } = false;
361
362         /// <summary>
363         /// Get current page.
364         /// Working property with SnapToPage property.
365         /// </summary>
366         /// <since_tizen> 8 </since_tizen>
367         public int CurrentPage { get; private set; } = 0;
368
369         /// <summary>
370         /// Duration of scroll animation.
371         /// Default value is 125ms.
372         /// </summary>
373         /// <since_tizen> 8 </since_tizen>
374         public int ScrollDuration
375         {
376             get
377             {
378                 return (int)GetValue(ScrollDurationProperty);
379             }
380             set
381             {
382                 SetValue(ScrollDurationProperty, value);
383                 NotifyPropertyChanged();
384             }
385         }
386         private int InternalScrollDuration
387         {
388             set
389             {
390                 mScrollDuration = value >= 0 ? value : mScrollDuration;
391             }
392             get
393             {
394                 return mScrollDuration;
395             }
396         }
397
398         /// <summary>
399         /// Scroll Available area.
400         /// </summary>
401         /// <since_tizen> 8 </since_tizen>
402         public Vector2 ScrollAvailableArea
403         {
404             get
405             {
406                 return GetValue(ScrollAvailableAreaProperty) as Vector2;
407             }
408             set
409             {
410                 SetValue(ScrollAvailableAreaProperty, value);
411                 NotifyPropertyChanged();
412             }
413         }
414         private Vector2 InternalScrollAvailableArea { set; get; }
415
416         /// <summary>
417         /// An event emitted when user starts dragging ScrollableBase, user can subscribe or unsubscribe to this event handler.<br />
418         /// </summary>
419         /// <since_tizen> 8 </since_tizen>
420         public event EventHandler<ScrollEventArgs> ScrollDragStarted;
421
422         /// <summary>
423         /// An event emitted when user stops dragging ScrollableBase, user can subscribe or unsubscribe to this event handler.<br />
424         /// </summary>
425         /// <since_tizen> 8 </since_tizen>
426         public event EventHandler<ScrollEventArgs> ScrollDragEnded;
427
428         /// <summary>
429         /// An event emitted when the scrolling slide animation starts, user can subscribe or unsubscribe to this event handler.<br />
430         /// </summary>
431         /// <since_tizen> 8 </since_tizen>
432         public event EventHandler<ScrollEventArgs> ScrollAnimationStarted;
433
434         /// <summary>
435         /// An event emitted when the scrolling slide animation ends, user can subscribe or unsubscribe to this event handler.<br />
436         /// </summary>
437         /// <since_tizen> 8 </since_tizen>
438         public event EventHandler<ScrollEventArgs> ScrollAnimationEnded;
439
440         /// <summary>
441         /// An event emitted when scrolling, user can subscribe or unsubscribe to this event handler.<br />
442         /// </summary>
443         /// <since_tizen> 8 </since_tizen>
444         public event EventHandler<ScrollEventArgs> Scrolling;
445
446         /// <summary>
447         /// An event emitted when scrolling out of bound, user can subscribe or unsubscribe to this event handler.<br />
448         /// </summary>
449         [EditorBrowsable(EditorBrowsableState.Never)]
450         public event EventHandler<ScrollOutOfBoundEventArgs> ScrollOutOfBound;
451
452         /// <summary>
453         /// Scrollbar for ScrollableBase.
454         /// </summary>
455         /// <since_tizen> 8 </since_tizen>
456         public ScrollbarBase Scrollbar
457         {
458             get
459             {
460                 return GetValue(ScrollbarProperty) as ScrollbarBase;
461             }
462             set
463             {
464                 SetValue(ScrollbarProperty, value);
465                 NotifyPropertyChanged();
466             }
467         }
468         private ScrollbarBase InternalScrollbar
469         {
470             get
471             {
472                 return scrollBar;
473             }
474             set
475             {
476                 if (scrollBar)
477                 {
478                     base.Remove(scrollBar);
479                 }
480                 scrollBar = value;
481
482                 if (scrollBar != null)
483                 {
484                     scrollBar.Name = "ScrollBar";
485                     base.Add(scrollBar);
486
487                     if (hideScrollbar)
488                     {
489                         scrollBar.Hide();
490                     }
491                     else
492                     {
493                         scrollBar.Show();
494                     }
495
496                     SetScrollbar();
497                 }
498             }
499         }
500
501         /// <summary>
502         /// Always hide Scrollbar.
503         /// </summary>
504         /// <since_tizen> 8 </since_tizen>
505         public bool HideScrollbar
506         {
507             get
508             {
509                 return (bool)GetValue(HideScrollbarProperty);
510             }
511             set
512             {
513                 SetValue(HideScrollbarProperty, value);
514                 NotifyPropertyChanged();
515             }
516         }
517         private bool InternalHideScrollbar
518         {
519             get
520             {
521                 return hideScrollbar;
522             }
523             set
524             {
525                 hideScrollbar = value;
526
527                 if (scrollBar)
528                 {
529                     if (value)
530                     {
531                         scrollBar.Hide();
532                     }
533                     else
534                     {
535                         scrollBar.Show();
536                         if (fadeScrollbar)
537                         {
538                             scrollBar.Opacity = 1.0f;
539                             scrollBar.FadeOut();
540                         }
541                     }
542                 }
543             }
544         }
545
546         /// <summary>
547         /// The boolean flag for automatic fading Scrollbar.
548         /// Scrollbar will be faded out when scroll stay in certain position longer than the threshold.
549         /// Scrollbar will be faded in scroll position changes.
550         /// </summary>
551         [EditorBrowsable(EditorBrowsableState.Never)]
552         public bool FadeScrollbar
553         {
554             get => (bool)GetValue(FadeScrollbarProperty);
555             set => SetValue(FadeScrollbarProperty, value);
556         }
557
558         private bool InternalFadeScrollbar
559         {
560             get
561             {
562                 return fadeScrollbar;
563             }
564             set
565             {
566                 fadeScrollbar = value;
567
568                 if (scrollBar != null && !hideScrollbar)
569                 {
570                     if (value)
571                     {
572                         scrollBar.FadeOut();
573                     }
574                     else
575                     {
576                         scrollBar.Opacity = 1.0f;
577                         // Removing fadeout timer and animation.
578                         scrollBar.FadeIn();
579                     }
580                 }
581             }
582         }
583
584         /// <summary>
585         /// Container which has content of ScrollableBase.
586         /// </summary>
587         /// <since_tizen> 8 </since_tizen>
588         public View ContentContainer { get; private set; }
589
590         /// <summary>
591         /// Set the layout on this View. Replaces any existing Layout.
592         /// </summary>
593         /// <since_tizen> 8 </since_tizen>
594         public new LayoutItem Layout
595         {
596             get
597             {
598                 return GetValue(LayoutProperty) as LayoutItem;
599             }
600             set
601             {
602                 SetValue(LayoutProperty, value);
603                 NotifyPropertyChanged();
604             }
605         }
606         private LayoutItem InternalLayout
607         {
608             get
609             {
610                 return ContentContainer.Layout;
611             }
612             set
613             {
614                 ContentContainer.Layout = value;
615             }
616         }
617
618         /// <summary>
619         /// List of children of Container.
620         /// </summary>
621         /// <since_tizen> 8 </since_tizen>
622         public new List<View> Children
623         {
624             get
625             {
626                 return ContentContainer.Children;
627             }
628         }
629
630         /// <summary>
631         /// Deceleration rate of scrolling by finger.
632         /// Rate should be bigger than 0 and smaller than 1.
633         /// Default value is 0.998f;
634         /// </summary>
635         /// <since_tizen> 8 </since_tizen>
636         public float DecelerationRate
637         {
638             get
639             {
640                 return (float)GetValue(DecelerationRateProperty);
641             }
642             set
643             {
644                 SetValue(DecelerationRateProperty, value);
645                 NotifyPropertyChanged();
646             }
647         }
648         private float InternalDecelerationRate
649         {
650             get
651             {
652                 return decelerationRate;
653             }
654             set
655             {
656                 decelerationRate = (value < 1 && value > 0) ? value : decelerationRate;
657                 logValueOfDeceleration = (float)Math.Log(value);
658             }
659         }
660
661         /// <summary>
662         /// Threshold not to go infinite at the end of scrolling animation.
663         /// </summary>
664         [EditorBrowsable(EditorBrowsableState.Never)]
665         public float DecelerationThreshold
666         {
667             get
668             {
669                 return (float)GetValue(DecelerationThresholdProperty);
670             }
671             set
672             {
673                 SetValue(DecelerationThresholdProperty, value);
674                 NotifyPropertyChanged();
675             }
676         }
677         private float InternalDecelerationThreshold { get; set; } = 0.1f;
678
679         /// <summary>
680         /// Scrolling event will be thrown when this amount of scroll position is changed.
681         /// If this threshold becomes smaller, the tracking detail increases but the scrolling range that can be tracked becomes smaller.
682         /// If large sized ContentContainer is required, please use larger threshold value.
683         /// Default ScrollingEventThreshold value is 0.001f.
684         /// </summary>
685         [EditorBrowsable(EditorBrowsableState.Never)]
686         public float ScrollingEventThreshold
687         {
688             get
689             {
690                 return (float)GetValue(ScrollingEventThresholdProperty);
691             }
692             set
693             {
694                 SetValue(ScrollingEventThresholdProperty, value);
695                 NotifyPropertyChanged();
696             }
697         }
698         private float InternalScrollingEventThreshold
699         {
700             get
701             {
702                 return mScrollingEventThreshold;
703             }
704             set
705             {
706                 if (mScrollingEventThreshold != value && value > 0)
707                 {
708                     ContentContainer.RemovePropertyNotification(propertyNotification);
709                     propertyNotification = ContentContainer.AddPropertyNotification("position", PropertyCondition.Step(value));
710                     propertyNotification.Notified += OnPropertyChanged;
711                     mScrollingEventThreshold = value;
712                 }
713             }
714         }
715
716         /// <summary>
717         /// Page will be changed when velocity of panning is over threshold.
718         /// The unit of threshold is pixel per millisecond.
719         /// </summary>
720         /// <since_tizen> 8 </since_tizen>
721         public float PageFlickThreshold
722         {
723             get
724             {
725                 return (float)GetValue(PageFlickThresholdProperty);
726             }
727             set
728             {
729                 SetValue(PageFlickThresholdProperty, value);
730                 NotifyPropertyChanged();
731             }
732         }
733         private float InternalPageFlickThreshold
734         {
735             get
736             {
737                 return mPageFlickThreshold;
738             }
739             set
740             {
741                 mPageFlickThreshold = value >= 0f ? value : mPageFlickThreshold;
742             }
743         }
744
745         /// <summary>
746         /// Padding for the ScrollableBase
747         /// </summary>
748         [EditorBrowsable(EditorBrowsableState.Never)]
749         public new Extents Padding
750         {
751             get
752             {
753                 return GetValue(PaddingProperty) as Extents;
754             }
755             set
756             {
757                 SetValue(PaddingProperty, value);
758                 NotifyPropertyChanged();
759             }
760         }
761         private Extents InternalPadding
762         {
763             get
764             {
765                 return ContentContainer.Padding;
766             }
767             set
768             {
769                 ContentContainer.Padding = value;
770             }
771         }
772
773         /// <summary>
774         /// Alphafunction for scroll animation.
775         /// </summary>
776         [EditorBrowsable(EditorBrowsableState.Never)]
777         public AlphaFunction ScrollAlphaFunction
778         {
779             get
780             {
781                 return GetValue(ScrollAlphaFunctionProperty) as AlphaFunction;
782             }
783             set
784             {
785                 SetValue(ScrollAlphaFunctionProperty, value);
786                 NotifyPropertyChanged();
787             }
788         }
789         private AlphaFunction InternalScrollAlphaFunction { get; set; } = new AlphaFunction(AlphaFunction.BuiltinFunctions.Linear);
790
791         private bool hideScrollbar = true;
792         private float maxScrollDistance;
793         private float childTargetPosition = 0.0f;
794         private PanGestureDetector mPanGestureDetector;
795         private ScrollbarBase scrollBar;
796         private bool scrolling = false;
797         private float ratioOfScreenWidthToCompleteScroll = 0.5f;
798         private float totalDisplacementForPan = 0.0f;
799         private Size previousContainerSize = new Size();
800         private Size previousSize = new Size();
801         private PropertyNotification propertyNotification;
802         private float noticeAnimationEndBeforePosition = 0.0f;
803         private bool readyToNotice = false;
804
805         /// <summary>
806         /// Notice before animation is finished.
807         /// </summary>
808         [EditorBrowsable(EditorBrowsableState.Never)]
809         // Let's consider more whether this needs to be set as protected.
810         public float NoticeAnimationEndBeforePosition
811         {
812             get
813             {
814                 return (float)GetValue(NoticeAnimationEndBeforePositionProperty);
815             }
816             set
817             {
818                 SetValue(NoticeAnimationEndBeforePositionProperty, value);
819                 NotifyPropertyChanged();
820             }
821         }
822         private float InternalNoticeAnimationEndBeforePosition
823         {
824             get => noticeAnimationEndBeforePosition;
825             set => noticeAnimationEndBeforePosition = value;
826         }
827
828         /// <summary>
829         /// Step scroll move distance.
830         /// Key focus originally moves focusable objects, but in ScrollableBase,
831         /// if focusable object is too far or un-exist and ScrollableBase is focusable,
832         /// it can scroll move itself by key input.
833         /// this value decide how long distance will it moves in one step.
834         /// if any value is not set, step will be moved quater size of ScrollableBase length.
835         /// </summary>
836         [EditorBrowsable(EditorBrowsableState.Never)]
837         public float StepScrollDistance
838         {
839             get
840             {
841                 return (float)GetValue(StepScrollDistanceProperty);
842             }
843             set
844             {
845                 SetValue(StepScrollDistanceProperty, value);
846                 NotifyPropertyChanged();
847             }
848         }
849         private float stepScrollDistance = 0f;
850
851         /// <summary>
852         /// Wheel scroll move distance.
853         /// This value decide how long distance will it moves in wheel event.
854         /// </summary>
855         [EditorBrowsable(EditorBrowsableState.Never)]
856         public float WheelScrollDistance
857         {
858             get
859             {
860                 return (float)GetValue(WheelScrollDistanceProperty);
861             }
862             set
863             {
864                 SetValue(WheelScrollDistanceProperty, value);
865                 NotifyPropertyChanged();
866             }
867         }
868         private float wheelScrollDistance = 50f;
869
870
871         // Let's consider more whether this needs to be set as protected.
872         private float finalTargetPosition;
873
874         private Animation scrollAnimation;
875         // Declare user alpha function delegate
876         [UnmanagedFunctionPointer(CallingConvention.StdCall)]
877         private delegate float UserAlphaFunctionDelegate(float progress);
878         private UserAlphaFunctionDelegate customScrollAlphaFunction;
879         private float velocityOfLastPan = 0.0f;
880         private float panAnimationDuration = 0.0f;
881         private float panAnimationDelta = 0.0f;
882         private float logValueOfDeceleration = 0.0f;
883         private float decelerationRate = 0.0f;
884
885         private View topOverShootingShadowView;
886         private View bottomOverShootingShadowView;
887         private View leftOverShootingShadowView;
888         private View rightOverShootingShadowView;
889         private const int overShootingShadowScaleHeightLimit = 64 * 3;
890         private const int overShootingShadowAnimationDuration = 300;
891         private Animation overShootingShadowAnimation;
892         private bool isOverShootingShadowShown = false;
893         private float startShowShadowDisplacement;
894
895         private void Initialize()
896         {
897             DecelerationRate = 0.998f;
898
899             base.Layout = new ScrollableBaseCustomLayout();
900             mPanGestureDetector = new PanGestureDetector();
901             mPanGestureDetector.Attach(this);
902             mPanGestureDetector.AddDirection(PanGestureDetector.DirectionVertical);
903             if (mPanGestureDetector.GetMaximumTouchesRequired() < 2) mPanGestureDetector.SetMaximumTouchesRequired(2);
904             mPanGestureDetector.Detected += OnPanGestureDetected;
905
906             ClippingMode = ClippingModeType.ClipToBoundingBox;
907
908             //Default Scrolling child
909             ContentContainer = new View()
910             {
911                 Name = "ContentContainer",
912                 WidthSpecification = ScrollingDirection == Direction.Vertical ? LayoutParamPolicies.MatchParent : LayoutParamPolicies.WrapContent,
913                 HeightSpecification = ScrollingDirection == Direction.Vertical ? LayoutParamPolicies.WrapContent : LayoutParamPolicies.MatchParent,
914             };
915             // Check if children's sizes change to update Scrollbar
916             ContentContainer.Relayout += OnScrollingChildRelayout;
917             propertyNotification = ContentContainer.AddPropertyNotification("position", PropertyCondition.Step(mScrollingEventThreshold));
918             propertyNotification.Notified += OnPropertyChanged;
919             base.Add(ContentContainer);
920             // Check if ScrollableBase's size changes to update Scrollbar
921             base.Relayout += OnScrollingChildRelayout;
922
923             Scrollbar = new Scrollbar();
924
925             //Show vertical shadow on the top (or bottom) of the scrollable when panning down (or up).
926             topOverShootingShadowView = new View
927             {
928                 BackgroundImage = FrameworkInformation.ResourcePath + "nui_component_default_scroll_over_shooting_top.png",
929                 Opacity = 1.0f,
930                 SizeHeight = 0.0f,
931                 PositionUsesPivotPoint = true,
932                 ParentOrigin = NUI.ParentOrigin.TopCenter,
933                 PivotPoint = NUI.PivotPoint.TopCenter,
934             };
935             bottomOverShootingShadowView = new View
936             {
937                 BackgroundImage = FrameworkInformation.ResourcePath + "nui_component_default_scroll_over_shooting_bottom.png",
938                 Opacity = 1.0f,
939                 SizeHeight = 0.0f,
940                 PositionUsesPivotPoint = true,
941                 ParentOrigin = NUI.ParentOrigin.BottomCenter,
942                 PivotPoint = NUI.PivotPoint.BottomCenter,
943             };
944             //Show horizontal shadow on the left (or right) of the scrollable when panning down (or up).
945             leftOverShootingShadowView = new View
946             {
947                 BackgroundImage = FrameworkInformation.ResourcePath + "nui_component_default_scroll_over_shooting_left.png",
948                 Opacity = 1.0f,
949                 SizeWidth = 0.0f,
950                 PositionUsesPivotPoint = true,
951                 ParentOrigin = NUI.ParentOrigin.CenterLeft,
952                 PivotPoint = NUI.PivotPoint.CenterLeft,
953             };
954             rightOverShootingShadowView = new View
955             {
956                 BackgroundImage = FrameworkInformation.ResourcePath + "nui_component_default_scroll_over_shooting_right.png",
957                 Opacity = 1.0f,
958                 SizeWidth = 0.0f,
959                 PositionUsesPivotPoint = true,
960                 ParentOrigin = NUI.ParentOrigin.CenterRight,
961                 PivotPoint = NUI.PivotPoint.CenterRight,
962             };
963
964             WheelEvent += OnWheelEvent;
965
966             AccessibilityManager.Instance.SetAccessibilityAttribute(this, AccessibilityManager.AccessibilityAttribute.Trait, "ScrollableBase");
967
968             SetKeyboardNavigationSupport(true);
969         }
970
971         /// <summary>
972         /// Default Constructor
973         /// </summary>
974         /// <since_tizen> 8 </since_tizen>
975         public ScrollableBase() : base()
976         {
977             Initialize();
978         }
979
980         /// <summary>
981         /// Creates a new instance of a ScrollableBase with style.
982         /// </summary>
983         /// <param name="style">Creates ScrollableBase by special style defined in UX.</param>
984         [EditorBrowsable(EditorBrowsableState.Never)]
985         public ScrollableBase(string style) : base(style)
986         {
987             Initialize();
988         }
989
990         /// <summary>
991         /// Creates a new instance of a ScrollableBase with style.
992         /// </summary>
993         /// <param name="style">A style applied to the newly created ScrollableBase.</param>
994         [EditorBrowsable(EditorBrowsableState.Never)]
995         public ScrollableBase(ControlStyle style) : base(style)
996         {
997             Initialize();
998         }
999
1000         private bool OnInterruptTouchingChildTouched(object source, View.TouchEventArgs args)
1001         {
1002             if (args.Touch.GetState(0) == PointStateType.Down)
1003             {
1004                 if (scrolling && !SnapToPage)
1005                 {
1006                     StopScroll();
1007                 }
1008             }
1009             return true;
1010         }
1011
1012         private void OnPropertyChanged(object source, PropertyNotification.NotifyEventArgs args)
1013         {
1014             OnScroll();
1015         }
1016
1017         /// <summary>
1018         /// Called after a child has been added to the owning view.
1019         /// </summary>
1020         /// <param name="view">The child which has been added.</param>
1021         /// <since_tizen> 8 </since_tizen>
1022         public override void Add(View view)
1023         {
1024             ContentContainer.Add(view);
1025         }
1026
1027         /// <summary>
1028         /// Called after a child has been removed from the owning view.
1029         /// </summary>
1030         /// <param name="view">The child which has been removed.</param>
1031         /// <since_tizen> 8 </since_tizen>
1032         public override void Remove(View view)
1033         {
1034             if (SnapToPage && CurrentPage == Children.IndexOf(view) && CurrentPage == Children.Count - 1 && Children.Count > 1)
1035             {
1036                 // Target View is current page and also last child.
1037                 // CurrentPage should be changed to previous page.
1038                 ScrollToIndex(CurrentPage - 1);
1039             }
1040
1041             ContentContainer.Remove(view);
1042         }
1043
1044         private void OnScrollingChildRelayout(object source, EventArgs args)
1045         {
1046             // Size is changed. Calculate maxScrollDistance.
1047             bool isSizeChanged = previousContainerSize.Width != ContentContainer.Size.Width || previousContainerSize.Height != ContentContainer.Size.Height ||
1048                 previousSize.Width != Size.Width || previousSize.Height != Size.Height;
1049
1050             if (isSizeChanged)
1051             {
1052                 maxScrollDistance = CalculateMaximumScrollDistance();
1053                 if (!ReviseContainerPositionIfNeed())
1054                 {
1055                     UpdateScrollbar();
1056                 }
1057             }
1058
1059             previousContainerSize = new Size(ContentContainer.Size);
1060             previousSize = new Size(Size);
1061         }
1062
1063         private bool ReviseContainerPositionIfNeed()
1064         {
1065             bool isHorizontal = ScrollingDirection == Direction.Horizontal;
1066             float currentPosition = isHorizontal ? ContentContainer.CurrentPosition.X : ContentContainer.CurrentPosition.Y;
1067
1068             if (Math.Abs(currentPosition) > maxScrollDistance)
1069             {
1070                 StopScroll();
1071                 var targetPosition = BoundScrollPosition(-maxScrollDistance);
1072                 if (isHorizontal) ContentContainer.PositionX = targetPosition;
1073                 else ContentContainer.PositionY = targetPosition;
1074                 return true;
1075             }
1076
1077             return false;
1078         }
1079
1080         /// <summary>
1081         /// The composition of a Scrollbar can vary depending on how you use ScrollableBase.
1082         /// Set the composition that will go into the ScrollableBase according to your ScrollableBase.
1083         /// </summary>
1084         /// <since_tizen> 8 </since_tizen>
1085         [EditorBrowsable(EditorBrowsableState.Never)]
1086         protected virtual void SetScrollbar()
1087         {
1088             if (Scrollbar)
1089             {
1090                 bool isHorizontal = ScrollingDirection == Direction.Horizontal;
1091                 float contentLength = isHorizontal ? ContentContainer.Size.Width : ContentContainer.Size.Height;
1092                 float viewportLength = isHorizontal ? Size.Width : Size.Height;
1093                 float currentPosition = isHorizontal ? ContentContainer.CurrentPosition.X : ContentContainer.CurrentPosition.Y;
1094                 Scrollbar.Initialize(contentLength, viewportLength, -currentPosition, isHorizontal);
1095             }
1096         }
1097
1098         /// Update scrollbar position and size.
1099         [EditorBrowsable(EditorBrowsableState.Never)]
1100         protected virtual void UpdateScrollbar()
1101         {
1102             if (Scrollbar)
1103             {
1104                 bool isHorizontal = ScrollingDirection == Direction.Horizontal;
1105                 float contentLength = isHorizontal ? ContentContainer.Size.Width : ContentContainer.Size.Height;
1106                 float viewportLength = isHorizontal ? Size.Width : Size.Height;
1107                 float currentPosition = isHorizontal ? ContentContainer.CurrentPosition.X : ContentContainer.CurrentPosition.Y;
1108                 Scrollbar.Update(contentLength, viewportLength, -currentPosition);
1109
1110                 if (!hideScrollbar && fadeScrollbar)
1111                 {
1112                     Scrollbar.FadeOut();
1113                 }
1114             }
1115         }
1116
1117         /// <summary>
1118         /// Scrolls to the item at the specified index.
1119         /// </summary>
1120         /// <param name="index">Index of item.</param>
1121         /// <since_tizen> 8 </since_tizen>
1122         public void ScrollToIndex(int index)
1123         {
1124             if (ContentContainer.ChildCount - 1 < index || index < 0)
1125             {
1126                 return;
1127             }
1128
1129             if (SnapToPage)
1130             {
1131                 CurrentPage = index;
1132             }
1133
1134             float targetPosition = Math.Min(ScrollingDirection == Direction.Vertical ? Children[index].Position.Y : Children[index].Position.X, maxScrollDistance);
1135             AnimateChildTo(ScrollDuration, -targetPosition);
1136         }
1137
1138         internal void ScrollToChild(View child, bool anim = false)
1139         {
1140             if (null == FindDescendantByID(child.ID)) return;
1141
1142             bool isHorizontal = (ScrollingDirection == Direction.Horizontal);
1143
1144             float viewScreenPosition = (isHorizontal ? ScreenPosition.X : ScreenPosition.Y);
1145             float childScreenPosition = (isHorizontal ? child.ScreenPosition.X : child.ScreenPosition.Y);
1146             float scrollPosition = (isHorizontal ? ScrollPosition.X : ScrollPosition.Y);
1147             float viewSize = (isHorizontal ? SizeWidth : SizeHeight);
1148             float childSize = (isHorizontal ? child.SizeWidth : child.SizeHeight);
1149
1150             if (viewScreenPosition > childScreenPosition ||
1151                 viewScreenPosition + viewSize < childScreenPosition + childSize)
1152             {// if object is outside
1153                 float targetPosition;
1154                 float dist = viewScreenPosition - childScreenPosition;
1155                 if (dist > 0)
1156                 {// if object is upper side
1157                     targetPosition = scrollPosition - dist;
1158                 }
1159                 else
1160                 {// if object is down side
1161                     targetPosition = scrollPosition - dist + childSize - viewSize;
1162                 }
1163                 ScrollTo(targetPosition, anim);
1164             }
1165         }
1166
1167         private void OnScrollDragStarted()
1168         {
1169             ScrollEventArgs eventArgs = new ScrollEventArgs(ContentContainer.CurrentPosition);
1170             ScrollDragStarted?.Invoke(this, eventArgs);
1171             EmitScrollStartedEvent();
1172
1173             if (!hideScrollbar && fadeScrollbar)
1174             {
1175                 scrollBar?.FadeIn();
1176             }
1177         }
1178
1179         private void OnScrollDragEnded()
1180         {
1181             ScrollEventArgs eventArgs = new ScrollEventArgs(ContentContainer.CurrentPosition);
1182             ScrollDragEnded?.Invoke(this, eventArgs);
1183             EmitScrollFinishedEvent();
1184
1185             if (!hideScrollbar && fadeScrollbar)
1186             {
1187                 scrollBar?.FadeOut();
1188             }
1189         }
1190
1191         private void OnScrollAnimationStarted()
1192         {
1193             ScrollEventArgs eventArgs = new ScrollEventArgs(ContentContainer.CurrentPosition);
1194             ScrollAnimationStarted?.Invoke(this, eventArgs);
1195             EmitScrollStartedEvent();
1196
1197             if (!hideScrollbar && fadeScrollbar)
1198             {
1199                 scrollBar?.FadeIn();
1200             }
1201         }
1202
1203         private void OnScrollAnimationEnded()
1204         {
1205             scrolling = false;
1206             this.InterceptTouchEvent -= OnInterruptTouchingChildTouched;
1207
1208             ScrollEventArgs eventArgs = new ScrollEventArgs(ContentContainer.CurrentPosition);
1209             ScrollAnimationEnded?.Invoke(this, eventArgs);
1210             EmitScrollFinishedEvent();
1211
1212             if (!hideScrollbar && fadeScrollbar)
1213             {
1214                 scrollBar?.FadeOut();
1215             }
1216         }
1217
1218         private void OnScroll()
1219         {
1220             ScrollEventArgs eventArgs = new ScrollEventArgs(ContentContainer.CurrentPosition);
1221             Scrolling?.Invoke(this, eventArgs);
1222
1223             bool isHorizontal = ScrollingDirection == Direction.Horizontal;
1224             float contentLength = isHorizontal ? ContentContainer.Size.Width : ContentContainer.Size.Height;
1225             float currentPosition = isHorizontal ? ContentContainer.CurrentPosition.X : ContentContainer.CurrentPosition.Y;
1226
1227             scrollBar?.Update(contentLength, Math.Abs(currentPosition));
1228             CheckPreReachedTargetPosition();
1229         }
1230
1231         private void CheckPreReachedTargetPosition()
1232         {
1233             // Check whether we reached pre-reached target position
1234             if (readyToNotice &&
1235                 ContentContainer.CurrentPosition.Y <= finalTargetPosition + NoticeAnimationEndBeforePosition &&
1236                 ContentContainer.CurrentPosition.Y >= finalTargetPosition - NoticeAnimationEndBeforePosition)
1237             {
1238                 //Notice first
1239                 readyToNotice = false;
1240                 OnPreReachedTargetPosition(finalTargetPosition);
1241             }
1242         }
1243
1244         /// <summary>
1245         /// This helps developer who wants to know before scroll is reaching target position.
1246         /// </summary>
1247         /// <param name="targetPosition">Index of item.</param>
1248         /// This may be public opened in tizen_6.0 after ACR done. Before ACR, need to be hidden as inhouse API
1249         [EditorBrowsable(EditorBrowsableState.Never)]
1250         protected virtual void OnPreReachedTargetPosition(float targetPosition)
1251         {
1252
1253         }
1254
1255         private void StopScroll()
1256         {
1257             if (scrollAnimation != null)
1258             {
1259                 if (scrollAnimation.State == Animation.States.Playing)
1260                 {
1261                     Debug.WriteLineIf(LayoutDebugScrollableBase, "StopScroll Animation Playing");
1262                     scrollAnimation.Stop(Animation.EndActions.Cancel);
1263                     OnScrollAnimationEnded();
1264                 }
1265                 scrollAnimation.Clear();
1266             }
1267         }
1268
1269         private void AnimateChildTo(int duration, float axisPosition)
1270         {
1271             Debug.WriteLineIf(LayoutDebugScrollableBase, "AnimationTo Animation Duration:" + duration + " Destination:" + axisPosition);
1272             finalTargetPosition = axisPosition;
1273
1274             StopScroll(); // Will replace previous animation so will stop existing one.
1275
1276             if (scrollAnimation == null)
1277             {
1278                 scrollAnimation = new Animation();
1279                 scrollAnimation.Finished += ScrollAnimationFinished;
1280             }
1281
1282             scrollAnimation.Duration = duration;
1283             scrollAnimation.DefaultAlphaFunction = new AlphaFunction(AlphaFunction.BuiltinFunctions.EaseOutSquare);
1284             scrollAnimation.AnimateTo(ContentContainer, (ScrollingDirection == Direction.Horizontal) ? "PositionX" : "PositionY", axisPosition, ScrollAlphaFunction);
1285             scrolling = true;
1286             OnScrollAnimationStarted();
1287             scrollAnimation.Play();
1288         }
1289
1290         /// <summary>
1291         /// Scroll to specific position with or without animation.
1292         /// </summary>
1293         /// <param name="position">Destination.</param>
1294         /// <param name="animate">Scroll with or without animation</param>
1295         /// <since_tizen> 8 </since_tizen>
1296         public void ScrollTo(float position, bool animate)
1297         {
1298             StopScroll();
1299             float currentPositionX = ContentContainer.CurrentPosition.X != 0 ? ContentContainer.CurrentPosition.X : ContentContainer.Position.X;
1300             float currentPositionY = ContentContainer.CurrentPosition.Y != 0 ? ContentContainer.CurrentPosition.Y : ContentContainer.Position.Y;
1301             float delta = ScrollingDirection == Direction.Horizontal ? currentPositionX : currentPositionY;
1302             // The argument position is the new pan position. So the new position of ScrollableBase becomes (-position).
1303             // To move ScrollableBase's position to (-position), it moves by (-position - currentPosition).
1304             delta = -position - delta;
1305
1306             ScrollBy(delta, animate);
1307         }
1308
1309         private float BoundScrollPosition(float targetPosition)
1310         {
1311             if (ScrollAvailableArea != null)
1312             {
1313                 float minScrollPosition = ScrollAvailableArea.X;
1314                 float maxScrollPosition = ScrollAvailableArea.Y;
1315
1316                 targetPosition = Math.Min(-minScrollPosition, targetPosition);
1317                 targetPosition = Math.Max(-maxScrollPosition, targetPosition);
1318             }
1319             else
1320             {
1321                 targetPosition = Math.Min(0, targetPosition);
1322                 targetPosition = Math.Max(-maxScrollDistance, targetPosition);
1323             }
1324
1325             return targetPosition;
1326         }
1327
1328         private void ScrollBy(float displacement, bool animate)
1329         {
1330             if (GetChildCount() == 0 || maxScrollDistance < 0)
1331             {
1332                 return;
1333             }
1334
1335             float childCurrentPosition = (ScrollingDirection == Direction.Horizontal) ? ContentContainer.PositionX : ContentContainer.PositionY;
1336
1337             Debug.WriteLineIf(LayoutDebugScrollableBase, "ScrollBy childCurrentPosition:" + childCurrentPosition +
1338                 " displacement:" + displacement,
1339                 " maxScrollDistance:" + maxScrollDistance);
1340
1341             childTargetPosition = childCurrentPosition + displacement; // child current position + gesture displacement
1342
1343             Debug.WriteLineIf(LayoutDebugScrollableBase, "ScrollBy currentAxisPosition:" + childCurrentPosition + "childTargetPosition:" + childTargetPosition);
1344
1345             if (animate)
1346             {
1347                 // Calculate scroll animation duration
1348                 float scrollDistance = Math.Abs(displacement);
1349                 readyToNotice = true;
1350
1351                 AnimateChildTo(ScrollDuration, BoundScrollPosition(AdjustTargetPositionOfScrollAnimation(BoundScrollPosition(childTargetPosition))));
1352             }
1353             else
1354             {
1355                 StopScroll();
1356                 finalTargetPosition = BoundScrollPosition(childTargetPosition);
1357
1358                 // Set position of scrolling child without an animation
1359                 if (ScrollingDirection == Direction.Horizontal)
1360                 {
1361                     ContentContainer.PositionX = finalTargetPosition;
1362                 }
1363                 else
1364                 {
1365                     ContentContainer.PositionY = finalTargetPosition;
1366                 }
1367             }
1368         }
1369
1370         /// <summary>
1371         /// you can override it to clean-up your own resources.
1372         /// </summary>
1373         /// <param name="type">DisposeTypes</param>
1374         /// This will be public opened in tizen_5.5 after ACR done. Before ACR, need to be hidden as inhouse API.
1375         [EditorBrowsable(EditorBrowsableState.Never)]
1376         protected override void Dispose(DisposeTypes type)
1377         {
1378             if (disposed)
1379             {
1380                 return;
1381             }
1382
1383             StopOverShootingShadowAnimation();
1384             StopScroll();
1385
1386             if (type == DisposeTypes.Explicit)
1387             {
1388                 mPanGestureDetector?.Dispose();
1389                 mPanGestureDetector = null;
1390
1391                 ContentContainer?.RemovePropertyNotification(propertyNotification);
1392                 propertyNotification?.Dispose();
1393                 propertyNotification = null;
1394             }
1395
1396             WheelEvent -= OnWheelEvent;
1397
1398             if (type == DisposeTypes.Explicit)
1399             {
1400
1401             }
1402             base.Dispose(type);
1403         }
1404
1405         private float CalculateMaximumScrollDistance()
1406         {
1407             float scrollingChildLength = 0;
1408             float scrollerLength = 0;
1409             if (ScrollingDirection == Direction.Horizontal)
1410             {
1411                 Debug.WriteLineIf(LayoutDebugScrollableBase, "Horizontal");
1412
1413                 scrollingChildLength = ContentContainer.Size.Width;
1414                 scrollerLength = Size.Width;
1415             }
1416             else
1417             {
1418                 Debug.WriteLineIf(LayoutDebugScrollableBase, "Vertical");
1419                 scrollingChildLength = ContentContainer.Size.Height;
1420                 scrollerLength = Size.Height;
1421             }
1422
1423             Debug.WriteLineIf(LayoutDebugScrollableBase, "ScrollBy maxScrollDistance:" + (scrollingChildLength - scrollerLength) +
1424                 " parent length:" + scrollerLength +
1425                 " scrolling child length:" + scrollingChildLength);
1426
1427             return Math.Max(scrollingChildLength - scrollerLength, 0);
1428         }
1429
1430         private void PageSnap(float velocity)
1431         {
1432             float destination;
1433
1434             Debug.WriteLineIf(LayoutDebugScrollableBase, "PageSnap with pan candidate totalDisplacement:" + totalDisplacementForPan +
1435                 " currentPage[" + CurrentPage + "]");
1436
1437             //Increment current page if total displacement enough to warrant a page change.
1438             if (Math.Abs(totalDisplacementForPan) > (mPageWidth * ratioOfScreenWidthToCompleteScroll))
1439             {
1440                 if (totalDisplacementForPan < 0)
1441                 {
1442                     CurrentPage = Math.Min(Math.Max(Children.Count - 1, 0), ++CurrentPage);
1443                 }
1444                 else
1445                 {
1446                     CurrentPage = Math.Max(0, --CurrentPage);
1447                 }
1448             }
1449             else if (Math.Abs(velocity) > PageFlickThreshold)
1450             {
1451                 if (velocity < 0)
1452                 {
1453                     CurrentPage = Math.Min(Math.Max(Children.Count - 1, 0), ++CurrentPage);
1454                 }
1455                 else
1456                 {
1457                     CurrentPage = Math.Max(0, --CurrentPage);
1458                 }
1459             }
1460
1461             // Animate to new page or reposition to current page
1462             if (ScrollingDirection == Direction.Horizontal)
1463                 destination = -(Children[CurrentPage].Position.X + Children[CurrentPage].CurrentSize.Width / 2 - CurrentSize.Width / 2); // set to middle of current page
1464             else
1465                 destination = -(Children[CurrentPage].Position.Y + Children[CurrentPage].CurrentSize.Height / 2 - CurrentSize.Height / 2);
1466
1467             AnimateChildTo(ScrollDuration, destination);
1468         }
1469
1470         /// <summary>
1471         /// Enable/Disable overshooting effect. default is disabled.
1472         /// </summary>
1473         [EditorBrowsable(EditorBrowsableState.Never)]
1474         public bool EnableOverShootingEffect
1475         {
1476             get
1477             {
1478                 return (bool)GetValue(EnableOverShootingEffectProperty);
1479             }
1480             set
1481             {
1482                 SetValue(EnableOverShootingEffectProperty, value);
1483                 NotifyPropertyChanged();
1484             }
1485         }
1486         private bool InternalEnableOverShootingEffect { get; set; } = false;
1487
1488         private void AttachOverShootingShadowView()
1489         {
1490             if (!EnableOverShootingEffect)
1491                 return;
1492
1493             // stop animation if necessary.
1494             StopOverShootingShadowAnimation();
1495
1496             if (ScrollingDirection == Direction.Horizontal)
1497             {
1498                 base.Add(leftOverShootingShadowView);
1499                 base.Add(rightOverShootingShadowView);
1500
1501                 leftOverShootingShadowView.Size = new Size(0.0f, SizeHeight);
1502                 leftOverShootingShadowView.Opacity = 1.0f;
1503                 leftOverShootingShadowView.RaiseToTop();
1504
1505                 rightOverShootingShadowView.Size = new Size(0.0f, SizeHeight);
1506                 rightOverShootingShadowView.Opacity = 1.0f;
1507                 rightOverShootingShadowView.RaiseToTop();
1508             }
1509             else
1510             {
1511                 base.Add(topOverShootingShadowView);
1512                 base.Add(bottomOverShootingShadowView);
1513
1514                 topOverShootingShadowView.Size = new Size(SizeWidth, 0.0f);
1515                 topOverShootingShadowView.Opacity = 1.0f;
1516                 topOverShootingShadowView.RaiseToTop();
1517
1518                 bottomOverShootingShadowView.Size = new Size(SizeWidth, 0.0f);
1519                 bottomOverShootingShadowView.Opacity = 1.0f;
1520                 bottomOverShootingShadowView.RaiseToTop();
1521             }
1522
1523             // at the beginning, height or width of overshooting shadow is 0, so it is invisible.
1524             isOverShootingShadowShown = false;
1525         }
1526
1527         private void DragOverShootingShadow(float totalPanDisplacement, float panDisplacement)
1528         {
1529             if (!EnableOverShootingEffect)
1530                 return;
1531
1532             if (totalPanDisplacement > 0) // downwards
1533             {
1534                 // check if reaching at the top / left.
1535                 if ((int)finalTargetPosition != 0)
1536                 {
1537                     isOverShootingShadowShown = false;
1538                     return;
1539                 }
1540
1541                 // save start displacement, and re-calculate displacement.
1542                 if (!isOverShootingShadowShown)
1543                 {
1544                     startShowShadowDisplacement = totalPanDisplacement;
1545                 }
1546                 isOverShootingShadowShown = true;
1547
1548                 float newDisplacement = (int)totalPanDisplacement < (int)startShowShadowDisplacement ? 0 : totalPanDisplacement - startShowShadowDisplacement;
1549
1550                 if (ScrollingDirection == Direction.Horizontal)
1551                 {
1552                     // scale limit of height is 60%.
1553                     float heightScale = newDisplacement / overShootingShadowScaleHeightLimit;
1554                     leftOverShootingShadowView.SizeHeight = heightScale > 0.6f ? SizeHeight * 0.4f : SizeHeight * (1.0f - heightScale);
1555
1556                     // scale limit of width is 300%.
1557                     leftOverShootingShadowView.SizeWidth = newDisplacement > overShootingShadowScaleHeightLimit ? overShootingShadowScaleHeightLimit : newDisplacement;
1558
1559                     // trigger event
1560                     ScrollOutOfBoundEventArgs.Direction scrollDirection = panDisplacement > 0 ?
1561                        ScrollOutOfBoundEventArgs.Direction.Right : ScrollOutOfBoundEventArgs.Direction.Left;
1562                     OnScrollOutOfBound(scrollDirection, totalPanDisplacement);
1563                 }
1564                 else
1565                 {
1566                     // scale limit of width is 60%.
1567                     float widthScale = newDisplacement / overShootingShadowScaleHeightLimit;
1568                     topOverShootingShadowView.SizeWidth = widthScale > 0.6f ? SizeWidth * 0.4f : SizeWidth * (1.0f - widthScale);
1569
1570                     // scale limit of height is 300%.
1571                     topOverShootingShadowView.SizeHeight = newDisplacement > overShootingShadowScaleHeightLimit ? overShootingShadowScaleHeightLimit : newDisplacement;
1572
1573                     // trigger event
1574                     ScrollOutOfBoundEventArgs.Direction scrollDirection = panDisplacement > 0 ?
1575                        ScrollOutOfBoundEventArgs.Direction.Down : ScrollOutOfBoundEventArgs.Direction.Up;
1576                     OnScrollOutOfBound(scrollDirection, totalPanDisplacement);
1577                 }
1578             }
1579             else if (totalPanDisplacement < 0) // upwards
1580             {
1581                 // check if reaching at the bottom.
1582                 if (-(int)finalTargetPosition != (int)maxScrollDistance)
1583                 {
1584                     isOverShootingShadowShown = false;
1585                     return;
1586                 }
1587
1588                 // save start displacement, and re-calculate displacement.
1589                 if (!isOverShootingShadowShown)
1590                 {
1591                     startShowShadowDisplacement = totalPanDisplacement;
1592                 }
1593                 isOverShootingShadowShown = true;
1594
1595                 float newDisplacement = (int)startShowShadowDisplacement < (int)totalPanDisplacement ? 0 : startShowShadowDisplacement - totalPanDisplacement;
1596
1597                 if (ScrollingDirection == Direction.Horizontal)
1598                 {
1599                     // scale limit of height is 60%.
1600                     float heightScale = newDisplacement / overShootingShadowScaleHeightLimit;
1601                     rightOverShootingShadowView.SizeHeight = heightScale > 0.6f ? SizeHeight * 0.4f : SizeHeight * (1.0f - heightScale);
1602
1603                     // scale limit of width is 300%.
1604                     rightOverShootingShadowView.SizeWidth = newDisplacement > overShootingShadowScaleHeightLimit ? overShootingShadowScaleHeightLimit : newDisplacement;
1605
1606                     // trigger event
1607                     ScrollOutOfBoundEventArgs.Direction scrollDirection = panDisplacement > 0 ?
1608                        ScrollOutOfBoundEventArgs.Direction.Right : ScrollOutOfBoundEventArgs.Direction.Left;
1609                     OnScrollOutOfBound(scrollDirection, totalPanDisplacement);
1610                 }
1611                 else
1612                 {
1613                     // scale limit of width is 60%.
1614                     float widthScale = newDisplacement / overShootingShadowScaleHeightLimit;
1615                     bottomOverShootingShadowView.SizeWidth = widthScale > 0.6f ? SizeWidth * 0.4f : SizeWidth * (1.0f - widthScale);
1616
1617                     // scale limit of height is 300%.
1618                     bottomOverShootingShadowView.SizeHeight = newDisplacement > overShootingShadowScaleHeightLimit ? overShootingShadowScaleHeightLimit : newDisplacement;
1619
1620                     // trigger event
1621                     ScrollOutOfBoundEventArgs.Direction scrollDirection = panDisplacement > 0 ?
1622                        ScrollOutOfBoundEventArgs.Direction.Down : ScrollOutOfBoundEventArgs.Direction.Up;
1623                     OnScrollOutOfBound(scrollDirection, totalPanDisplacement);
1624                 }
1625             }
1626             else
1627             {
1628                 // if total displacement is 0, shadow would become invisible.
1629                 isOverShootingShadowShown = false;
1630             }
1631         }
1632
1633         private void PlayOverShootingShadowAnimation()
1634         {
1635             if (!EnableOverShootingEffect)
1636                 return;
1637
1638             // stop animation if necessary.
1639             StopOverShootingShadowAnimation();
1640
1641             if (overShootingShadowAnimation == null)
1642             {
1643                 overShootingShadowAnimation = new Animation(overShootingShadowAnimationDuration);
1644                 overShootingShadowAnimation.Finished += OnOverShootingShadowAnimationFinished;
1645             }
1646
1647             if (ScrollingDirection == Direction.Horizontal)
1648             {
1649                 View targetView = totalDisplacementForPan < 0 ? rightOverShootingShadowView : leftOverShootingShadowView;
1650                 overShootingShadowAnimation.AnimateTo(targetView, "SizeHeight", SizeHeight);
1651                 overShootingShadowAnimation.AnimateTo(targetView, "SizeWidth", 0.0f);
1652                 overShootingShadowAnimation.AnimateTo(targetView, "Opacity", 0.0f);
1653             }
1654             else
1655             {
1656                 View targetView = totalDisplacementForPan < 0 ? bottomOverShootingShadowView : topOverShootingShadowView;
1657                 overShootingShadowAnimation.AnimateTo(targetView, "SizeWidth", SizeWidth);
1658                 overShootingShadowAnimation.AnimateTo(targetView, "SizeHeight", 0.0f);
1659                 overShootingShadowAnimation.AnimateTo(targetView, "Opacity", 0.0f);
1660             }
1661             overShootingShadowAnimation.Play();
1662         }
1663
1664         private void StopOverShootingShadowAnimation()
1665         {
1666             if (overShootingShadowAnimation == null || overShootingShadowAnimation.State != Animation.States.Playing)
1667                 return;
1668
1669             overShootingShadowAnimation.Stop(Animation.EndActions.Cancel);
1670             OnOverShootingShadowAnimationFinished(null, null);
1671             overShootingShadowAnimation.Clear();
1672         }
1673
1674         private void OnOverShootingShadowAnimationFinished(object sender, EventArgs e)
1675         {
1676             if (ScrollingDirection == Direction.Horizontal)
1677             {
1678                 base.Remove(leftOverShootingShadowView);
1679                 base.Remove(rightOverShootingShadowView);
1680
1681                 leftOverShootingShadowView.Size = new Size(0.0f, SizeHeight);
1682                 rightOverShootingShadowView.Size = new Size(0.0f, SizeHeight);
1683             }
1684             else
1685             {
1686                 base.Remove(topOverShootingShadowView);
1687                 base.Remove(bottomOverShootingShadowView);
1688
1689                 topOverShootingShadowView.Size = new Size(SizeWidth, 0.0f);
1690                 bottomOverShootingShadowView.Size = new Size(SizeWidth, 0.0f);
1691             }
1692
1693             // after animation finished, height/width & opacity of vertical shadow both are 0, so it is invisible.
1694             isOverShootingShadowShown = false;
1695         }
1696
1697         private void OnScrollOutOfBound(ScrollOutOfBoundEventArgs.Direction direction, float displacement)
1698         {
1699             ScrollOutOfBoundEventArgs args = new ScrollOutOfBoundEventArgs(direction, displacement);
1700             ScrollOutOfBound?.Invoke(this, args);
1701         }
1702
1703         private void OnPanGestureDetected(object source, PanGestureDetector.DetectedEventArgs e)
1704         {
1705             e.Handled = OnPanGesture(e.PanGesture);
1706         }
1707
1708         private bool OnPanGesture(PanGesture panGesture)
1709         {
1710             bool handled = true;
1711             if (SnapToPage && scrollAnimation != null && scrollAnimation.State == Animation.States.Playing)
1712             {
1713                 return handled;
1714             }
1715             if (panGesture.State == Gesture.StateType.Started)
1716             {
1717                 readyToNotice = false;
1718                 AttachOverShootingShadowView();
1719                 Debug.WriteLineIf(LayoutDebugScrollableBase, "Gesture Start");
1720                 if (scrolling && !SnapToPage)
1721                 {
1722                     StopScroll();
1723                 }
1724                 totalDisplacementForPan = 0.0f;
1725
1726                 // check if gesture need to propagation
1727                 var checkDisplacement = (ScrollingDirection == Direction.Horizontal) ? panGesture.Displacement.X : panGesture.Displacement.Y;
1728                 var checkChildCurrentPosition = (ScrollingDirection == Direction.Horizontal) ? ContentContainer.PositionX : ContentContainer.PositionY;
1729                 var checkChildTargetPosition = checkChildCurrentPosition + checkDisplacement;
1730                 var checkFinalTargetPosition = BoundScrollPosition(checkChildTargetPosition);
1731                 handled = !((int)checkFinalTargetPosition == 0 || -(int)checkFinalTargetPosition == (int)maxScrollDistance);
1732                 // If you propagate a gesture event, return;
1733                 if (!handled)
1734                 {
1735                     return handled;
1736                 }
1737
1738                 //Interrupt touching when panning is started
1739                 this.InterceptTouchEvent += OnInterruptTouchingChildTouched;
1740                 OnScrollDragStarted();
1741             }
1742             else if (panGesture.State == Gesture.StateType.Continuing)
1743             {
1744                 if (ScrollingDirection == Direction.Horizontal)
1745                 {
1746                     // if vertical shadow is shown, does not scroll.
1747                     if (!isOverShootingShadowShown)
1748                     {
1749                         ScrollBy(panGesture.Displacement.X, false);
1750                     }
1751                     totalDisplacementForPan += panGesture.Displacement.X;
1752                     DragOverShootingShadow(totalDisplacementForPan, panGesture.Displacement.X);
1753                 }
1754                 else
1755                 {
1756                     // if vertical shadow is shown, does not scroll.
1757                     if (!isOverShootingShadowShown)
1758                     {
1759                         ScrollBy(panGesture.Displacement.Y, false);
1760                     }
1761                     totalDisplacementForPan += panGesture.Displacement.Y;
1762                     DragOverShootingShadow(totalDisplacementForPan, panGesture.Displacement.Y);
1763                 }
1764                 Debug.WriteLineIf(LayoutDebugScrollableBase, "OnPanGestureDetected Continue totalDisplacementForPan:" + totalDisplacementForPan);
1765
1766             }
1767             else if (panGesture.State == Gesture.StateType.Finished || panGesture.State == Gesture.StateType.Cancelled)
1768             {
1769                 PlayOverShootingShadowAnimation();
1770                 OnScrollDragEnded();
1771                 StopScroll(); // Will replace previous animation so will stop existing one.
1772
1773                 if (scrollAnimation == null)
1774                 {
1775                     scrollAnimation = new Animation();
1776                     scrollAnimation.Finished += ScrollAnimationFinished;
1777                 }
1778
1779                 float panVelocity = (ScrollingDirection == Direction.Horizontal) ? panGesture.Velocity.X : panGesture.Velocity.Y;
1780
1781                 if (SnapToPage)
1782                 {
1783                     PageSnap(panVelocity);
1784                 }
1785                 else
1786                 {
1787                     if (panVelocity == 0)
1788                     {
1789                         float currentScrollPosition = (ScrollingDirection == Direction.Horizontal ? ContentContainer.CurrentPosition.X : ContentContainer.CurrentPosition.Y);
1790                         scrollAnimation.DefaultAlphaFunction = new AlphaFunction(AlphaFunction.BuiltinFunctions.Linear);
1791                         scrollAnimation.Duration = 0;
1792                         scrollAnimation.AnimateTo(ContentContainer, (ScrollingDirection == Direction.Horizontal) ? "PositionX" : "PositionY", currentScrollPosition);
1793                         scrollAnimation.Play();
1794                     }
1795                     else
1796                     {
1797                         Decelerating(panVelocity, scrollAnimation);
1798                     }
1799                 }
1800
1801                 totalDisplacementForPan = 0;
1802                 scrolling = true;
1803                 readyToNotice = true;
1804                 OnScrollAnimationStarted();
1805             }
1806             return handled;
1807         }
1808
1809         internal void BaseRemove(View view)
1810         {
1811             base.Remove(view);
1812         }
1813
1814         internal override bool OnAccessibilityPan(PanGesture gestures)
1815         {
1816             if (SnapToPage && scrollAnimation != null && scrollAnimation.State == Animation.States.Playing)
1817             {
1818                 return false;
1819             }
1820
1821             OnPanGesture(gestures);
1822             return true;
1823         }
1824
1825         private float CustomScrollAlphaFunction(float progress)
1826         {
1827             if (panAnimationDelta == 0)
1828             {
1829                 return 1.0f;
1830             }
1831             else
1832             {
1833                 // Parameter "progress" is normalized value. We need to multiply target duration to calculate distance.
1834                 // Can get real distance using equation of deceleration (check Decelerating function)
1835                 // After get real distance, normalize it
1836                 float realDuration = progress * panAnimationDuration;
1837                 float realDistance = velocityOfLastPan * ((float)Math.Pow(decelerationRate, realDuration) - 1) / logValueOfDeceleration;
1838                 float result = Math.Min(realDistance / Math.Abs(panAnimationDelta), 1.0f);
1839
1840                 // This is hot-fix for if the velocity has very small value, result is not updated even progress done.
1841                 if (progress > 0.99) result = 1.0f;
1842
1843                 return result;
1844             }
1845         }
1846
1847         /// <summary>
1848         /// you can override it to custom your decelerating
1849         /// </summary>
1850         /// <param name="velocity">Velocity of current pan.</param>
1851         /// <param name="animation">Scroll animation.</param>
1852         [EditorBrowsable(EditorBrowsableState.Never)]
1853         protected virtual void Decelerating(float velocity, Animation animation)
1854         {
1855             if (animation == null) throw new ArgumentNullException(nameof(animation));
1856             // Decelerating using deceleration equation ===========
1857             //
1858             // V   : velocity (pixel per millisecond)
1859             // V0  : initial velocity
1860             // d   : deceleration rate,
1861             // t   : time
1862             // X   : final position after decelerating
1863             // log : natural logarithm
1864             //
1865             // V(t) = V0 * d pow t;
1866             // X(t) = V0 * (d pow t - 1) / log d;  <-- Integrate the velocity function
1867             // X(∞) = V0 * d / (1 - d); <-- Result using infinite T can be final position because T is tending to infinity.
1868             //
1869             // Because of final T is tending to infinity, we should use threshold value to finish.
1870             // Final T = log(-threshold * log d / |V0| ) / log d;
1871
1872             velocityOfLastPan = Math.Abs(velocity);
1873
1874             float currentScrollPosition = -(ScrollingDirection == Direction.Horizontal ? ContentContainer.CurrentPosition.X : ContentContainer.CurrentPosition.Y);
1875             panAnimationDelta = (velocityOfLastPan * decelerationRate) / (1 - decelerationRate);
1876             panAnimationDelta = velocity > 0 ? -panAnimationDelta : panAnimationDelta;
1877
1878             float destination = -(panAnimationDelta + currentScrollPosition);
1879             float adjustDestination = AdjustTargetPositionOfScrollAnimation(destination);
1880             float maxPosition = ScrollAvailableArea != null ? ScrollAvailableArea.Y : maxScrollDistance;
1881             float minPosition = ScrollAvailableArea != null ? ScrollAvailableArea.X : 0;
1882
1883             if (destination < -maxPosition || destination > minPosition)
1884             {
1885                 panAnimationDelta = velocity > 0 ? (currentScrollPosition - minPosition) : (maxPosition - currentScrollPosition);
1886                 destination = velocity > 0 ? minPosition : -maxPosition;
1887
1888                 if (panAnimationDelta == 0)
1889                 {
1890                     panAnimationDuration = 0.0f;
1891                 }
1892                 else
1893                 {
1894                     panAnimationDuration = (float)Math.Log((panAnimationDelta * logValueOfDeceleration / velocityOfLastPan + 1), decelerationRate);
1895                 }
1896
1897                 Debug.WriteLineIf(LayoutDebugScrollableBase, "\n" +
1898                     "OverRange======================= \n" +
1899                     "[decelerationRate] " + decelerationRate + "\n" +
1900                     "[logValueOfDeceleration] " + logValueOfDeceleration + "\n" +
1901                     "[Velocity] " + velocityOfLastPan + "\n" +
1902                     "[CurrentPosition] " + currentScrollPosition + "\n" +
1903                     "[CandidateDelta] " + panAnimationDelta + "\n" +
1904                     "[Destination] " + destination + "\n" +
1905                     "[Duration] " + panAnimationDuration + "\n" +
1906                     "================================ \n"
1907                 );
1908             }
1909             else
1910             {
1911                 panAnimationDuration = (float)Math.Log(-DecelerationThreshold * logValueOfDeceleration / velocityOfLastPan) / logValueOfDeceleration;
1912
1913                 if (adjustDestination != destination)
1914                 {
1915                     destination = adjustDestination;
1916                     panAnimationDelta = destination + currentScrollPosition;
1917                     velocityOfLastPan = Math.Abs(panAnimationDelta * logValueOfDeceleration / ((float)Math.Pow(decelerationRate, panAnimationDuration) - 1));
1918                     panAnimationDuration = (float)Math.Log(-DecelerationThreshold * logValueOfDeceleration / velocityOfLastPan) / logValueOfDeceleration;
1919                 }
1920
1921                 Debug.WriteLineIf(LayoutDebugScrollableBase, "\n" +
1922                     "================================ \n" +
1923                     "[decelerationRate] " + decelerationRate + "\n" +
1924                     "[logValueOfDeceleration] " + logValueOfDeceleration + "\n" +
1925                     "[Velocity] " + velocityOfLastPan + "\n" +
1926                     "[CurrentPosition] " + currentScrollPosition + "\n" +
1927                     "[CandidateDelta] " + panAnimationDelta + "\n" +
1928                     "[Destination] " + destination + "\n" +
1929                     "[Duration] " + panAnimationDuration + "\n" +
1930                     "================================ \n"
1931                 );
1932             }
1933
1934             finalTargetPosition = destination;
1935
1936             customScrollAlphaFunction = new UserAlphaFunctionDelegate(CustomScrollAlphaFunction);
1937             animation.DefaultAlphaFunction = new AlphaFunction(customScrollAlphaFunction);
1938             GC.KeepAlive(customScrollAlphaFunction);
1939             animation.Duration = (int)panAnimationDuration;
1940             animation.AnimateTo(ContentContainer, (ScrollingDirection == Direction.Horizontal) ? "PositionX" : "PositionY", destination);
1941             animation.Play();
1942         }
1943
1944         private void ScrollAnimationFinished(object sender, EventArgs e)
1945         {
1946             OnScrollAnimationEnded();
1947         }
1948
1949         /// <summary>
1950         /// Adjust scrolling position by own scrolling rules.
1951         /// Override this function when developer wants to change destination of flicking.(e.g. always snap to center of item)
1952         /// </summary>
1953         /// This may be public opened in tizen_6.0 after ACR done. Before ACR, need to be hidden as inhouse API
1954         [EditorBrowsable(EditorBrowsableState.Never)]
1955         protected virtual float AdjustTargetPositionOfScrollAnimation(float position)
1956         {
1957             return position;
1958         }
1959
1960         /// <summary>
1961         /// Scroll position given to ScrollTo.
1962         /// This is the position in the opposite direction to the position of ContentContainer.
1963         /// </summary>
1964         /// <since_tizen> 8 </since_tizen>
1965         public Position ScrollPosition
1966         {
1967             get
1968             {
1969                 return new Position(-ContentContainer.Position);
1970             }
1971         }
1972
1973         /// <summary>
1974         /// Current scroll position in the middle of ScrollTo animation.
1975         /// This is the position in the opposite direction to the current position of ContentContainer.
1976         /// </summary>
1977         /// <since_tizen> 8 </since_tizen>
1978         public Position ScrollCurrentPosition
1979         {
1980             get
1981             {
1982                 return new Position(-ContentContainer.CurrentPosition);
1983             }
1984         }
1985
1986         /// <summary>
1987         /// Remove all children in ContentContainer.
1988         /// </summary>
1989         /// <param name="dispose">If true, removed child is disposed.</param>
1990         [EditorBrowsable(EditorBrowsableState.Never)]
1991         public void RemoveAllChildren(bool dispose = false)
1992         {
1993             RecursiveRemoveChildren(ContentContainer, dispose);
1994         }
1995
1996         private void RecursiveRemoveChildren(View parent, bool dispose)
1997         {
1998             if (parent == null)
1999             {
2000                 return;
2001             }
2002             int maxChild = (int)parent.GetChildCount();
2003             for (int i = maxChild - 1; i >= 0; --i)
2004             {
2005                 View child = parent.GetChildAt((uint)i);
2006                 if (child == null)
2007                 {
2008                     continue;
2009                 }
2010                 RecursiveRemoveChildren(child, dispose);
2011                 parent.Remove(child);
2012                 if (dispose)
2013                 {
2014                     child.Dispose();
2015                 }
2016             }
2017         }
2018
2019         internal bool IsChildNearlyVisble(View child, float offset = 0)
2020         {
2021             if (ScreenPosition.X - offset < child.ScreenPosition.X + child.SizeWidth &&
2022                 ScreenPosition.X + SizeWidth + offset > child.ScreenPosition.X &&
2023                 ScreenPosition.Y - offset < child.ScreenPosition.Y + child.SizeHeight &&
2024                 ScreenPosition.Y + SizeHeight + offset > child.ScreenPosition.Y)
2025             {
2026                 return true;
2027             }
2028             else
2029             {
2030                 return false;
2031             }
2032         }
2033
2034         /// <inheritdoc/>
2035         [EditorBrowsable(EditorBrowsableState.Never)]
2036         public override View GetNextFocusableView(View currentFocusedView, View.FocusDirection direction, bool loopEnabled)
2037         {
2038             bool isHorizontal = (ScrollingDirection == Direction.Horizontal);
2039             float targetPosition = -(ScrollingDirection == Direction.Horizontal ? ContentContainer.CurrentPosition.X : ContentContainer.CurrentPosition.Y);
2040             float stepDistance = (stepScrollDistance != 0 ? stepScrollDistance : (isHorizontal ? Size.Width * 0.25f : Size.Height * 0.25f));
2041
2042             bool forward = ((isHorizontal && direction == View.FocusDirection.Right) ||
2043                             (!isHorizontal && direction == View.FocusDirection.Down) ||
2044                             (direction == View.FocusDirection.Clockwise));
2045             bool backward = ((isHorizontal && direction == View.FocusDirection.Left) ||
2046                              (!isHorizontal && direction == View.FocusDirection.Up) ||
2047                              (direction == View.FocusDirection.CounterClockwise));
2048
2049             View nextFocusedView = FocusManager.Instance.GetNearestFocusableActor(this, currentFocusedView, direction);
2050
2051             // Move out focus from ScrollableBase.
2052             // FIXME: Forward, Backward is unimplemented other components.
2053             if (direction == View.FocusDirection.Forward ||
2054                 direction == View.FocusDirection.Backward ||
2055                 (nextFocusedView == null &&
2056                 ((forward && maxScrollDistance - targetPosition < 0.1f) ||
2057                  (backward && targetPosition < 0.1f))))
2058             {
2059                 var next = FocusManager.Instance.GetNearestFocusableActor(this.Parent, this, direction);
2060                 Debug.WriteLineIf(focusDebugScrollableBase, $"Focus move [{direction}] out from ScrollableBase! Next focus target {next}:{next?.ID}");
2061                 return next;
2062             }
2063
2064             if (focusDebugScrollableBase)
2065             {
2066                 global::System.Text.StringBuilder debugMessage = new global::System.Text.StringBuilder("=========================================================\n");
2067                 debugMessage.Append($"GetNextFocusableView On: {this}:{this.ID}\n");
2068                 debugMessage.Append($"----------------Current: {currentFocusedView}:{currentFocusedView?.ID}\n");
2069                 debugMessage.Append($"-------------------Next: {nextFocusedView}:{nextFocusedView?.ID}\n");
2070                 debugMessage.Append($"--------------Direction: {direction}\n");
2071                 debugMessage.Append("=========================================================");
2072                 Debug.WriteLineIf(focusDebugScrollableBase, debugMessage);
2073             }
2074
2075             if (nextFocusedView != null)
2076             {
2077                 if (null != FindDescendantByID(nextFocusedView.ID))
2078                 {
2079                     if (IsChildNearlyVisble(nextFocusedView, stepDistance) == true)
2080                     {
2081                         ScrollToChild(nextFocusedView, true);
2082                         return nextFocusedView;
2083                     }
2084                 }
2085             }
2086
2087             if (forward || backward)
2088             {
2089                 // Fallback to current focus or scrollableBase till next focus visible in scrollable.
2090                 if (null != currentFocusedView && null != FindDescendantByID(currentFocusedView.ID))
2091                 {
2092                     nextFocusedView = currentFocusedView;
2093                 }
2094                 else
2095                 {
2096                     Debug.WriteLineIf(focusDebugScrollableBase, "current focus view is not decendant. return ScrollableBase!");
2097                     return this;
2098                 }
2099
2100                 if (forward)
2101                 {
2102                     targetPosition += stepDistance;
2103                     targetPosition = targetPosition > maxScrollDistance ? maxScrollDistance : targetPosition;
2104
2105                 }
2106                 else if (backward)
2107                 {
2108                     targetPosition -= stepDistance;
2109                     targetPosition = targetPosition < 0 ? 0 : targetPosition;
2110                 }
2111
2112                 ScrollTo(targetPosition, true);
2113
2114                 Debug.WriteLineIf(focusDebugScrollableBase, $"ScrollTo :({targetPosition})");
2115             }
2116
2117             Debug.WriteLineIf(focusDebugScrollableBase, $"return end : {nextFocusedView}:{nextFocusedView?.ID}");
2118             return nextFocusedView;
2119         }
2120
2121         /// <inheritdoc/>
2122         [EditorBrowsable(EditorBrowsableState.Never)]
2123         protected override bool AccessibilityScrollToChild(View child)
2124         {
2125             if (child == null)
2126             {
2127                 return false;
2128             }
2129
2130             if (ScrollingDirection == Direction.Horizontal)
2131             {
2132                 if (child.ScreenPosition.X + child.Size.Width <= this.ScreenPosition.X)
2133                 {
2134                     if (SnapToPage)
2135                     {
2136                         PageSnap(PageFlickThreshold + 1);
2137                     }
2138                     else
2139                     {
2140                         ScrollTo((float)(child.ScreenPosition.X - ContentContainer.ScreenPosition.X), false);
2141                     }
2142                 }
2143                 else if (child.ScreenPosition.X >= this.ScreenPosition.X + this.Size.Width)
2144                 {
2145                     if (SnapToPage)
2146                     {
2147                         PageSnap(-(PageFlickThreshold + 1));
2148                     }
2149                     else
2150                     {
2151                         ScrollTo((float)(child.ScreenPosition.X + child.Size.Width - ContentContainer.ScreenPosition.X - this.Size.Width), false);
2152                     }
2153                 }
2154             }
2155             else
2156             {
2157                 if (child.ScreenPosition.Y + child.Size.Height <= this.ScreenPosition.Y)
2158                 {
2159                     if (SnapToPage)
2160                     {
2161                         PageSnap(PageFlickThreshold + 1);
2162                     }
2163                     else
2164                     {
2165                         ScrollTo((float)(child.ScreenPosition.Y - ContentContainer.ScreenPosition.Y), false);
2166                     }
2167                 }
2168                 else if (child.ScreenPosition.Y >= this.ScreenPosition.Y + this.Size.Height)
2169                 {
2170                     if (SnapToPage)
2171                     {
2172                         PageSnap(-(PageFlickThreshold + 1));
2173                     }
2174                     else
2175                     {
2176                         ScrollTo((float)(child.ScreenPosition.Y + child.Size.Height - ContentContainer.ScreenPosition.Y - this.Size.Height), false);
2177                     }
2178                 }
2179             }
2180
2181             return true;
2182         }
2183
2184         /// <inheritdoc/>
2185         [EditorBrowsable(EditorBrowsableState.Never)]
2186         public override bool OnWheel(Wheel wheel)
2187         {
2188             if (wheel == null)
2189             {
2190                 return false;
2191             }
2192
2193             float currentScrollPosition = -(ScrollingDirection == Direction.Horizontal ? ContentContainer.CurrentPosition.X : ContentContainer.CurrentPosition.Y);
2194             ScrollTo(currentScrollPosition + (wheelScrollDistance * wheel.Z), false);
2195
2196             return true;
2197         }
2198
2199         private bool OnWheelEvent(object o, WheelEventArgs e)
2200         {
2201             return OnWheel(e?.Wheel);
2202         }
2203     }
2204
2205 } // namespace