Skip to content

Commit

Permalink
Scrollable controls expose a new property IScrollInfo.NumberOfVisible…
Browse files Browse the repository at this point in the history
…Lines that will be used for limiting MouseWheelScrolling
  • Loading branch information
morpheusxx committed Apr 14, 2013
1 parent b0d95b3 commit fd2def3
Show file tree
Hide file tree
Showing 7 changed files with 65 additions and 19 deletions.
10 changes: 10 additions & 0 deletions MediaPortal/Source/UI/SkinEngine/Controls/Panels/StackPanel.cs
Expand Up @@ -824,6 +824,16 @@ public virtual bool IsViewPortAtRight
get { return Orientation == Orientation.Vertical || _actualLastVisibleChildIndex == GetVisibleChildren().Count - 1; }
}

public int NumberOfVisibleLines
{
get
{
int numLines = 0;
CalcHelper.Bound(ref numLines, 0, _actualLastVisibleChildIndex - _actualFirstVisibleChildIndex);
return numLines > 0 ? numLines : 0;
}
}

#endregion
}
}
Expand Up @@ -568,6 +568,11 @@ public bool IsViewPortAtRight
get { return _scrollIndexX + _actualNumVisibleCols == _actualColumns; }
}

public int NumberOfVisibleLines
{
get { return _actualNumVisibleRows; }
}

#endregion
}
}
Expand Up @@ -110,7 +110,7 @@ public static LineMeasurement Create()
protected IList<LineMeasurement> _arrangedLines = new List<LineMeasurement>();
protected int _actualFirstVisibleLineIndex = 0;
protected int _actualLastVisibleLineIndex = -1;

#endregion

#region Ctor
Expand Down Expand Up @@ -990,6 +990,11 @@ public virtual bool IsViewPortAtRight
get { return Orientation == Orientation.Horizontal || _actualLastVisibleLineIndex == GetVisibleChildren().Count - 1; }
}

public int NumberOfVisibleLines
{
get { return _actualLastVisibleLineIndex - _actualFirstVisibleLineIndex + 1; }
}

#endregion
}
}
Expand Up @@ -22,6 +22,8 @@

#endregion

using System.Windows.Forms;

namespace MediaPortal.UI.SkinEngine.Controls.Visuals
{
/// <summary>
Expand Down Expand Up @@ -113,5 +115,12 @@ public interface IScrollInfo
/// Returns the information if the viewport is at the right side of the available area.
/// </summary>
bool IsViewPortAtRight { get; }

/// <summary>
/// Returns the number of lines that can be shown inside the viewport. This value can be used for calculating
/// the number of lines to be scrolled when using mouse wheel. If this value is <c>0</c>, the system default
/// scrolling will be used (usually scrolling by <seealso cref="SystemInformation.MouseWheelScrollLines"/>).
/// </summary>
int NumberOfVisibleLines { get; }
}
}
Expand Up @@ -379,7 +379,7 @@ public bool IsViewPortAtTop
get
{
IScrollInfo si = _itemsHostPanel as IScrollInfo;
return si == null ? true : si.IsViewPortAtTop;
return si == null || si.IsViewPortAtTop;
}
}

Expand Down Expand Up @@ -410,6 +410,15 @@ public bool IsViewPortAtRight
}
}

public int NumberOfVisibleLines
{
get
{
IScrollInfo si = _itemsHostPanel as IScrollInfo;
return si == null ? 0 : si.NumberOfVisibleLines;
}
}

#endregion
}
}
Expand Up @@ -154,7 +154,7 @@ public override void BringIntoView(UIElement element, RectangleF elementBounds)
if (IsHorzCentering)
differenceX = CalculateCenteredScrollPos(elementBounds.X, elementBounds.Width, ActualPosition.X, ActualWidth);
else if (_doScroll)
differenceX = CalculateVisibleScrollDifference(elementBounds.X, elementBounds.Width, ActualPosition.X, ActualWidth);
differenceX = CalculateVisibleScrollDifference(elementBounds.X, elementBounds.Width, ActualPosition.X, ActualWidth);

