1 /* Copyright (c) 2020 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.ComponentModel;
19 using System.Diagnostics;
21 namespace Tizen.NUI.Components
24 /// [Draft] This class provides a View that can scroll a single View with a layout. This View can be a nest of Views.
26 /// This may be public opened in tizen_6.0 after ACR done. Before ACR, need to be hidden as inhouse API.
27 [EditorBrowsable(EditorBrowsableState.Never)]
28 public class ScrollableBase : Control
30 static bool LayoutDebugScrollableBase = false; // Debug flag
31 private Direction mScrollingDirection = Direction.Vertical;
32 private bool mScrollEnabled = true;
33 private int mPageWidth = 0;
35 private class ScrollableBaseCustomLayout : LayoutGroup
37 protected override void OnMeasure(MeasureSpecification widthMeasureSpec, MeasureSpecification heightMeasureSpec)
39 Extents padding = Padding;
40 float totalHeight = padding.Top + padding.Bottom;
41 float totalWidth = padding.Start + padding.End;
43 MeasuredSize.StateType childWidthState = MeasuredSize.StateType.MeasuredSizeOK;
44 MeasuredSize.StateType childHeightState = MeasuredSize.StateType.MeasuredSizeOK;
46 Direction scrollingDirection = Direction.Vertical;
47 ScrollableBase scrollableBase = this.Owner as ScrollableBase;
50 scrollingDirection = scrollableBase.ScrollingDirection;
53 // measure child, should be a single scrolling child
54 foreach (LayoutItem childLayout in LayoutChildren)
56 if (childLayout != null)
59 // Use an Unspecified MeasureSpecification mode so scrolling child is not restricted to it's parents size in Height (for vertical scrolling)
60 // or Width for horizontal scrolling
61 MeasureSpecification unrestrictedMeasureSpec = new MeasureSpecification(heightMeasureSpec.Size, MeasureSpecification.ModeType.Unspecified);
63 if (scrollingDirection == Direction.Vertical)
65 MeasureChild(childLayout, widthMeasureSpec, unrestrictedMeasureSpec); // Height unrestricted by parent
69 MeasureChild(childLayout, unrestrictedMeasureSpec, heightMeasureSpec); // Width unrestricted by parent
72 float childWidth = childLayout.MeasuredWidth.Size.AsDecimal();
73 float childHeight = childLayout.MeasuredHeight.Size.AsDecimal();
75 // Determine the width and height needed by the children using their given position and size.
76 // Children could overlap so find the left most and right most child.
77 Position2D childPosition = childLayout.Owner.Position2D;
78 float childLeft = childPosition.X;
79 float childTop = childPosition.Y;
81 // Store current width and height needed to contain all children.
82 Extents childMargin = childLayout.Margin;
83 totalWidth = childWidth + childMargin.Start + childMargin.End;
84 totalHeight = childHeight + childMargin.Top + childMargin.Bottom;
86 if (childLayout.MeasuredWidth.State == MeasuredSize.StateType.MeasuredSizeTooSmall)
88 childWidthState = MeasuredSize.StateType.MeasuredSizeTooSmall;
90 if (childLayout.MeasuredWidth.State == MeasuredSize.StateType.MeasuredSizeTooSmall)
92 childHeightState = MeasuredSize.StateType.MeasuredSizeTooSmall;
98 MeasuredSize widthSizeAndState = ResolveSizeAndState(new LayoutLength(totalWidth + Padding.Start + Padding.End), widthMeasureSpec, MeasuredSize.StateType.MeasuredSizeOK);
99 MeasuredSize heightSizeAndState = ResolveSizeAndState(new LayoutLength(totalHeight + Padding.Top + Padding.Bottom), heightMeasureSpec, MeasuredSize.StateType.MeasuredSizeOK);
100 totalWidth = widthSizeAndState.Size.AsDecimal();
101 totalHeight = heightSizeAndState.Size.AsDecimal();
103 // Ensure layout respects it's given minimum size
104 totalWidth = Math.Max(totalWidth, SuggestedMinimumWidth.AsDecimal());
105 totalHeight = Math.Max(totalHeight, SuggestedMinimumHeight.AsDecimal());
107 widthSizeAndState.State = childWidthState;
108 heightSizeAndState.State = childHeightState;
110 SetMeasuredDimensions(ResolveSizeAndState(new LayoutLength(totalWidth + Padding.Start + Padding.End), widthMeasureSpec, childWidthState),
111 ResolveSizeAndState(new LayoutLength(totalHeight + Padding.Top + Padding.Bottom), heightMeasureSpec, childHeightState));
113 // Size of ScrollableBase is changed. Change Page width too.
114 scrollableBase.mPageWidth = (int)MeasuredWidth.Size.AsRoundedValue();
117 protected override void OnLayout(bool changed, LayoutLength left, LayoutLength top, LayoutLength right, LayoutLength bottom)
119 foreach (LayoutItem childLayout in LayoutChildren)
121 if (childLayout != null)
123 LayoutLength childWidth = childLayout.MeasuredWidth.Size;
124 LayoutLength childHeight = childLayout.MeasuredHeight.Size;
126 Position2D childPosition = childLayout.Owner.Position2D;
127 Extents padding = Padding;
128 Extents childMargin = childLayout.Margin;
130 LayoutLength childLeft = new LayoutLength(childPosition.X + childMargin.Start + padding.Start);
131 LayoutLength childTop = new LayoutLength(childPosition.Y + childMargin.Top + padding.Top);
133 childLayout.Layout(childLeft, childTop, childLeft + childWidth, childTop + childHeight);
137 } // ScrollableBaseCustomLayout
140 /// The direction axis to scroll.
142 /// <since_tizen> 6 </since_tizen>
143 /// This may be public opened in tizen_6.0 after ACR done. Before ACR, need to be hidden as inhouse API.
144 [EditorBrowsable(EditorBrowsableState.Never)]
145 public enum Direction
150 /// <since_tizen> 6 </since_tizen>
156 /// <since_tizen> 6 </since_tizen>
161 /// [Draft] Configurable speed threshold that register the gestures as a flick.
162 /// If the flick speed less than the threshold then will not be considered a flick.
164 /// This may be public opened in tizen_6.0 after ACR done. Before ACR, need to be hidden as inhouse API.
165 [EditorBrowsable(EditorBrowsableState.Never)]
166 public float FlickThreshold { get; set; } = 0.2f;
169 /// [Draft] Configurable duration modifer for the flick animation.
170 /// Determines the speed of the scroll, large value results in a longer flick animation. Range (0.1 - 1.0)
172 /// This may be public opened in tizen_6.0 after ACR done. Before ACR, need to be hidden as inhouse API
173 [EditorBrowsable(EditorBrowsableState.Never)]
174 public float FlickAnimationSpeed { get; set; } = 0.4f;
177 /// [Draft] Configurable modifer for the distance to be scrolled when flicked detected.
178 /// It a ratio of the ScrollableBase's length. (not child's length).
179 /// First value is the ratio of the distance to scroll with the weakest flick.
180 /// Second value is the ratio of the distance to scroll with the strongest flick.
183 /// This may be public opened in tizen_6.0 after ACR done. Before ACR, need to be hidden as inhouse API
184 [EditorBrowsable(EditorBrowsableState.Never)]
185 public Vector2 FlickDistanceMultiplierRange { get; set; } = new Vector2(0.6f, 1.8f);
188 /// [Draft] Scrolling direction mode.
189 /// Default is Vertical scrolling.
191 /// This may be public opened in tizen_6.0 after ACR done. Before ACR, need to be hidden as inhouse API
192 [EditorBrowsable(EditorBrowsableState.Never)]
193 public Direction ScrollingDirection
197 return mScrollingDirection;
201 if (value != mScrollingDirection)
203 mScrollingDirection = value;
204 mPanGestureDetector.RemoveDirection(value == Direction.Horizontal ? PanGestureDetector.DirectionVertical : PanGestureDetector.DirectionHorizontal);
205 mPanGestureDetector.AddDirection(value == Direction.Horizontal ? PanGestureDetector.DirectionHorizontal : PanGestureDetector.DirectionVertical);
211 /// [Draft] Enable or disable scrolling.
213 /// This may be public opened in tizen_6.0 after ACR done. Before ACR, need to be hidden as inhouse API
214 [EditorBrowsable(EditorBrowsableState.Never)]
215 public bool ScrollEnabled
219 return mScrollEnabled;
223 if (value != mScrollEnabled)
225 mScrollEnabled = value;
228 mPanGestureDetector.Detected += OnPanGestureDetected;
229 mTapGestureDetector.Detected += OnTapGestureDetected;
233 mPanGestureDetector.Detected -= OnPanGestureDetected;
234 mTapGestureDetector.Detected -= OnTapGestureDetected;
241 /// [Draft] Pages mode, enables moving to the next or return to current page depending on pan displacement.
242 /// Default is false.
244 /// This may be public opened in tizen_6.0 after ACR done. Before ACR, need to be hidden as inhouse API
245 [EditorBrowsable(EditorBrowsableState.Never)]
246 public bool SnapToPage { set; get; } = false;
249 /// [Draft] Get current page.
250 /// Working propery with SnapToPage property.
252 /// This may be public opened in tizen_6.0 after ACR done. Before ACR, need to be hidden as inhouse API
253 [EditorBrowsable(EditorBrowsableState.Never)]
254 public int CurrentPage { get; private set; } = 0;
257 /// [Draft] Duration of scroll animation.
259 /// This may be public opened in tizen_6.0 after ACR done. Before ACR, need to be hidden as inhouse API
260 [EditorBrowsable(EditorBrowsableState.Never)]
262 public int ScrollDuration { set; get; } = 125;
264 /// [Draft] Scroll Available area.
266 /// This may be public opened in tizen_6.0 after ACR done. Before ACR, need to be hidden as inhouse API
267 [EditorBrowsable(EditorBrowsableState.Never)]
268 public Vector2 ScrollAvailableArea { set; get; }
271 /// ScrollEventArgs is a class to record scroll event arguments which will sent to user.
273 /// <since_tizen> 6 </since_tizen>
274 /// This may be public opened in tizen_6.0 after ACR done. Before ACR, need to be hidden as inhouse API
275 [EditorBrowsable(EditorBrowsableState.Never)]
276 public class ScrollEventArgs : EventArgs
281 /// Default constructor.
283 /// <param name="position">Current scroll position</param>
284 /// <since_tizen> 6 </since_tizen>
285 /// This may be public opened in tizen_6.0 after ACR done. Before ACR, need to be hidden as inhouse API
286 public ScrollEventArgs(Position position)
288 this.position = position;
292 /// [Draft] Current scroll position.
294 /// This may be public opened in tizen_6.0 after ACR done. Before ACR, need to be hidden as inhouse API
295 [EditorBrowsable(EditorBrowsableState.Never)]
296 public Position Position
306 /// An event emitted when user starts dragging ScrollableBase, user can subscribe or unsubscribe to this event handler.<br />
308 /// <since_tizen> 6 </since_tizen>
309 /// This may be public opened in tizen_6.0 after ACR done. Before ACR, need to be hidden as inhouse API
310 [EditorBrowsable(EditorBrowsableState.Never)]
311 public event EventHandler<ScrollEventArgs> ScrollDragStartEvent;
314 /// An event emitted when user stops dragging ScrollableBase, user can subscribe or unsubscribe to this event handler.<br />
316 /// <since_tizen> 6 </since_tizen>
317 /// This may be public opened in tizen_6.0 after ACR done. Before ACR, need to be hidden as inhouse API
318 [EditorBrowsable(EditorBrowsableState.Never)]
319 public event EventHandler<ScrollEventArgs> ScrollDragEndEvent;
323 /// An event emitted when the scrolling slide animation starts, user can subscribe or unsubscribe to this event handler.<br />
325 /// <since_tizen> 6 </since_tizen>
326 /// This may be public opened in tizen_6.0 after ACR done. Before ACR, need to be hidden as inhouse API
327 [EditorBrowsable(EditorBrowsableState.Never)]
328 public event EventHandler<ScrollEventArgs> ScrollAnimationStartEvent;
331 /// An event emitted when the scrolling slide animation ends, user can subscribe or unsubscribe to this event handler.<br />
333 /// <since_tizen> 6 </since_tizen>
334 /// This may be public opened in tizen_6.0 after ACR done. Before ACR, need to be hidden as inhouse API
335 [EditorBrowsable(EditorBrowsableState.Never)]
336 public event EventHandler<ScrollEventArgs> ScrollAnimationEndEvent;
340 /// An event emitted when scrolling, user can subscribe or unsubscribe to this event handler.<br />
342 /// <since_tizen> 6 </since_tizen>
343 /// This may be public opened in tizen_6.0 after ACR done. Before ACR, need to be hidden as inhouse API
344 [EditorBrowsable(EditorBrowsableState.Never)]
345 public event EventHandler<ScrollEventArgs> ScrollEvent;
347 private Animation scrollAnimation;
348 private float maxScrollDistance;
349 private float childTargetPosition = 0.0f;
350 private PanGestureDetector mPanGestureDetector;
351 private TapGestureDetector mTapGestureDetector;
352 private View mScrollingChild;
353 private View mInterruptTouchingChild;
354 private float multiplier = 1.0f;
355 private bool scrolling = false;
356 private float ratioOfScreenWidthToCompleteScroll = 0.5f;
357 private float totalDisplacementForPan = 0.0f;
359 // If false then can only flick pages when the current animation/scroll as ended.
360 private bool flickWhenAnimating = false;
361 private PropertyNotification propertyNotification;
362 protected float finalTargetPosition;
365 /// [Draft] Constructor
367 /// <since_tizen> 6 </since_tizen>
368 /// This may be public opened in tizen_6.0 after ACR done. Before ACR, need to be hidden as inhouse API
369 [EditorBrowsable(EditorBrowsableState.Never)]
370 public ScrollableBase() : base()
372 mPanGestureDetector = new PanGestureDetector();
373 mPanGestureDetector.Attach(this);
374 mPanGestureDetector.AddDirection(PanGestureDetector.DirectionVertical);
375 mPanGestureDetector.Detected += OnPanGestureDetected;
377 mTapGestureDetector = new TapGestureDetector();
378 mTapGestureDetector.Attach(this);
379 mTapGestureDetector.Detected += OnTapGestureDetected;
381 ClippingMode = ClippingModeType.ClipToBoundingBox;
383 mScrollingChild = new View();
384 mScrollingChild.Name = "DefaultScrollingChild";
386 //Interrupt touching when panning is started;
387 mInterruptTouchingChild = new View()
389 Name = "InterruptTouchingChild",
390 Size = new Size(Window.Instance.WindowSize),
391 BackgroundColor = Color.Transparent,
394 mInterruptTouchingChild.TouchEvent += OnIterruptTouchingChildTouched;
396 Layout = new ScrollableBaseCustomLayout();
399 private bool OnIterruptTouchingChildTouched(object source, View.TouchEventArgs args)
404 private void OnPropertyChanged(object source, PropertyNotification.NotifyEventArgs args)
410 /// Called after a child has been added to the owning view.
412 /// <param name="view">The child which has been added.</param>
413 /// <since_tizen> 6 </since_tizen>
414 /// This may be public opened in tizen_6.0 after ACR done. Before ACR, need to be hidden as inhouse API
415 [EditorBrowsable(EditorBrowsableState.Never)]
416 public override void OnChildAdd(View view)
418 if (view.Name != "InterruptTouchingChild")
420 if (mScrollingChild.Name != "DefaultScrollingChild")
422 propertyNotification.Notified -= OnPropertyChanged;
423 mScrollingChild.RemovePropertyNotification(propertyNotification);
424 mScrollingChild.Relayout -= OnScrollingChildRelayout;
427 mScrollingChild = view;
428 propertyNotification = mScrollingChild?.AddPropertyNotification("position", PropertyCondition.Step(1.0f));
429 propertyNotification.Notified += OnPropertyChanged;
430 mScrollingChild.Relayout += OnScrollingChildRelayout;
435 /// Called after a child has been removed from the owning view.
437 /// <param name="view">The child which has been removed.</param>
438 /// <since_tizen> 6 </since_tizen>
439 /// This may be public opened in tizen_6.0 after ACR done. Before ACR, need to be hidden as inhouse API
440 [EditorBrowsable(EditorBrowsableState.Never)]
441 public override void OnChildRemove(View view)
443 if (view.Name != "InterruptTouchingChild")
445 propertyNotification.Notified -= OnPropertyChanged;
446 mScrollingChild.RemovePropertyNotification(propertyNotification);
447 mScrollingChild.Relayout -= OnScrollingChildRelayout;
449 mScrollingChild = new View();
453 private void OnScrollingChildRelayout(object source, EventArgs args)
455 // Size is changed. Calculate maxScrollDistance.
456 maxScrollDistance = CalculateMaximumScrollDistance();
460 /// Scrolls to the item at the specified index.
462 /// <param name="index">Index of item.</param>
463 /// <since_tizen> 6 </since_tizen>
464 /// This may be public opened in tizen_6.0 after ACR done. Before ACR, need to be hidden as inhouse API
465 [EditorBrowsable(EditorBrowsableState.Never)]
466 public void ScrollToIndex(int index)
468 if (mScrollingChild.ChildCount - 1 < index || index < 0)
478 float targetPosition = Math.Min(ScrollingDirection == Direction.Vertical ? mScrollingChild.Children[index].Position.Y : mScrollingChild.Children[index].Position.X, maxScrollDistance);
479 AnimateChildTo(ScrollDuration, -targetPosition);
482 private void OnScrollDragStart()
484 ScrollEventArgs eventArgs = new ScrollEventArgs(mScrollingChild.CurrentPosition);
485 ScrollDragStartEvent?.Invoke(this, eventArgs);
488 private void OnScrollDragEnd()
490 ScrollEventArgs eventArgs = new ScrollEventArgs(mScrollingChild.CurrentPosition);
491 ScrollDragEndEvent?.Invoke(this, eventArgs);
494 private void OnScrollAnimationStart()
496 ScrollEventArgs eventArgs = new ScrollEventArgs(mScrollingChild.CurrentPosition);
497 ScrollAnimationStartEvent?.Invoke(this, eventArgs);
500 private void OnScrollAnimationEnd()
502 ScrollEventArgs eventArgs = new ScrollEventArgs(mScrollingChild.CurrentPosition);
503 ScrollAnimationEndEvent?.Invoke(this, eventArgs);
506 private bool readyToNotice = false;
508 protected float noticeAnimationEndBeforePosition = 0.0f;
510 private void OnScroll()
512 ScrollEventArgs eventArgs = new ScrollEventArgs(mScrollingChild.CurrentPosition);
513 ScrollEvent?.Invoke(this, eventArgs);
515 CheckPreReachedTargetPosition();
518 private void CheckPreReachedTargetPosition()
520 // Check whether we reached pre-reached target position
522 mScrollingChild.CurrentPosition.Y <= finalTargetPosition + noticeAnimationEndBeforePosition &&
523 mScrollingChild.CurrentPosition.Y >= finalTargetPosition - noticeAnimationEndBeforePosition)
526 readyToNotice = false;
527 OnPreReachedTargetPosition(finalTargetPosition);
532 /// This helps developer who wants to know before scroll is reaching target position.
534 /// <param name="targetPosition">Index of item.</param>
535 /// <since_tizen> 6 </since_tizen>
536 /// This may be public opened in tizen_6.0 after ACR done. Before ACR, need to be hidden as inhouse API
537 [EditorBrowsable(EditorBrowsableState.Never)]
538 protected virtual void OnPreReachedTargetPosition(float targetPosition)
543 private void StopScroll()
545 if (scrollAnimation != null)
547 if (scrollAnimation.State == Animation.States.Playing)
549 Debug.WriteLineIf(LayoutDebugScrollableBase, "StopScroll Animation Playing");
550 scrollAnimation.Stop(Animation.EndActions.Cancel);
551 OnScrollAnimationEnd();
553 scrollAnimation.Clear();
557 // static constructor registers the control type
558 static ScrollableBase()
560 // ViewRegistry registers control type with DALi type registry
561 // also uses introspection to find any properties that need to be registered with type registry
562 CustomViewRegistry.Instance.Register(CreateInstance, typeof(ScrollableBase));
565 internal static CustomView CreateInstance()
567 return new ScrollableBase();
570 private void AnimateChildTo(int duration, float axisPosition)
572 Debug.WriteLineIf(LayoutDebugScrollableBase, "AnimationTo Animation Duration:" + duration + " Destination:" + axisPosition);
573 finalTargetPosition = axisPosition;
575 StopScroll(); // Will replace previous animation so will stop existing one.
577 if (scrollAnimation == null)
579 scrollAnimation = new Animation();
580 scrollAnimation.Finished += ScrollAnimationFinished;
583 scrollAnimation.Duration = duration;
584 scrollAnimation.DefaultAlphaFunction = new AlphaFunction(AlphaFunction.BuiltinFunctions.EaseOutSine);
585 scrollAnimation.AnimateTo(mScrollingChild, (ScrollingDirection == Direction.Horizontal) ? "PositionX" : "PositionY", axisPosition);
587 OnScrollAnimationStart();
588 scrollAnimation.Play();
592 /// Scroll to specific position with or without animation.
594 /// <param name="position">Destination.</param>
595 /// <param name="animate">Scroll with or without animation</param>
596 [EditorBrowsable(EditorBrowsableState.Never)]
597 public void ScrollTo(float position, bool animate)
599 float currentPositionX = mScrollingChild.CurrentPosition.X != 0 ? mScrollingChild.CurrentPosition.X : mScrollingChild.Position.X;
600 float currentPositionY = mScrollingChild.CurrentPosition.Y != 0 ? mScrollingChild.CurrentPosition.Y : mScrollingChild.Position.Y;
601 float delta = ScrollingDirection == Direction.Horizontal ? currentPositionX : currentPositionY;
602 // The argument position is the new pan position. So the new position of ScrollableBase becomes (-position).
603 // To move ScrollableBase's position to (-position), it moves by (-position - currentPosition).
604 delta = -position - delta;
606 ScrollBy(delta, animate);
609 private float BoundScrollPosition(float targetPosition)
611 if (ScrollAvailableArea != null)
613 float minScrollPosition = ScrollAvailableArea.X;
614 float maxScrollPosition = ScrollAvailableArea.Y;
616 targetPosition = Math.Min(-minScrollPosition, targetPosition);
617 targetPosition = Math.Max(-maxScrollPosition, targetPosition);
621 targetPosition = Math.Min(0, targetPosition);
622 targetPosition = Math.Max(-maxScrollDistance, targetPosition);
625 return targetPosition;
628 private void ScrollBy(float displacement, bool animate)
630 if (GetChildCount() == 0 || maxScrollDistance < 0)
635 float childCurrentPosition = (ScrollingDirection == Direction.Horizontal) ? mScrollingChild.PositionX : mScrollingChild.PositionY;
637 Debug.WriteLineIf(LayoutDebugScrollableBase, "ScrollBy childCurrentPosition:" + childCurrentPosition +
638 " displacement:" + displacement,
639 " maxScrollDistance:" + maxScrollDistance);
641 childTargetPosition = childCurrentPosition + displacement; // child current position + gesture displacement
644 Debug.WriteLineIf(LayoutDebugScrollableBase, "ScrollBy currentAxisPosition:" + childCurrentPosition + "childTargetPosition:" + childTargetPosition);
648 // Calculate scroll animaton duration
649 float scrollDistance = Math.Abs(displacement);
650 int duration = (int)((320 * FlickAnimationSpeed) + (scrollDistance * FlickAnimationSpeed));
651 Debug.WriteLineIf(LayoutDebugScrollableBase, "Scroll Animation Duration:" + duration + " Distance:" + scrollDistance);
653 readyToNotice = true;
655 AnimateChildTo(duration, BoundScrollPosition(AdjustTargetPositionOfScrollAnimation(BoundScrollPosition(childTargetPosition))));
659 finalTargetPosition = BoundScrollPosition(childTargetPosition);
661 // Set position of scrolling child without an animation
662 if (ScrollingDirection == Direction.Horizontal)
664 mScrollingChild.PositionX = finalTargetPosition;
668 mScrollingChild.PositionY = finalTargetPosition;
675 /// you can override it to clean-up your own resources.
677 /// <param name="type">DisposeTypes</param>
678 /// <since_tizen> 6 </since_tizen>
679 /// This will be public opened in tizen_5.5 after ACR done. Before ACR, need to be hidden as inhouse API.
680 [EditorBrowsable(EditorBrowsableState.Never)]
681 protected override void Dispose(DisposeTypes type)
688 if (type == DisposeTypes.Explicit)
692 if (mPanGestureDetector != null)
694 mPanGestureDetector.Detected -= OnPanGestureDetected;
695 mPanGestureDetector.Dispose();
696 mPanGestureDetector = null;
699 if (mTapGestureDetector != null)
701 mTapGestureDetector.Detected -= OnTapGestureDetected;
702 mTapGestureDetector.Dispose();
703 mTapGestureDetector = null;
709 private float CalculateDisplacementFromVelocity(float axisVelocity)
711 // Map: flick speed of range (2.0 - 6.0) to flick multiplier of range (0.7 - 1.6)
712 float speedMinimum = FlickThreshold;
713 float speedMaximum = FlickThreshold + 6.0f;
714 float multiplierMinimum = FlickDistanceMultiplierRange.X;
715 float multiplierMaximum = FlickDistanceMultiplierRange.Y;
717 float flickDisplacement = 0.0f;
719 float speed = Math.Min(4.0f, Math.Abs(axisVelocity));
721 Debug.WriteLineIf(LayoutDebugScrollableBase, "ScrollableBase Candidate Flick speed:" + speed);
723 if (speed > FlickThreshold)
725 // Flick length is the length of the ScrollableBase.
726 float flickLength = (ScrollingDirection == Direction.Horizontal) ? CurrentSize.Width : CurrentSize.Height;
728 // Calculate multiplier by mapping speed between the multiplier minimum and maximum.
729 multiplier = ((speed - speedMinimum) / ((speedMaximum - speedMinimum) * (multiplierMaximum - multiplierMinimum))) + multiplierMinimum;
731 // flick displacement is the product of the flick length and multiplier
732 flickDisplacement = ((flickLength * multiplier) * speed) / axisVelocity; // *speed and /velocity to perserve sign.
734 Debug.WriteLineIf(LayoutDebugScrollableBase, "Calculated FlickDisplacement[" + flickDisplacement + "] from speed[" + speed + "] multiplier:"
737 return flickDisplacement;
740 private float CalculateMaximumScrollDistance()
742 int scrollingChildLength = 0;
743 int scrollerLength = 0;
744 if (ScrollingDirection == Direction.Horizontal)
746 Debug.WriteLineIf(LayoutDebugScrollableBase, "Horizontal");
748 scrollingChildLength = (int)mScrollingChild.Layout.MeasuredWidth.Size.AsRoundedValue();
749 scrollerLength = CurrentSize.Width;
753 Debug.WriteLineIf(LayoutDebugScrollableBase, "Vertical");
754 scrollingChildLength = (int)mScrollingChild.Layout.MeasuredHeight.Size.AsRoundedValue();
755 scrollerLength = CurrentSize.Height;
758 Debug.WriteLineIf(LayoutDebugScrollableBase, "ScrollBy maxScrollDistance:" + (scrollingChildLength - scrollerLength) +
759 " parent length:" + scrollerLength +
760 " scrolling child length:" + scrollingChildLength);
762 return Math.Max(scrollingChildLength - scrollerLength, 0);
765 private void PageSnap()
767 Debug.WriteLineIf(LayoutDebugScrollableBase, "PageSnap with pan candidate totalDisplacement:" + totalDisplacementForPan +
768 " currentPage[" + CurrentPage + "]");
770 //Increment current page if total displacement enough to warrant a page change.
771 if (Math.Abs(totalDisplacementForPan) > (mPageWidth * ratioOfScreenWidthToCompleteScroll))
773 if (totalDisplacementForPan < 0)
775 CurrentPage = Math.Min(Math.Max(mScrollingChild.Children.Count - 1, 0), ++CurrentPage);
779 CurrentPage = Math.Max(0, --CurrentPage);
783 // Animate to new page or reposition to current page
784 float destinationX = -(mScrollingChild.Children[CurrentPage].Position.X + mScrollingChild.Children[CurrentPage].CurrentSize.Width / 2 - CurrentSize.Width / 2); // set to middle of current page
785 Debug.WriteLineIf(LayoutDebugScrollableBase, "Snapping to page[" + CurrentPage + "] to:" + destinationX + " from:" + mScrollingChild.PositionX);
786 AnimateChildTo(ScrollDuration, destinationX);
789 private void Flick(float flickDisplacement)
793 if ((flickWhenAnimating && scrolling == true) || (scrolling == false))
795 if (flickDisplacement < 0)
797 CurrentPage = Math.Min(Math.Max(mScrollingChild.Children.Count - 1, 0), CurrentPage + 1);
798 Debug.WriteLineIf(LayoutDebugScrollableBase, "Snap - to page:" + CurrentPage);
802 CurrentPage = Math.Max(0, CurrentPage - 1);
803 Debug.WriteLineIf(LayoutDebugScrollableBase, "Snap + to page:" + CurrentPage);
806 float destinationX = -(mScrollingChild.Children[CurrentPage].Position.X + mScrollingChild.Children[CurrentPage].CurrentSize.Width / 2.0f - CurrentSize.Width / 2.0f); // set to middle of current page
807 Debug.WriteLineIf(LayoutDebugScrollableBase, "Snapping to :" + destinationX);
808 AnimateChildTo(ScrollDuration, destinationX);
813 ScrollBy(flickDisplacement, true); // Animate flickDisplacement.
817 private void OnPanGestureDetected(object source, PanGestureDetector.DetectedEventArgs e)
819 if (e.PanGesture.State == Gesture.StateType.Started)
821 Add(mInterruptTouchingChild);
822 Debug.WriteLineIf(LayoutDebugScrollableBase, "Gesture Start");
823 if (scrolling && !SnapToPage)
827 totalDisplacementForPan = 0.0f;
830 else if (e.PanGesture.State == Gesture.StateType.Continuing)
832 if (ScrollingDirection == Direction.Horizontal)
834 ScrollBy(e.PanGesture.Displacement.X, false);
835 totalDisplacementForPan += e.PanGesture.Displacement.X;
839 ScrollBy(e.PanGesture.Displacement.Y, false);
840 totalDisplacementForPan += e.PanGesture.Displacement.Y;
842 Debug.WriteLineIf(LayoutDebugScrollableBase, "OnPanGestureDetected Continue totalDisplacementForPan:" + totalDisplacementForPan);
844 else if (e.PanGesture.State == Gesture.StateType.Finished)
846 float axisVelocity = (ScrollingDirection == Direction.Horizontal) ? e.PanGesture.Velocity.X : e.PanGesture.Velocity.Y;
847 float flickDisplacement = CalculateDisplacementFromVelocity(axisVelocity);
849 Debug.WriteLineIf(LayoutDebugScrollableBase, "FlickDisplacement:" + flickDisplacement + "TotalDisplacementForPan:" + totalDisplacementForPan);
852 if (flickDisplacement > 0 | flickDisplacement < 0)// Flick detected
854 Flick(flickDisplacement);
858 // End of panning gesture but was not a flick
868 totalDisplacementForPan = 0;
870 Remove(mInterruptTouchingChild);
874 private new void OnTapGestureDetected(object source, TapGestureDetector.DetectedEventArgs e)
876 if (e.TapGesture.Type == Gesture.GestureType.Tap)
878 // Stop scrolling if tap detected (press then relase).
879 // Unless in Pages mode, do not want a page change to stop part way.
880 if (scrolling && !SnapToPage)
887 private void ScrollAnimationFinished(object sender, EventArgs e)
890 CheckPreReachedTargetPosition();
891 OnScrollAnimationEnd();
895 /// Adjust scrolling position by own scrolling rules.
896 /// Override this function when developer wants to change destination of flicking.(e.g. always snap to center of item)
898 /// <since_tizen> 6 </since_tizen>
899 /// This may be public opened in tizen_6.0 after ACR done. Before ACR, need to be hidden as inhouse API
900 [EditorBrowsable(EditorBrowsableState.Never)]
901 protected virtual float AdjustTargetPositionOfScrollAnimation(float position)