[NUI] Fix Svace issues (#1719)
[platform/core/csapi/tizenfx.git] / src / Tizen.NUI.Components / Controls / ScrollableBase.cs
1 /* Copyright (c) 2020 Samsung Electronics Co., Ltd.
2  *
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  *
15  */
16 using System;
17 using Tizen.NUI.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                             MeasureChildWithMargins(childLayout, widthMeasureSpec, new LayoutLength(0), unrestrictedMeasureSpec, new LayoutLength(0));  // Height unrestricted by parent
66                         }
67                         else
68                         {
69                             MeasureChildWithMargins(childLayout, unrestrictedMeasureSpec, new LayoutLength(0), heightMeasureSpec, new LayoutLength(0));  // 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 + Padding.Start + Padding.End), widthMeasureSpec, MeasuredSize.StateType.MeasuredSizeOK);
99                 MeasuredSize heightSizeAndState = ResolveSizeAndState(new LayoutLength(totalHeight + Padding.Top + Padding.Bottom), 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 + Padding.Start + Padding.End), widthMeasureSpec, childWidthState),
111                                        ResolveSizeAndState(new LayoutLength(totalHeight + Padding.Top + Padding.Bottom), 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
262         public int ScrollDuration { set; get; } = 125;
263         /// <summary>
264         /// [Draft] Scroll Available area.
265         /// </summary>
266         /// This may be public opened in tizen_6.0 after ACR done. Before ACR, need to be hidden as inhouse API
267         [EditorBrowsable(EditorBrowsableState.Never)]
268         public Vector2 ScrollAvailableArea { set; get; }
269
270         /// <summary>
271         /// ScrollEventArgs is a class to record scroll event arguments which will sent to user.
272         /// </summary>
273         /// This may be public opened in tizen_6.0 after ACR done. Before ACR, need to be hidden as inhouse API
274         [EditorBrowsable(EditorBrowsableState.Never)]
275         public class ScrollEventArgs : EventArgs
276         {
277             Position position;
278
279             /// <summary>
280             /// Default constructor.
281             /// </summary>
282             /// <param name="position">Current scroll position</param>
283             /// <since_tizen> 6 </since_tizen>
284             /// This may be public opened in tizen_6.0 after ACR done. Before ACR, need to be hidden as inhouse API
285             public ScrollEventArgs(Position position)
286             {
287                 this.position = position;
288             }
289
290             /// <summary>
291             /// [Draft] Current scroll position.
292             /// </summary>
293             /// This may be public opened in tizen_6.0 after ACR done. Before ACR, need to be hidden as inhouse API
294             [EditorBrowsable(EditorBrowsableState.Never)]
295             public Position Position
296             {
297                 get
298                 {
299                     return position;
300                 }
301             }
302         }
303
304         /// <summary>
305         /// An event emitted when user starts dragging ScrollableBase, user can subscribe or unsubscribe to this event handler.<br />
306         /// </summary>
307         /// This may be public opened in tizen_6.0 after ACR done. Before ACR, need to be hidden as inhouse API
308         [EditorBrowsable(EditorBrowsableState.Never)]
309         public event EventHandler<ScrollEventArgs> ScrollDragStartEvent;
310
311         /// <summary>
312         /// An event emitted when user stops dragging ScrollableBase, user can subscribe or unsubscribe to this event handler.<br />
313         /// </summary>
314         /// This may be public opened in tizen_6.0 after ACR done. Before ACR, need to be hidden as inhouse API
315         [EditorBrowsable(EditorBrowsableState.Never)]
316         public event EventHandler<ScrollEventArgs> ScrollDragEndEvent;
317
318
319         /// <summary>
320         /// An event emitted when the scrolling slide animation starts, user can subscribe or unsubscribe to this event handler.<br />
321         /// </summary>
322         /// This may be public opened in tizen_6.0 after ACR done. Before ACR, need to be hidden as inhouse API
323         [EditorBrowsable(EditorBrowsableState.Never)]
324         public event EventHandler<ScrollEventArgs> ScrollAnimationStartEvent;
325
326         /// <summary>
327         /// An event emitted when the scrolling slide animation ends, user can subscribe or unsubscribe to this event handler.<br />
328         /// </summary>
329         /// This may be public opened in tizen_6.0 after ACR done. Before ACR, need to be hidden as inhouse API
330         [EditorBrowsable(EditorBrowsableState.Never)]
331         public event EventHandler<ScrollEventArgs> ScrollAnimationEndEvent;
332
333
334         /// <summary>
335         /// An event emitted when scrolling, user can subscribe or unsubscribe to this event handler.<br />
336         /// </summary>
337         /// This may be public opened in tizen_6.0 after ACR done. Before ACR, need to be hidden as inhouse API
338         [EditorBrowsable(EditorBrowsableState.Never)]
339         public event EventHandler<ScrollEventArgs> ScrollEvent;
340
341
342         /// <summary>
343         /// Scrollbar for ScrollableBase.<br />
344         /// </summary>
345         /// This may be public opened in tizen_6.0 after ACR done. Before ACR, need to be hidden as inhouse API
346         [EditorBrowsable(EditorBrowsableState.Never)]
347         public ScrollbarBase Scrollbar
348         {
349             get
350             {
351                 return scrollBar;
352             }
353             set
354             {
355                 if (scrollBar)
356                 {
357                     scrollBar.Unparent();
358                 }
359
360                 scrollBar = value;
361                 scrollBar.Name = "ScrollBar";
362                 Add(scrollBar);
363
364                 if (hideScrollbar)
365                 {
366                     scrollBar.Hide();
367                 }
368                 else
369                 {
370                     scrollBar.Show();
371                 }
372
373                 SetScrollbar();
374             }
375         }
376
377         /// <summary>
378         /// [Draft] Always hide Scrollbar.
379         /// </summary>
380         /// This may be public opened in tizen_6.0 after ACR done. Before ACR, need to be hidden as inhouse API
381         [EditorBrowsable(EditorBrowsableState.Never)]
382         public bool HideScrollBar
383         {
384             get
385             {
386                 return hideScrollbar;
387             }
388             set
389             {
390                 hideScrollbar = value;
391
392                 if (scrollBar)
393                 {
394                     if (value)
395                     {
396                         scrollBar.Hide();
397                     }
398                     else
399                     {
400                         scrollBar.Show();
401                     }
402                 }
403             }
404         }
405
406         private bool hideScrollbar = true;
407         private Animation scrollAnimation;
408         private float maxScrollDistance;
409         private float childTargetPosition = 0.0f;
410         private PanGestureDetector mPanGestureDetector;
411         private TapGestureDetector mTapGestureDetector;
412         private View mScrollingChild;
413         private View mInterruptTouchingChild;
414         private ScrollbarBase scrollBar;
415         private float multiplier = 1.0f;
416         private bool scrolling = false;
417         private float ratioOfScreenWidthToCompleteScroll = 0.5f;
418         private float totalDisplacementForPan = 0.0f;
419         private Size previousContainerSize = new Size();
420
421         // If false then can only flick pages when the current animation/scroll as ended.
422         private bool flickWhenAnimating = false;
423         private PropertyNotification propertyNotification;
424
425         // Let's consider more whether this needs to be set as protected.
426         private float finalTargetPosition;
427
428         /// <summary>
429         /// [Draft] Constructor
430         /// </summary>
431         /// This may be public opened in tizen_6.0 after ACR done. Before ACR, need to be hidden as inhouse API
432         [EditorBrowsable(EditorBrowsableState.Never)]
433         public ScrollableBase() : base()
434         {
435             mPanGestureDetector = new PanGestureDetector();
436             mPanGestureDetector.Attach(this);
437             mPanGestureDetector.AddDirection(PanGestureDetector.DirectionVertical);
438             mPanGestureDetector.Detected += OnPanGestureDetected;
439
440             mTapGestureDetector = new TapGestureDetector();
441             mTapGestureDetector.Attach(this);
442             mTapGestureDetector.Detected += OnTapGestureDetected;
443
444             ClippingMode = ClippingModeType.ClipChildren;
445
446             mScrollingChild = new View();
447             mScrollingChild.Name = "DefaultScrollingChild";
448
449             //Interrupt touching when panning is started;
450             mInterruptTouchingChild = new View()
451             {
452                 Name = "InterruptTouchingChild",
453                 Size = new Size(Window.Instance.WindowSize),
454                 BackgroundColor = Color.Transparent,
455             };
456
457             mInterruptTouchingChild.TouchEvent += OnIterruptTouchingChildTouched;
458
459             Layout = new ScrollableBaseCustomLayout();
460
461             Scrollbar = new Scrollbar();
462         }
463
464         private bool OnIterruptTouchingChildTouched(object source, View.TouchEventArgs args)
465         {
466             return true;
467         }
468
469         private void OnPropertyChanged(object source, PropertyNotification.NotifyEventArgs args)
470         {
471             OnScroll();
472         }
473
474         /// <summary>
475         /// Called after a child has been added to the owning view.
476         /// </summary>
477         /// <param name="view">The child which has been added.</param>
478         /// This may be public opened in tizen_6.0 after ACR done. Before ACR, need to be hidden as inhouse API
479         [EditorBrowsable(EditorBrowsableState.Never)]
480         public override void OnChildAdd(View view)
481         {
482             if (null != view && view.Name != "InterruptTouchingChild" && view.Name != "ScrollBar")
483             {
484                 if (null != mScrollingChild && mScrollingChild.Name != "DefaultScrollingChild")
485                 {
486                     propertyNotification.Notified -= OnPropertyChanged;
487                     mScrollingChild.RemovePropertyNotification(propertyNotification);
488                     mScrollingChild.Relayout -= OnScrollingChildRelayout;
489                 }
490
491                 mScrollingChild = view;
492                 mScrollingChild.Layout.SetPositionByLayout = false;
493                 propertyNotification = mScrollingChild?.AddPropertyNotification("position", PropertyCondition.Step(1.0f));
494                 if (null != propertyNotification)
495                 {
496                     propertyNotification.Notified += OnPropertyChanged;
497                 }
498                 mScrollingChild.Relayout += OnScrollingChildRelayout;
499                 mScrollingChild.LowerToBottom();
500             }
501         }
502
503         /// <summary>
504         /// Called after a child has been removed from the owning view.
505         /// </summary>
506         /// <param name="view">The child which has been removed.</param>
507         /// This may be public opened in tizen_6.0 after ACR done. Before ACR, need to be hidden as inhouse API
508         [EditorBrowsable(EditorBrowsableState.Never)]
509         public override void OnChildRemove(View view)
510         {
511             if (view.Name != "InterruptTouchingChild" && view.Name != "ScrollBar")
512             {
513                 propertyNotification.Notified -= OnPropertyChanged;
514                 mScrollingChild.RemovePropertyNotification(propertyNotification);
515                 mScrollingChild.Relayout -= OnScrollingChildRelayout;
516
517                 mScrollingChild.Layout.SetPositionByLayout = true;
518                 mScrollingChild = new View();
519             }
520         }
521
522         private void OnScrollingChildRelayout(object source, EventArgs args)
523         {
524             // Size is changed. Calculate maxScrollDistance.
525             bool isSizeChanged = previousContainerSize.Width != mScrollingChild.Size.Width || previousContainerSize.Height != mScrollingChild.Size.Height;
526
527             if (isSizeChanged)
528             {
529                 maxScrollDistance = CalculateMaximumScrollDistance();
530                 SetScrollbar();
531             }
532
533             previousContainerSize = mScrollingChild.Size;
534         }
535
536         /// <summary>
537         /// The composition of a Scrollbar can vary depending on how you use ScrollableBase. 
538         /// Set the composition that will go into the ScrollableBase according to your ScrollableBase.
539         /// </summary>
540         /// This may be public opened in tizen_6.0 after ACR done. Before ACR, need to be hidden as inhouse API
541         [EditorBrowsable(EditorBrowsableState.Never)]
542         protected virtual void SetScrollbar()
543         {
544             if (Scrollbar)
545             {
546                 bool isHorizontal = ScrollingDirection == Direction.Horizontal;
547                 float contentLength = isHorizontal ? mScrollingChild.Size.Width : mScrollingChild.Size.Height;
548                 float viewportLength = isHorizontal ? Size.Width : Size.Height;
549                 float currentPosition = isHorizontal ? mScrollingChild.CurrentPosition.X : mScrollingChild.CurrentPosition.Y;
550                 Scrollbar.Initialize(contentLength, viewportLength, currentPosition, isHorizontal);
551             }
552         }
553
554         /// <summary>
555         /// Scrolls to the item at the specified index.
556         /// </summary>
557         /// <param name="index">Index of item.</param>
558         /// This may be public opened in tizen_6.0 after ACR done. Before ACR, need to be hidden as inhouse API
559         [EditorBrowsable(EditorBrowsableState.Never)]
560         public void ScrollToIndex(int index)
561         {
562             if (mScrollingChild.ChildCount - 1 < index || index < 0)
563             {
564                 return;
565             }
566
567             if (SnapToPage)
568             {
569                 CurrentPage = index;
570             }
571
572             float targetPosition = Math.Min(ScrollingDirection == Direction.Vertical ? mScrollingChild.Children[index].Position.Y : mScrollingChild.Children[index].Position.X, maxScrollDistance);
573             AnimateChildTo(ScrollDuration, -targetPosition);
574         }
575
576         private void OnScrollDragStart()
577         {
578             ScrollEventArgs eventArgs = new ScrollEventArgs(mScrollingChild.CurrentPosition);
579             ScrollDragStartEvent?.Invoke(this, eventArgs);
580         }
581
582         private void OnScrollDragEnd()
583         {
584             ScrollEventArgs eventArgs = new ScrollEventArgs(mScrollingChild.CurrentPosition);
585             ScrollDragEndEvent?.Invoke(this, eventArgs);
586         }
587
588         private void OnScrollAnimationStart()
589         {
590             ScrollEventArgs eventArgs = new ScrollEventArgs(mScrollingChild.CurrentPosition);
591             ScrollAnimationStartEvent?.Invoke(this, eventArgs);
592         }
593
594         private void OnScrollAnimationEnd()
595         {
596             ScrollEventArgs eventArgs = new ScrollEventArgs(mScrollingChild.CurrentPosition);
597             ScrollAnimationEndEvent?.Invoke(this, eventArgs);
598         }
599
600         private bool readyToNotice = false;
601
602         private float noticeAnimationEndBeforePosition = 0.0f;
603         // Let's consider more whether this needs to be set as protected.
604         public float NoticeAnimationEndBeforePosition { get => noticeAnimationEndBeforePosition; set => noticeAnimationEndBeforePosition = value; }
605
606         private void OnScroll()
607         {
608             ScrollEventArgs eventArgs = new ScrollEventArgs(mScrollingChild.CurrentPosition);
609             ScrollEvent?.Invoke(this, eventArgs);
610
611             bool isHorizontal = ScrollingDirection == Direction.Horizontal;
612             float contentLength = isHorizontal ? mScrollingChild.Size.Width : mScrollingChild.Size.Height;
613             float currentPosition = isHorizontal ? mScrollingChild.CurrentPosition.X : mScrollingChild.CurrentPosition.Y;
614
615             scrollBar.Update(contentLength, Math.Abs(currentPosition));
616             CheckPreReachedTargetPosition();
617         }
618
619         private void CheckPreReachedTargetPosition()
620         {
621             // Check whether we reached pre-reached target position
622             if (readyToNotice &&
623                 mScrollingChild.CurrentPosition.Y <= finalTargetPosition + NoticeAnimationEndBeforePosition &&
624                 mScrollingChild.CurrentPosition.Y >= finalTargetPosition - NoticeAnimationEndBeforePosition)
625             {
626                 //Notice first
627                 readyToNotice = false;
628                 OnPreReachedTargetPosition(finalTargetPosition);
629             }
630         }
631
632         /// <summary>
633         /// This helps developer who wants to know before scroll is reaching target position.
634         /// </summary>
635         /// <param name="targetPosition">Index of item.</param>
636         /// This may be public opened in tizen_6.0 after ACR done. Before ACR, need to be hidden as inhouse API
637         [EditorBrowsable(EditorBrowsableState.Never)]
638         protected virtual void OnPreReachedTargetPosition(float targetPosition)
639         {
640
641         }
642
643         private void StopScroll()
644         {
645             if (scrollAnimation != null)
646             {
647                 if (scrollAnimation.State == Animation.States.Playing)
648                 {
649                     Debug.WriteLineIf(LayoutDebugScrollableBase, "StopScroll Animation Playing");
650                     scrollAnimation.Stop(Animation.EndActions.Cancel);
651                     OnScrollAnimationEnd();
652                 }
653                 scrollAnimation.Clear();
654             }
655         }
656
657         // static constructor registers the control type
658         static ScrollableBase()
659         {
660             // ViewRegistry registers control type with DALi type registry
661             // also uses introspection to find any properties that need to be registered with type registry
662             CustomViewRegistry.Instance.Register(CreateInstance, typeof(ScrollableBase));
663         }
664
665         internal static CustomView CreateInstance()
666         {
667             return new ScrollableBase();
668         }
669
670         private void AnimateChildTo(int duration, float axisPosition)
671         {
672             Debug.WriteLineIf(LayoutDebugScrollableBase, "AnimationTo Animation Duration:" + duration + " Destination:" + axisPosition);
673             finalTargetPosition = axisPosition;
674
675             StopScroll(); // Will replace previous animation so will stop existing one.
676
677             if (scrollAnimation == null)
678             {
679                 scrollAnimation = new Animation();
680                 scrollAnimation.Finished += ScrollAnimationFinished;
681             }
682
683             scrollAnimation.Duration = duration;
684             scrollAnimation.DefaultAlphaFunction = new AlphaFunction(AlphaFunction.BuiltinFunctions.EaseOutSine);
685             scrollAnimation.AnimateTo(mScrollingChild, (ScrollingDirection == Direction.Horizontal) ? "PositionX" : "PositionY", axisPosition);
686             scrolling = true;
687             OnScrollAnimationStart();
688             scrollAnimation.Play();
689         }
690
691         /// <summary>
692         /// Scroll to specific position with or without animation.
693         /// </summary>
694         /// <param name="position">Destination.</param>
695         /// <param name="animate">Scroll with or without animation</param>
696         [EditorBrowsable(EditorBrowsableState.Never)]
697         public void ScrollTo(float position, bool animate)
698         {
699             float currentPositionX = mScrollingChild.CurrentPosition.X != 0 ? mScrollingChild.CurrentPosition.X : mScrollingChild.Position.X;
700             float currentPositionY = mScrollingChild.CurrentPosition.Y != 0 ? mScrollingChild.CurrentPosition.Y : mScrollingChild.Position.Y;
701             float delta = ScrollingDirection == Direction.Horizontal ? currentPositionX : currentPositionY;
702             // The argument position is the new pan position. So the new position of ScrollableBase becomes (-position).
703             // To move ScrollableBase's position to (-position), it moves by (-position - currentPosition).
704             delta = -position - delta;
705
706             ScrollBy(delta, animate);
707         }
708
709         private float BoundScrollPosition(float targetPosition)
710         {
711             if (ScrollAvailableArea != null)
712             {
713                 float minScrollPosition = ScrollAvailableArea.X;
714                 float maxScrollPosition = ScrollAvailableArea.Y;
715
716                 targetPosition = Math.Min(-minScrollPosition, targetPosition);
717                 targetPosition = Math.Max(-maxScrollPosition, targetPosition);
718             }
719             else
720             {
721                 targetPosition = Math.Min(0, targetPosition);
722                 targetPosition = Math.Max(-maxScrollDistance, targetPosition);
723             }
724
725             return targetPosition;
726         }
727
728         private void ScrollBy(float displacement, bool animate)
729         {
730             if (GetChildCount() == 0 || maxScrollDistance < 0)
731             {
732                 return;
733             }
734
735             float childCurrentPosition = (ScrollingDirection == Direction.Horizontal) ? mScrollingChild.PositionX : mScrollingChild.PositionY;
736
737             Debug.WriteLineIf(LayoutDebugScrollableBase, "ScrollBy childCurrentPosition:" + childCurrentPosition +
738                                                    " displacement:" + displacement,
739                                                    " maxScrollDistance:" + maxScrollDistance);
740
741             childTargetPosition = childCurrentPosition + displacement; // child current position + gesture displacement
742
743
744             Debug.WriteLineIf(LayoutDebugScrollableBase, "ScrollBy currentAxisPosition:" + childCurrentPosition + "childTargetPosition:" + childTargetPosition);
745
746             if (animate)
747             {
748                 // Calculate scroll animaton duration
749                 float scrollDistance = Math.Abs(displacement);
750                 int duration = (int)((320 * FlickAnimationSpeed) + (scrollDistance * FlickAnimationSpeed));
751                 Debug.WriteLineIf(LayoutDebugScrollableBase, "Scroll Animation Duration:" + duration + " Distance:" + scrollDistance);
752
753                 readyToNotice = true;
754
755                 AnimateChildTo(duration, BoundScrollPosition(AdjustTargetPositionOfScrollAnimation(BoundScrollPosition(childTargetPosition))));
756             }
757             else
758             {
759                 finalTargetPosition = BoundScrollPosition(childTargetPosition);
760
761                 // Set position of scrolling child without an animation
762                 if (ScrollingDirection == Direction.Horizontal)
763                 {
764                     mScrollingChild.PositionX = finalTargetPosition;
765                 }
766                 else
767                 {
768                     mScrollingChild.PositionY = finalTargetPosition;
769                 }
770
771             }
772         }
773
774         /// <summary>
775         /// you can override it to clean-up your own resources.
776         /// </summary>
777         /// <param name="type">DisposeTypes</param>
778         /// This will be public opened in tizen_5.5 after ACR done. Before ACR, need to be hidden as inhouse API.
779         [EditorBrowsable(EditorBrowsableState.Never)]
780         protected override void Dispose(DisposeTypes type)
781         {
782             if (disposed)
783             {
784                 return;
785             }
786
787             if (type == DisposeTypes.Explicit)
788             {
789                 StopScroll();
790
791                 if (mPanGestureDetector != null)
792                 {
793                     mPanGestureDetector.Detected -= OnPanGestureDetected;
794                     mPanGestureDetector.Dispose();
795                     mPanGestureDetector = null;
796                 }
797
798                 if (mTapGestureDetector != null)
799                 {
800                     mTapGestureDetector.Detected -= OnTapGestureDetected;
801                     mTapGestureDetector.Dispose();
802                     mTapGestureDetector = null;
803                 }
804             }
805             base.Dispose(type);
806         }
807
808         private float CalculateDisplacementFromVelocity(float axisVelocity)
809         {
810             // Map: flick speed of range (2.0 - 6.0) to flick multiplier of range (0.7 - 1.6)
811             float speedMinimum = FlickThreshold;
812             float speedMaximum = FlickThreshold + 6.0f;
813             float multiplierMinimum = FlickDistanceMultiplierRange.X;
814             float multiplierMaximum = FlickDistanceMultiplierRange.Y;
815
816             float flickDisplacement = 0.0f;
817
818             float speed = Math.Min(4.0f, Math.Abs(axisVelocity));
819
820             Debug.WriteLineIf(LayoutDebugScrollableBase, "ScrollableBase Candidate Flick speed:" + speed);
821
822             if (speed > FlickThreshold)
823             {
824                 // Flick length is the length of the ScrollableBase.
825                 float flickLength = (ScrollingDirection == Direction.Horizontal) ? CurrentSize.Width : CurrentSize.Height;
826
827                 // Calculate multiplier by mapping speed between the multiplier minimum and maximum.
828                 multiplier = ((speed - speedMinimum) / ((speedMaximum - speedMinimum) * (multiplierMaximum - multiplierMinimum))) + multiplierMinimum;
829
830                 // flick displacement is the product of the flick length and multiplier
831                 flickDisplacement = ((flickLength * multiplier) * speed) / axisVelocity;  // *speed and /velocity to perserve sign.
832
833                 Debug.WriteLineIf(LayoutDebugScrollableBase, "Calculated FlickDisplacement[" + flickDisplacement + "] from speed[" + speed + "] multiplier:"
834                                                         + multiplier);
835             }
836             return flickDisplacement;
837         }
838
839         private float CalculateMaximumScrollDistance()
840         {
841             float scrollingChildLength = 0;
842             float scrollerLength = 0;
843             if (ScrollingDirection == Direction.Horizontal)
844             {
845                 Debug.WriteLineIf(LayoutDebugScrollableBase, "Horizontal");
846
847                 scrollingChildLength = mScrollingChild.Size.Width;
848                 scrollerLength = Size.Width;
849             }
850             else
851             {
852                 Debug.WriteLineIf(LayoutDebugScrollableBase, "Vertical");
853                 scrollingChildLength = mScrollingChild.Size.Height;
854                 scrollerLength = Size.Height;
855             }
856
857             Debug.WriteLineIf(LayoutDebugScrollableBase, "ScrollBy maxScrollDistance:" + (scrollingChildLength - scrollerLength) +
858                                                    " parent length:" + scrollerLength +
859                                                    " scrolling child length:" + scrollingChildLength);
860
861             return Math.Max(scrollingChildLength - scrollerLength, 0);
862         }
863
864         private void PageSnap()
865         {
866             Debug.WriteLineIf(LayoutDebugScrollableBase, "PageSnap with pan candidate totalDisplacement:" + totalDisplacementForPan +
867                                                                 " currentPage[" + CurrentPage + "]");
868
869             //Increment current page if total displacement enough to warrant a page change.
870             if (Math.Abs(totalDisplacementForPan) > (mPageWidth * ratioOfScreenWidthToCompleteScroll))
871             {
872                 if (totalDisplacementForPan < 0)
873                 {
874                     CurrentPage = Math.Min(Math.Max(mScrollingChild.Children.Count - 1, 0), ++CurrentPage);
875                 }
876                 else
877                 {
878                     CurrentPage = Math.Max(0, --CurrentPage);
879                 }
880             }
881
882             // Animate to new page or reposition to current page
883             float destinationX = -(mScrollingChild.Children[CurrentPage].Position.X + mScrollingChild.Children[CurrentPage].CurrentSize.Width / 2 - CurrentSize.Width / 2); // set to middle of current page
884             Debug.WriteLineIf(LayoutDebugScrollableBase, "Snapping to page[" + CurrentPage + "] to:" + destinationX + " from:" + mScrollingChild.PositionX);
885             AnimateChildTo(ScrollDuration, destinationX);
886         }
887
888         private void Flick(float flickDisplacement)
889         {
890             if (SnapToPage)
891             {
892                 if ((flickWhenAnimating && scrolling == true) || (scrolling == false))
893                 {
894                     if (flickDisplacement < 0)
895                     {
896                         CurrentPage = Math.Min(Math.Max(mScrollingChild.Children.Count - 1, 0), CurrentPage + 1);
897                         Debug.WriteLineIf(LayoutDebugScrollableBase, "Snap - to page:" + CurrentPage);
898                     }
899                     else
900                     {
901                         CurrentPage = Math.Max(0, CurrentPage - 1);
902                         Debug.WriteLineIf(LayoutDebugScrollableBase, "Snap + to page:" + CurrentPage);
903                     }
904
905                     float destinationX = -(mScrollingChild.Children[CurrentPage].Position.X + mScrollingChild.Children[CurrentPage].CurrentSize.Width / 2.0f - CurrentSize.Width / 2.0f); // set to middle of current page
906                     Debug.WriteLineIf(LayoutDebugScrollableBase, "Snapping to :" + destinationX);
907                     AnimateChildTo(ScrollDuration, destinationX);
908                 }
909             }
910             else
911             {
912                 ScrollBy(flickDisplacement, true); // Animate flickDisplacement.
913             }
914         }
915
916         private void OnPanGestureDetected(object source, PanGestureDetector.DetectedEventArgs e)
917         {
918             if (e.PanGesture.State == Gesture.StateType.Started)
919             {
920                 Add(mInterruptTouchingChild);
921                 Debug.WriteLineIf(LayoutDebugScrollableBase, "Gesture Start");
922                 if (scrolling && !SnapToPage)
923                 {
924                     StopScroll();
925                 }
926                 totalDisplacementForPan = 0.0f;
927                 OnScrollDragStart();
928             }
929             else if (e.PanGesture.State == Gesture.StateType.Continuing)
930             {
931                 if (ScrollingDirection == Direction.Horizontal)
932                 {
933                     ScrollBy(e.PanGesture.Displacement.X, false);
934                     totalDisplacementForPan += e.PanGesture.Displacement.X;
935                 }
936                 else
937                 {
938                     ScrollBy(e.PanGesture.Displacement.Y, false);
939                     totalDisplacementForPan += e.PanGesture.Displacement.Y;
940                 }
941                 Debug.WriteLineIf(LayoutDebugScrollableBase, "OnPanGestureDetected Continue totalDisplacementForPan:" + totalDisplacementForPan);
942             }
943             else if (e.PanGesture.State == Gesture.StateType.Finished)
944             {
945                 float axisVelocity = (ScrollingDirection == Direction.Horizontal) ? e.PanGesture.Velocity.X : e.PanGesture.Velocity.Y;
946                 float flickDisplacement = CalculateDisplacementFromVelocity(axisVelocity);
947
948                 Debug.WriteLineIf(LayoutDebugScrollableBase, "FlickDisplacement:" + flickDisplacement + "TotalDisplacementForPan:" + totalDisplacementForPan);
949                 OnScrollDragEnd();
950
951                 if (flickDisplacement > 0 | flickDisplacement < 0)// Flick detected
952                 {
953                     Flick(flickDisplacement);
954                 }
955                 else
956                 {
957                     // End of panning gesture but was not a flick
958                     if (SnapToPage)
959                     {
960                         PageSnap();
961                     }
962                     else
963                     {
964                         ScrollBy(0, true);
965                     }
966                 }
967                 totalDisplacementForPan = 0;
968
969                 Remove(mInterruptTouchingChild);
970             }
971         }
972
973         private new void OnTapGestureDetected(object source, TapGestureDetector.DetectedEventArgs e)
974         {
975             if (e.TapGesture.Type == Gesture.GestureType.Tap)
976             {
977                 // Stop scrolling if tap detected (press then relase).
978                 // Unless in Pages mode, do not want a page change to stop part way.
979                 if (scrolling && !SnapToPage)
980                 {
981                     StopScroll();
982                 }
983             }
984         }
985
986         private void ScrollAnimationFinished(object sender, EventArgs e)
987         {
988             scrolling = false;
989             CheckPreReachedTargetPosition();
990             OnScrollAnimationEnd();
991         }
992
993         /// <summary>
994         /// Adjust scrolling position by own scrolling rules.
995         /// Override this function when developer wants to change destination of flicking.(e.g. always snap to center of item)
996         /// </summary>
997         /// This may be public opened in tizen_6.0 after ACR done. Before ACR, need to be hidden as inhouse API
998         [EditorBrowsable(EditorBrowsableState.Never)]
999         protected virtual float AdjustTargetPositionOfScrollAnimation(float position)
1000         {
1001             return position;
1002         }
1003
1004     }
1005
1006 } // namespace