if (IsVertCentering)
differenceY = CalculateCenteredScrollPos(elementBounds.Y, elementBounds.Height, ActualPosition.Y, ActualHeight) - _actualScrollOffsetY;
Expand All @@ -173,7 +173,7 @@ protected float CalculateVisibleScrollDifference(double elementPos, double eleme
{
double difference = 0.0f;
if (elementPos + elementSize > actualPos + actualSize)
difference = - (elementPos + elementSize - actualPos - actualSize);
difference = -(elementPos + elementSize - actualPos - actualSize);
if (elementPos + difference < actualPos)
difference = actualPos - elementPos;
return (float) difference;
Expand Down Expand Up @@ -260,7 +260,7 @@ public override void Render(RenderContext parentRenderContext)
{
if (OpacityMask == null && (TotalHeight > ActualHeight || TotalWidth > ActualWidth))
{
SolidColorBrush brush = new SolidColorBrush {Color = Color.Black};
SolidColorBrush brush = new SolidColorBrush { Color = Color.Black };
OpacityMask = brush;
_forcedOpacityMask = true;
}
Expand Down Expand Up @@ -288,7 +288,7 @@ public override void SaveUIState(IDictionary<string, object> state, string prefi
base.SaveUIState(state, prefix);
state[prefix + "/ScrollOffsetX"] = _scrollOffsetX;
state[prefix + "/ScrollOffsetY"] = _scrollOffsetX;
}
}

public override void RestoreUIState(IDictionary<string, object> state, string prefix)
{
Expand Down Expand Up @@ -450,7 +450,7 @@ public bool FocusPageDown()
{
if (currentElement.ActualPosition.Y + currentElement.ActualHeight + DELTA_DOUBLE > ActualPosition.Y + ActualHeight)
// Already at bottom
limitPosition = (float) (ActualPosition.Y + 2*ActualHeight);
limitPosition = (float) (ActualPosition.Y + 2 * ActualHeight);
else
limitPosition = (float) (ActualPosition.Y + ActualHeight);
}
Expand Down Expand Up @@ -526,7 +526,7 @@ public bool FocusPageRight()
{
if (currentElement.ActualPosition.X + ActualWidth + DELTA_DOUBLE > ActualPosition.X + ActualWidth)
// Already at right
limitPosition = (float) (ActualPosition.X + 2*ActualWidth);
limitPosition = (float) (ActualPosition.X + 2 * ActualWidth);
else
limitPosition = (float) (ActualPosition.X + ActualWidth);
}
Expand Down Expand Up @@ -651,6 +651,11 @@ public bool IsViewPortAtRight
get { return TemplateControl == null || -_actualScrollOffsetX + ActualWidth + DELTA_DOUBLE >= TotalWidth; }
}

public int NumberOfVisibleLines
{
get { return 0; /* As we are scrolling only by pixels here, we keep the default behavior */ }
}

#endregion
}
}
25 changes: 14 additions & 11 deletions MediaPortal/Source/UI/SkinEngine/Controls/Visuals/ScrollViewer.cs
Expand Up @@ -390,19 +390,22 @@ public override void OnMouseWheel(int numDetents)
if (!IsMouseOver)
return;

int numLines = numDetents * SystemInformation.MouseWheelScrollLines;
IScrollViewerFocusSupport svfs = FindScrollControl() as IScrollViewerFocusSupport;
if (svfs == null)
return;

int scrollByLines = SystemInformation.MouseWheelScrollLines; // Use the system setting as default.

IScrollInfo scrollInfo = svfs as IScrollInfo;
if (scrollInfo != null && scrollInfo.NumberOfVisibleLines != 0) // If ScrollControl can shown less items, use this as limit.
scrollByLines = scrollInfo.NumberOfVisibleLines;

int numLines = numDetents * scrollByLines;

if (numLines < 0)
{
IScrollViewerFocusSupport svfs = FindScrollControl() as IScrollViewerFocusSupport;
if (svfs != null)
svfs.ScrollDown(-1 * numLines);
}
svfs.ScrollDown(-1*numLines);
else if (numLines > 0)
{
IScrollViewerFocusSupport svfs = FindScrollControl() as IScrollViewerFocusSupport;
if (svfs != null)
svfs.ScrollUp(numLines);
}
svfs.ScrollUp(numLines);
}

public override void OnKeyPressed(ref Key key)
Expand Down

0 comments on commit fd2def3

Please sign in to comment.