-/* Copyright (c) 2019 Samsung Electronics Co., Ltd.
+/* Copyright (c) 2020 Samsung Electronics Co., Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
using Tizen.NUI.BaseComponents;
using System.ComponentModel;
using System.Diagnostics;
+
namespace Tizen.NUI.Components
{
/// <summary>
[EditorBrowsable(EditorBrowsableState.Never)]
public class ScrollableBase : Control
{
- static bool LayoutDebugScrollableBase = false; // Debug flag
+ static bool LayoutDebugScrollableBase = false; // Debug flag
private Direction mScrollingDirection = Direction.Vertical;
+ private bool mScrollEnabled = true;
+ private int mPageWidth = 0;
private class ScrollableBaseCustomLayout : LayoutGroup
{
ScrollableBase scrollableBase = this.Owner as ScrollableBase;
if (scrollableBase)
{
- scrollingDirection = scrollableBase.ScrollingDirection;
+ scrollingDirection = scrollableBase.ScrollingDirection;
}
// measure child, should be a single scrolling child
- foreach( LayoutItem childLayout in LayoutChildren )
+ foreach (LayoutItem childLayout in LayoutChildren)
{
if (childLayout != null)
{
// Get size of child
// Use an Unspecified MeasureSpecification mode so scrolling child is not restricted to it's parents size in Height (for vertical scrolling)
// or Width for horizontal scrolling
- MeasureSpecification unrestrictedMeasureSpec = new MeasureSpecification( heightMeasureSpec.Size, MeasureSpecification.ModeType.Unspecified);
+ MeasureSpecification unrestrictedMeasureSpec = new MeasureSpecification(heightMeasureSpec.Size, MeasureSpecification.ModeType.Unspecified);
if (scrollingDirection == Direction.Vertical)
{
- MeasureChild( childLayout, widthMeasureSpec, unrestrictedMeasureSpec ); // Height unrestricted by parent
+ MeasureChildWithMargins(childLayout, widthMeasureSpec, new LayoutLength(0), unrestrictedMeasureSpec, new LayoutLength(0)); // Height unrestricted by parent
}
else
{
- MeasureChild( childLayout, unrestrictedMeasureSpec, heightMeasureSpec ); // Width unrestricted by parent
+ MeasureChildWithMargins(childLayout, unrestrictedMeasureSpec, new LayoutLength(0), heightMeasureSpec, new LayoutLength(0)); // Width unrestricted by parent
}
float childWidth = childLayout.MeasuredWidth.Size.AsDecimal();
}
- MeasuredSize widthSizeAndState = ResolveSizeAndState(new LayoutLength(totalWidth), widthMeasureSpec, MeasuredSize.StateType.MeasuredSizeOK);
- MeasuredSize heightSizeAndState = ResolveSizeAndState(new LayoutLength(totalHeight), heightMeasureSpec, MeasuredSize.StateType.MeasuredSizeOK);
+ MeasuredSize widthSizeAndState = ResolveSizeAndState(new LayoutLength(totalWidth + Padding.Start + Padding.End), widthMeasureSpec, MeasuredSize.StateType.MeasuredSizeOK);
+ MeasuredSize heightSizeAndState = ResolveSizeAndState(new LayoutLength(totalHeight + Padding.Top + Padding.Bottom), heightMeasureSpec, MeasuredSize.StateType.MeasuredSizeOK);
totalWidth = widthSizeAndState.Size.AsDecimal();
totalHeight = heightSizeAndState.Size.AsDecimal();
// Ensure layout respects it's given minimum size
- totalWidth = Math.Max( totalWidth, SuggestedMinimumWidth.AsDecimal() );
- totalHeight = Math.Max( totalHeight, SuggestedMinimumHeight.AsDecimal() );
+ totalWidth = Math.Max(totalWidth, SuggestedMinimumWidth.AsDecimal());
+ totalHeight = Math.Max(totalHeight, SuggestedMinimumHeight.AsDecimal());
widthSizeAndState.State = childWidthState;
heightSizeAndState.State = childHeightState;
- SetMeasuredDimensions( ResolveSizeAndState( new LayoutLength(totalWidth), widthMeasureSpec, childWidthState ),
- ResolveSizeAndState( new LayoutLength(totalHeight), heightMeasureSpec, childHeightState ) );
+ SetMeasuredDimensions(ResolveSizeAndState(new LayoutLength(totalWidth + Padding.Start + Padding.End), widthMeasureSpec, childWidthState),
+ ResolveSizeAndState(new LayoutLength(totalHeight + Padding.Top + Padding.Bottom), heightMeasureSpec, childHeightState));
+ // Size of ScrollableBase is changed. Change Page width too.
+ scrollableBase.mPageWidth = (int)MeasuredWidth.Size.AsRoundedValue();
}
protected override void OnLayout(bool changed, LayoutLength left, LayoutLength top, LayoutLength right, LayoutLength bottom)
{
- foreach( LayoutItem childLayout in LayoutChildren )
+ foreach (LayoutItem childLayout in LayoutChildren)
{
- if( childLayout != null )
+ if (childLayout != null)
{
LayoutLength childWidth = childLayout.MeasuredWidth.Size;
LayoutLength childHeight = childLayout.MeasuredHeight.Size;
LayoutLength childLeft = new LayoutLength(childPosition.X + childMargin.Start + padding.Start);
LayoutLength childTop = new LayoutLength(childPosition.Y + childMargin.Top + padding.Top);
- childLayout.Layout( childLeft, childTop, childLeft + childWidth, childTop + childHeight );
+ childLayout.Layout(childLeft, childTop, childLeft + childWidth, childTop + childHeight);
}
}
}
}
set
{
- if(value != mScrollingDirection)
+ if (value != mScrollingDirection)
{
mScrollingDirection = value;
mPanGestureDetector.RemoveDirection(value == Direction.Horizontal ? PanGestureDetector.DirectionVertical : PanGestureDetector.DirectionHorizontal);
}
/// <summary>
+ /// [Draft] Enable or disable scrolling.
+ /// </summary>
+ /// This may be public opened in tizen_6.0 after ACR done. Before ACR, need to be hidden as inhouse API
+ [EditorBrowsable(EditorBrowsableState.Never)]
+ public bool ScrollEnabled
+ {
+ get
+ {
+ return mScrollEnabled;
+ }
+ set
+ {
+ if (value != mScrollEnabled)
+ {
+ mScrollEnabled = value;
+ if (mScrollEnabled)
+ {
+ mPanGestureDetector.Detected += OnPanGestureDetected;
+ mTapGestureDetector.Detected += OnTapGestureDetected;
+ }
+ else
+ {
+ mPanGestureDetector.Detected -= OnPanGestureDetected;
+ mTapGestureDetector.Detected -= OnTapGestureDetected;
+ }
+ }
+ }
+ }
+
+ /// <summary>
/// [Draft] Pages mode, enables moving to the next or return to current page depending on pan displacement.
/// Default is false.
/// </summary>
public bool SnapToPage { set; get; } = false;
/// <summary>
- /// [Draft] Pages mode, Number of pages.
+ /// [Draft] Get current page.
+ /// Working propery with SnapToPage property.
/// </summary>
/// This may be public opened in tizen_6.0 after ACR done. Before ACR, need to be hidden as inhouse API
[EditorBrowsable(EditorBrowsableState.Never)]
- public int NumberOfPages { set; get; } = 1;
+ public int CurrentPage { get; private set; } = 0;
/// <summary>
/// [Draft] Duration of scroll animation.
/// </summary>
/// This may be public opened in tizen_6.0 after ACR done. Before ACR, need to be hidden as inhouse API
[EditorBrowsable(EditorBrowsableState.Never)]
- public int ScrollDuration { set; get; } = 125;
+ public int ScrollDuration { set; get; } = 125;
/// <summary>
- /// [Draft] Width of the Page.
+ /// [Draft] Scroll Available area.
/// </summary>
/// This may be public opened in tizen_6.0 after ACR done. Before ACR, need to be hidden as inhouse API
[EditorBrowsable(EditorBrowsableState.Never)]
- public int PageWidth { set; get; } = 1080; // Temporary use for prototype, should get ScrollableBase width
+ public Vector2 ScrollAvailableArea { set; get; }
/// <summary>
/// ScrollEventArgs is a class to record scroll event arguments which will sent to user.
/// </summary>
- /// <since_tizen> 6 </since_tizen>
/// This may be public opened in tizen_6.0 after ACR done. Before ACR, need to be hidden as inhouse API
[EditorBrowsable(EditorBrowsableState.Never)]
public class ScrollEventArgs : EventArgs
{
+ Position position;
+
+ /// <summary>
+ /// Default constructor.
+ /// </summary>
+ /// <param name="position">Current scroll position</param>
+ /// <since_tizen> 6 </since_tizen>
+ /// This may be public opened in tizen_6.0 after ACR done. Before ACR, need to be hidden as inhouse API
+ public ScrollEventArgs(Position position)
+ {
+ this.position = position;
+ }
+
+ /// <summary>
+ /// [Draft] Current scroll position.
+ /// </summary>
+ /// This may be public opened in tizen_6.0 after ACR done. Before ACR, need to be hidden as inhouse API
+ [EditorBrowsable(EditorBrowsableState.Never)]
+ public Position Position
+ {
+ get
+ {
+ return position;
+ }
+ }
}
/// <summary>
- /// An event emitted when the scrolling starts, user can subscribe or unsubscribe to this event handler.<br />
+ /// An event emitted when user starts dragging ScrollableBase, user can subscribe or unsubscribe to this event handler.<br />
/// </summary>
- /// <since_tizen> 6 </since_tizen>
/// This may be public opened in tizen_6.0 after ACR done. Before ACR, need to be hidden as inhouse API
[EditorBrowsable(EditorBrowsableState.Never)]
- public event EventHandler<ScrollEventArgs> ScrollStartedEvent;
+ public event EventHandler<ScrollEventArgs> ScrollDragStartEvent;
/// <summary>
- /// An event emitted when the scrolling ends, user can subscribe or unsubscribe to this event handler.<br />
+ /// An event emitted when user stops dragging ScrollableBase, user can subscribe or unsubscribe to this event handler.<br />
/// </summary>
- /// <since_tizen> 6 </since_tizen>
/// This may be public opened in tizen_6.0 after ACR done. Before ACR, need to be hidden as inhouse API
[EditorBrowsable(EditorBrowsableState.Never)]
- public event EventHandler<ScrollEventArgs> ScrollEndedEvent;
+ public event EventHandler<ScrollEventArgs> ScrollDragEndEvent;
+
+ /// <summary>
+ /// An event emitted when the scrolling slide animation starts, user can subscribe or unsubscribe to this event handler.<br />
+ /// </summary>
+ /// This may be public opened in tizen_6.0 after ACR done. Before ACR, need to be hidden as inhouse API
+ [EditorBrowsable(EditorBrowsableState.Never)]
+ public event EventHandler<ScrollEventArgs> ScrollAnimationStartEvent;
+
+ /// <summary>
+ /// An event emitted when the scrolling slide animation ends, user can subscribe or unsubscribe to this event handler.<br />
+ /// </summary>
+ /// This may be public opened in tizen_6.0 after ACR done. Before ACR, need to be hidden as inhouse API
+ [EditorBrowsable(EditorBrowsableState.Never)]
+ public event EventHandler<ScrollEventArgs> ScrollAnimationEndEvent;
+
+
+ /// <summary>
+ /// An event emitted when scrolling, user can subscribe or unsubscribe to this event handler.<br />
+ /// </summary>
+ /// This may be public opened in tizen_6.0 after ACR done. Before ACR, need to be hidden as inhouse API
+ [EditorBrowsable(EditorBrowsableState.Never)]
+ public event EventHandler<ScrollEventArgs> ScrollEvent;
+
+
+ /// <summary>
+ /// Scrollbar for ScrollableBase.<br />
+ /// </summary>
+ /// This may be public opened in tizen_6.0 after ACR done. Before ACR, need to be hidden as inhouse API
+ [EditorBrowsable(EditorBrowsableState.Never)]
+ public ScrollbarBase Scrollbar
+ {
+ get
+ {
+ return scrollBar;
+ }
+ set
+ {
+ if (scrollBar)
+ {
+ scrollBar.Unparent();
+ }
+
+ scrollBar = value;
+ scrollBar.Name = "ScrollBar";
+ Add(scrollBar);
+
+ if (hideScrollbar)
+ {
+ scrollBar.Hide();
+ }
+ else
+ {
+ scrollBar.Show();
+ }
+
+ SetScrollbar();
+ }
+ }
+
+ /// <summary>
+ /// [Draft] Always hide Scrollbar.
+ /// </summary>
+ /// This may be public opened in tizen_6.0 after ACR done. Before ACR, need to be hidden as inhouse API
+ [EditorBrowsable(EditorBrowsableState.Never)]
+ public bool HideScrollBar
+ {
+ get
+ {
+ return hideScrollbar;
+ }
+ set
+ {
+ hideScrollbar = value;
+
+ if (scrollBar)
+ {
+ if (value)
+ {
+ scrollBar.Hide();
+ }
+ else
+ {
+ scrollBar.Show();
+ }
+ }
+ }
+ }
+
+ private bool hideScrollbar = true;
private Animation scrollAnimation;
private float maxScrollDistance;
private float childTargetPosition = 0.0f;
private PanGestureDetector mPanGestureDetector;
private TapGestureDetector mTapGestureDetector;
private View mScrollingChild;
- private float multiplier =1.0f;
+ private View mInterruptTouchingChild;
+ private ScrollbarBase scrollBar;
+ private float multiplier = 1.0f;
private bool scrolling = false;
private float ratioOfScreenWidthToCompleteScroll = 0.5f;
private float totalDisplacementForPan = 0.0f;
+ private Size previousContainerSize = new Size();
// If false then can only flick pages when the current animation/scroll as ended.
private bool flickWhenAnimating = false;
+ private PropertyNotification propertyNotification;
- private int currentPage = 0;
+ // Let's consider more whether this needs to be set as protected.
+ private float finalTargetPosition;
/// <summary>
/// [Draft] Constructor
/// </summary>
- /// <since_tizen> 6 </since_tizen>
/// This may be public opened in tizen_6.0 after ACR done. Before ACR, need to be hidden as inhouse API
[EditorBrowsable(EditorBrowsableState.Never)]
public ScrollableBase() : base()
{
mPanGestureDetector = new PanGestureDetector();
mPanGestureDetector.Attach(this);
+ mPanGestureDetector.AddDirection(PanGestureDetector.DirectionVertical);
mPanGestureDetector.Detected += OnPanGestureDetected;
mTapGestureDetector = new TapGestureDetector();
mTapGestureDetector.Attach(this);
mTapGestureDetector.Detected += OnTapGestureDetected;
- ClippingMode = ClippingModeType.ClipToBoundingBox;
+ ClippingMode = ClippingModeType.ClipChildren;
mScrollingChild = new View();
+ mScrollingChild.Name = "DefaultScrollingChild";
+
+ //Interrupt touching when panning is started;
+ mInterruptTouchingChild = new View()
+ {
+ Name = "InterruptTouchingChild",
+ Size = new Size(Window.Instance.WindowSize),
+ BackgroundColor = Color.Transparent,
+ };
+
+ mInterruptTouchingChild.TouchEvent += OnIterruptTouchingChildTouched;
Layout = new ScrollableBaseCustomLayout();
+
+ Scrollbar = new Scrollbar();
+ }
+
+ private bool OnIterruptTouchingChildTouched(object source, View.TouchEventArgs args)
+ {
+ return true;
+ }
+
+ private void OnPropertyChanged(object source, PropertyNotification.NotifyEventArgs args)
+ {
+ OnScroll();
}
/// <summary>
/// Called after a child has been added to the owning view.
/// </summary>
/// <param name="view">The child which has been added.</param>
- /// <since_tizen> 6 </since_tizen>
/// This may be public opened in tizen_6.0 after ACR done. Before ACR, need to be hidden as inhouse API
[EditorBrowsable(EditorBrowsableState.Never)]
public override void OnChildAdd(View view)
{
- mScrollingChild = view;
+ if (view.Name != "InterruptTouchingChild" && view.Name != "ScrollBar")
{
- if (Children.Count > 1)
- Log.Error("ScrollableBase", $"Only 1 child should be added to ScrollableBase.");
+ if (mScrollingChild.Name != "DefaultScrollingChild")
+ {
+ propertyNotification.Notified -= OnPropertyChanged;
+ mScrollingChild.RemovePropertyNotification(propertyNotification);
+ mScrollingChild.Relayout -= OnScrollingChildRelayout;
+ }
+
+ mScrollingChild = view;
+ mScrollingChild.Layout.SetPositionByLayout = false;
+ propertyNotification = mScrollingChild?.AddPropertyNotification("position", PropertyCondition.Step(1.0f));
+ propertyNotification.Notified += OnPropertyChanged;
+ mScrollingChild.Relayout += OnScrollingChildRelayout;
+ mScrollingChild.LowerToBottom();
}
}
/// Called after a child has been removed from the owning view.
/// </summary>
/// <param name="view">The child which has been removed.</param>
- /// <since_tizen> 6 </since_tizen>
/// This may be public opened in tizen_6.0 after ACR done. Before ACR, need to be hidden as inhouse API
[EditorBrowsable(EditorBrowsableState.Never)]
public override void OnChildRemove(View view)
{
- mScrollingChild = new View();
+ if (view.Name != "InterruptTouchingChild" && view.Name != "ScrollBar")
+ {
+ propertyNotification.Notified -= OnPropertyChanged;
+ mScrollingChild.RemovePropertyNotification(propertyNotification);
+ mScrollingChild.Relayout -= OnScrollingChildRelayout;
+
+ mScrollingChild.Layout.SetPositionByLayout = true;
+ mScrollingChild = new View();
+ }
}
- private void OnScrollStart()
+ private void OnScrollingChildRelayout(object source, EventArgs args)
{
- ScrollEventArgs eventArgs = new ScrollEventArgs();
- ScrollStartedEvent?.Invoke(this, eventArgs);
+ // Size is changed. Calculate maxScrollDistance.
+ bool isSizeChanged = previousContainerSize.Width != mScrollingChild.Size.Width || previousContainerSize.Height != mScrollingChild.Size.Height;
+
+ if (isSizeChanged)
+ {
+ maxScrollDistance = CalculateMaximumScrollDistance();
+ SetScrollbar();
+ }
+
+ previousContainerSize = mScrollingChild.Size;
+ }
+
+ /// <summary>
+ /// The composition of a Scrollbar can vary depending on how you use ScrollableBase.
+ /// Set the composition that will go into the ScrollableBase according to your ScrollableBase.
+ /// </summary>
+ /// This may be public opened in tizen_6.0 after ACR done. Before ACR, need to be hidden as inhouse API
+ [EditorBrowsable(EditorBrowsableState.Never)]
+ protected virtual void SetScrollbar()
+ {
+ if (Scrollbar)
+ {
+ bool isHorizontal = ScrollingDirection == Direction.Horizontal;
+ float contentLength = isHorizontal ? mScrollingChild.Size.Width : mScrollingChild.Size.Height;
+ float viewportLength = isHorizontal ? Size.Width : Size.Height;
+ float currentPosition = isHorizontal ? mScrollingChild.CurrentPosition.X : mScrollingChild.CurrentPosition.Y;
+ Scrollbar.Initialize(contentLength, viewportLength, currentPosition, isHorizontal);
+ }
+ }
+
+ /// <summary>
+ /// Scrolls to the item at the specified index.
+ /// </summary>
+ /// <param name="index">Index of item.</param>
+ /// This may be public opened in tizen_6.0 after ACR done. Before ACR, need to be hidden as inhouse API
+ [EditorBrowsable(EditorBrowsableState.Never)]
+ public void ScrollToIndex(int index)
+ {
+ if (mScrollingChild.ChildCount - 1 < index || index < 0)
+ {
+ return;
+ }
+
+ if (SnapToPage)
+ {
+ CurrentPage = index;
+ }
+
+ float targetPosition = Math.Min(ScrollingDirection == Direction.Vertical ? mScrollingChild.Children[index].Position.Y : mScrollingChild.Children[index].Position.X, maxScrollDistance);
+ AnimateChildTo(ScrollDuration, -targetPosition);
+ }
+
+ private void OnScrollDragStart()
+ {
+ ScrollEventArgs eventArgs = new ScrollEventArgs(mScrollingChild.CurrentPosition);
+ ScrollDragStartEvent?.Invoke(this, eventArgs);
+ }
+
+ private void OnScrollDragEnd()
+ {
+ ScrollEventArgs eventArgs = new ScrollEventArgs(mScrollingChild.CurrentPosition);
+ ScrollDragEndEvent?.Invoke(this, eventArgs);
+ }
+
+ private void OnScrollAnimationStart()
+ {
+ ScrollEventArgs eventArgs = new ScrollEventArgs(mScrollingChild.CurrentPosition);
+ ScrollAnimationStartEvent?.Invoke(this, eventArgs);
+ }
+
+ private void OnScrollAnimationEnd()
+ {
+ ScrollEventArgs eventArgs = new ScrollEventArgs(mScrollingChild.CurrentPosition);
+ ScrollAnimationEndEvent?.Invoke(this, eventArgs);
+ }
+
+ private bool readyToNotice = false;
+
+ private float noticeAnimationEndBeforePosition = 0.0f;
+ // Let's consider more whether this needs to be set as protected.
+ public float NoticeAnimationEndBeforePosition { get => noticeAnimationEndBeforePosition; set => noticeAnimationEndBeforePosition = value; }
+
+ private void OnScroll()
+ {
+ ScrollEventArgs eventArgs = new ScrollEventArgs(mScrollingChild.CurrentPosition);
+ ScrollEvent?.Invoke(this, eventArgs);
+
+ bool isHorizontal = ScrollingDirection == Direction.Horizontal;
+ float contentLength = isHorizontal ? mScrollingChild.Size.Width : mScrollingChild.Size.Height;
+ float currentPosition = isHorizontal ? mScrollingChild.CurrentPosition.X : mScrollingChild.CurrentPosition.Y;
+
+ scrollBar.Update(contentLength, Math.Abs(currentPosition));
+ CheckPreReachedTargetPosition();
+ }
+
+ private void CheckPreReachedTargetPosition()
+ {
+ // Check whether we reached pre-reached target position
+ if (readyToNotice &&
+ mScrollingChild.CurrentPosition.Y <= finalTargetPosition + NoticeAnimationEndBeforePosition &&
+ mScrollingChild.CurrentPosition.Y >= finalTargetPosition - NoticeAnimationEndBeforePosition)
+ {
+ //Notice first
+ readyToNotice = false;
+ OnPreReachedTargetPosition(finalTargetPosition);
+ }
}
- private void OnScrollEnd()
+ /// <summary>
+ /// This helps developer who wants to know before scroll is reaching target position.
+ /// </summary>
+ /// <param name="targetPosition">Index of item.</param>
+ /// This may be public opened in tizen_6.0 after ACR done. Before ACR, need to be hidden as inhouse API
+ [EditorBrowsable(EditorBrowsableState.Never)]
+ protected virtual void OnPreReachedTargetPosition(float targetPosition)
{
- ScrollEventArgs eventArgs = new ScrollEventArgs();
- ScrollEndedEvent?.Invoke(this, eventArgs);
+
}
private void StopScroll()
{
Debug.WriteLineIf(LayoutDebugScrollableBase, "StopScroll Animation Playing");
scrollAnimation.Stop(Animation.EndActions.Cancel);
- OnScrollEnd();
+ OnScrollAnimationEnd();
}
scrollAnimation.Clear();
}
private void AnimateChildTo(int duration, float axisPosition)
{
Debug.WriteLineIf(LayoutDebugScrollableBase, "AnimationTo Animation Duration:" + duration + " Destination:" + axisPosition);
+ finalTargetPosition = axisPosition;
StopScroll(); // Will replace previous animation so will stop existing one.
scrollAnimation.DefaultAlphaFunction = new AlphaFunction(AlphaFunction.BuiltinFunctions.EaseOutSine);
scrollAnimation.AnimateTo(mScrollingChild, (ScrollingDirection == Direction.Horizontal) ? "PositionX" : "PositionY", axisPosition);
scrolling = true;
- OnScrollStart();
+ OnScrollAnimationStart();
scrollAnimation.Play();
}
+ /// <summary>
+ /// Scroll to specific position with or without animation.
+ /// </summary>
+ /// <param name="position">Destination.</param>
+ /// <param name="animate">Scroll with or without animation</param>
+ [EditorBrowsable(EditorBrowsableState.Never)]
+ public void ScrollTo(float position, bool animate)
+ {
+ float currentPositionX = mScrollingChild.CurrentPosition.X != 0 ? mScrollingChild.CurrentPosition.X : mScrollingChild.Position.X;
+ float currentPositionY = mScrollingChild.CurrentPosition.Y != 0 ? mScrollingChild.CurrentPosition.Y : mScrollingChild.Position.Y;
+ float delta = ScrollingDirection == Direction.Horizontal ? currentPositionX : currentPositionY;
+ // The argument position is the new pan position. So the new position of ScrollableBase becomes (-position).
+ // To move ScrollableBase's position to (-position), it moves by (-position - currentPosition).
+ delta = -position - delta;
+
+ ScrollBy(delta, animate);
+ }
+
+ private float BoundScrollPosition(float targetPosition)
+ {
+ if (ScrollAvailableArea != null)
+ {
+ float minScrollPosition = ScrollAvailableArea.X;
+ float maxScrollPosition = ScrollAvailableArea.Y;
+
+ targetPosition = Math.Min(-minScrollPosition, targetPosition);
+ targetPosition = Math.Max(-maxScrollPosition, targetPosition);
+ }
+ else
+ {
+ targetPosition = Math.Min(0, targetPosition);
+ targetPosition = Math.Max(-maxScrollDistance, targetPosition);
+ }
+
+ return targetPosition;
+ }
+
private void ScrollBy(float displacement, bool animate)
{
- if (GetChildCount() == 0 || displacement == 0)
+ if (GetChildCount() == 0 || maxScrollDistance < 0)
{
return;
}
- float childCurrentPosition = (ScrollingDirection == Direction.Horizontal) ? mScrollingChild.PositionX: mScrollingChild.PositionY;
+ float childCurrentPosition = (ScrollingDirection == Direction.Horizontal) ? mScrollingChild.PositionX : mScrollingChild.PositionY;
Debug.WriteLineIf(LayoutDebugScrollableBase, "ScrollBy childCurrentPosition:" + childCurrentPosition +
" displacement:" + displacement,
- " maxScrollDistance:" + maxScrollDistance );
+ " maxScrollDistance:" + maxScrollDistance);
childTargetPosition = childCurrentPosition + displacement; // child current position + gesture displacement
- childTargetPosition = Math.Min(0,childTargetPosition);
- childTargetPosition = Math.Max(-maxScrollDistance,childTargetPosition);
- Debug.WriteLineIf( LayoutDebugScrollableBase, "ScrollBy currentAxisPosition:" + childCurrentPosition + "childTargetPosition:" + childTargetPosition);
+
+ Debug.WriteLineIf(LayoutDebugScrollableBase, "ScrollBy currentAxisPosition:" + childCurrentPosition + "childTargetPosition:" + childTargetPosition);
if (animate)
{
// Calculate scroll animaton duration
- float scrollDistance = 0.0f;
- if (childCurrentPosition < childTargetPosition)
- {
- scrollDistance = Math.Abs(childCurrentPosition + childTargetPosition);
- }
- else
- {
- scrollDistance = Math.Abs(childCurrentPosition - childTargetPosition);
- }
-
- int duration = (int)((320*FlickAnimationSpeed) + (scrollDistance * FlickAnimationSpeed));
+ float scrollDistance = Math.Abs(displacement);
+ int duration = (int)((320 * FlickAnimationSpeed) + (scrollDistance * FlickAnimationSpeed));
Debug.WriteLineIf(LayoutDebugScrollableBase, "Scroll Animation Duration:" + duration + " Distance:" + scrollDistance);
- AnimateChildTo(duration, childTargetPosition);
+ readyToNotice = true;
+
+ AnimateChildTo(duration, BoundScrollPosition(AdjustTargetPositionOfScrollAnimation(BoundScrollPosition(childTargetPosition))));
}
else
{
+ finalTargetPosition = BoundScrollPosition(childTargetPosition);
+
// Set position of scrolling child without an animation
if (ScrollingDirection == Direction.Horizontal)
{
- mScrollingChild.PositionX = childTargetPosition;
+ mScrollingChild.PositionX = finalTargetPosition;
}
else
{
- mScrollingChild.PositionY = childTargetPosition;
+ mScrollingChild.PositionY = finalTargetPosition;
}
+
}
}
/// you can override it to clean-up your own resources.
/// </summary>
/// <param name="type">DisposeTypes</param>
- /// <since_tizen> 6 </since_tizen>
/// This will be public opened in tizen_5.5 after ACR done. Before ACR, need to be hidden as inhouse API.
[EditorBrowsable(EditorBrowsableState.Never)]
protected override void Dispose(DisposeTypes type)
float flickDisplacement = 0.0f;
- float speed = Math.Min(4.0f,Math.Abs(axisVelocity));
+ float speed = Math.Min(4.0f, Math.Abs(axisVelocity));
Debug.WriteLineIf(LayoutDebugScrollableBase, "ScrollableBase Candidate Flick speed:" + speed);
if (speed > FlickThreshold)
{
// Flick length is the length of the ScrollableBase.
- float flickLength = (ScrollingDirection == Direction.Horizontal) ?CurrentSize.Width:CurrentSize.Height;
+ float flickLength = (ScrollingDirection == Direction.Horizontal) ? CurrentSize.Width : CurrentSize.Height;
// Calculate multiplier by mapping speed between the multiplier minimum and maximum.
- multiplier =( (speed - speedMinimum) / ( (speedMaximum - speedMinimum) * (multiplierMaximum - multiplierMinimum) ) )+ multiplierMinimum;
+ multiplier = ((speed - speedMinimum) / ((speedMaximum - speedMinimum) * (multiplierMaximum - multiplierMinimum))) + multiplierMinimum;
// flick displacement is the product of the flick length and multiplier
flickDisplacement = ((flickLength * multiplier) * speed) / axisVelocity; // *speed and /velocity to perserve sign.
- Debug.WriteLineIf(LayoutDebugScrollableBase, "Calculated FlickDisplacement[" + flickDisplacement +"] from speed[" + speed + "] multiplier:"
+ Debug.WriteLineIf(LayoutDebugScrollableBase, "Calculated FlickDisplacement[" + flickDisplacement + "] from speed[" + speed + "] multiplier:"
+ multiplier);
}
return flickDisplacement;
private float CalculateMaximumScrollDistance()
{
- int scrollingChildLength = 0;
- int scrollerLength = 0;
+ float scrollingChildLength = 0;
+ float scrollerLength = 0;
if (ScrollingDirection == Direction.Horizontal)
{
Debug.WriteLineIf(LayoutDebugScrollableBase, "Horizontal");
- scrollingChildLength = (int)mScrollingChild.Layout.MeasuredWidth.Size.AsRoundedValue();
- scrollerLength = CurrentSize.Width;
+ scrollingChildLength = mScrollingChild.Size.Width;
+ scrollerLength = Size.Width;
}
else
{
Debug.WriteLineIf(LayoutDebugScrollableBase, "Vertical");
- scrollingChildLength = (int)mScrollingChild.Layout.MeasuredHeight.Size.AsRoundedValue();
- scrollerLength = CurrentSize.Height;
+ scrollingChildLength = mScrollingChild.Size.Height;
+ scrollerLength = Size.Height;
}
Debug.WriteLineIf(LayoutDebugScrollableBase, "ScrollBy maxScrollDistance:" + (scrollingChildLength - scrollerLength) +
" parent length:" + scrollerLength +
" scrolling child length:" + scrollingChildLength);
- return scrollingChildLength - scrollerLength;
+ return Math.Max(scrollingChildLength - scrollerLength, 0);
}
private void PageSnap()
{
Debug.WriteLineIf(LayoutDebugScrollableBase, "PageSnap with pan candidate totalDisplacement:" + totalDisplacementForPan +
- " currentPage[" + currentPage + "]" );
+ " currentPage[" + CurrentPage + "]");
//Increment current page if total displacement enough to warrant a page change.
- if (Math.Abs(totalDisplacementForPan) > (PageWidth * ratioOfScreenWidthToCompleteScroll))
+ if (Math.Abs(totalDisplacementForPan) > (mPageWidth * ratioOfScreenWidthToCompleteScroll))
{
if (totalDisplacementForPan < 0)
{
- currentPage = Math.Min(NumberOfPages-1, ++currentPage);
+ CurrentPage = Math.Min(Math.Max(mScrollingChild.Children.Count - 1, 0), ++CurrentPage);
}
else
{
- currentPage = Math.Max(0, --currentPage);
+ CurrentPage = Math.Max(0, --CurrentPage);
}
}
// Animate to new page or reposition to current page
- int destinationX = -(currentPage * PageWidth);
- Debug.WriteLineIf(LayoutDebugScrollableBase, "Snapping to page[" + currentPage + "] to:"+ destinationX + " from:" + mScrollingChild.PositionX);
+ float destinationX = -(mScrollingChild.Children[CurrentPage].Position.X + mScrollingChild.Children[CurrentPage].CurrentSize.Width / 2 - CurrentSize.Width / 2); // set to middle of current page
+ Debug.WriteLineIf(LayoutDebugScrollableBase, "Snapping to page[" + CurrentPage + "] to:" + destinationX + " from:" + mScrollingChild.PositionX);
AnimateChildTo(ScrollDuration, destinationX);
}
private void Flick(float flickDisplacement)
{
- if (SnapToPage)
- {
- if ( ( flickWhenAnimating && scrolling == true) || ( scrolling == false) )
- {
- if(flickDisplacement < 0)
- {
- currentPage = Math.Min(NumberOfPages - 1, currentPage + 1);
- Debug.WriteLineIf(LayoutDebugScrollableBase, "Snap - to page:" + currentPage);
- }
- else
- {
- currentPage = Math.Max(0, currentPage - 1);
- Debug.WriteLineIf(LayoutDebugScrollableBase, "Snap + to page:" + currentPage);
- }
- float targetPosition = -(currentPage* PageWidth); // page size
- Debug.WriteLineIf(LayoutDebugScrollableBase, "Snapping to :" + targetPosition);
- AnimateChildTo(ScrollDuration,targetPosition);
- }
- }
- else
- {
- ScrollBy(flickDisplacement, true); // Animate flickDisplacement.
- }
+ if (SnapToPage)
+ {
+ if ((flickWhenAnimating && scrolling == true) || (scrolling == false))
+ {
+ if (flickDisplacement < 0)
+ {
+ CurrentPage = Math.Min(Math.Max(mScrollingChild.Children.Count - 1, 0), CurrentPage + 1);
+ Debug.WriteLineIf(LayoutDebugScrollableBase, "Snap - to page:" + CurrentPage);
+ }
+ else
+ {
+ CurrentPage = Math.Max(0, CurrentPage - 1);
+ Debug.WriteLineIf(LayoutDebugScrollableBase, "Snap + to page:" + CurrentPage);
+ }
+
+ float destinationX = -(mScrollingChild.Children[CurrentPage].Position.X + mScrollingChild.Children[CurrentPage].CurrentSize.Width / 2.0f - CurrentSize.Width / 2.0f); // set to middle of current page
+ Debug.WriteLineIf(LayoutDebugScrollableBase, "Snapping to :" + destinationX);
+ AnimateChildTo(ScrollDuration, destinationX);
+ }
+ }
+ else
+ {
+ ScrollBy(flickDisplacement, true); // Animate flickDisplacement.
+ }
}
private void OnPanGestureDetected(object source, PanGestureDetector.DetectedEventArgs e)
{
if (e.PanGesture.State == Gesture.StateType.Started)
{
+ Add(mInterruptTouchingChild);
Debug.WriteLineIf(LayoutDebugScrollableBase, "Gesture Start");
if (scrolling && !SnapToPage)
{
StopScroll();
}
- maxScrollDistance = CalculateMaximumScrollDistance();
totalDisplacementForPan = 0.0f;
+ OnScrollDragStart();
}
else if (e.PanGesture.State == Gesture.StateType.Continuing)
{
totalDisplacementForPan += e.PanGesture.Displacement.Y;
}
Debug.WriteLineIf(LayoutDebugScrollableBase, "OnPanGestureDetected Continue totalDisplacementForPan:" + totalDisplacementForPan);
-
}
else if (e.PanGesture.State == Gesture.StateType.Finished)
{
float flickDisplacement = CalculateDisplacementFromVelocity(axisVelocity);
Debug.WriteLineIf(LayoutDebugScrollableBase, "FlickDisplacement:" + flickDisplacement + "TotalDisplacementForPan:" + totalDisplacementForPan);
+ OnScrollDragEnd();
if (flickDisplacement > 0 | flickDisplacement < 0)// Flick detected
{
{
PageSnap();
}
+ else
+ {
+ ScrollBy(0, true);
+ }
}
totalDisplacementForPan = 0;
+
+ Remove(mInterruptTouchingChild);
}
}
{
// Stop scrolling if tap detected (press then relase).
// Unless in Pages mode, do not want a page change to stop part way.
- if(scrolling && !SnapToPage)
+ if (scrolling && !SnapToPage)
{
StopScroll();
}
private void ScrollAnimationFinished(object sender, EventArgs e)
{
scrolling = false;
- OnScrollEnd();
+ CheckPreReachedTargetPosition();
+ OnScrollAnimationEnd();
+ }
+
+ /// <summary>
+ /// Adjust scrolling position by own scrolling rules.
+ /// Override this function when developer wants to change destination of flicking.(e.g. always snap to center of item)
+ /// </summary>
+ /// This may be public opened in tizen_6.0 after ACR done. Before ACR, need to be hidden as inhouse API
+ [EditorBrowsable(EditorBrowsableState.Never)]
+ protected virtual float AdjustTargetPositionOfScrollAnimation(float position)
+ {
+ return position;
}
}