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 static bool focusDebugScrollableBase = false; // Debug flag
152 private Direction mScrollingDirection = Direction.Vertical;
153 private bool mScrollEnabled = true;
154 private int mScrollDuration = 125;
155 private int mPageWidth = 0;
156 private float mPageFlickThreshold = 0.4f;
157 private float mScrollingEventThreshold = 0.001f;
158 private bool fadeScrollbar = true;
160 private class ScrollableBaseCustomLayout : AbsoluteLayout
162 protected override void OnMeasure(MeasureSpecification widthMeasureSpec, MeasureSpecification heightMeasureSpec)
164 MeasuredSize.StateType childWidthState = MeasuredSize.StateType.MeasuredSizeOK;
165 MeasuredSize.StateType childHeightState = MeasuredSize.StateType.MeasuredSizeOK;
167 Direction scrollingDirection = Direction.Vertical;
168 ScrollableBase scrollableBase = this.Owner as ScrollableBase;
169 if (scrollableBase != null)
171 scrollingDirection = scrollableBase.ScrollingDirection;
174 float totalWidth = 0.0f;
175 float totalHeight = 0.0f;
177 // measure child, should be a single scrolling child
178 foreach (LayoutItem childLayout in LayoutChildren)
180 if (childLayout != null && childLayout.Owner.Name == "ContentContainer")
183 // Use an Unspecified MeasureSpecification mode so scrolling child is not restricted to it's parents size in Height (for vertical scrolling)
184 // or Width for horizontal scrolling
185 if (scrollingDirection == Direction.Vertical)
187 MeasureSpecification unrestrictedMeasureSpec = new MeasureSpecification(heightMeasureSpec.Size, MeasureSpecification.ModeType.Unspecified);
188 MeasureChildWithMargins(childLayout, widthMeasureSpec, new LayoutLength(0), unrestrictedMeasureSpec, new LayoutLength(0)); // Height unrestricted by parent
192 MeasureSpecification unrestrictedMeasureSpec = new MeasureSpecification(widthMeasureSpec.Size, MeasureSpecification.ModeType.Unspecified);
193 MeasureChildWithMargins(childLayout, unrestrictedMeasureSpec, new LayoutLength(0), heightMeasureSpec, new LayoutLength(0)); // Width unrestricted by parent
196 totalWidth = (childLayout.MeasuredWidth.Size + (childLayout.Padding.Start + childLayout.Padding.End)).AsDecimal();
197 totalHeight = (childLayout.MeasuredHeight.Size + (childLayout.Padding.Top + childLayout.Padding.Bottom)).AsDecimal();
199 if (childLayout.MeasuredWidth.State == MeasuredSize.StateType.MeasuredSizeTooSmall)
201 childWidthState = MeasuredSize.StateType.MeasuredSizeTooSmall;
203 if (childLayout.MeasuredHeight.State == MeasuredSize.StateType.MeasuredSizeTooSmall)
205 childHeightState = MeasuredSize.StateType.MeasuredSizeTooSmall;
210 MeasuredSize widthSizeAndState = ResolveSizeAndState(new LayoutLength(totalWidth), widthMeasureSpec, MeasuredSize.StateType.MeasuredSizeOK);
211 MeasuredSize heightSizeAndState = ResolveSizeAndState(new LayoutLength(totalHeight), heightMeasureSpec, MeasuredSize.StateType.MeasuredSizeOK);
212 totalWidth = widthSizeAndState.Size.AsDecimal();
213 totalHeight = heightSizeAndState.Size.AsDecimal();
215 // Ensure layout respects it's given minimum size
216 totalWidth = Math.Max(totalWidth, SuggestedMinimumWidth.AsDecimal());
217 totalHeight = Math.Max(totalHeight, SuggestedMinimumHeight.AsDecimal());
219 widthSizeAndState.State = childWidthState;
220 heightSizeAndState.State = childHeightState;
222 SetMeasuredDimensions(ResolveSizeAndState(new LayoutLength(totalWidth), widthMeasureSpec, childWidthState),
223 ResolveSizeAndState(new LayoutLength(totalHeight), heightMeasureSpec, childHeightState));
225 // Size of ScrollableBase is changed. Change Page width too.
226 if (scrollableBase != null)
228 scrollableBase.mPageWidth = (int)MeasuredWidth.Size.AsRoundedValue();
229 scrollableBase.OnScrollingChildRelayout(null, null);
232 } // ScrollableBaseCustomLayout
235 /// The direction axis to scroll.
237 /// <since_tizen> 8 </since_tizen>
238 public enum Direction
243 /// <since_tizen> 8 </since_tizen>
249 /// <since_tizen> 8 </since_tizen>
254 /// Scrolling direction mode.
255 /// Default is Vertical scrolling.
257 /// <since_tizen> 8 </since_tizen>
258 public Direction ScrollingDirection
262 return (Direction)GetValue(ScrollingDirectionProperty);
266 SetValue(ScrollingDirectionProperty, value);
267 NotifyPropertyChanged();
270 private Direction InternalScrollingDirection
274 return mScrollingDirection;
278 if (value != mScrollingDirection)
280 //Reset scroll position and stop scroll animation
283 mScrollingDirection = value;
284 mPanGestureDetector.ClearAngles();
285 mPanGestureDetector.AddDirection(value == Direction.Horizontal ?
286 PanGestureDetector.DirectionHorizontal : PanGestureDetector.DirectionVertical);
288 ContentContainer.WidthSpecification = ScrollingDirection == Direction.Vertical ? LayoutParamPolicies.MatchParent : LayoutParamPolicies.WrapContent;
289 ContentContainer.HeightSpecification = ScrollingDirection == Direction.Vertical ? LayoutParamPolicies.WrapContent : LayoutParamPolicies.MatchParent;
296 /// Enable or disable scrolling.
298 /// <since_tizen> 8 </since_tizen>
299 public bool ScrollEnabled
303 return (bool)GetValue(ScrollEnabledProperty);
307 SetValue(ScrollEnabledProperty, value);
308 NotifyPropertyChanged();
311 private bool InternalScrollEnabled
315 return mScrollEnabled;
319 if (value != mScrollEnabled)
321 mScrollEnabled = value;
324 mPanGestureDetector.Detected += OnPanGestureDetected;
328 mPanGestureDetector.Detected -= OnPanGestureDetected;
335 /// Gets scrollable status.
337 [EditorBrowsable(EditorBrowsableState.Never)]
338 protected override bool AccessibilityIsScrollable()
344 /// Pages mode, enables moving to the next or return to current page depending on pan displacement.
345 /// Default is false.
347 /// <since_tizen> 8 </since_tizen>
348 public bool SnapToPage
352 return (bool)GetValue(SnapToPageProperty);
356 SetValue(SnapToPageProperty, value);
357 NotifyPropertyChanged();
360 private bool InternalSnapToPage { set; get; } = false;
363 /// Get current page.
364 /// Working property with SnapToPage property.
366 /// <since_tizen> 8 </since_tizen>
367 public int CurrentPage { get; private set; } = 0;
370 /// Duration of scroll animation.
371 /// Default value is 125ms.
373 /// <since_tizen> 8 </since_tizen>
374 public int ScrollDuration
378 return (int)GetValue(ScrollDurationProperty);
382 SetValue(ScrollDurationProperty, value);
383 NotifyPropertyChanged();
386 private int InternalScrollDuration
390 mScrollDuration = value >= 0 ? value : mScrollDuration;
394 return mScrollDuration;
399 /// Scroll Available area.
401 /// <since_tizen> 8 </since_tizen>
402 public Vector2 ScrollAvailableArea
406 return GetValue(ScrollAvailableAreaProperty) as Vector2;
410 SetValue(ScrollAvailableAreaProperty, value);
411 NotifyPropertyChanged();
414 private Vector2 InternalScrollAvailableArea { set; get; }
417 /// An event emitted when user starts dragging ScrollableBase, user can subscribe or unsubscribe to this event handler.<br />
419 /// <since_tizen> 8 </since_tizen>
420 public event EventHandler<ScrollEventArgs> ScrollDragStarted;
423 /// An event emitted when user stops dragging ScrollableBase, user can subscribe or unsubscribe to this event handler.<br />
425 /// <since_tizen> 8 </since_tizen>
426 public event EventHandler<ScrollEventArgs> ScrollDragEnded;
429 /// An event emitted when the scrolling slide animation starts, user can subscribe or unsubscribe to this event handler.<br />
431 /// <since_tizen> 8 </since_tizen>
432 public event EventHandler<ScrollEventArgs> ScrollAnimationStarted;
435 /// An event emitted when the scrolling slide animation ends, user can subscribe or unsubscribe to this event handler.<br />
437 /// <since_tizen> 8 </since_tizen>
438 public event EventHandler<ScrollEventArgs> ScrollAnimationEnded;
441 /// An event emitted when scrolling, user can subscribe or unsubscribe to this event handler.<br />
443 /// <since_tizen> 8 </since_tizen>
444 public event EventHandler<ScrollEventArgs> Scrolling;
447 /// An event emitted when scrolling out of bound, user can subscribe or unsubscribe to this event handler.<br />
449 [EditorBrowsable(EditorBrowsableState.Never)]
450 public event EventHandler<ScrollOutOfBoundEventArgs> ScrollOutOfBound;
453 /// Scrollbar for ScrollableBase.
455 /// <since_tizen> 8 </since_tizen>
456 public ScrollbarBase Scrollbar
460 return GetValue(ScrollbarProperty) as ScrollbarBase;
464 SetValue(ScrollbarProperty, value);
465 NotifyPropertyChanged();
468 private ScrollbarBase InternalScrollbar
478 base.Remove(scrollBar);
482 if (scrollBar != null)
484 scrollBar.Name = "ScrollBar";
502 /// Always hide Scrollbar.
504 /// <since_tizen> 8 </since_tizen>
505 public bool HideScrollbar
509 return (bool)GetValue(HideScrollbarProperty);
513 SetValue(HideScrollbarProperty, value);
514 NotifyPropertyChanged();
517 private bool InternalHideScrollbar
521 return hideScrollbar;
525 hideScrollbar = value;
538 scrollBar.Opacity = 1.0f;
547 /// The boolean flag for automatic fading Scrollbar.
548 /// Scrollbar will be faded out when scroll stay in certain position longer than the threshold.
549 /// Scrollbar will be faded in scroll position changes.
551 [EditorBrowsable(EditorBrowsableState.Never)]
552 public bool FadeScrollbar
554 get => (bool)GetValue(FadeScrollbarProperty);
555 set => SetValue(FadeScrollbarProperty, value);
558 private bool InternalFadeScrollbar
562 return fadeScrollbar;
566 fadeScrollbar = value;
568 if (scrollBar != null && !hideScrollbar)
576 scrollBar.Opacity = 1.0f;
577 // Removing fadeout timer and animation.
585 /// Container which has content of ScrollableBase.
587 /// <since_tizen> 8 </since_tizen>
588 public View ContentContainer { get; private set; }
591 /// Set the layout on this View. Replaces any existing Layout.
593 /// <since_tizen> 8 </since_tizen>
594 public new LayoutItem Layout
598 return GetValue(LayoutProperty) as LayoutItem;
602 SetValue(LayoutProperty, value);
603 NotifyPropertyChanged();
606 private LayoutItem InternalLayout
610 return ContentContainer.Layout;
614 ContentContainer.Layout = value;
619 /// List of children of Container.
621 /// <since_tizen> 8 </since_tizen>
622 public new List<View> Children
626 return ContentContainer.Children;
631 /// Deceleration rate of scrolling by finger.
632 /// Rate should be bigger than 0 and smaller than 1.
633 /// Default value is 0.998f;
635 /// <since_tizen> 8 </since_tizen>
636 public float DecelerationRate
640 return (float)GetValue(DecelerationRateProperty);
644 SetValue(DecelerationRateProperty, value);
645 NotifyPropertyChanged();
648 private float InternalDecelerationRate
652 return decelerationRate;
656 decelerationRate = (value < 1 && value > 0) ? value : decelerationRate;
657 logValueOfDeceleration = (float)Math.Log(value);
662 /// Threshold not to go infinite at the end of scrolling animation.
664 [EditorBrowsable(EditorBrowsableState.Never)]
665 public float DecelerationThreshold
669 return (float)GetValue(DecelerationThresholdProperty);
673 SetValue(DecelerationThresholdProperty, value);
674 NotifyPropertyChanged();
677 private float InternalDecelerationThreshold { get; set; } = 0.1f;
680 /// Scrolling event will be thrown when this amount of scroll position is changed.
681 /// If this threshold becomes smaller, the tracking detail increases but the scrolling range that can be tracked becomes smaller.
682 /// If large sized ContentContainer is required, please use larger threshold value.
683 /// Default ScrollingEventThreshold value is 0.001f.
685 [EditorBrowsable(EditorBrowsableState.Never)]
686 public float ScrollingEventThreshold
690 return (float)GetValue(ScrollingEventThresholdProperty);
694 SetValue(ScrollingEventThresholdProperty, value);
695 NotifyPropertyChanged();
698 private float InternalScrollingEventThreshold
702 return mScrollingEventThreshold;
706 if (mScrollingEventThreshold != value && value > 0)
708 ContentContainer.RemovePropertyNotification(propertyNotification);
709 propertyNotification = ContentContainer.AddPropertyNotification("position", PropertyCondition.Step(value));
710 propertyNotification.Notified += OnPropertyChanged;
711 mScrollingEventThreshold = value;
717 /// Page will be changed when velocity of panning is over threshold.
718 /// The unit of threshold is pixel per millisecond.
720 /// <since_tizen> 8 </since_tizen>
721 public float PageFlickThreshold
725 return (float)GetValue(PageFlickThresholdProperty);
729 SetValue(PageFlickThresholdProperty, value);
730 NotifyPropertyChanged();
733 private float InternalPageFlickThreshold
737 return mPageFlickThreshold;
741 mPageFlickThreshold = value >= 0f ? value : mPageFlickThreshold;
746 /// Padding for the ScrollableBase
748 [EditorBrowsable(EditorBrowsableState.Never)]
749 public new Extents Padding
753 return GetValue(PaddingProperty) as Extents;
757 SetValue(PaddingProperty, value);
758 NotifyPropertyChanged();
761 private Extents InternalPadding
765 return ContentContainer.Padding;
769 ContentContainer.Padding = value;
774 /// Alphafunction for scroll animation.
776 [EditorBrowsable(EditorBrowsableState.Never)]
777 public AlphaFunction ScrollAlphaFunction
781 return GetValue(ScrollAlphaFunctionProperty) as AlphaFunction;
785 SetValue(ScrollAlphaFunctionProperty, value);
786 NotifyPropertyChanged();
789 private AlphaFunction InternalScrollAlphaFunction { get; set; } = new AlphaFunction(AlphaFunction.BuiltinFunctions.Linear);
791 private bool hideScrollbar = true;
792 private float maxScrollDistance;
793 private float childTargetPosition = 0.0f;
794 private PanGestureDetector mPanGestureDetector;
795 private ScrollbarBase scrollBar;
796 private bool scrolling = false;
797 private float ratioOfScreenWidthToCompleteScroll = 0.5f;
798 private float totalDisplacementForPan = 0.0f;
799 private Size previousContainerSize = new Size();
800 private Size previousSize = new Size();
801 private PropertyNotification propertyNotification;
802 private float noticeAnimationEndBeforePosition = 0.0f;
803 private bool readyToNotice = false;
806 /// Notice before animation is finished.
808 [EditorBrowsable(EditorBrowsableState.Never)]
809 // Let's consider more whether this needs to be set as protected.
810 public float NoticeAnimationEndBeforePosition
814 return (float)GetValue(NoticeAnimationEndBeforePositionProperty);
818 SetValue(NoticeAnimationEndBeforePositionProperty, value);
819 NotifyPropertyChanged();
822 private float InternalNoticeAnimationEndBeforePosition
824 get => noticeAnimationEndBeforePosition;
825 set => noticeAnimationEndBeforePosition = value;
829 /// Step scroll move distance.
830 /// Key focus originally moves focusable objects, but in ScrollableBase,
831 /// if focusable object is too far or un-exist and ScrollableBase is focusable,
832 /// it can scroll move itself by key input.
833 /// this value decide how long distance will it moves in one step.
834 /// if any value is not set, step will be moved quater size of ScrollableBase length.
836 [EditorBrowsable(EditorBrowsableState.Never)]
837 public float StepScrollDistance
841 return (float)GetValue(StepScrollDistanceProperty);
845 SetValue(StepScrollDistanceProperty, value);
846 NotifyPropertyChanged();
849 private float stepScrollDistance = 0f;
852 /// Wheel scroll move distance.
853 /// This value decide how long distance will it moves in wheel event.
855 [EditorBrowsable(EditorBrowsableState.Never)]
856 public float WheelScrollDistance
860 return (float)GetValue(WheelScrollDistanceProperty);
864 SetValue(WheelScrollDistanceProperty, value);
865 NotifyPropertyChanged();
868 private float wheelScrollDistance = 50f;
871 // Let's consider more whether this needs to be set as protected.
872 private float finalTargetPosition;
874 private Animation scrollAnimation;
875 // Declare user alpha function delegate
876 [UnmanagedFunctionPointer(CallingConvention.StdCall)]
877 private delegate float UserAlphaFunctionDelegate(float progress);
878 private UserAlphaFunctionDelegate customScrollAlphaFunction;
879 private float velocityOfLastPan = 0.0f;
880 private float panAnimationDuration = 0.0f;
881 private float panAnimationDelta = 0.0f;
882 private float logValueOfDeceleration = 0.0f;
883 private float decelerationRate = 0.0f;
885 private View topOverShootingShadowView;
886 private View bottomOverShootingShadowView;
887 private View leftOverShootingShadowView;
888 private View rightOverShootingShadowView;
889 private const int overShootingShadowScaleHeightLimit = 64 * 3;
890 private const int overShootingShadowAnimationDuration = 300;
891 private Animation overShootingShadowAnimation;
892 private bool isOverShootingShadowShown = false;
893 private float startShowShadowDisplacement;
895 private void Initialize()
897 DecelerationRate = 0.998f;
899 base.Layout = new ScrollableBaseCustomLayout();
900 mPanGestureDetector = new PanGestureDetector();
901 mPanGestureDetector.Attach(this);
902 mPanGestureDetector.AddDirection(PanGestureDetector.DirectionVertical);
903 if (mPanGestureDetector.GetMaximumTouchesRequired() < 2) mPanGestureDetector.SetMaximumTouchesRequired(2);
904 mPanGestureDetector.Detected += OnPanGestureDetected;
906 ClippingMode = ClippingModeType.ClipToBoundingBox;
908 //Default Scrolling child
909 ContentContainer = new View()
911 Name = "ContentContainer",
912 WidthSpecification = ScrollingDirection == Direction.Vertical ? LayoutParamPolicies.MatchParent : LayoutParamPolicies.WrapContent,
913 HeightSpecification = ScrollingDirection == Direction.Vertical ? LayoutParamPolicies.WrapContent : LayoutParamPolicies.MatchParent,
915 // Check if children's sizes change to update Scrollbar
916 ContentContainer.Relayout += OnScrollingChildRelayout;
917 propertyNotification = ContentContainer.AddPropertyNotification("position", PropertyCondition.Step(mScrollingEventThreshold));
918 propertyNotification.Notified += OnPropertyChanged;
919 base.Add(ContentContainer);
920 // Check if ScrollableBase's size changes to update Scrollbar
921 base.Relayout += OnScrollingChildRelayout;
923 Scrollbar = new Scrollbar();
925 //Show vertical shadow on the top (or bottom) of the scrollable when panning down (or up).
926 topOverShootingShadowView = new View
928 BackgroundImage = FrameworkInformation.ResourcePath + "nui_component_default_scroll_over_shooting_top.png",
931 PositionUsesPivotPoint = true,
932 ParentOrigin = NUI.ParentOrigin.TopCenter,
933 PivotPoint = NUI.PivotPoint.TopCenter,
935 bottomOverShootingShadowView = new View
937 BackgroundImage = FrameworkInformation.ResourcePath + "nui_component_default_scroll_over_shooting_bottom.png",
940 PositionUsesPivotPoint = true,
941 ParentOrigin = NUI.ParentOrigin.BottomCenter,
942 PivotPoint = NUI.PivotPoint.BottomCenter,
944 //Show horizontal shadow on the left (or right) of the scrollable when panning down (or up).
945 leftOverShootingShadowView = new View
947 BackgroundImage = FrameworkInformation.ResourcePath + "nui_component_default_scroll_over_shooting_left.png",
950 PositionUsesPivotPoint = true,
951 ParentOrigin = NUI.ParentOrigin.CenterLeft,
952 PivotPoint = NUI.PivotPoint.CenterLeft,
954 rightOverShootingShadowView = new View
956 BackgroundImage = FrameworkInformation.ResourcePath + "nui_component_default_scroll_over_shooting_right.png",
959 PositionUsesPivotPoint = true,
960 ParentOrigin = NUI.ParentOrigin.CenterRight,
961 PivotPoint = NUI.PivotPoint.CenterRight,
964 WheelEvent += OnWheelEvent;
966 AccessibilityManager.Instance.SetAccessibilityAttribute(this, AccessibilityManager.AccessibilityAttribute.Trait, "ScrollableBase");
968 SetKeyboardNavigationSupport(true);
972 /// Default Constructor
974 /// <since_tizen> 8 </since_tizen>
975 public ScrollableBase() : base()
981 /// Creates a new instance of a ScrollableBase with style.
983 /// <param name="style">Creates ScrollableBase by special style defined in UX.</param>
984 [EditorBrowsable(EditorBrowsableState.Never)]
985 public ScrollableBase(string style) : base(style)
991 /// Creates a new instance of a ScrollableBase with style.
993 /// <param name="style">A style applied to the newly created ScrollableBase.</param>
994 [EditorBrowsable(EditorBrowsableState.Never)]
995 public ScrollableBase(ControlStyle style) : base(style)
1000 private bool OnInterruptTouchingChildTouched(object source, View.TouchEventArgs args)
1002 if (args.Touch.GetState(0) == PointStateType.Down)
1004 if (scrolling && !SnapToPage)
1012 private void OnPropertyChanged(object source, PropertyNotification.NotifyEventArgs args)
1018 /// Called after a child has been added to the owning view.
1020 /// <param name="view">The child which has been added.</param>
1021 /// <since_tizen> 8 </since_tizen>
1022 public override void Add(View view)
1024 ContentContainer.Add(view);
1028 /// Called after a child has been removed from the owning view.
1030 /// <param name="view">The child which has been removed.</param>
1031 /// <since_tizen> 8 </since_tizen>
1032 public override void Remove(View view)
1034 if (SnapToPage && CurrentPage == Children.IndexOf(view) && CurrentPage == Children.Count - 1 && Children.Count > 1)
1036 // Target View is current page and also last child.
1037 // CurrentPage should be changed to previous page.
1038 ScrollToIndex(CurrentPage - 1);
1041 ContentContainer.Remove(view);
1044 private void OnScrollingChildRelayout(object source, EventArgs args)
1046 // Size is changed. Calculate maxScrollDistance.
1047 bool isSizeChanged = previousContainerSize.Width != ContentContainer.Size.Width || previousContainerSize.Height != ContentContainer.Size.Height ||
1048 previousSize.Width != Size.Width || previousSize.Height != Size.Height;
1052 maxScrollDistance = CalculateMaximumScrollDistance();
1053 if (!ReviseContainerPositionIfNeed())
1059 previousContainerSize = new Size(ContentContainer.Size);
1060 previousSize = new Size(Size);
1063 private bool ReviseContainerPositionIfNeed()
1065 bool isHorizontal = ScrollingDirection == Direction.Horizontal;
1066 float currentPosition = isHorizontal ? ContentContainer.CurrentPosition.X : ContentContainer.CurrentPosition.Y;
1068 if (Math.Abs(currentPosition) > maxScrollDistance)
1071 var targetPosition = BoundScrollPosition(-maxScrollDistance);
1072 if (isHorizontal) ContentContainer.PositionX = targetPosition;
1073 else ContentContainer.PositionY = targetPosition;
1081 /// The composition of a Scrollbar can vary depending on how you use ScrollableBase.
1082 /// Set the composition that will go into the ScrollableBase according to your ScrollableBase.
1084 /// <since_tizen> 8 </since_tizen>
1085 [EditorBrowsable(EditorBrowsableState.Never)]
1086 protected virtual void SetScrollbar()
1090 bool isHorizontal = ScrollingDirection == Direction.Horizontal;
1091 float contentLength = isHorizontal ? ContentContainer.Size.Width : ContentContainer.Size.Height;
1092 float viewportLength = isHorizontal ? Size.Width : Size.Height;
1093 float currentPosition = isHorizontal ? ContentContainer.CurrentPosition.X : ContentContainer.CurrentPosition.Y;
1094 Scrollbar.Initialize(contentLength, viewportLength, -currentPosition, isHorizontal);
1098 /// Update scrollbar position and size.
1099 [EditorBrowsable(EditorBrowsableState.Never)]
1100 protected virtual void UpdateScrollbar()
1104 bool isHorizontal = ScrollingDirection == Direction.Horizontal;
1105 float contentLength = isHorizontal ? ContentContainer.Size.Width : ContentContainer.Size.Height;
1106 float viewportLength = isHorizontal ? Size.Width : Size.Height;
1107 float currentPosition = isHorizontal ? ContentContainer.CurrentPosition.X : ContentContainer.CurrentPosition.Y;
1108 Scrollbar.Update(contentLength, viewportLength, -currentPosition);
1110 if (!hideScrollbar && fadeScrollbar)
1112 Scrollbar.FadeOut();
1118 /// return the children of ContentContainer when user try to get the children of ScrollableBase.
1120 /// <returns></returns>
1121 [EditorBrowsable(EditorBrowsableState.Never)]
1122 protected override List<View> GetChildren()
1124 return ContentContainer.Children;
1128 /// Scrolls to the item at the specified index.
1130 /// <param name="index">Index of item.</param>
1131 /// <since_tizen> 8 </since_tizen>
1132 public void ScrollToIndex(int index)
1134 if (ContentContainer.ChildCount - 1 < index || index < 0)
1141 CurrentPage = index;
1144 float targetPosition = Math.Min(ScrollingDirection == Direction.Vertical ? Children[index].Position.Y : Children[index].Position.X, maxScrollDistance);
1145 AnimateChildTo(ScrollDuration, -targetPosition);
1148 internal void ScrollToChild(View child, bool anim = false)
1150 if (null == FindDescendantByID(child.ID)) return;
1152 bool isHorizontal = (ScrollingDirection == Direction.Horizontal);
1154 float viewScreenPosition = (isHorizontal ? ScreenPosition.X : ScreenPosition.Y);
1155 float childScreenPosition = (isHorizontal ? child.ScreenPosition.X : child.ScreenPosition.Y);
1156 float scrollPosition = (isHorizontal ? ScrollPosition.X : ScrollPosition.Y);
1157 float viewSize = (isHorizontal ? SizeWidth : SizeHeight);
1158 float childSize = (isHorizontal ? child.SizeWidth : child.SizeHeight);
1160 if (viewScreenPosition > childScreenPosition ||
1161 viewScreenPosition + viewSize < childScreenPosition + childSize)
1162 {// if object is outside
1163 float targetPosition;
1164 float dist = viewScreenPosition - childScreenPosition;
1166 {// if object is upper side
1167 targetPosition = scrollPosition - dist;
1170 {// if object is down side
1171 targetPosition = scrollPosition - dist + childSize - viewSize;
1173 ScrollTo(targetPosition, anim);
1177 private void OnScrollDragStarted()
1179 ScrollEventArgs eventArgs = new ScrollEventArgs(ContentContainer.CurrentPosition);
1180 ScrollDragStarted?.Invoke(this, eventArgs);
1181 EmitScrollStartedEvent();
1183 if (!hideScrollbar && fadeScrollbar)
1185 scrollBar?.FadeIn();
1189 private void OnScrollDragEnded()
1191 ScrollEventArgs eventArgs = new ScrollEventArgs(ContentContainer.CurrentPosition);
1192 ScrollDragEnded?.Invoke(this, eventArgs);
1193 EmitScrollFinishedEvent();
1195 if (!hideScrollbar && fadeScrollbar)
1197 scrollBar?.FadeOut();
1201 private void OnScrollAnimationStarted()
1203 ScrollEventArgs eventArgs = new ScrollEventArgs(ContentContainer.CurrentPosition);
1204 ScrollAnimationStarted?.Invoke(this, eventArgs);
1205 EmitScrollStartedEvent();
1207 if (!hideScrollbar && fadeScrollbar)
1209 scrollBar?.FadeIn();
1213 private void OnScrollAnimationEnded()
1216 this.InterceptTouchEvent -= OnInterruptTouchingChildTouched;
1218 ScrollEventArgs eventArgs = new ScrollEventArgs(ContentContainer.CurrentPosition);
1219 ScrollAnimationEnded?.Invoke(this, eventArgs);
1220 EmitScrollFinishedEvent();
1222 if (!hideScrollbar && fadeScrollbar)
1224 scrollBar?.FadeOut();
1228 private void OnScroll()
1230 ScrollEventArgs eventArgs = new ScrollEventArgs(ContentContainer.CurrentPosition);
1231 Scrolling?.Invoke(this, eventArgs);
1233 bool isHorizontal = ScrollingDirection == Direction.Horizontal;
1234 float contentLength = isHorizontal ? ContentContainer.Size.Width : ContentContainer.Size.Height;
1235 float currentPosition = isHorizontal ? ContentContainer.CurrentPosition.X : ContentContainer.CurrentPosition.Y;
1237 scrollBar?.Update(contentLength, Math.Abs(currentPosition));
1238 CheckPreReachedTargetPosition();
1241 private void CheckPreReachedTargetPosition()
1243 // Check whether we reached pre-reached target position
1244 if (readyToNotice &&
1245 ContentContainer.CurrentPosition.Y <= finalTargetPosition + NoticeAnimationEndBeforePosition &&
1246 ContentContainer.CurrentPosition.Y >= finalTargetPosition - NoticeAnimationEndBeforePosition)
1249 readyToNotice = false;
1250 OnPreReachedTargetPosition(finalTargetPosition);
1255 /// This helps developer who wants to know before scroll is reaching target position.
1257 /// <param name="targetPosition">Index of item.</param>
1258 /// This may be public opened in tizen_6.0 after ACR done. Before ACR, need to be hidden as inhouse API
1259 [EditorBrowsable(EditorBrowsableState.Never)]
1260 protected virtual void OnPreReachedTargetPosition(float targetPosition)
1265 private void StopScroll()
1267 if (scrollAnimation != null)
1269 if (scrollAnimation.State == Animation.States.Playing)
1271 Debug.WriteLineIf(LayoutDebugScrollableBase, "StopScroll Animation Playing");
1272 scrollAnimation.Stop(Animation.EndActions.Cancel);
1273 OnScrollAnimationEnded();
1275 scrollAnimation.Clear();
1279 private void AnimateChildTo(int duration, float axisPosition)
1281 Debug.WriteLineIf(LayoutDebugScrollableBase, "AnimationTo Animation Duration:" + duration + " Destination:" + axisPosition);
1282 finalTargetPosition = axisPosition;
1284 StopScroll(); // Will replace previous animation so will stop existing one.
1286 if (scrollAnimation == null)
1288 scrollAnimation = new Animation();
1289 scrollAnimation.Finished += ScrollAnimationFinished;
1292 scrollAnimation.Duration = duration;
1293 scrollAnimation.DefaultAlphaFunction = new AlphaFunction(AlphaFunction.BuiltinFunctions.EaseOutSquare);
1294 scrollAnimation.AnimateTo(ContentContainer, (ScrollingDirection == Direction.Horizontal) ? "PositionX" : "PositionY", axisPosition, ScrollAlphaFunction);
1296 OnScrollAnimationStarted();
1297 scrollAnimation.Play();
1301 /// Scroll to specific position with or without animation.
1303 /// <param name="position">Destination.</param>
1304 /// <param name="animate">Scroll with or without animation</param>
1305 /// <since_tizen> 8 </since_tizen>
1306 public void ScrollTo(float position, bool animate)
1309 float currentPositionX = ContentContainer.CurrentPosition.X != 0 ? ContentContainer.CurrentPosition.X : ContentContainer.Position.X;
1310 float currentPositionY = ContentContainer.CurrentPosition.Y != 0 ? ContentContainer.CurrentPosition.Y : ContentContainer.Position.Y;
1311 float delta = ScrollingDirection == Direction.Horizontal ? currentPositionX : currentPositionY;
1312 // The argument position is the new pan position. So the new position of ScrollableBase becomes (-position).
1313 // To move ScrollableBase's position to (-position), it moves by (-position - currentPosition).
1314 delta = -position - delta;
1316 ScrollBy(delta, animate);
1319 private float BoundScrollPosition(float targetPosition)
1321 if (ScrollAvailableArea != null)
1323 float minScrollPosition = ScrollAvailableArea.X;
1324 float maxScrollPosition = ScrollAvailableArea.Y;
1326 targetPosition = Math.Min(-minScrollPosition, targetPosition);
1327 targetPosition = Math.Max(-maxScrollPosition, targetPosition);
1331 targetPosition = Math.Min(0, targetPosition);
1332 targetPosition = Math.Max(-maxScrollDistance, targetPosition);
1335 return targetPosition;
1338 private void ScrollBy(float displacement, bool animate)
1340 if (GetChildCount() == 0 || maxScrollDistance < 0)
1345 float childCurrentPosition = (ScrollingDirection == Direction.Horizontal) ? ContentContainer.PositionX : ContentContainer.PositionY;
1347 Debug.WriteLineIf(LayoutDebugScrollableBase, "ScrollBy childCurrentPosition:" + childCurrentPosition +
1348 " displacement:" + displacement,
1349 " maxScrollDistance:" + maxScrollDistance);
1351 childTargetPosition = childCurrentPosition + displacement; // child current position + gesture displacement
1353 Debug.WriteLineIf(LayoutDebugScrollableBase, "ScrollBy currentAxisPosition:" + childCurrentPosition + "childTargetPosition:" + childTargetPosition);
1357 // Calculate scroll animation duration
1358 float scrollDistance = Math.Abs(displacement);
1359 readyToNotice = true;
1361 AnimateChildTo(ScrollDuration, BoundScrollPosition(AdjustTargetPositionOfScrollAnimation(BoundScrollPosition(childTargetPosition))));
1366 finalTargetPosition = BoundScrollPosition(childTargetPosition);
1368 // Set position of scrolling child without an animation
1369 if (ScrollingDirection == Direction.Horizontal)
1371 ContentContainer.PositionX = finalTargetPosition;
1375 ContentContainer.PositionY = finalTargetPosition;
1381 /// you can override it to clean-up your own resources.
1383 /// <param name="type">DisposeTypes</param>
1384 /// This will be public opened in tizen_5.5 after ACR done. Before ACR, need to be hidden as inhouse API.
1385 [EditorBrowsable(EditorBrowsableState.Never)]
1386 protected override void Dispose(DisposeTypes type)
1393 StopOverShootingShadowAnimation();
1396 if (type == DisposeTypes.Explicit)
1398 mPanGestureDetector?.Dispose();
1399 mPanGestureDetector = null;
1401 ContentContainer?.RemovePropertyNotification(propertyNotification);
1402 propertyNotification?.Dispose();
1403 propertyNotification = null;
1406 WheelEvent -= OnWheelEvent;
1408 if (type == DisposeTypes.Explicit)
1415 private float CalculateMaximumScrollDistance()
1417 float scrollingChildLength = 0;
1418 float scrollerLength = 0;
1419 if (ScrollingDirection == Direction.Horizontal)
1421 Debug.WriteLineIf(LayoutDebugScrollableBase, "Horizontal");
1423 scrollingChildLength = ContentContainer.Size.Width;
1424 scrollerLength = Size.Width;
1428 Debug.WriteLineIf(LayoutDebugScrollableBase, "Vertical");
1429 scrollingChildLength = ContentContainer.Size.Height;
1430 scrollerLength = Size.Height;
1433 Debug.WriteLineIf(LayoutDebugScrollableBase, "ScrollBy maxScrollDistance:" + (scrollingChildLength - scrollerLength) +
1434 " parent length:" + scrollerLength +
1435 " scrolling child length:" + scrollingChildLength);
1437 return Math.Max(scrollingChildLength - scrollerLength, 0);
1440 private void PageSnap(float velocity)
1444 Debug.WriteLineIf(LayoutDebugScrollableBase, "PageSnap with pan candidate totalDisplacement:" + totalDisplacementForPan +
1445 " currentPage[" + CurrentPage + "]");
1447 //Increment current page if total displacement enough to warrant a page change.
1448 if (Math.Abs(totalDisplacementForPan) > (mPageWidth * ratioOfScreenWidthToCompleteScroll))
1450 if (totalDisplacementForPan < 0)
1452 CurrentPage = Math.Min(Math.Max(Children.Count - 1, 0), ++CurrentPage);
1456 CurrentPage = Math.Max(0, --CurrentPage);
1459 else if (Math.Abs(velocity) > PageFlickThreshold)
1463 CurrentPage = Math.Min(Math.Max(Children.Count - 1, 0), ++CurrentPage);
1467 CurrentPage = Math.Max(0, --CurrentPage);
1471 // Animate to new page or reposition to current page
1472 if (ScrollingDirection == Direction.Horizontal)
1473 destination = -(Children[CurrentPage].Position.X + Children[CurrentPage].CurrentSize.Width / 2 - CurrentSize.Width / 2); // set to middle of current page
1475 destination = -(Children[CurrentPage].Position.Y + Children[CurrentPage].CurrentSize.Height / 2 - CurrentSize.Height / 2);
1477 AnimateChildTo(ScrollDuration, destination);
1481 /// Enable/Disable overshooting effect. default is disabled.
1483 [EditorBrowsable(EditorBrowsableState.Never)]
1484 public bool EnableOverShootingEffect
1488 return (bool)GetValue(EnableOverShootingEffectProperty);
1492 SetValue(EnableOverShootingEffectProperty, value);
1493 NotifyPropertyChanged();
1496 private bool InternalEnableOverShootingEffect { get; set; } = false;
1498 private void AttachOverShootingShadowView()
1500 if (!EnableOverShootingEffect)
1503 // stop animation if necessary.
1504 StopOverShootingShadowAnimation();
1506 if (ScrollingDirection == Direction.Horizontal)
1508 base.Add(leftOverShootingShadowView);
1509 base.Add(rightOverShootingShadowView);
1511 leftOverShootingShadowView.Size = new Size(0.0f, SizeHeight);
1512 leftOverShootingShadowView.Opacity = 1.0f;
1513 leftOverShootingShadowView.RaiseToTop();
1515 rightOverShootingShadowView.Size = new Size(0.0f, SizeHeight);
1516 rightOverShootingShadowView.Opacity = 1.0f;
1517 rightOverShootingShadowView.RaiseToTop();
1521 base.Add(topOverShootingShadowView);
1522 base.Add(bottomOverShootingShadowView);
1524 topOverShootingShadowView.Size = new Size(SizeWidth, 0.0f);
1525 topOverShootingShadowView.Opacity = 1.0f;
1526 topOverShootingShadowView.RaiseToTop();
1528 bottomOverShootingShadowView.Size = new Size(SizeWidth, 0.0f);
1529 bottomOverShootingShadowView.Opacity = 1.0f;
1530 bottomOverShootingShadowView.RaiseToTop();
1533 // at the beginning, height or width of overshooting shadow is 0, so it is invisible.
1534 isOverShootingShadowShown = false;
1537 private void DragOverShootingShadow(float totalPanDisplacement, float panDisplacement)
1539 if (!EnableOverShootingEffect)
1542 if (totalPanDisplacement > 0) // downwards
1544 // check if reaching at the top / left.
1545 if ((int)finalTargetPosition != 0)
1547 isOverShootingShadowShown = false;
1551 // save start displacement, and re-calculate displacement.
1552 if (!isOverShootingShadowShown)
1554 startShowShadowDisplacement = totalPanDisplacement;
1556 isOverShootingShadowShown = true;
1558 float newDisplacement = (int)totalPanDisplacement < (int)startShowShadowDisplacement ? 0 : totalPanDisplacement - startShowShadowDisplacement;
1560 if (ScrollingDirection == Direction.Horizontal)
1562 // scale limit of height is 60%.
1563 float heightScale = newDisplacement / overShootingShadowScaleHeightLimit;
1564 leftOverShootingShadowView.SizeHeight = heightScale > 0.6f ? SizeHeight * 0.4f : SizeHeight * (1.0f - heightScale);
1566 // scale limit of width is 300%.
1567 leftOverShootingShadowView.SizeWidth = newDisplacement > overShootingShadowScaleHeightLimit ? overShootingShadowScaleHeightLimit : newDisplacement;
1570 ScrollOutOfBoundEventArgs.Direction scrollDirection = panDisplacement > 0 ?
1571 ScrollOutOfBoundEventArgs.Direction.Right : ScrollOutOfBoundEventArgs.Direction.Left;
1572 OnScrollOutOfBound(scrollDirection, totalPanDisplacement);
1576 // scale limit of width is 60%.
1577 float widthScale = newDisplacement / overShootingShadowScaleHeightLimit;
1578 topOverShootingShadowView.SizeWidth = widthScale > 0.6f ? SizeWidth * 0.4f : SizeWidth * (1.0f - widthScale);
1580 // scale limit of height is 300%.
1581 topOverShootingShadowView.SizeHeight = newDisplacement > overShootingShadowScaleHeightLimit ? overShootingShadowScaleHeightLimit : newDisplacement;
1584 ScrollOutOfBoundEventArgs.Direction scrollDirection = panDisplacement > 0 ?
1585 ScrollOutOfBoundEventArgs.Direction.Down : ScrollOutOfBoundEventArgs.Direction.Up;
1586 OnScrollOutOfBound(scrollDirection, totalPanDisplacement);
1589 else if (totalPanDisplacement < 0) // upwards
1591 // check if reaching at the bottom.
1592 if (-(int)finalTargetPosition != (int)maxScrollDistance)
1594 isOverShootingShadowShown = false;
1598 // save start displacement, and re-calculate displacement.
1599 if (!isOverShootingShadowShown)
1601 startShowShadowDisplacement = totalPanDisplacement;
1603 isOverShootingShadowShown = true;
1605 float newDisplacement = (int)startShowShadowDisplacement < (int)totalPanDisplacement ? 0 : startShowShadowDisplacement - totalPanDisplacement;
1607 if (ScrollingDirection == Direction.Horizontal)
1609 // scale limit of height is 60%.
1610 float heightScale = newDisplacement / overShootingShadowScaleHeightLimit;
1611 rightOverShootingShadowView.SizeHeight = heightScale > 0.6f ? SizeHeight * 0.4f : SizeHeight * (1.0f - heightScale);
1613 // scale limit of width is 300%.
1614 rightOverShootingShadowView.SizeWidth = newDisplacement > overShootingShadowScaleHeightLimit ? overShootingShadowScaleHeightLimit : newDisplacement;
1617 ScrollOutOfBoundEventArgs.Direction scrollDirection = panDisplacement > 0 ?
1618 ScrollOutOfBoundEventArgs.Direction.Right : ScrollOutOfBoundEventArgs.Direction.Left;
1619 OnScrollOutOfBound(scrollDirection, totalPanDisplacement);
1623 // scale limit of width is 60%.
1624 float widthScale = newDisplacement / overShootingShadowScaleHeightLimit;
1625 bottomOverShootingShadowView.SizeWidth = widthScale > 0.6f ? SizeWidth * 0.4f : SizeWidth * (1.0f - widthScale);
1627 // scale limit of height is 300%.
1628 bottomOverShootingShadowView.SizeHeight = newDisplacement > overShootingShadowScaleHeightLimit ? overShootingShadowScaleHeightLimit : newDisplacement;
1631 ScrollOutOfBoundEventArgs.Direction scrollDirection = panDisplacement > 0 ?
1632 ScrollOutOfBoundEventArgs.Direction.Down : ScrollOutOfBoundEventArgs.Direction.Up;
1633 OnScrollOutOfBound(scrollDirection, totalPanDisplacement);
1638 // if total displacement is 0, shadow would become invisible.
1639 isOverShootingShadowShown = false;
1643 private void PlayOverShootingShadowAnimation()
1645 if (!EnableOverShootingEffect)
1648 // stop animation if necessary.
1649 StopOverShootingShadowAnimation();
1651 if (overShootingShadowAnimation == null)
1653 overShootingShadowAnimation = new Animation(overShootingShadowAnimationDuration);
1654 overShootingShadowAnimation.Finished += OnOverShootingShadowAnimationFinished;
1657 if (ScrollingDirection == Direction.Horizontal)
1659 View targetView = totalDisplacementForPan < 0 ? rightOverShootingShadowView : leftOverShootingShadowView;
1660 overShootingShadowAnimation.AnimateTo(targetView, "SizeHeight", SizeHeight);
1661 overShootingShadowAnimation.AnimateTo(targetView, "SizeWidth", 0.0f);
1662 overShootingShadowAnimation.AnimateTo(targetView, "Opacity", 0.0f);
1666 View targetView = totalDisplacementForPan < 0 ? bottomOverShootingShadowView : topOverShootingShadowView;
1667 overShootingShadowAnimation.AnimateTo(targetView, "SizeWidth", SizeWidth);
1668 overShootingShadowAnimation.AnimateTo(targetView, "SizeHeight", 0.0f);
1669 overShootingShadowAnimation.AnimateTo(targetView, "Opacity", 0.0f);
1671 overShootingShadowAnimation.Play();
1674 private void StopOverShootingShadowAnimation()
1676 if (overShootingShadowAnimation == null || overShootingShadowAnimation.State != Animation.States.Playing)
1679 overShootingShadowAnimation.Stop(Animation.EndActions.Cancel);
1680 OnOverShootingShadowAnimationFinished(null, null);
1681 overShootingShadowAnimation.Clear();
1684 private void OnOverShootingShadowAnimationFinished(object sender, EventArgs e)
1686 if (ScrollingDirection == Direction.Horizontal)
1688 base.Remove(leftOverShootingShadowView);
1689 base.Remove(rightOverShootingShadowView);
1691 leftOverShootingShadowView.Size = new Size(0.0f, SizeHeight);
1692 rightOverShootingShadowView.Size = new Size(0.0f, SizeHeight);
1696 base.Remove(topOverShootingShadowView);
1697 base.Remove(bottomOverShootingShadowView);
1699 topOverShootingShadowView.Size = new Size(SizeWidth, 0.0f);
1700 bottomOverShootingShadowView.Size = new Size(SizeWidth, 0.0f);
1703 // after animation finished, height/width & opacity of vertical shadow both are 0, so it is invisible.
1704 isOverShootingShadowShown = false;
1707 private void OnScrollOutOfBound(ScrollOutOfBoundEventArgs.Direction direction, float displacement)
1709 ScrollOutOfBoundEventArgs args = new ScrollOutOfBoundEventArgs(direction, displacement);
1710 ScrollOutOfBound?.Invoke(this, args);
1713 private void OnPanGestureDetected(object source, PanGestureDetector.DetectedEventArgs e)
1715 e.Handled = OnPanGesture(e.PanGesture);
1718 private bool OnPanGesture(PanGesture panGesture)
1720 bool handled = true;
1721 if (SnapToPage && scrollAnimation != null && scrollAnimation.State == Animation.States.Playing)
1725 if (panGesture.State == Gesture.StateType.Started)
1727 readyToNotice = false;
1728 AttachOverShootingShadowView();
1729 Debug.WriteLineIf(LayoutDebugScrollableBase, "Gesture Start");
1730 if (scrolling && !SnapToPage)
1734 totalDisplacementForPan = 0.0f;
1736 // check if gesture need to propagation
1737 var checkDisplacement = (ScrollingDirection == Direction.Horizontal) ? panGesture.Displacement.X : panGesture.Displacement.Y;
1738 var checkChildCurrentPosition = (ScrollingDirection == Direction.Horizontal) ? ContentContainer.PositionX : ContentContainer.PositionY;
1739 var checkChildTargetPosition = checkChildCurrentPosition + checkDisplacement;
1740 var checkFinalTargetPosition = BoundScrollPosition(checkChildTargetPosition);
1741 handled = !((int)checkFinalTargetPosition == 0 || -(int)checkFinalTargetPosition == (int)maxScrollDistance);
1742 // If you propagate a gesture event, return;
1748 //Interrupt touching when panning is started
1749 this.InterceptTouchEvent += OnInterruptTouchingChildTouched;
1750 OnScrollDragStarted();
1752 else if (panGesture.State == Gesture.StateType.Continuing)
1754 if (ScrollingDirection == Direction.Horizontal)
1756 // if vertical shadow is shown, does not scroll.
1757 if (!isOverShootingShadowShown)
1759 ScrollBy(panGesture.Displacement.X, false);
1761 totalDisplacementForPan += panGesture.Displacement.X;
1762 DragOverShootingShadow(totalDisplacementForPan, panGesture.Displacement.X);
1766 // if vertical shadow is shown, does not scroll.
1767 if (!isOverShootingShadowShown)
1769 ScrollBy(panGesture.Displacement.Y, false);
1771 totalDisplacementForPan += panGesture.Displacement.Y;
1772 DragOverShootingShadow(totalDisplacementForPan, panGesture.Displacement.Y);
1774 Debug.WriteLineIf(LayoutDebugScrollableBase, "OnPanGestureDetected Continue totalDisplacementForPan:" + totalDisplacementForPan);
1777 else if (panGesture.State == Gesture.StateType.Finished || panGesture.State == Gesture.StateType.Cancelled)
1779 PlayOverShootingShadowAnimation();
1780 OnScrollDragEnded();
1781 StopScroll(); // Will replace previous animation so will stop existing one.
1783 if (scrollAnimation == null)
1785 scrollAnimation = new Animation();
1786 scrollAnimation.Finished += ScrollAnimationFinished;
1789 float panVelocity = (ScrollingDirection == Direction.Horizontal) ? panGesture.Velocity.X : panGesture.Velocity.Y;
1793 PageSnap(panVelocity);
1797 if (panVelocity == 0)
1799 float currentScrollPosition = (ScrollingDirection == Direction.Horizontal ? ContentContainer.CurrentPosition.X : ContentContainer.CurrentPosition.Y);
1800 scrollAnimation.DefaultAlphaFunction = new AlphaFunction(AlphaFunction.BuiltinFunctions.Linear);
1801 scrollAnimation.Duration = 0;
1802 scrollAnimation.AnimateTo(ContentContainer, (ScrollingDirection == Direction.Horizontal) ? "PositionX" : "PositionY", currentScrollPosition);
1803 scrollAnimation.Play();
1807 Decelerating(panVelocity, scrollAnimation);
1811 totalDisplacementForPan = 0;
1813 readyToNotice = true;
1814 OnScrollAnimationStarted();
1819 internal void BaseRemove(View view)
1824 internal override bool OnAccessibilityPan(PanGesture gestures)
1826 if (SnapToPage && scrollAnimation != null && scrollAnimation.State == Animation.States.Playing)
1831 OnPanGesture(gestures);
1835 private float CustomScrollAlphaFunction(float progress)
1837 if (panAnimationDelta == 0)
1843 // Parameter "progress" is normalized value. We need to multiply target duration to calculate distance.
1844 // Can get real distance using equation of deceleration (check Decelerating function)
1845 // After get real distance, normalize it
1846 float realDuration = progress * panAnimationDuration;
1847 float realDistance = velocityOfLastPan * ((float)Math.Pow(decelerationRate, realDuration) - 1) / logValueOfDeceleration;
1848 float result = Math.Min(realDistance / Math.Abs(panAnimationDelta), 1.0f);
1850 // This is hot-fix for if the velocity has very small value, result is not updated even progress done.
1851 if (progress > 0.99) result = 1.0f;
1858 /// you can override it to custom your decelerating
1860 /// <param name="velocity">Velocity of current pan.</param>
1861 /// <param name="animation">Scroll animation.</param>
1862 [EditorBrowsable(EditorBrowsableState.Never)]
1863 protected virtual void Decelerating(float velocity, Animation animation)
1865 if (animation == null) throw new ArgumentNullException(nameof(animation));
1866 // Decelerating using deceleration equation ===========
1868 // V : velocity (pixel per millisecond)
1869 // V0 : initial velocity
1870 // d : deceleration rate,
1872 // X : final position after decelerating
1873 // log : natural logarithm
1875 // V(t) = V0 * d pow t;
1876 // X(t) = V0 * (d pow t - 1) / log d; <-- Integrate the velocity function
1877 // X(∞) = V0 * d / (1 - d); <-- Result using infinite T can be final position because T is tending to infinity.
1879 // Because of final T is tending to infinity, we should use threshold value to finish.
1880 // Final T = log(-threshold * log d / |V0| ) / log d;
1882 velocityOfLastPan = Math.Abs(velocity);
1884 float currentScrollPosition = -(ScrollingDirection == Direction.Horizontal ? ContentContainer.CurrentPosition.X : ContentContainer.CurrentPosition.Y);
1885 panAnimationDelta = (velocityOfLastPan * decelerationRate) / (1 - decelerationRate);
1886 panAnimationDelta = velocity > 0 ? -panAnimationDelta : panAnimationDelta;
1888 float destination = -(panAnimationDelta + currentScrollPosition);
1889 float adjustDestination = AdjustTargetPositionOfScrollAnimation(destination);
1890 float maxPosition = ScrollAvailableArea != null ? ScrollAvailableArea.Y : maxScrollDistance;
1891 float minPosition = ScrollAvailableArea != null ? ScrollAvailableArea.X : 0;
1893 if (destination < -maxPosition || destination > minPosition)
1895 panAnimationDelta = velocity > 0 ? (currentScrollPosition - minPosition) : (maxPosition - currentScrollPosition);
1896 destination = velocity > 0 ? minPosition : -maxPosition;
1898 if (panAnimationDelta == 0)
1900 panAnimationDuration = 0.0f;
1904 panAnimationDuration = (float)Math.Log((panAnimationDelta * logValueOfDeceleration / velocityOfLastPan + 1), decelerationRate);
1907 Debug.WriteLineIf(LayoutDebugScrollableBase, "\n" +
1908 "OverRange======================= \n" +
1909 "[decelerationRate] " + decelerationRate + "\n" +
1910 "[logValueOfDeceleration] " + logValueOfDeceleration + "\n" +
1911 "[Velocity] " + velocityOfLastPan + "\n" +
1912 "[CurrentPosition] " + currentScrollPosition + "\n" +
1913 "[CandidateDelta] " + panAnimationDelta + "\n" +
1914 "[Destination] " + destination + "\n" +
1915 "[Duration] " + panAnimationDuration + "\n" +
1916 "================================ \n"
1921 panAnimationDuration = (float)Math.Log(-DecelerationThreshold * logValueOfDeceleration / velocityOfLastPan) / logValueOfDeceleration;
1923 if (adjustDestination != destination)
1925 destination = adjustDestination;
1926 panAnimationDelta = destination + currentScrollPosition;
1927 velocityOfLastPan = Math.Abs(panAnimationDelta * logValueOfDeceleration / ((float)Math.Pow(decelerationRate, panAnimationDuration) - 1));
1928 panAnimationDuration = (float)Math.Log(-DecelerationThreshold * logValueOfDeceleration / velocityOfLastPan) / logValueOfDeceleration;
1931 Debug.WriteLineIf(LayoutDebugScrollableBase, "\n" +
1932 "================================ \n" +
1933 "[decelerationRate] " + decelerationRate + "\n" +
1934 "[logValueOfDeceleration] " + logValueOfDeceleration + "\n" +
1935 "[Velocity] " + velocityOfLastPan + "\n" +
1936 "[CurrentPosition] " + currentScrollPosition + "\n" +
1937 "[CandidateDelta] " + panAnimationDelta + "\n" +
1938 "[Destination] " + destination + "\n" +
1939 "[Duration] " + panAnimationDuration + "\n" +
1940 "================================ \n"
1944 finalTargetPosition = destination;
1946 customScrollAlphaFunction = new UserAlphaFunctionDelegate(CustomScrollAlphaFunction);
1947 animation.DefaultAlphaFunction = new AlphaFunction(customScrollAlphaFunction);
1948 GC.KeepAlive(customScrollAlphaFunction);
1949 animation.Duration = (int)panAnimationDuration;
1950 animation.AnimateTo(ContentContainer, (ScrollingDirection == Direction.Horizontal) ? "PositionX" : "PositionY", destination);
1954 private void ScrollAnimationFinished(object sender, EventArgs e)
1956 OnScrollAnimationEnded();
1960 /// Adjust scrolling position by own scrolling rules.
1961 /// Override this function when developer wants to change destination of flicking.(e.g. always snap to center of item)
1963 /// This may be public opened in tizen_6.0 after ACR done. Before ACR, need to be hidden as inhouse API
1964 [EditorBrowsable(EditorBrowsableState.Never)]
1965 protected virtual float AdjustTargetPositionOfScrollAnimation(float position)
1971 /// Scroll position given to ScrollTo.
1972 /// This is the position in the opposite direction to the position of ContentContainer.
1974 /// <since_tizen> 8 </since_tizen>
1975 public Position ScrollPosition
1979 return new Position(-ContentContainer.Position);
1984 /// Current scroll position in the middle of ScrollTo animation.
1985 /// This is the position in the opposite direction to the current position of ContentContainer.
1987 /// <since_tizen> 8 </since_tizen>
1988 public Position ScrollCurrentPosition
1992 return new Position(-ContentContainer.CurrentPosition);
1997 /// Remove all children in ContentContainer.
1999 /// <param name="dispose">If true, removed child is disposed.</param>
2000 [EditorBrowsable(EditorBrowsableState.Never)]
2001 public void RemoveAllChildren(bool dispose = false)
2003 RecursiveRemoveChildren(ContentContainer, dispose);
2006 private void RecursiveRemoveChildren(View parent, bool dispose)
2012 int maxChild = (int)parent.GetChildCount();
2013 for (int i = maxChild - 1; i >= 0; --i)
2015 View child = parent.GetChildAt((uint)i);
2020 RecursiveRemoveChildren(child, dispose);
2021 parent.Remove(child);
2029 internal bool IsChildNearlyVisble(View child, float offset = 0)
2031 if (ScreenPosition.X - offset < child.ScreenPosition.X + child.SizeWidth &&
2032 ScreenPosition.X + SizeWidth + offset > child.ScreenPosition.X &&
2033 ScreenPosition.Y - offset < child.ScreenPosition.Y + child.SizeHeight &&
2034 ScreenPosition.Y + SizeHeight + offset > child.ScreenPosition.Y)
2045 [EditorBrowsable(EditorBrowsableState.Never)]
2046 public override View GetNextFocusableView(View currentFocusedView, View.FocusDirection direction, bool loopEnabled)
2048 bool isHorizontal = (ScrollingDirection == Direction.Horizontal);
2049 float targetPosition = -(ScrollingDirection == Direction.Horizontal ? ContentContainer.CurrentPosition.X : ContentContainer.CurrentPosition.Y);
2050 float stepDistance = (stepScrollDistance != 0 ? stepScrollDistance : (isHorizontal ? Size.Width * 0.25f : Size.Height * 0.25f));
2052 bool forward = ((isHorizontal && direction == View.FocusDirection.Right) ||
2053 (!isHorizontal && direction == View.FocusDirection.Down) ||
2054 (direction == View.FocusDirection.Clockwise));
2055 bool backward = ((isHorizontal && direction == View.FocusDirection.Left) ||
2056 (!isHorizontal && direction == View.FocusDirection.Up) ||
2057 (direction == View.FocusDirection.CounterClockwise));
2059 View nextFocusedView = FocusManager.Instance.GetNearestFocusableActor(this, currentFocusedView, direction);
2061 // Move out focus from ScrollableBase.
2062 // FIXME: Forward, Backward is unimplemented other components.
2063 if (direction == View.FocusDirection.Forward ||
2064 direction == View.FocusDirection.Backward ||
2065 (nextFocusedView == null &&
2066 ((forward && maxScrollDistance - targetPosition < 0.1f) ||
2067 (backward && targetPosition < 0.1f))))
2069 var next = FocusManager.Instance.GetNearestFocusableActor(this.Parent, this, direction);
2070 Debug.WriteLineIf(focusDebugScrollableBase, $"Focus move [{direction}] out from ScrollableBase! Next focus target {next}:{next?.ID}");
2074 if (focusDebugScrollableBase)
2076 global::System.Text.StringBuilder debugMessage = new global::System.Text.StringBuilder("=========================================================\n");
2077 debugMessage.Append($"GetNextFocusableView On: {this}:{this.ID}\n");
2078 debugMessage.Append($"----------------Current: {currentFocusedView}:{currentFocusedView?.ID}\n");
2079 debugMessage.Append($"-------------------Next: {nextFocusedView}:{nextFocusedView?.ID}\n");
2080 debugMessage.Append($"--------------Direction: {direction}\n");
2081 debugMessage.Append("=========================================================");
2082 Debug.WriteLineIf(focusDebugScrollableBase, debugMessage);
2085 if (nextFocusedView != null)
2087 if (null != FindDescendantByID(nextFocusedView.ID))
2089 if (IsChildNearlyVisble(nextFocusedView, stepDistance) == true)
2091 ScrollToChild(nextFocusedView, true);
2092 return nextFocusedView;
2097 if (forward || backward)
2099 // Fallback to current focus or scrollableBase till next focus visible in scrollable.
2100 if (null != currentFocusedView && null != FindDescendantByID(currentFocusedView.ID))
2102 nextFocusedView = currentFocusedView;
2106 Debug.WriteLineIf(focusDebugScrollableBase, "current focus view is not decendant. return ScrollableBase!");
2112 targetPosition += stepDistance;
2113 targetPosition = targetPosition > maxScrollDistance ? maxScrollDistance : targetPosition;
2118 targetPosition -= stepDistance;
2119 targetPosition = targetPosition < 0 ? 0 : targetPosition;
2122 ScrollTo(targetPosition, true);
2124 Debug.WriteLineIf(focusDebugScrollableBase, $"ScrollTo :({targetPosition})");
2127 Debug.WriteLineIf(focusDebugScrollableBase, $"return end : {nextFocusedView}:{nextFocusedView?.ID}");
2128 return nextFocusedView;
2132 [EditorBrowsable(EditorBrowsableState.Never)]
2133 protected override bool AccessibilityScrollToChild(View child)
2140 if (ScrollingDirection == Direction.Horizontal)
2142 if (child.ScreenPosition.X + child.Size.Width <= this.ScreenPosition.X)
2146 PageSnap(PageFlickThreshold + 1);
2150 ScrollTo((float)(child.ScreenPosition.X - ContentContainer.ScreenPosition.X), false);
2153 else if (child.ScreenPosition.X >= this.ScreenPosition.X + this.Size.Width)
2157 PageSnap(-(PageFlickThreshold + 1));
2161 ScrollTo((float)(child.ScreenPosition.X + child.Size.Width - ContentContainer.ScreenPosition.X - this.Size.Width), false);
2167 if (child.ScreenPosition.Y + child.Size.Height <= this.ScreenPosition.Y)
2171 PageSnap(PageFlickThreshold + 1);
2175 ScrollTo((float)(child.ScreenPosition.Y - ContentContainer.ScreenPosition.Y), false);
2178 else if (child.ScreenPosition.Y >= this.ScreenPosition.Y + this.Size.Height)
2182 PageSnap(-(PageFlickThreshold + 1));
2186 ScrollTo((float)(child.ScreenPosition.Y + child.Size.Height - ContentContainer.ScreenPosition.Y - this.Size.Height), false);
2195 [EditorBrowsable(EditorBrowsableState.Never)]
2196 public override bool OnWheel(Wheel wheel)
2203 float currentScrollPosition = -(ScrollingDirection == Direction.Horizontal ? ContentContainer.CurrentPosition.X : ContentContainer.CurrentPosition.Y);
2204 ScrollTo(currentScrollPosition + (wheelScrollDistance * wheel.Z), false);
2209 private bool OnWheelEvent(object o, WheelEventArgs e)
2211 return OnWheel(e?.Wheel);