[NUI] Add ScrollEvent to ScrollableBase (#1430)
[platform/core/csapi/tizenfx.git] / src / Tizen.NUI.Components / Controls / ScrollableBase.cs
1 /* Copyright (c) 2019 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.ComponentModel;
19 using System.Diagnostics;
20
21 namespace Tizen.NUI.Components
22 {
23     /// <summary>
24     /// [Draft] This class provides a View that can scroll a single View with a layout. This View can be a nest of Views.
25     /// </summary>
26     /// This may be public opened in tizen_6.0 after ACR done. Before ACR, need to be hidden as inhouse API.
27     [EditorBrowsable(EditorBrowsableState.Never)]
28     public class ScrollableBase : Control
29     {
30             static bool LayoutDebugScrollableBase = false; // Debug flag
31         private Direction mScrollingDirection = Direction.Vertical;
32         private bool mScrollEnabled = true;
33         private int mPageWidth = 0;
34
35         private class ScrollableBaseCustomLayout : LayoutGroup
36         {
37             protected override void OnMeasure(MeasureSpecification widthMeasureSpec, MeasureSpecification heightMeasureSpec)
38             {
39                 Extents padding = Padding;
40                 float totalHeight = padding.Top + padding.Bottom;
41                 float totalWidth = padding.Start + padding.End;
42
43                 MeasuredSize.StateType childWidthState = MeasuredSize.StateType.MeasuredSizeOK;
44                 MeasuredSize.StateType childHeightState = MeasuredSize.StateType.MeasuredSizeOK;
45
46                 Direction scrollingDirection = Direction.Vertical;
47                 ScrollableBase scrollableBase = this.Owner as ScrollableBase;
48                 if (scrollableBase)
49                 {
50                    scrollingDirection = scrollableBase.ScrollingDirection;
51                 }
52
53                 // measure child, should be a single scrolling child
54                 foreach( LayoutItem childLayout in LayoutChildren )
55                 {
56                     if (childLayout != null)
57                     {
58                         // Get size of child
59                         // Use an Unspecified MeasureSpecification mode so scrolling child is not restricted to it's parents size in Height (for vertical scrolling)
60                         // or Width for horizontal scrolling
61                         MeasureSpecification unrestrictedMeasureSpec = new MeasureSpecification( heightMeasureSpec.Size, MeasureSpecification.ModeType.Unspecified);
62
63                         if (scrollingDirection == Direction.Vertical)
64                         {
65                             MeasureChild( childLayout, widthMeasureSpec, unrestrictedMeasureSpec );  // Height unrestricted by parent
66                         }
67                         else
68                         {
69                             MeasureChild( childLayout, unrestrictedMeasureSpec, heightMeasureSpec );  // Width unrestricted by parent
70                         }
71
72                         float childWidth = childLayout.MeasuredWidth.Size.AsDecimal();
73                         float childHeight = childLayout.MeasuredHeight.Size.AsDecimal();
74
75                         // Determine the width and height needed by the children using their given position and size.
76                         // Children could overlap so find the left most and right most child.
77                         Position2D childPosition = childLayout.Owner.Position2D;
78                         float childLeft = childPosition.X;
79                         float childTop = childPosition.Y;
80
81                         // Store current width and height needed to contain all children.
82                         Extents childMargin = childLayout.Margin;
83                         totalWidth = childWidth + childMargin.Start + childMargin.End;
84                         totalHeight = childHeight + childMargin.Top + childMargin.Bottom;
85
86                         if (childLayout.MeasuredWidth.State == MeasuredSize.StateType.MeasuredSizeTooSmall)
87                         {
88                             childWidthState = MeasuredSize.StateType.MeasuredSizeTooSmall;
89                         }
90                         if (childLayout.MeasuredWidth.State == MeasuredSize.StateType.MeasuredSizeTooSmall)
91                         {
92                             childHeightState = MeasuredSize.StateType.MeasuredSizeTooSmall;
93                         }
94                     }
95                 }
96
97
98                 MeasuredSize widthSizeAndState = ResolveSizeAndState(new LayoutLength(totalWidth), widthMeasureSpec, MeasuredSize.StateType.MeasuredSizeOK);
99                 MeasuredSize heightSizeAndState = ResolveSizeAndState(new LayoutLength(totalHeight), heightMeasureSpec, MeasuredSize.StateType.MeasuredSizeOK);
100                 totalWidth = widthSizeAndState.Size.AsDecimal();
101                 totalHeight = heightSizeAndState.Size.AsDecimal();
102
103                 // Ensure layout respects it's given minimum size
104                 totalWidth = Math.Max( totalWidth, SuggestedMinimumWidth.AsDecimal() );
105                 totalHeight = Math.Max( totalHeight, SuggestedMinimumHeight.AsDecimal() );
106
107                 widthSizeAndState.State = childWidthState;
108                 heightSizeAndState.State = childHeightState;
109
110                 SetMeasuredDimensions( ResolveSizeAndState( new LayoutLength(totalWidth), widthMeasureSpec, childWidthState ),
111                                        ResolveSizeAndState( new LayoutLength(totalHeight), heightMeasureSpec, childHeightState ) );
112
113                 // Size of ScrollableBase is changed. Change Page width too.
114                 scrollableBase.mPageWidth = (int)MeasuredWidth.Size.AsRoundedValue();
115             }
116
117             protected override void OnLayout(bool changed, LayoutLength left, LayoutLength top, LayoutLength right, LayoutLength bottom)
118             {
119                 foreach( LayoutItem childLayout in LayoutChildren )
120                 {
121                     if( childLayout != null )
122                     {
123                         LayoutLength childWidth = childLayout.MeasuredWidth.Size;
124                         LayoutLength childHeight = childLayout.MeasuredHeight.Size;
125
126                         Position2D childPosition = childLayout.Owner.Position2D;
127                         Extents padding = Padding;
128                         Extents childMargin = childLayout.Margin;
129
130                         LayoutLength childLeft = new LayoutLength(childPosition.X + childMargin.Start + padding.Start);
131                         LayoutLength childTop = new LayoutLength(childPosition.Y + childMargin.Top + padding.Top);
132
133                         childLayout.Layout( childLeft, childTop, childLeft + childWidth, childTop + childHeight );
134                     }
135                 }
136             }
137         } //  ScrollableBaseCustomLayout
138
139         /// <summary>
140         /// The direction axis to scroll.
141         /// </summary>
142         /// <since_tizen> 6 </since_tizen>
143         /// This may be public opened in tizen_6.0 after ACR done. Before ACR, need to be hidden as inhouse API.
144         [EditorBrowsable(EditorBrowsableState.Never)]
145         public enum Direction
146         {
147             /// <summary>
148             /// Horizontal axis.
149             /// </summary>
150             /// <since_tizen> 6 </since_tizen>
151             Horizontal,
152
153             /// <summary>
154             /// Vertical axis.
155             /// </summary>
156             /// <since_tizen> 6 </since_tizen>
157             Vertical
158         }
159
160         /// <summary>
161         /// [Draft] Configurable speed threshold that register the gestures as a flick.
162         /// If the flick speed less than the threshold then will not be considered a flick.
163         /// </summary>
164         /// This may be public opened in tizen_6.0 after ACR done. Before ACR, need to be hidden as inhouse API.
165         [EditorBrowsable(EditorBrowsableState.Never)]
166         public float FlickThreshold { get; set; } = 0.2f;
167
168         /// <summary>
169         /// [Draft] Configurable duration modifer for the flick animation.
170         /// Determines the speed of the scroll, large value results in a longer flick animation. Range (0.1 - 1.0)
171         /// </summary>
172         /// This may be public opened in tizen_6.0 after ACR done. Before ACR, need to be hidden as inhouse API
173         [EditorBrowsable(EditorBrowsableState.Never)]
174         public float FlickAnimationSpeed { get; set; } = 0.4f;
175
176         /// <summary>
177         /// [Draft] Configurable modifer for the distance to be scrolled when flicked detected.
178         /// It a ratio of the ScrollableBase's length. (not child's length).
179         /// First value is the ratio of the distance to scroll with the weakest flick.
180         /// Second value is the ratio of the distance to scroll with the strongest flick.
181         /// Second > First.
182         /// </summary>
183         /// This may be public opened in tizen_6.0 after ACR done. Before ACR, need to be hidden as inhouse API
184         [EditorBrowsable(EditorBrowsableState.Never)]
185         public Vector2 FlickDistanceMultiplierRange { get; set; } = new Vector2(0.6f, 1.8f);
186
187         /// <summary>
188         /// [Draft] Scrolling direction mode.
189         /// Default is Vertical scrolling.
190         /// </summary>
191         /// This may be public opened in tizen_6.0 after ACR done. Before ACR, need to be hidden as inhouse API
192         [EditorBrowsable(EditorBrowsableState.Never)]
193         public Direction ScrollingDirection
194         {
195             get
196             {
197                 return mScrollingDirection;
198             }
199             set
200             {
201                 if(value != mScrollingDirection)
202                 {
203                     mScrollingDirection = value;
204                     mPanGestureDetector.RemoveDirection(value == Direction.Horizontal ? PanGestureDetector.DirectionVertical : PanGestureDetector.DirectionHorizontal);
205                     mPanGestureDetector.AddDirection(value == Direction.Horizontal ? PanGestureDetector.DirectionHorizontal : PanGestureDetector.DirectionVertical);
206                 }
207             }
208         }
209
210         /// <summary>
211         /// [Draft] Enable or disable scrolling.
212         /// </summary>
213         /// This may be public opened in tizen_6.0 after ACR done. Before ACR, need to be hidden as inhouse API
214         [EditorBrowsable(EditorBrowsableState.Never)]
215         public bool ScrollEnabled
216         {
217             get
218             {
219                 return mScrollEnabled;
220             }
221             set
222             {
223                 if (value != mScrollEnabled)
224                 {
225                     mScrollEnabled = value;
226                     if(mScrollEnabled)
227                     {
228                         mPanGestureDetector.Detected += OnPanGestureDetected;
229                         mTapGestureDetector.Detected += OnTapGestureDetected;
230                     }
231                     else
232                     {
233                         mPanGestureDetector.Detected -= OnPanGestureDetected;
234                         mTapGestureDetector.Detected -= OnTapGestureDetected;
235                     }
236                 }
237             }
238         }
239
240         /// <summary>
241         /// [Draft] Pages mode, enables moving to the next or return to current page depending on pan displacement.
242         /// Default is false.
243         /// </summary>
244         /// This may be public opened in tizen_6.0 after ACR done. Before ACR, need to be hidden as inhouse API
245         [EditorBrowsable(EditorBrowsableState.Never)]
246         public bool SnapToPage { set; get; } = false;
247
248         /// <summary>
249         /// [Draft] Get current page.
250         /// Working propery with SnapToPage property.
251         /// </summary>
252         /// This may be public opened in tizen_6.0 after ACR done. Before ACR, need to be hidden as inhouse API
253         [EditorBrowsable(EditorBrowsableState.Never)]
254         public int CurrentPage { get; private set; } = 0;
255
256         /// <summary>
257         /// [Draft] Duration of scroll animation.
258         /// </summary>
259         /// This may be public opened in tizen_6.0 after ACR done. Before ACR, need to be hidden as inhouse API
260         [EditorBrowsable(EditorBrowsableState.Never)]
261         public int ScrollDuration { set; get; } = 125;
262
263         /// <summary>
264         /// ScrollEventArgs is a class to record scroll event arguments which will sent to user.
265         /// </summary>
266         /// <since_tizen> 6 </since_tizen>
267         /// This may be public opened in tizen_6.0 after ACR done. Before ACR, need to be hidden as inhouse API
268         [EditorBrowsable(EditorBrowsableState.Never)]
269         public class ScrollEventArgs : EventArgs
270         {
271             Position position;
272
273             /// <summary>
274             /// Default constructor.
275             /// </summary>
276             /// <param name="position">Current scroll position</param>
277             /// <since_tizen> 6 </since_tizen>
278             /// This may be public opened in tizen_6.0 after ACR done. Before ACR, need to be hidden as inhouse API
279             public ScrollEventArgs(Position position)
280             {
281                 this.position = position;
282             }
283
284             /// <summary>
285             /// [Draft] Current scroll position.
286             /// </summary>
287             /// This may be public opened in tizen_6.0 after ACR done. Before ACR, need to be hidden as inhouse API
288             [EditorBrowsable(EditorBrowsableState.Never)]
289             public Position Position
290             {
291                 get
292                 {
293                     return position;
294                 }
295             }
296         }
297
298         /// <summary>
299         /// An event emitted when user starts dragging ScrollableBase, user can subscribe or unsubscribe to this event handler.<br />
300         /// </summary>
301         /// <since_tizen> 6 </since_tizen>
302         /// This may be public opened in tizen_6.0 after ACR done. Before ACR, need to be hidden as inhouse API
303         [EditorBrowsable(EditorBrowsableState.Never)]
304         public event EventHandler<ScrollEventArgs> ScrollDragStartEvent;
305
306         /// <summary>
307         /// An event emitted when user stops dragging ScrollableBase, user can subscribe or unsubscribe to this event handler.<br />
308         /// </summary>
309         /// <since_tizen> 6 </since_tizen>
310         /// This may be public opened in tizen_6.0 after ACR done. Before ACR, need to be hidden as inhouse API
311         [EditorBrowsable(EditorBrowsableState.Never)]
312         public event EventHandler<ScrollEventArgs> ScrollDragEndEvent;
313
314
315         /// <summary>
316         /// An event emitted when the scrolling slide animation starts, user can subscribe or unsubscribe to this event handler.<br />
317         /// </summary>
318         /// <since_tizen> 6 </since_tizen>
319         /// This may be public opened in tizen_6.0 after ACR done. Before ACR, need to be hidden as inhouse API
320         [EditorBrowsable(EditorBrowsableState.Never)]
321         public event EventHandler<ScrollEventArgs> ScrollAnimationStartEvent;
322
323         /// <summary>
324         /// An event emitted when the scrolling slide animation ends, user can subscribe or unsubscribe to this event handler.<br />
325         /// </summary>
326         /// <since_tizen> 6 </since_tizen>
327         /// This may be public opened in tizen_6.0 after ACR done. Before ACR, need to be hidden as inhouse API
328         [EditorBrowsable(EditorBrowsableState.Never)]
329         public event EventHandler<ScrollEventArgs> ScrollAnimationEndEvent;
330
331
332         /// <summary>
333         /// An event emitted when scrolling, user can subscribe or unsubscribe to this event handler.<br />
334         /// </summary>
335         /// <since_tizen> 6 </since_tizen>
336         /// This may be public opened in tizen_6.0 after ACR done. Before ACR, need to be hidden as inhouse API
337         [EditorBrowsable(EditorBrowsableState.Never)]
338         public event EventHandler<ScrollEventArgs> ScrollEvent;
339
340         private Animation scrollAnimation;
341         private float maxScrollDistance;
342         private float childTargetPosition = 0.0f;
343         private PanGestureDetector mPanGestureDetector;
344         private TapGestureDetector mTapGestureDetector;
345         private View mScrollingChild;
346         private float multiplier =1.0f;
347         private bool scrolling = false;
348         private float ratioOfScreenWidthToCompleteScroll = 0.5f;
349         private float totalDisplacementForPan = 0.0f;
350
351         // If false then can only flick pages when the current animation/scroll as ended.
352         private bool flickWhenAnimating = false;
353         private PropertyNotification propertyNotification;
354
355         /// <summary>
356         /// [Draft] Constructor
357         /// </summary>
358         /// <since_tizen> 6 </since_tizen>
359         /// This may be public opened in tizen_6.0 after ACR done. Before ACR, need to be hidden as inhouse API
360         [EditorBrowsable(EditorBrowsableState.Never)]
361         public ScrollableBase() : base()
362         {
363             mPanGestureDetector = new PanGestureDetector();
364             mPanGestureDetector.Attach(this);
365             mPanGestureDetector.AddDirection(PanGestureDetector.DirectionVertical);
366             mPanGestureDetector.Detected += OnPanGestureDetected;
367
368             mTapGestureDetector = new TapGestureDetector();
369             mTapGestureDetector.Attach(this);
370             mTapGestureDetector.Detected += OnTapGestureDetected;
371
372
373             ClippingMode = ClippingModeType.ClipToBoundingBox;
374
375             mScrollingChild = new View();
376             mScrollingChild.Name = "DefaultScrollingChild";
377
378             Layout = new ScrollableBaseCustomLayout();
379         }
380
381         private void OnPropertyChanged(object source, PropertyNotification.NotifyEventArgs args)
382         {
383             OnScroll();
384         }
385
386         /// <summary>
387         /// Called after a child has been added to the owning view.
388         /// </summary>
389         /// <param name="view">The child which has been added.</param>
390         /// <since_tizen> 6 </since_tizen>
391         /// This may be public opened in tizen_6.0 after ACR done. Before ACR, need to be hidden as inhouse API
392         [EditorBrowsable(EditorBrowsableState.Never)]
393         public override void OnChildAdd(View view)
394         {
395             if(mScrollingChild.Name != "DefaultScrollingChild")
396             {
397                 propertyNotification.Notified -= OnPropertyChanged;
398                 mScrollingChild.RemovePropertyNotification(propertyNotification);
399             }
400
401             mScrollingChild = view;
402             propertyNotification = mScrollingChild?.AddPropertyNotification("position", PropertyCondition.Step(1.0f));
403             propertyNotification.Notified += OnPropertyChanged;
404
405             {
406                 if (Children.Count > 1)
407                     Log.Error("ScrollableBase", $"Only 1 child should be added to ScrollableBase.");
408             }
409         }
410
411         /// <summary>
412         /// Called after a child has been removed from the owning view.
413         /// </summary>
414         /// <param name="view">The child which has been removed.</param>
415         /// <since_tizen> 6 </since_tizen>
416         /// This may be public opened in tizen_6.0 after ACR done. Before ACR, need to be hidden as inhouse API
417         [EditorBrowsable(EditorBrowsableState.Never)]
418         public override void OnChildRemove(View view)
419         {
420             propertyNotification.Notified -= OnPropertyChanged;
421             mScrollingChild.RemovePropertyNotification(propertyNotification);
422
423             mScrollingChild = new View();
424         }
425
426
427         /// <summary>
428         /// Scrolls to the item at the specified index.
429         /// </summary>
430         /// <param name="index">Index of item.</param>
431         /// <since_tizen> 6 </since_tizen>
432         /// This may be public opened in tizen_6.0 after ACR done. Before ACR, need to be hidden as inhouse API
433         [EditorBrowsable(EditorBrowsableState.Never)]
434         public void ScrollToIndex(int index)
435         {
436             if(mScrollingChild.ChildCount-1 < index || index < 0)
437             {
438                 return;
439             }
440
441             if(SnapToPage)
442             {
443                 CurrentPage = index;
444             }
445
446             maxScrollDistance = CalculateMaximumScrollDistance();
447
448             float targetPosition = Math.Min(ScrollingDirection == Direction.Vertical ? mScrollingChild.Children[index].Position.Y : mScrollingChild.Children[index].Position.X, maxScrollDistance);
449             AnimateChildTo(ScrollDuration, -targetPosition);
450         }
451
452         private void OnScrollDragStart()
453         {
454             ScrollEventArgs eventArgs = new ScrollEventArgs(mScrollingChild.CurrentPosition);
455             ScrollDragStartEvent?.Invoke(this, eventArgs);
456         }
457
458         private void OnScrollDragEnd()
459         {
460             ScrollEventArgs eventArgs = new ScrollEventArgs(mScrollingChild.CurrentPosition);
461             ScrollDragEndEvent?.Invoke(this, eventArgs);
462         }
463
464         private void OnScrollAnimationStart()
465         {
466             ScrollEventArgs eventArgs = new ScrollEventArgs(mScrollingChild.CurrentPosition);
467             ScrollAnimationStartEvent?.Invoke(this, eventArgs);
468         }
469
470         private void OnScrollAnimationEnd()
471         {
472             ScrollEventArgs eventArgs = new ScrollEventArgs(mScrollingChild.CurrentPosition);
473             ScrollAnimationEndEvent?.Invoke(this, eventArgs);
474         }
475
476         private void OnScroll()
477         {
478             ScrollEventArgs eventArgs = new ScrollEventArgs(mScrollingChild.CurrentPosition);
479             ScrollEvent?.Invoke(this, eventArgs);
480         }
481
482         private void StopScroll()
483         {
484             if (scrollAnimation != null)
485             {
486                 if (scrollAnimation.State == Animation.States.Playing)
487                 {
488                     Debug.WriteLineIf(LayoutDebugScrollableBase, "StopScroll Animation Playing");
489                     scrollAnimation.Stop(Animation.EndActions.Cancel);
490                     OnScrollAnimationEnd();
491                 }
492                 scrollAnimation.Clear();
493             }
494         }
495
496         // static constructor registers the control type
497         static ScrollableBase()
498         {
499             // ViewRegistry registers control type with DALi type registry
500             // also uses introspection to find any properties that need to be registered with type registry
501             CustomViewRegistry.Instance.Register(CreateInstance, typeof(ScrollableBase));
502         }
503
504         internal static CustomView CreateInstance()
505         {
506             return new ScrollableBase();
507         }
508
509         private void AnimateChildTo(int duration, float axisPosition)
510         {
511             Debug.WriteLineIf(LayoutDebugScrollableBase, "AnimationTo Animation Duration:" + duration + " Destination:" + axisPosition);
512
513             StopScroll(); // Will replace previous animation so will stop existing one.
514
515             if (scrollAnimation == null)
516             {
517                 scrollAnimation = new Animation();
518                 scrollAnimation.Finished += ScrollAnimationFinished;
519             }
520
521             scrollAnimation.Duration = duration;
522             scrollAnimation.DefaultAlphaFunction = new AlphaFunction(AlphaFunction.BuiltinFunctions.EaseOutSine);
523             scrollAnimation.AnimateTo(mScrollingChild, (ScrollingDirection == Direction.Horizontal) ? "PositionX" : "PositionY", axisPosition);
524             scrolling = true;
525             OnScrollAnimationStart();
526             scrollAnimation.Play();
527         }
528
529         private void ScrollBy(float displacement, bool animate)
530         {
531             if (GetChildCount() == 0 || displacement == 0 || maxScrollDistance < 0)
532             {
533                 return;
534             }
535
536             float childCurrentPosition = (ScrollingDirection == Direction.Horizontal) ? mScrollingChild.PositionX: mScrollingChild.PositionY;
537
538             Debug.WriteLineIf(LayoutDebugScrollableBase, "ScrollBy childCurrentPosition:" + childCurrentPosition +
539                                                    " displacement:" + displacement,
540                                                    " maxScrollDistance:" + maxScrollDistance );
541
542             childTargetPosition = childCurrentPosition + displacement; // child current position + gesture displacement
543             childTargetPosition = Math.Min(0,childTargetPosition);
544             childTargetPosition = Math.Max(-maxScrollDistance,childTargetPosition);
545
546             Debug.WriteLineIf( LayoutDebugScrollableBase, "ScrollBy currentAxisPosition:" + childCurrentPosition + "childTargetPosition:" + childTargetPosition);
547
548             if (animate)
549             {
550                 // Calculate scroll animaton duration
551                 float scrollDistance = 0.0f;
552                 if (childCurrentPosition < childTargetPosition)
553                 {
554                     scrollDistance = Math.Abs(childCurrentPosition + childTargetPosition);
555                 }
556                 else
557                 {
558                     scrollDistance = Math.Abs(childCurrentPosition - childTargetPosition);
559                 }
560
561                 int duration = (int)((320*FlickAnimationSpeed) + (scrollDistance * FlickAnimationSpeed));
562                 Debug.WriteLineIf(LayoutDebugScrollableBase, "Scroll Animation Duration:" + duration + " Distance:" + scrollDistance);
563
564                 AnimateChildTo(duration, childTargetPosition);
565             }
566             else
567             {
568                 // Set position of scrolling child without an animation
569                 if (ScrollingDirection == Direction.Horizontal)
570                 {
571                     mScrollingChild.PositionX = childTargetPosition;
572                 }
573                 else
574                 {
575                     mScrollingChild.PositionY = childTargetPosition;
576                 }
577             }
578         }
579
580         /// <summary>
581         /// you can override it to clean-up your own resources.
582         /// </summary>
583         /// <param name="type">DisposeTypes</param>
584         /// <since_tizen> 6 </since_tizen>
585         /// This will be public opened in tizen_5.5 after ACR done. Before ACR, need to be hidden as inhouse API.
586         [EditorBrowsable(EditorBrowsableState.Never)]
587         protected override void Dispose(DisposeTypes type)
588         {
589             if (disposed)
590             {
591                 return;
592             }
593
594             if (type == DisposeTypes.Explicit)
595             {
596                 StopScroll();
597
598                 if (mPanGestureDetector != null)
599                 {
600                     mPanGestureDetector.Detected -= OnPanGestureDetected;
601                     mPanGestureDetector.Dispose();
602                     mPanGestureDetector = null;
603                 }
604
605                 if (mTapGestureDetector != null)
606                 {
607                     mTapGestureDetector.Detected -= OnTapGestureDetected;
608                     mTapGestureDetector.Dispose();
609                     mTapGestureDetector = null;
610                 }
611             }
612             base.Dispose(type);
613         }
614
615         private float CalculateDisplacementFromVelocity(float axisVelocity)
616         {
617             // Map: flick speed of range (2.0 - 6.0) to flick multiplier of range (0.7 - 1.6)
618             float speedMinimum = FlickThreshold;
619             float speedMaximum = FlickThreshold + 6.0f;
620             float multiplierMinimum = FlickDistanceMultiplierRange.X;
621             float multiplierMaximum = FlickDistanceMultiplierRange.Y;
622
623             float flickDisplacement = 0.0f;
624
625             float speed = Math.Min(4.0f,Math.Abs(axisVelocity));
626
627             Debug.WriteLineIf(LayoutDebugScrollableBase, "ScrollableBase Candidate Flick speed:" + speed);
628
629             if (speed > FlickThreshold)
630             {
631                 // Flick length is the length of the ScrollableBase.
632                 float flickLength = (ScrollingDirection == Direction.Horizontal) ?CurrentSize.Width:CurrentSize.Height;
633
634                 // Calculate multiplier by mapping speed between the multiplier minimum and maximum.
635                 multiplier =( (speed - speedMinimum) / ( (speedMaximum - speedMinimum) * (multiplierMaximum - multiplierMinimum) ) )+ multiplierMinimum;
636
637                 // flick displacement is the product of the flick length and multiplier
638                 flickDisplacement = ((flickLength * multiplier) * speed) / axisVelocity;  // *speed and /velocity to perserve sign.
639
640                 Debug.WriteLineIf(LayoutDebugScrollableBase, "Calculated FlickDisplacement[" + flickDisplacement +"] from speed[" + speed + "] multiplier:"
641                                                         + multiplier);
642             }
643             return flickDisplacement;
644         }
645
646         private float CalculateMaximumScrollDistance()
647         {
648             int scrollingChildLength = 0;
649             int scrollerLength = 0;
650             if (ScrollingDirection == Direction.Horizontal)
651             {
652                 Debug.WriteLineIf(LayoutDebugScrollableBase, "Horizontal");
653
654                 scrollingChildLength = (int)mScrollingChild.Layout.MeasuredWidth.Size.AsRoundedValue();
655                 scrollerLength = CurrentSize.Width;
656             }
657             else
658             {
659                 Debug.WriteLineIf(LayoutDebugScrollableBase, "Vertical");
660                 scrollingChildLength = (int)mScrollingChild.Layout.MeasuredHeight.Size.AsRoundedValue();
661                 scrollerLength = CurrentSize.Height;
662             }
663
664             Debug.WriteLineIf(LayoutDebugScrollableBase, "ScrollBy maxScrollDistance:" + (scrollingChildLength - scrollerLength) +
665                                                    " parent length:" + scrollerLength +
666                                                    " scrolling child length:" + scrollingChildLength);
667
668             return Math.Max(scrollingChildLength - scrollerLength,0);
669         }
670
671         private void PageSnap()
672         {
673             Debug.WriteLineIf(LayoutDebugScrollableBase, "PageSnap with pan candidate totalDisplacement:" + totalDisplacementForPan +
674                                                                 " currentPage[" + CurrentPage + "]" );
675
676             //Increment current page if total displacement enough to warrant a page change.
677             if (Math.Abs(totalDisplacementForPan) > (mPageWidth * ratioOfScreenWidthToCompleteScroll))
678             {
679                 if (totalDisplacementForPan < 0)
680                 {
681                     CurrentPage = Math.Min(Math.Max(mScrollingChild.Children.Count - 1,0), ++CurrentPage);
682                 }
683                 else
684                 {
685                     CurrentPage = Math.Max(0, --CurrentPage);
686                 }
687             }
688
689             // Animate to new page or reposition to current page
690             int destinationX = -(CurrentPage * mPageWidth);
691             Debug.WriteLineIf(LayoutDebugScrollableBase, "Snapping to page[" + CurrentPage + "] to:"+ destinationX + " from:" + mScrollingChild.PositionX);
692             AnimateChildTo(ScrollDuration, destinationX);
693         }
694
695         private void Flick(float flickDisplacement)
696         {
697           if (SnapToPage)
698           {
699               if ( ( flickWhenAnimating && scrolling == true) || ( scrolling == false) )
700               {
701                   if(flickDisplacement < 0)
702                   {
703                       CurrentPage = Math.Min(Math.Max(mScrollingChild.Children.Count - 1,0), CurrentPage + 1);
704                       Debug.WriteLineIf(LayoutDebugScrollableBase, "Snap - to page:" + CurrentPage);
705                   }
706                   else
707                   {
708                       CurrentPage = Math.Max(0, CurrentPage - 1);
709                       Debug.WriteLineIf(LayoutDebugScrollableBase, "Snap + to page:" + CurrentPage);
710                   }
711                   float targetPosition = -(CurrentPage* mPageWidth); // page size
712                   Debug.WriteLineIf(LayoutDebugScrollableBase, "Snapping to :" + targetPosition);
713                   AnimateChildTo(ScrollDuration,targetPosition);
714               }
715           }
716           else
717           {
718               ScrollBy(flickDisplacement, true); // Animate flickDisplacement.
719           }
720         }
721
722         private void OnPanGestureDetected(object source, PanGestureDetector.DetectedEventArgs e)
723         {
724             if (e.PanGesture.State == Gesture.StateType.Started)
725             {
726                 Debug.WriteLineIf(LayoutDebugScrollableBase, "Gesture Start");
727                 if (scrolling && !SnapToPage)
728                 {
729                     StopScroll();
730                 }
731                 maxScrollDistance = CalculateMaximumScrollDistance();
732                 totalDisplacementForPan = 0.0f;
733                 OnScrollDragStart();
734             }
735             else if (e.PanGesture.State == Gesture.StateType.Continuing)
736             {
737                 if (ScrollingDirection == Direction.Horizontal)
738                 {
739                     ScrollBy(e.PanGesture.Displacement.X, false);
740                     totalDisplacementForPan += e.PanGesture.Displacement.X;
741                 }
742                 else
743                 {
744                     ScrollBy(e.PanGesture.Displacement.Y, false);
745                     totalDisplacementForPan += e.PanGesture.Displacement.Y;
746                 }
747                 Debug.WriteLineIf(LayoutDebugScrollableBase, "OnPanGestureDetected Continue totalDisplacementForPan:" + totalDisplacementForPan);
748             }
749             else if (e.PanGesture.State == Gesture.StateType.Finished)
750             {
751                 float axisVelocity = (ScrollingDirection == Direction.Horizontal) ? e.PanGesture.Velocity.X : e.PanGesture.Velocity.Y;
752                 float flickDisplacement = CalculateDisplacementFromVelocity(axisVelocity);
753
754                 Debug.WriteLineIf(LayoutDebugScrollableBase, "FlickDisplacement:" + flickDisplacement + "TotalDisplacementForPan:" + totalDisplacementForPan);
755                 OnScrollDragEnd();
756
757                 if (flickDisplacement > 0 | flickDisplacement < 0)// Flick detected
758                 {
759                     Flick(flickDisplacement);
760                 }
761                 else
762                 {
763                     // End of panning gesture but was not a flick
764                     if (SnapToPage)
765                     {
766                         PageSnap();
767                     }
768                 }
769                 totalDisplacementForPan = 0;
770             }
771         }
772
773         private new void OnTapGestureDetected(object source, TapGestureDetector.DetectedEventArgs e)
774         {
775             if (e.TapGesture.Type == Gesture.GestureType.Tap)
776             {
777                 // Stop scrolling if tap detected (press then relase).
778                 // Unless in Pages mode, do not want a page change to stop part way.
779                 if(scrolling && !SnapToPage)
780                 {
781                     StopScroll();
782                 }
783             }
784         }
785
786         private void ScrollAnimationFinished(object sender, EventArgs e)
787         {
788             scrolling = false;
789             OnScrollAnimationEnd();
790         }
791     }
792
793 } // namespace