1 /* Copyright (c) 2021 Samsung Electronics Co., Ltd.
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
7 * http://www.apache.org/licenses/LICENSE-2.0
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.
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;
24 namespace Tizen.NUI.Components
27 /// ScrollEventArgs is a class to record scroll event arguments which will sent to user.
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
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;
39 /// Default constructor.
41 /// <param name="position">Current container position</param>
42 /// <since_tizen> 8 </since_tizen>
43 public ScrollEventArgs(Position position)
45 this.position = position;
46 this.scrollPosition = new Position(-position);
50 /// Current position of ContentContainer.
52 /// <since_tizen> 8 </since_tizen>
53 public Position Position
61 /// Current scroll position of scrollableBase pan.
62 /// This is the position in the opposite direction to the current position of ContentContainer.
64 [EditorBrowsable(EditorBrowsableState.Never)]
65 public Position ScrollPosition
69 return scrollPosition;
75 /// ScrollOutofBoundEventArgs is to record scroll out-of-bound event arguments which will be sent to user.
77 [EditorBrowsable(EditorBrowsableState.Never)]
78 public class ScrollOutOfBoundEventArgs : EventArgs
81 /// The direction to be touched.
83 [EditorBrowsable(EditorBrowsableState.Never)]
89 [EditorBrowsable(EditorBrowsableState.Never)]
95 [EditorBrowsable(EditorBrowsableState.Never)]
101 [EditorBrowsable(EditorBrowsableState.Never)]
107 [EditorBrowsable(EditorBrowsableState.Never)]
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)
119 PanDirection = direction;
120 Displacement = displacement;
124 /// Current pan direction of ContentContainer.
126 [EditorBrowsable(EditorBrowsableState.Never)]
127 public Direction PanDirection
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.
137 [EditorBrowsable(EditorBrowsableState.Never)]
138 public float Displacement
145 /// This class provides a View that can scroll a single View with a layout. This View can be a nest of Views.
147 /// <since_tizen> 8 </since_tizen>
148 public partial class ScrollableBase : Control
150 static bool LayoutDebugScrollableBase = false; // Debug flag
151 private Direction mScrollingDirection = Direction.Vertical;
152 private bool mScrollEnabled = true;
153 private int mScrollDuration = 125;
154 private int mPageWidth = 0;
155 private float mPageFlickThreshold = 0.4f;
156 private float mScrollingEventThreshold = 0.001f;
157 private bool fadeScrollbar = true;
159 private class ScrollableBaseCustomLayout : AbsoluteLayout
161 protected override void OnMeasure(MeasureSpecification widthMeasureSpec, MeasureSpecification heightMeasureSpec)
163 MeasuredSize.StateType childWidthState = MeasuredSize.StateType.MeasuredSizeOK;
164 MeasuredSize.StateType childHeightState = MeasuredSize.StateType.MeasuredSizeOK;
166 Direction scrollingDirection = Direction.Vertical;
167 ScrollableBase scrollableBase = this.Owner as ScrollableBase;
168 if (scrollableBase != null)
170 scrollingDirection = scrollableBase.ScrollingDirection;
173 float totalWidth = 0.0f;
174 float totalHeight = 0.0f;
176 // measure child, should be a single scrolling child
177 foreach (LayoutItem childLayout in LayoutChildren)
179 if (childLayout != null && childLayout.Owner.Name == "ContentContainer")
182 // Use an Unspecified MeasureSpecification mode so scrolling child is not restricted to it's parents size in Height (for vertical scrolling)
183 // or Width for horizontal scrolling
184 if (scrollingDirection == Direction.Vertical)
186 MeasureSpecification unrestrictedMeasureSpec = new MeasureSpecification(heightMeasureSpec.Size, MeasureSpecification.ModeType.Unspecified);
187 MeasureChildWithMargins(childLayout, widthMeasureSpec, new LayoutLength(0), unrestrictedMeasureSpec, new LayoutLength(0)); // Height unrestricted by parent
191 MeasureSpecification unrestrictedMeasureSpec = new MeasureSpecification(widthMeasureSpec.Size, MeasureSpecification.ModeType.Unspecified);
192 MeasureChildWithMargins(childLayout, unrestrictedMeasureSpec, new LayoutLength(0), heightMeasureSpec, new LayoutLength(0)); // Width unrestricted by parent
195 totalWidth = (childLayout.MeasuredWidth.Size + (childLayout.Padding.Start + childLayout.Padding.End)).AsDecimal();
196 totalHeight = (childLayout.MeasuredHeight.Size + (childLayout.Padding.Top + childLayout.Padding.Bottom)).AsDecimal();
198 if (childLayout.MeasuredWidth.State == MeasuredSize.StateType.MeasuredSizeTooSmall)
200 childWidthState = MeasuredSize.StateType.MeasuredSizeTooSmall;
202 if (childLayout.MeasuredHeight.State == MeasuredSize.StateType.MeasuredSizeTooSmall)
204 childHeightState = MeasuredSize.StateType.MeasuredSizeTooSmall;
209 MeasuredSize widthSizeAndState = ResolveSizeAndState(new LayoutLength(totalWidth), widthMeasureSpec, MeasuredSize.StateType.MeasuredSizeOK);
210 MeasuredSize heightSizeAndState = ResolveSizeAndState(new LayoutLength(totalHeight), heightMeasureSpec, MeasuredSize.StateType.MeasuredSizeOK);
211 totalWidth = widthSizeAndState.Size.AsDecimal();
212 totalHeight = heightSizeAndState.Size.AsDecimal();
214 // Ensure layout respects it's given minimum size
215 totalWidth = Math.Max(totalWidth, SuggestedMinimumWidth.AsDecimal());
216 totalHeight = Math.Max(totalHeight, SuggestedMinimumHeight.AsDecimal());
218 widthSizeAndState.State = childWidthState;
219 heightSizeAndState.State = childHeightState;
221 SetMeasuredDimensions(ResolveSizeAndState(new LayoutLength(totalWidth), widthMeasureSpec, childWidthState),
222 ResolveSizeAndState(new LayoutLength(totalHeight), heightMeasureSpec, childHeightState));
224 // Size of ScrollableBase is changed. Change Page width too.
225 if (scrollableBase != null)
227 scrollableBase.mPageWidth = (int)MeasuredWidth.Size.AsRoundedValue();
228 scrollableBase.OnScrollingChildRelayout(null, null);
231 } // ScrollableBaseCustomLayout
234 /// The direction axis to scroll.
236 /// <since_tizen> 8 </since_tizen>
237 public enum Direction
242 /// <since_tizen> 8 </since_tizen>
248 /// <since_tizen> 8 </since_tizen>
253 /// Scrolling direction mode.
254 /// Default is Vertical scrolling.
256 /// <since_tizen> 8 </since_tizen>
257 public Direction ScrollingDirection
261 return (Direction)GetValue(ScrollingDirectionProperty);
265 SetValue(ScrollingDirectionProperty, value);
266 NotifyPropertyChanged();
269 private Direction InternalScrollingDirection
273 return mScrollingDirection;
277 if (value != mScrollingDirection)
279 //Reset scroll position and stop scroll animation
282 mScrollingDirection = value;
283 mPanGestureDetector.ClearAngles();
284 mPanGestureDetector.AddDirection(value == Direction.Horizontal ?
285 PanGestureDetector.DirectionHorizontal : PanGestureDetector.DirectionVertical);
287 ContentContainer.WidthSpecification = ScrollingDirection == Direction.Vertical ? LayoutParamPolicies.MatchParent : LayoutParamPolicies.WrapContent;
288 ContentContainer.HeightSpecification = ScrollingDirection == Direction.Vertical ? LayoutParamPolicies.WrapContent : LayoutParamPolicies.MatchParent;
295 /// Enable or disable scrolling.
297 /// <since_tizen> 8 </since_tizen>
298 public bool ScrollEnabled
302 return (bool)GetValue(ScrollEnabledProperty);
306 SetValue(ScrollEnabledProperty, value);
307 NotifyPropertyChanged();
310 private bool InternalScrollEnabled
314 return mScrollEnabled;
318 if (value != mScrollEnabled)
320 mScrollEnabled = value;
323 mPanGestureDetector.Detected += OnPanGestureDetected;
327 mPanGestureDetector.Detected -= OnPanGestureDetected;
334 /// Gets scrollable status.
336 [EditorBrowsable(EditorBrowsableState.Never)]
337 protected override bool AccessibilityIsScrollable()
343 /// Pages mode, enables moving to the next or return to current page depending on pan displacement.
344 /// Default is false.
346 /// <since_tizen> 8 </since_tizen>
347 public bool SnapToPage
351 return (bool)GetValue(SnapToPageProperty);
355 SetValue(SnapToPageProperty, value);
356 NotifyPropertyChanged();
359 private bool InternalSnapToPage { set; get; } = false;
362 /// Get current page.
363 /// Working property with SnapToPage property.
365 /// <since_tizen> 8 </since_tizen>
366 public int CurrentPage { get; private set; } = 0;
369 /// Duration of scroll animation.
370 /// Default value is 125ms.
372 /// <since_tizen> 8 </since_tizen>
373 public int ScrollDuration
377 return (int)GetValue(ScrollDurationProperty);
381 SetValue(ScrollDurationProperty, value);
382 NotifyPropertyChanged();
385 private int InternalScrollDuration
389 mScrollDuration = value >= 0 ? value : mScrollDuration;
393 return mScrollDuration;
398 /// Scroll Available area.
400 /// <since_tizen> 8 </since_tizen>
401 public Vector2 ScrollAvailableArea
405 return GetValue(ScrollAvailableAreaProperty) as Vector2;
409 SetValue(ScrollAvailableAreaProperty, value);
410 NotifyPropertyChanged();
413 private Vector2 InternalScrollAvailableArea { set; get; }
416 /// An event emitted when user starts dragging ScrollableBase, user can subscribe or unsubscribe to this event handler.<br />
418 /// <since_tizen> 8 </since_tizen>
419 public event EventHandler<ScrollEventArgs> ScrollDragStarted;
422 /// An event emitted when user stops dragging ScrollableBase, user can subscribe or unsubscribe to this event handler.<br />
424 /// <since_tizen> 8 </since_tizen>
425 public event EventHandler<ScrollEventArgs> ScrollDragEnded;
428 /// An event emitted when the scrolling slide animation starts, user can subscribe or unsubscribe to this event handler.<br />
430 /// <since_tizen> 8 </since_tizen>
431 public event EventHandler<ScrollEventArgs> ScrollAnimationStarted;
434 /// An event emitted when the scrolling slide animation ends, user can subscribe or unsubscribe to this event handler.<br />
436 /// <since_tizen> 8 </since_tizen>
437 public event EventHandler<ScrollEventArgs> ScrollAnimationEnded;
440 /// An event emitted when scrolling, user can subscribe or unsubscribe to this event handler.<br />
442 /// <since_tizen> 8 </since_tizen>
443 public event EventHandler<ScrollEventArgs> Scrolling;
446 /// An event emitted when scrolling out of bound, user can subscribe or unsubscribe to this event handler.<br />
448 [EditorBrowsable(EditorBrowsableState.Never)]
449 public event EventHandler<ScrollOutOfBoundEventArgs> ScrollOutOfBound;
452 /// Scrollbar for ScrollableBase.
454 /// <since_tizen> 8 </since_tizen>
455 public ScrollbarBase Scrollbar
459 return GetValue(ScrollbarProperty) as ScrollbarBase;
463 SetValue(ScrollbarProperty, value);
464 NotifyPropertyChanged();
467 private ScrollbarBase InternalScrollbar
477 base.Remove(scrollBar);
481 if (scrollBar != null)
483 scrollBar.Name = "ScrollBar";
501 /// Always hide Scrollbar.
503 /// <since_tizen> 8 </since_tizen>
504 public bool HideScrollbar
508 return (bool)GetValue(HideScrollbarProperty);
512 SetValue(HideScrollbarProperty, value);
513 NotifyPropertyChanged();
516 private bool InternalHideScrollbar
520 return hideScrollbar;
524 hideScrollbar = value;
537 scrollBar.Opacity = 1.0f;
546 /// The boolean flag for automatic fading Scrollbar.
547 /// Scrollbar will be faded out when scroll stay in certain position longer than the threshold.
548 /// Scrollbar will be faded in scroll position changes.
550 [EditorBrowsable(EditorBrowsableState.Never)]
551 public bool FadeScrollbar
553 get => (bool)GetValue(FadeScrollbarProperty);
554 set => SetValue(FadeScrollbarProperty, value);
557 private bool InternalFadeScrollbar
561 return fadeScrollbar;
565 fadeScrollbar = value;
567 if (scrollBar != null && !hideScrollbar)
575 scrollBar.Opacity = 1.0f;
576 // Removing fadeout timer and animation.
584 /// Container which has content of ScrollableBase.
586 /// <since_tizen> 8 </since_tizen>
587 public View ContentContainer { get; private set; }
590 /// Set the layout on this View. Replaces any existing Layout.
592 /// <since_tizen> 8 </since_tizen>
593 public new LayoutItem Layout
597 return GetValue(LayoutProperty) as LayoutItem;
601 SetValue(LayoutProperty, value);
602 NotifyPropertyChanged();
605 private LayoutItem InternalLayout
609 return ContentContainer.Layout;
613 ContentContainer.Layout = value;
618 /// List of children of Container.
620 /// <since_tizen> 8 </since_tizen>
621 public new List<View> Children
625 return ContentContainer.Children;
630 /// Deceleration rate of scrolling by finger.
631 /// Rate should be bigger than 0 and smaller than 1.
632 /// Default value is 0.998f;
634 /// <since_tizen> 8 </since_tizen>
635 public float DecelerationRate
639 return (float)GetValue(DecelerationRateProperty);
643 SetValue(DecelerationRateProperty, value);
644 NotifyPropertyChanged();
647 private float InternalDecelerationRate
651 return decelerationRate;
655 decelerationRate = (value < 1 && value > 0) ? value : decelerationRate;
656 logValueOfDeceleration = (float)Math.Log(value);
661 /// Threshold not to go infinite at the end of scrolling animation.
663 [EditorBrowsable(EditorBrowsableState.Never)]
664 public float DecelerationThreshold
668 return (float)GetValue(DecelerationThresholdProperty);
672 SetValue(DecelerationThresholdProperty, value);
673 NotifyPropertyChanged();
676 private float InternalDecelerationThreshold { get; set; } = 0.1f;
679 /// Scrolling event will be thrown when this amount of scroll position is changed.
680 /// If this threshold becomes smaller, the tracking detail increases but the scrolling range that can be tracked becomes smaller.
681 /// If large sized ContentContainer is required, please use larger threshold value.
682 /// Default ScrollingEventThreshold value is 0.001f.
684 [EditorBrowsable(EditorBrowsableState.Never)]
685 public float ScrollingEventThreshold
689 return (float)GetValue(ScrollingEventThresholdProperty);
693 SetValue(ScrollingEventThresholdProperty, value);
694 NotifyPropertyChanged();
697 private float InternalScrollingEventThreshold
701 return mScrollingEventThreshold;
705 if (mScrollingEventThreshold != value && value > 0)
707 ContentContainer.RemovePropertyNotification(propertyNotification);
708 propertyNotification = ContentContainer.AddPropertyNotification("position", PropertyCondition.Step(value));
709 propertyNotification.Notified += OnPropertyChanged;
710 mScrollingEventThreshold = value;
716 /// Page will be changed when velocity of panning is over threshold.
717 /// The unit of threshold is pixel per millisecond.
719 /// <since_tizen> 8 </since_tizen>
720 public float PageFlickThreshold
724 return (float)GetValue(PageFlickThresholdProperty);
728 SetValue(PageFlickThresholdProperty, value);
729 NotifyPropertyChanged();
732 private float InternalPageFlickThreshold
736 return mPageFlickThreshold;
740 mPageFlickThreshold = value >= 0f ? value : mPageFlickThreshold;
745 /// Padding for the ScrollableBase
747 [EditorBrowsable(EditorBrowsableState.Never)]
748 public new Extents Padding
752 return GetValue(PaddingProperty) as Extents;
756 SetValue(PaddingProperty, value);
757 NotifyPropertyChanged();
760 private Extents InternalPadding
764 return ContentContainer.Padding;
768 ContentContainer.Padding = value;
773 /// Alphafunction for scroll animation.
775 [EditorBrowsable(EditorBrowsableState.Never)]
776 public AlphaFunction ScrollAlphaFunction
780 return GetValue(ScrollAlphaFunctionProperty) as AlphaFunction;
784 SetValue(ScrollAlphaFunctionProperty, value);
785 NotifyPropertyChanged();
788 private AlphaFunction InternalScrollAlphaFunction { get; set; } = new AlphaFunction(AlphaFunction.BuiltinFunctions.Linear);
790 private bool hideScrollbar = true;
791 private float maxScrollDistance;
792 private float childTargetPosition = 0.0f;
793 private PanGestureDetector mPanGestureDetector;
794 private ScrollbarBase scrollBar;
795 private bool scrolling = false;
796 private float ratioOfScreenWidthToCompleteScroll = 0.5f;
797 private float totalDisplacementForPan = 0.0f;
798 private Size previousContainerSize = new Size();
799 private Size previousSize = new Size();
800 private PropertyNotification propertyNotification;
801 private float noticeAnimationEndBeforePosition = 0.0f;
802 private bool readyToNotice = false;
805 /// Notice before animation is finished.
807 [EditorBrowsable(EditorBrowsableState.Never)]
808 // Let's consider more whether this needs to be set as protected.
809 public float NoticeAnimationEndBeforePosition
813 return (float)GetValue(NoticeAnimationEndBeforePositionProperty);
817 SetValue(NoticeAnimationEndBeforePositionProperty, value);
818 NotifyPropertyChanged();
821 private float InternalNoticeAnimationEndBeforePosition
823 get => noticeAnimationEndBeforePosition;
824 set => noticeAnimationEndBeforePosition = value;
828 /// Step scroll move distance.
829 /// Key focus originally moves focusable objects, but in ScrollableBase,
830 /// if focusable object is too far or un-exist and ScrollableBase is focusable,
831 /// it can scroll move itself by key input.
832 /// this value decide how long distance will it moves in one step.
833 /// if any value is not set, step will be moved quater size of ScrollableBase length.
835 [EditorBrowsable(EditorBrowsableState.Never)]
836 public float StepScrollDistance
840 return (float)GetValue(StepScrollDistanceProperty);
844 SetValue(StepScrollDistanceProperty, value);
845 NotifyPropertyChanged();
848 private float stepScrollDistance = 0f;
851 // Let's consider more whether this needs to be set as protected.
852 private float finalTargetPosition;
854 private Animation scrollAnimation;
855 // Declare user alpha function delegate
856 [UnmanagedFunctionPointer(CallingConvention.StdCall)]
857 private delegate float UserAlphaFunctionDelegate(float progress);
858 private UserAlphaFunctionDelegate customScrollAlphaFunction;
859 private float velocityOfLastPan = 0.0f;
860 private float panAnimationDuration = 0.0f;
861 private float panAnimationDelta = 0.0f;
862 private float logValueOfDeceleration = 0.0f;
863 private float decelerationRate = 0.0f;
865 private View topOverShootingShadowView;
866 private View bottomOverShootingShadowView;
867 private View leftOverShootingShadowView;
868 private View rightOverShootingShadowView;
869 private const int overShootingShadowScaleHeightLimit = 64 * 3;
870 private const int overShootingShadowAnimationDuration = 300;
871 private Animation overShootingShadowAnimation;
872 private bool isOverShootingShadowShown = false;
873 private float startShowShadowDisplacement;
876 /// Default Constructor
878 /// <since_tizen> 8 </since_tizen>
879 public ScrollableBase() : base()
881 DecelerationRate = 0.998f;
883 base.Layout = new ScrollableBaseCustomLayout();
884 mPanGestureDetector = new PanGestureDetector();
885 mPanGestureDetector.Attach(this);
886 mPanGestureDetector.AddDirection(PanGestureDetector.DirectionVertical);
887 if (mPanGestureDetector.GetMaximumTouchesRequired() < 2) mPanGestureDetector.SetMaximumTouchesRequired(2);
888 mPanGestureDetector.Detected += OnPanGestureDetected;
890 ClippingMode = ClippingModeType.ClipToBoundingBox;
892 //Default Scrolling child
893 ContentContainer = new View()
895 Name = "ContentContainer",
896 WidthSpecification = ScrollingDirection == Direction.Vertical ? LayoutParamPolicies.MatchParent : LayoutParamPolicies.WrapContent,
897 HeightSpecification = ScrollingDirection == Direction.Vertical ? LayoutParamPolicies.WrapContent : LayoutParamPolicies.MatchParent,
899 // Check if children's sizes change to update Scrollbar
900 ContentContainer.Relayout += OnScrollingChildRelayout;
901 propertyNotification = ContentContainer.AddPropertyNotification("position", PropertyCondition.Step(mScrollingEventThreshold));
902 propertyNotification.Notified += OnPropertyChanged;
903 base.Add(ContentContainer);
904 // Check if ScrollableBase's size changes to update Scrollbar
905 base.Relayout += OnScrollingChildRelayout;
907 Scrollbar = new Scrollbar();
909 //Show vertical shadow on the top (or bottom) of the scrollable when panning down (or up).
910 topOverShootingShadowView = new View
912 BackgroundImage = FrameworkInformation.ResourcePath + "nui_component_default_scroll_over_shooting_top.png",
915 PositionUsesPivotPoint = true,
916 ParentOrigin = NUI.ParentOrigin.TopCenter,
917 PivotPoint = NUI.PivotPoint.TopCenter,
919 bottomOverShootingShadowView = new View
921 BackgroundImage = FrameworkInformation.ResourcePath + "nui_component_default_scroll_over_shooting_bottom.png",
924 PositionUsesPivotPoint = true,
925 ParentOrigin = NUI.ParentOrigin.BottomCenter,
926 PivotPoint = NUI.PivotPoint.BottomCenter,
928 //Show horizontal shadow on the left (or right) of the scrollable when panning down (or up).
929 leftOverShootingShadowView = new View
931 BackgroundImage = FrameworkInformation.ResourcePath + "nui_component_default_scroll_over_shooting_left.png",
934 PositionUsesPivotPoint = true,
935 ParentOrigin = NUI.ParentOrigin.CenterLeft,
936 PivotPoint = NUI.PivotPoint.CenterLeft,
938 rightOverShootingShadowView = new View
940 BackgroundImage = FrameworkInformation.ResourcePath + "nui_component_default_scroll_over_shooting_right.png",
943 PositionUsesPivotPoint = true,
944 ParentOrigin = NUI.ParentOrigin.CenterRight,
945 PivotPoint = NUI.PivotPoint.CenterRight,
948 AccessibilityManager.Instance.SetAccessibilityAttribute(this, AccessibilityManager.AccessibilityAttribute.Trait, "ScrollableBase");
950 SetKeyboardNavigationSupport(true);
953 private bool OnInterruptTouchingChildTouched(object source, View.TouchEventArgs args)
955 if (args.Touch.GetState(0) == PointStateType.Down)
957 if (scrolling && !SnapToPage)
965 private void OnPropertyChanged(object source, PropertyNotification.NotifyEventArgs args)
971 /// Called after a child has been added to the owning view.
973 /// <param name="view">The child which has been added.</param>
974 /// <since_tizen> 8 </since_tizen>
975 public override void Add(View view)
977 ContentContainer.Add(view);
981 /// Called after a child has been removed from the owning view.
983 /// <param name="view">The child which has been removed.</param>
984 /// <since_tizen> 8 </since_tizen>
985 public override void Remove(View view)
987 if (SnapToPage && CurrentPage == Children.IndexOf(view) && CurrentPage == Children.Count - 1 && Children.Count > 1)
989 // Target View is current page and also last child.
990 // CurrentPage should be changed to previous page.
991 ScrollToIndex(CurrentPage - 1);
994 ContentContainer.Remove(view);
997 private void OnScrollingChildRelayout(object source, EventArgs args)
999 // Size is changed. Calculate maxScrollDistance.
1000 bool isSizeChanged = previousContainerSize.Width != ContentContainer.Size.Width || previousContainerSize.Height != ContentContainer.Size.Height ||
1001 previousSize.Width != Size.Width || previousSize.Height != Size.Height;
1005 maxScrollDistance = CalculateMaximumScrollDistance();
1006 if (!ReviseContainerPositionIfNeed())
1012 previousContainerSize = new Size(ContentContainer.Size);
1013 previousSize = new Size(Size);
1016 private bool ReviseContainerPositionIfNeed()
1018 bool isHorizontal = ScrollingDirection == Direction.Horizontal;
1019 float currentPosition = isHorizontal ? ContentContainer.CurrentPosition.X : ContentContainer.CurrentPosition.Y;
1021 if (Math.Abs(currentPosition) > maxScrollDistance)
1024 var targetPosition = BoundScrollPosition(-maxScrollDistance);
1025 if (isHorizontal) ContentContainer.PositionX = targetPosition;
1026 else ContentContainer.PositionY = targetPosition;
1034 /// The composition of a Scrollbar can vary depending on how you use ScrollableBase.
1035 /// Set the composition that will go into the ScrollableBase according to your ScrollableBase.
1037 /// <since_tizen> 8 </since_tizen>
1038 [EditorBrowsable(EditorBrowsableState.Never)]
1039 protected virtual void SetScrollbar()
1043 bool isHorizontal = ScrollingDirection == Direction.Horizontal;
1044 float contentLength = isHorizontal ? ContentContainer.Size.Width : ContentContainer.Size.Height;
1045 float viewportLength = isHorizontal ? Size.Width : Size.Height;
1046 float currentPosition = isHorizontal ? ContentContainer.CurrentPosition.X : ContentContainer.CurrentPosition.Y;
1047 Scrollbar.Initialize(contentLength, viewportLength, -currentPosition, isHorizontal);
1051 /// Update scrollbar position and size.
1052 [EditorBrowsable(EditorBrowsableState.Never)]
1053 protected virtual void UpdateScrollbar()
1057 bool isHorizontal = ScrollingDirection == Direction.Horizontal;
1058 float contentLength = isHorizontal ? ContentContainer.Size.Width : ContentContainer.Size.Height;
1059 float viewportLength = isHorizontal ? Size.Width : Size.Height;
1060 float currentPosition = isHorizontal ? ContentContainer.CurrentPosition.X : ContentContainer.CurrentPosition.Y;
1061 Scrollbar.Update(contentLength, viewportLength, -currentPosition);
1063 if (!hideScrollbar && fadeScrollbar)
1065 Scrollbar.FadeOut();
1071 /// Scrolls to the item at the specified index.
1073 /// <param name="index">Index of item.</param>
1074 /// <since_tizen> 8 </since_tizen>
1075 public void ScrollToIndex(int index)
1077 if (ContentContainer.ChildCount - 1 < index || index < 0)
1084 CurrentPage = index;
1087 float targetPosition = Math.Min(ScrollingDirection == Direction.Vertical ? Children[index].Position.Y : Children[index].Position.X, maxScrollDistance);
1088 AnimateChildTo(ScrollDuration, -targetPosition);
1091 internal void ScrollToChild(View child, bool anim = false)
1093 if (null == FindDescendantByID(child.ID)) return;
1095 bool isHorizontal = (ScrollingDirection == Direction.Horizontal);
1097 float viewScreenPosition = (isHorizontal? ScreenPosition.X : ScreenPosition.Y);
1098 float childScreenPosition = (isHorizontal? child.ScreenPosition.X : child.ScreenPosition.Y);
1099 float scrollPosition = (isHorizontal? ScrollPosition.X : ScrollPosition.Y);
1100 float viewSize = (isHorizontal? SizeWidth : SizeHeight);
1101 float childSize = (isHorizontal? child.SizeWidth : child.SizeHeight);
1103 if (viewScreenPosition > childScreenPosition ||
1104 viewScreenPosition + viewSize < childScreenPosition + childSize)
1105 {// if object is outside
1106 float targetPosition;
1107 float dist = viewScreenPosition - childScreenPosition;
1109 {// if object is upper side
1110 targetPosition = scrollPosition - dist;
1113 {// if object is down side
1114 targetPosition = scrollPosition - dist + childSize - viewSize;
1116 ScrollTo(targetPosition, anim);
1120 private void OnScrollDragStarted()
1122 ScrollEventArgs eventArgs = new ScrollEventArgs(ContentContainer.CurrentPosition);
1123 ScrollDragStarted?.Invoke(this, eventArgs);
1125 if (!hideScrollbar && fadeScrollbar)
1127 scrollBar?.FadeIn();
1131 private void OnScrollDragEnded()
1133 ScrollEventArgs eventArgs = new ScrollEventArgs(ContentContainer.CurrentPosition);
1134 ScrollDragEnded?.Invoke(this, eventArgs);
1136 if (!hideScrollbar && fadeScrollbar)
1138 scrollBar?.FadeOut();
1142 private void OnScrollAnimationStarted()
1144 ScrollEventArgs eventArgs = new ScrollEventArgs(ContentContainer.CurrentPosition);
1145 ScrollAnimationStarted?.Invoke(this, eventArgs);
1147 if (!hideScrollbar && fadeScrollbar)
1149 scrollBar?.FadeIn();
1153 private void OnScrollAnimationEnded()
1156 this.InterceptTouchEvent -= OnInterruptTouchingChildTouched;
1158 ScrollEventArgs eventArgs = new ScrollEventArgs(ContentContainer.CurrentPosition);
1159 ScrollAnimationEnded?.Invoke(this, eventArgs);
1161 if (!hideScrollbar && fadeScrollbar)
1163 scrollBar?.FadeOut();
1167 private void OnScroll()
1169 ScrollEventArgs eventArgs = new ScrollEventArgs(ContentContainer.CurrentPosition);
1170 Scrolling?.Invoke(this, eventArgs);
1172 bool isHorizontal = ScrollingDirection == Direction.Horizontal;
1173 float contentLength = isHorizontal ? ContentContainer.Size.Width : ContentContainer.Size.Height;
1174 float currentPosition = isHorizontal ? ContentContainer.CurrentPosition.X : ContentContainer.CurrentPosition.Y;
1176 scrollBar?.Update(contentLength, Math.Abs(currentPosition));
1177 CheckPreReachedTargetPosition();
1180 private void CheckPreReachedTargetPosition()
1182 // Check whether we reached pre-reached target position
1183 if (readyToNotice &&
1184 ContentContainer.CurrentPosition.Y <= finalTargetPosition + NoticeAnimationEndBeforePosition &&
1185 ContentContainer.CurrentPosition.Y >= finalTargetPosition - NoticeAnimationEndBeforePosition)
1188 readyToNotice = false;
1189 OnPreReachedTargetPosition(finalTargetPosition);
1194 /// This helps developer who wants to know before scroll is reaching target position.
1196 /// <param name="targetPosition">Index of item.</param>
1197 /// This may be public opened in tizen_6.0 after ACR done. Before ACR, need to be hidden as inhouse API
1198 [EditorBrowsable(EditorBrowsableState.Never)]
1199 protected virtual void OnPreReachedTargetPosition(float targetPosition)
1204 private void StopScroll()
1206 if (scrollAnimation != null)
1208 if (scrollAnimation.State == Animation.States.Playing)
1210 Debug.WriteLineIf(LayoutDebugScrollableBase, "StopScroll Animation Playing");
1211 scrollAnimation.Stop(Animation.EndActions.Cancel);
1212 OnScrollAnimationEnded();
1214 scrollAnimation.Clear();
1218 private void AnimateChildTo(int duration, float axisPosition)
1220 Debug.WriteLineIf(LayoutDebugScrollableBase, "AnimationTo Animation Duration:" + duration + " Destination:" + axisPosition);
1221 finalTargetPosition = axisPosition;
1223 StopScroll(); // Will replace previous animation so will stop existing one.
1225 if (scrollAnimation == null)
1227 scrollAnimation = new Animation();
1228 scrollAnimation.Finished += ScrollAnimationFinished;
1231 scrollAnimation.Duration = duration;
1232 scrollAnimation.DefaultAlphaFunction = new AlphaFunction(AlphaFunction.BuiltinFunctions.EaseOutSquare);
1233 scrollAnimation.AnimateTo(ContentContainer, (ScrollingDirection == Direction.Horizontal) ? "PositionX" : "PositionY", axisPosition, ScrollAlphaFunction);
1235 OnScrollAnimationStarted();
1236 scrollAnimation.Play();
1240 /// Scroll to specific position with or without animation.
1242 /// <param name="position">Destination.</param>
1243 /// <param name="animate">Scroll with or without animation</param>
1244 /// <since_tizen> 8 </since_tizen>
1245 public void ScrollTo(float position, bool animate)
1248 float currentPositionX = ContentContainer.CurrentPosition.X != 0 ? ContentContainer.CurrentPosition.X : ContentContainer.Position.X;
1249 float currentPositionY = ContentContainer.CurrentPosition.Y != 0 ? ContentContainer.CurrentPosition.Y : ContentContainer.Position.Y;
1250 float delta = ScrollingDirection == Direction.Horizontal ? currentPositionX : currentPositionY;
1251 // The argument position is the new pan position. So the new position of ScrollableBase becomes (-position).
1252 // To move ScrollableBase's position to (-position), it moves by (-position - currentPosition).
1253 delta = -position - delta;
1255 ScrollBy(delta, animate);
1258 private float BoundScrollPosition(float targetPosition)
1260 if (ScrollAvailableArea != null)
1262 float minScrollPosition = ScrollAvailableArea.X;
1263 float maxScrollPosition = ScrollAvailableArea.Y;
1265 targetPosition = Math.Min(-minScrollPosition, targetPosition);
1266 targetPosition = Math.Max(-maxScrollPosition, targetPosition);
1270 targetPosition = Math.Min(0, targetPosition);
1271 targetPosition = Math.Max(-maxScrollDistance, targetPosition);
1274 return targetPosition;
1277 private void ScrollBy(float displacement, bool animate)
1279 if (GetChildCount() == 0 || maxScrollDistance < 0)
1284 float childCurrentPosition = (ScrollingDirection == Direction.Horizontal) ? ContentContainer.PositionX : ContentContainer.PositionY;
1286 Debug.WriteLineIf(LayoutDebugScrollableBase, "ScrollBy childCurrentPosition:" + childCurrentPosition +
1287 " displacement:" + displacement,
1288 " maxScrollDistance:" + maxScrollDistance);
1290 childTargetPosition = childCurrentPosition + displacement; // child current position + gesture displacement
1292 Debug.WriteLineIf(LayoutDebugScrollableBase, "ScrollBy currentAxisPosition:" + childCurrentPosition + "childTargetPosition:" + childTargetPosition);
1296 // Calculate scroll animation duration
1297 float scrollDistance = Math.Abs(displacement);
1298 readyToNotice = true;
1300 AnimateChildTo(ScrollDuration, BoundScrollPosition(AdjustTargetPositionOfScrollAnimation(BoundScrollPosition(childTargetPosition))));
1305 finalTargetPosition = BoundScrollPosition(childTargetPosition);
1307 // Set position of scrolling child without an animation
1308 if (ScrollingDirection == Direction.Horizontal)
1310 ContentContainer.PositionX = finalTargetPosition;
1314 ContentContainer.PositionY = finalTargetPosition;
1320 /// you can override it to clean-up your own resources.
1322 /// <param name="type">DisposeTypes</param>
1323 /// This will be public opened in tizen_5.5 after ACR done. Before ACR, need to be hidden as inhouse API.
1324 [EditorBrowsable(EditorBrowsableState.Never)]
1325 protected override void Dispose(DisposeTypes type)
1332 if (type == DisposeTypes.Explicit)
1334 StopOverShootingShadowAnimation();
1337 if (mPanGestureDetector != null)
1339 mPanGestureDetector.Detected -= OnPanGestureDetected;
1340 mPanGestureDetector.Dispose();
1341 mPanGestureDetector = null;
1344 propertyNotification.Dispose();
1349 private float CalculateMaximumScrollDistance()
1351 float scrollingChildLength = 0;
1352 float scrollerLength = 0;
1353 if (ScrollingDirection == Direction.Horizontal)
1355 Debug.WriteLineIf(LayoutDebugScrollableBase, "Horizontal");
1357 scrollingChildLength = ContentContainer.Size.Width;
1358 scrollerLength = Size.Width;
1362 Debug.WriteLineIf(LayoutDebugScrollableBase, "Vertical");
1363 scrollingChildLength = ContentContainer.Size.Height;
1364 scrollerLength = Size.Height;
1367 Debug.WriteLineIf(LayoutDebugScrollableBase, "ScrollBy maxScrollDistance:" + (scrollingChildLength - scrollerLength) +
1368 " parent length:" + scrollerLength +
1369 " scrolling child length:" + scrollingChildLength);
1371 return Math.Max(scrollingChildLength - scrollerLength, 0);
1374 private void PageSnap(float velocity)
1378 Debug.WriteLineIf(LayoutDebugScrollableBase, "PageSnap with pan candidate totalDisplacement:" + totalDisplacementForPan +
1379 " currentPage[" + CurrentPage + "]");
1381 //Increment current page if total displacement enough to warrant a page change.
1382 if (Math.Abs(totalDisplacementForPan) > (mPageWidth * ratioOfScreenWidthToCompleteScroll))
1384 if (totalDisplacementForPan < 0)
1386 CurrentPage = Math.Min(Math.Max(Children.Count - 1, 0), ++CurrentPage);
1390 CurrentPage = Math.Max(0, --CurrentPage);
1393 else if (Math.Abs(velocity) > PageFlickThreshold)
1397 CurrentPage = Math.Min(Math.Max(Children.Count - 1, 0), ++CurrentPage);
1401 CurrentPage = Math.Max(0, --CurrentPage);
1405 // Animate to new page or reposition to current page
1406 if (ScrollingDirection == Direction.Horizontal)
1407 destination = -(Children[CurrentPage].Position.X + Children[CurrentPage].CurrentSize.Width / 2 - CurrentSize.Width / 2); // set to middle of current page
1409 destination = -(Children[CurrentPage].Position.Y + Children[CurrentPage].CurrentSize.Height / 2 - CurrentSize.Height / 2);
1411 AnimateChildTo(ScrollDuration, destination);
1415 /// Enable/Disable overshooting effect. default is disabled.
1417 [EditorBrowsable(EditorBrowsableState.Never)]
1418 public bool EnableOverShootingEffect
1422 return (bool)GetValue(EnableOverShootingEffectProperty);
1426 SetValue(EnableOverShootingEffectProperty, value);
1427 NotifyPropertyChanged();
1430 private bool InternalEnableOverShootingEffect { get; set; } = false;
1432 private void AttachOverShootingShadowView()
1434 if (!EnableOverShootingEffect)
1437 // stop animation if necessary.
1438 StopOverShootingShadowAnimation();
1440 if (ScrollingDirection == Direction.Horizontal)
1442 base.Add(leftOverShootingShadowView);
1443 base.Add(rightOverShootingShadowView);
1445 leftOverShootingShadowView.Size = new Size(0.0f, SizeHeight);
1446 leftOverShootingShadowView.Opacity = 1.0f;
1447 leftOverShootingShadowView.RaiseToTop();
1449 rightOverShootingShadowView.Size = new Size(0.0f, SizeHeight);
1450 rightOverShootingShadowView.Opacity = 1.0f;
1451 rightOverShootingShadowView.RaiseToTop();
1455 base.Add(topOverShootingShadowView);
1456 base.Add(bottomOverShootingShadowView);
1458 topOverShootingShadowView.Size = new Size(SizeWidth, 0.0f);
1459 topOverShootingShadowView.Opacity = 1.0f;
1460 topOverShootingShadowView.RaiseToTop();
1462 bottomOverShootingShadowView.Size = new Size(SizeWidth, 0.0f);
1463 bottomOverShootingShadowView.Opacity = 1.0f;
1464 bottomOverShootingShadowView.RaiseToTop();
1467 // at the beginning, height or width of overshooting shadow is 0, so it is invisible.
1468 isOverShootingShadowShown = false;
1471 private void DragOverShootingShadow(float totalPanDisplacement, float panDisplacement)
1473 if (!EnableOverShootingEffect)
1476 if (totalPanDisplacement > 0) // downwards
1478 // check if reaching at the top / left.
1479 if ((int)finalTargetPosition != 0)
1481 isOverShootingShadowShown = false;
1485 // save start displacement, and re-calculate displacement.
1486 if (!isOverShootingShadowShown)
1488 startShowShadowDisplacement = totalPanDisplacement;
1490 isOverShootingShadowShown = true;
1492 float newDisplacement = (int)totalPanDisplacement < (int)startShowShadowDisplacement ? 0 : totalPanDisplacement - startShowShadowDisplacement;
1494 if (ScrollingDirection == Direction.Horizontal)
1496 // scale limit of height is 60%.
1497 float heightScale = newDisplacement / overShootingShadowScaleHeightLimit;
1498 leftOverShootingShadowView.SizeHeight = heightScale > 0.6f ? SizeHeight * 0.4f : SizeHeight * (1.0f - heightScale);
1500 // scale limit of width is 300%.
1501 leftOverShootingShadowView.SizeWidth = newDisplacement > overShootingShadowScaleHeightLimit ? overShootingShadowScaleHeightLimit : newDisplacement;
1504 ScrollOutOfBoundEventArgs.Direction scrollDirection = panDisplacement > 0 ?
1505 ScrollOutOfBoundEventArgs.Direction.Right : ScrollOutOfBoundEventArgs.Direction.Left;
1506 OnScrollOutOfBound(scrollDirection, totalPanDisplacement);
1510 // scale limit of width is 60%.
1511 float widthScale = newDisplacement / overShootingShadowScaleHeightLimit;
1512 topOverShootingShadowView.SizeWidth = widthScale > 0.6f ? SizeWidth * 0.4f : SizeWidth * (1.0f - widthScale);
1514 // scale limit of height is 300%.
1515 topOverShootingShadowView.SizeHeight = newDisplacement > overShootingShadowScaleHeightLimit ? overShootingShadowScaleHeightLimit : newDisplacement;
1518 ScrollOutOfBoundEventArgs.Direction scrollDirection = panDisplacement > 0 ?
1519 ScrollOutOfBoundEventArgs.Direction.Down : ScrollOutOfBoundEventArgs.Direction.Up;
1520 OnScrollOutOfBound(scrollDirection, totalPanDisplacement);
1523 else if (totalPanDisplacement < 0) // upwards
1525 // check if reaching at the bottom.
1526 if (-(int)finalTargetPosition != (int)maxScrollDistance)
1528 isOverShootingShadowShown = false;
1532 // save start displacement, and re-calculate displacement.
1533 if (!isOverShootingShadowShown)
1535 startShowShadowDisplacement = totalPanDisplacement;
1537 isOverShootingShadowShown = true;
1539 float newDisplacement = (int)startShowShadowDisplacement < (int)totalPanDisplacement ? 0 : startShowShadowDisplacement - totalPanDisplacement;
1541 if (ScrollingDirection == Direction.Horizontal)
1543 // scale limit of height is 60%.
1544 float heightScale = newDisplacement / overShootingShadowScaleHeightLimit;
1545 rightOverShootingShadowView.SizeHeight = heightScale > 0.6f ? SizeHeight * 0.4f : SizeHeight * (1.0f - heightScale);
1547 // scale limit of width is 300%.
1548 rightOverShootingShadowView.SizeWidth = newDisplacement > overShootingShadowScaleHeightLimit ? overShootingShadowScaleHeightLimit : newDisplacement;
1551 ScrollOutOfBoundEventArgs.Direction scrollDirection = panDisplacement > 0 ?
1552 ScrollOutOfBoundEventArgs.Direction.Right : ScrollOutOfBoundEventArgs.Direction.Left;
1553 OnScrollOutOfBound(scrollDirection, totalPanDisplacement);
1557 // scale limit of width is 60%.
1558 float widthScale = newDisplacement / overShootingShadowScaleHeightLimit;
1559 bottomOverShootingShadowView.SizeWidth = widthScale > 0.6f ? SizeWidth * 0.4f : SizeWidth * (1.0f - widthScale);
1561 // scale limit of height is 300%.
1562 bottomOverShootingShadowView.SizeHeight = newDisplacement > overShootingShadowScaleHeightLimit ? overShootingShadowScaleHeightLimit : newDisplacement;
1565 ScrollOutOfBoundEventArgs.Direction scrollDirection = panDisplacement > 0 ?
1566 ScrollOutOfBoundEventArgs.Direction.Down : ScrollOutOfBoundEventArgs.Direction.Up;
1567 OnScrollOutOfBound(scrollDirection, totalPanDisplacement);
1572 // if total displacement is 0, shadow would become invisible.
1573 isOverShootingShadowShown = false;
1577 private void PlayOverShootingShadowAnimation()
1579 if (!EnableOverShootingEffect)
1582 // stop animation if necessary.
1583 StopOverShootingShadowAnimation();
1585 if (overShootingShadowAnimation == null)
1587 overShootingShadowAnimation = new Animation(overShootingShadowAnimationDuration);
1588 overShootingShadowAnimation.Finished += OnOverShootingShadowAnimationFinished;
1591 if (ScrollingDirection == Direction.Horizontal)
1593 View targetView = totalDisplacementForPan < 0 ? rightOverShootingShadowView : leftOverShootingShadowView;
1594 overShootingShadowAnimation.AnimateTo(targetView, "SizeHeight", SizeHeight);
1595 overShootingShadowAnimation.AnimateTo(targetView, "SizeWidth", 0.0f);
1596 overShootingShadowAnimation.AnimateTo(targetView, "Opacity", 0.0f);
1600 View targetView = totalDisplacementForPan < 0 ? bottomOverShootingShadowView : topOverShootingShadowView;
1601 overShootingShadowAnimation.AnimateTo(targetView, "SizeWidth", SizeWidth);
1602 overShootingShadowAnimation.AnimateTo(targetView, "SizeHeight", 0.0f);
1603 overShootingShadowAnimation.AnimateTo(targetView, "Opacity", 0.0f);
1605 overShootingShadowAnimation.Play();
1608 private void StopOverShootingShadowAnimation()
1610 if (overShootingShadowAnimation == null || overShootingShadowAnimation.State != Animation.States.Playing)
1613 overShootingShadowAnimation.Stop(Animation.EndActions.Cancel);
1614 OnOverShootingShadowAnimationFinished(null, null);
1615 overShootingShadowAnimation.Clear();
1618 private void OnOverShootingShadowAnimationFinished(object sender, EventArgs e)
1620 if (ScrollingDirection == Direction.Horizontal)
1622 base.Remove(leftOverShootingShadowView);
1623 base.Remove(rightOverShootingShadowView);
1625 leftOverShootingShadowView.Size = new Size(0.0f, SizeHeight);
1626 rightOverShootingShadowView.Size = new Size(0.0f, SizeHeight);
1630 base.Remove(topOverShootingShadowView);
1631 base.Remove(bottomOverShootingShadowView);
1633 topOverShootingShadowView.Size = new Size(SizeWidth, 0.0f);
1634 bottomOverShootingShadowView.Size = new Size(SizeWidth, 0.0f);
1637 // after animation finished, height/width & opacity of vertical shadow both are 0, so it is invisible.
1638 isOverShootingShadowShown = false;
1641 private void OnScrollOutOfBound(ScrollOutOfBoundEventArgs.Direction direction, float displacement)
1643 ScrollOutOfBoundEventArgs args = new ScrollOutOfBoundEventArgs(direction, displacement);
1644 ScrollOutOfBound?.Invoke(this, args);
1647 private void OnPanGestureDetected(object source, PanGestureDetector.DetectedEventArgs e)
1649 e.Handled = OnPanGesture(e.PanGesture);
1652 private bool OnPanGesture(PanGesture panGesture)
1654 bool handled = true;
1655 if (SnapToPage && scrollAnimation != null && scrollAnimation.State == Animation.States.Playing)
1659 if (panGesture.State == Gesture.StateType.Started)
1661 readyToNotice = false;
1662 AttachOverShootingShadowView();
1663 Debug.WriteLineIf(LayoutDebugScrollableBase, "Gesture Start");
1664 if (scrolling && !SnapToPage)
1668 totalDisplacementForPan = 0.0f;
1670 // check if gesture need to propagation
1671 var checkDisplacement = (ScrollingDirection == Direction.Horizontal) ? panGesture.Displacement.X : panGesture.Displacement.Y;
1672 var checkChildCurrentPosition = (ScrollingDirection == Direction.Horizontal) ? ContentContainer.PositionX : ContentContainer.PositionY;
1673 var checkChildTargetPosition = checkChildCurrentPosition + checkDisplacement;
1674 var checkFinalTargetPosition = BoundScrollPosition(checkChildTargetPosition);
1675 handled = !((int)checkFinalTargetPosition == 0 || -(int)checkFinalTargetPosition == (int)maxScrollDistance);
1676 // If you propagate a gesture event, return;
1682 //Interrupt touching when panning is started
1683 this.InterceptTouchEvent += OnInterruptTouchingChildTouched;
1684 OnScrollDragStarted();
1686 else if (panGesture.State == Gesture.StateType.Continuing)
1688 if (ScrollingDirection == Direction.Horizontal)
1690 // if vertical shadow is shown, does not scroll.
1691 if (!isOverShootingShadowShown)
1693 ScrollBy(panGesture.Displacement.X, false);
1695 totalDisplacementForPan += panGesture.Displacement.X;
1696 DragOverShootingShadow(totalDisplacementForPan, panGesture.Displacement.X);
1700 // if vertical shadow is shown, does not scroll.
1701 if (!isOverShootingShadowShown)
1703 ScrollBy(panGesture.Displacement.Y, false);
1705 totalDisplacementForPan += panGesture.Displacement.Y;
1706 DragOverShootingShadow(totalDisplacementForPan, panGesture.Displacement.Y);
1708 Debug.WriteLineIf(LayoutDebugScrollableBase, "OnPanGestureDetected Continue totalDisplacementForPan:" + totalDisplacementForPan);
1711 else if (panGesture.State == Gesture.StateType.Finished || panGesture.State == Gesture.StateType.Cancelled)
1713 PlayOverShootingShadowAnimation();
1714 OnScrollDragEnded();
1715 StopScroll(); // Will replace previous animation so will stop existing one.
1717 if (scrollAnimation == null)
1719 scrollAnimation = new Animation();
1720 scrollAnimation.Finished += ScrollAnimationFinished;
1723 float panVelocity = (ScrollingDirection == Direction.Horizontal) ? panGesture.Velocity.X : panGesture.Velocity.Y;
1727 PageSnap(panVelocity);
1731 if (panVelocity == 0)
1733 float currentScrollPosition = (ScrollingDirection == Direction.Horizontal ? ContentContainer.CurrentPosition.X : ContentContainer.CurrentPosition.Y);
1734 scrollAnimation.DefaultAlphaFunction = new AlphaFunction(AlphaFunction.BuiltinFunctions.Linear);
1735 scrollAnimation.Duration = 0;
1736 scrollAnimation.AnimateTo(ContentContainer, (ScrollingDirection == Direction.Horizontal) ? "PositionX" : "PositionY", currentScrollPosition);
1737 scrollAnimation.Play();
1741 Decelerating(panVelocity, scrollAnimation);
1745 totalDisplacementForPan = 0;
1747 readyToNotice = true;
1748 OnScrollAnimationStarted();
1753 internal void BaseRemove(View view)
1758 internal override bool OnAccessibilityPan(PanGesture gestures)
1760 if (SnapToPage && scrollAnimation != null && scrollAnimation.State == Animation.States.Playing)
1765 OnPanGesture(gestures);
1769 private float CustomScrollAlphaFunction(float progress)
1771 if (panAnimationDelta == 0)
1777 // Parameter "progress" is normalized value. We need to multiply target duration to calculate distance.
1778 // Can get real distance using equation of deceleration (check Decelerating function)
1779 // After get real distance, normalize it
1780 float realDuration = progress * panAnimationDuration;
1781 float realDistance = velocityOfLastPan * ((float)Math.Pow(decelerationRate, realDuration) - 1) / logValueOfDeceleration;
1782 float result = Math.Min(realDistance / Math.Abs(panAnimationDelta), 1.0f);
1788 /// you can override it to custom your decelerating
1790 /// <param name="velocity">Velocity of current pan.</param>
1791 /// <param name="animation">Scroll animation.</param>
1792 [EditorBrowsable(EditorBrowsableState.Never)]
1793 protected virtual void Decelerating(float velocity, Animation animation)
1795 if (animation == null) throw new ArgumentNullException(nameof(animation));
1796 // Decelerating using deceleration equation ===========
1798 // V : velocity (pixel per millisecond)
1799 // V0 : initial velocity
1800 // d : deceleration rate,
1802 // X : final position after decelerating
1803 // log : natural logarithm
1805 // V(t) = V0 * d pow t;
1806 // X(t) = V0 * (d pow t - 1) / log d; <-- Integrate the velocity function
1807 // X(∞) = V0 * d / (1 - d); <-- Result using infinite T can be final position because T is tending to infinity.
1809 // Because of final T is tending to infinity, we should use threshold value to finish.
1810 // Final T = log(-threshold * log d / |V0| ) / log d;
1812 velocityOfLastPan = Math.Abs(velocity);
1814 float currentScrollPosition = -(ScrollingDirection == Direction.Horizontal ? ContentContainer.CurrentPosition.X : ContentContainer.CurrentPosition.Y);
1815 panAnimationDelta = (velocityOfLastPan * decelerationRate) / (1 - decelerationRate);
1816 panAnimationDelta = velocity > 0 ? -panAnimationDelta : panAnimationDelta;
1818 float destination = -(panAnimationDelta + currentScrollPosition);
1819 float adjustDestination = AdjustTargetPositionOfScrollAnimation(destination);
1820 float maxPosition = ScrollAvailableArea != null ? ScrollAvailableArea.Y : maxScrollDistance;
1821 float minPosition = ScrollAvailableArea != null ? ScrollAvailableArea.X : 0;
1823 if (destination < -maxPosition || destination > minPosition)
1825 panAnimationDelta = velocity > 0 ? (currentScrollPosition - minPosition) : (maxPosition - currentScrollPosition);
1826 destination = velocity > 0 ? minPosition : -maxPosition;
1828 if (panAnimationDelta == 0)
1830 panAnimationDuration = 0.0f;
1834 panAnimationDuration = (float)Math.Log((panAnimationDelta * logValueOfDeceleration / velocityOfLastPan + 1), decelerationRate);
1837 Debug.WriteLineIf(LayoutDebugScrollableBase, "\n" +
1838 "OverRange======================= \n" +
1839 "[decelerationRate] " + decelerationRate + "\n" +
1840 "[logValueOfDeceleration] " + logValueOfDeceleration + "\n" +
1841 "[Velocity] " + velocityOfLastPan + "\n" +
1842 "[CurrentPosition] " + currentScrollPosition + "\n" +
1843 "[CandidateDelta] " + panAnimationDelta + "\n" +
1844 "[Destination] " + destination + "\n" +
1845 "[Duration] " + panAnimationDuration + "\n" +
1846 "================================ \n"
1851 panAnimationDuration = (float)Math.Log(-DecelerationThreshold * logValueOfDeceleration / velocityOfLastPan) / logValueOfDeceleration;
1853 if (adjustDestination != destination)
1855 destination = adjustDestination;
1856 panAnimationDelta = destination + currentScrollPosition;
1857 velocityOfLastPan = Math.Abs(panAnimationDelta * logValueOfDeceleration / ((float)Math.Pow(decelerationRate, panAnimationDuration) - 1));
1858 panAnimationDuration = (float)Math.Log(-DecelerationThreshold * logValueOfDeceleration / velocityOfLastPan) / logValueOfDeceleration;
1861 Debug.WriteLineIf(LayoutDebugScrollableBase, "\n" +
1862 "================================ \n" +
1863 "[decelerationRate] " + decelerationRate + "\n" +
1864 "[logValueOfDeceleration] " + logValueOfDeceleration + "\n" +
1865 "[Velocity] " + velocityOfLastPan + "\n" +
1866 "[CurrentPosition] " + currentScrollPosition + "\n" +
1867 "[CandidateDelta] " + panAnimationDelta + "\n" +
1868 "[Destination] " + destination + "\n" +
1869 "[Duration] " + panAnimationDuration + "\n" +
1870 "================================ \n"
1874 finalTargetPosition = destination;
1876 customScrollAlphaFunction = new UserAlphaFunctionDelegate(CustomScrollAlphaFunction);
1877 animation.DefaultAlphaFunction = new AlphaFunction(customScrollAlphaFunction);
1878 GC.KeepAlive(customScrollAlphaFunction);
1879 animation.Duration = (int)panAnimationDuration;
1880 animation.AnimateTo(ContentContainer, (ScrollingDirection == Direction.Horizontal) ? "PositionX" : "PositionY", destination);
1884 private void ScrollAnimationFinished(object sender, EventArgs e)
1886 OnScrollAnimationEnded();
1890 /// Adjust scrolling position by own scrolling rules.
1891 /// Override this function when developer wants to change destination of flicking.(e.g. always snap to center of item)
1893 /// This may be public opened in tizen_6.0 after ACR done. Before ACR, need to be hidden as inhouse API
1894 [EditorBrowsable(EditorBrowsableState.Never)]
1895 protected virtual float AdjustTargetPositionOfScrollAnimation(float position)
1901 /// Scroll position given to ScrollTo.
1902 /// This is the position in the opposite direction to the position of ContentContainer.
1904 /// <since_tizen> 8 </since_tizen>
1905 public Position ScrollPosition
1909 return new Position(-ContentContainer.Position);
1914 /// Current scroll position in the middle of ScrollTo animation.
1915 /// This is the position in the opposite direction to the current position of ContentContainer.
1917 /// <since_tizen> 8 </since_tizen>
1918 public Position ScrollCurrentPosition
1922 return new Position(-ContentContainer.CurrentPosition);
1927 /// Remove all children in ContentContainer.
1929 /// <param name="dispose">If true, removed child is disposed.</param>
1930 [EditorBrowsable(EditorBrowsableState.Never)]
1931 public void RemoveAllChildren(bool dispose = false)
1933 RecursiveRemoveChildren(ContentContainer, dispose);
1936 private void RecursiveRemoveChildren(View parent, bool dispose)
1942 int maxChild = (int)parent.GetChildCount();
1943 for (int i = maxChild - 1; i >= 0; --i)
1945 View child = parent.GetChildAt((uint)i);
1950 RecursiveRemoveChildren(child, dispose);
1951 parent.Remove(child);
1959 internal bool IsChildNearlyVisble(View child, float offset = 0)
1961 if (ScreenPosition.X - offset < child.ScreenPosition.X + child.SizeWidth &&
1962 ScreenPosition.X + SizeWidth + offset > child.ScreenPosition.X &&
1963 ScreenPosition.Y - offset < child.ScreenPosition.Y + child.SizeHeight &&
1964 ScreenPosition.Y + SizeHeight + offset > child.ScreenPosition.Y)
1976 [EditorBrowsable(EditorBrowsableState.Never)]
1977 public override View GetNextFocusableView(View currentFocusedView, View.FocusDirection direction, bool loopEnabled)
1979 bool isHorizontal = (ScrollingDirection == Direction.Horizontal);
1980 float targetPosition = -(ScrollingDirection == Direction.Horizontal ? ContentContainer.CurrentPosition.X : ContentContainer.CurrentPosition.Y);
1981 float stepDistance = (stepScrollDistance != 0? stepScrollDistance : (isHorizontal ? Size.Width * 0.25f : Size.Height * 0.25f));
1983 View nextFocusedView = FocusManager.Instance.GetNearestFocusableActor(this, currentFocusedView, direction);
1985 if (nextFocusedView != null)
1987 if (null != FindDescendantByID(nextFocusedView.ID))
1989 if (IsChildNearlyVisble(nextFocusedView, stepDistance) == true)
1991 ScrollToChild(nextFocusedView, true);
1995 if ((isHorizontal && direction == View.FocusDirection.Right) ||
1996 (!isHorizontal && direction == View.FocusDirection.Down))
1998 targetPosition += stepDistance;
1999 targetPosition = targetPosition > maxScrollDistance ? maxScrollDistance : targetPosition;
2002 else if ((isHorizontal && direction == View.FocusDirection.Left) ||
2003 (!isHorizontal && direction == View.FocusDirection.Up))
2005 targetPosition -= stepDistance;
2006 targetPosition = targetPosition < 0 ? 0 : targetPosition;
2009 ScrollTo(targetPosition, true);
2015 if((isHorizontal && direction == View.FocusDirection.Right) ||
2016 (!isHorizontal && direction == View.FocusDirection.Down))
2018 targetPosition += stepDistance;
2019 targetPosition = targetPosition > maxScrollDistance ? maxScrollDistance : targetPosition;
2022 else if((isHorizontal && direction == View.FocusDirection.Left) ||
2023 (!isHorizontal && direction == View.FocusDirection.Up))
2025 targetPosition -= stepDistance;
2026 targetPosition = targetPosition < 0 ? 0 : targetPosition;
2029 ScrollTo(targetPosition, true);
2031 // End of scroll. escape.
2032 if ((targetPosition == 0 || targetPosition == maxScrollDistance) == false)
2038 return nextFocusedView;
2042 [EditorBrowsable(EditorBrowsableState.Never)]
2043 protected override bool AccessibilityScrollToChild(View child)
2050 if (ScrollingDirection == Direction.Horizontal)
2052 if (child.ScreenPosition.X + child.Size.Width <= this.ScreenPosition.X)
2056 PageSnap(PageFlickThreshold + 1);
2060 ScrollTo((float)(child.ScreenPosition.X - ContentContainer.ScreenPosition.X), false);
2063 else if (child.ScreenPosition.X >= this.ScreenPosition.X + this.Size.Width)
2067 PageSnap(-(PageFlickThreshold + 1));
2071 ScrollTo((float)(child.ScreenPosition.X + child.Size.Width - ContentContainer.ScreenPosition.X - this.Size.Width), false);
2077 if (child.ScreenPosition.Y + child.Size.Height <= this.ScreenPosition.Y)
2081 PageSnap(PageFlickThreshold + 1);
2085 ScrollTo((float)(child.ScreenPosition.Y - ContentContainer.ScreenPosition.Y), false);
2088 else if (child.ScreenPosition.Y >= this.ScreenPosition.Y + this.Size.Height)
2092 PageSnap(-(PageFlickThreshold + 1));
2096 ScrollTo((float)(child.ScreenPosition.Y + child.Size.Height - ContentContainer.ScreenPosition.Y - this.Size.Height), false);