Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MaterialListBox improved scrolling #379

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 27 additions & 4 deletions MaterialSkin/Controls/MaterialListBox.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ public class MaterialListBox : Control, IMaterialControl
private bool _multiKeyDown;
private int _hoveredItem;
private MaterialScrollBar _scrollBar;
private bool _smoothScrolling = true;
private object _selectedValue;

private bool _updating=false;
Expand Down Expand Up @@ -241,6 +242,19 @@ public MaterialItemDensity Density
}
}

[Category("Material Skin"), DefaultValue(true)]
[Description("Enables Smoothly Scrolling")]
public bool SmoothScrolling
{
get { return _smoothScrolling; }
set
{
_smoothScrolling = value;
UpdateItemSpecs();
Invalidate();
}
}

#endregion Properties

#region Constructors
Expand Down Expand Up @@ -377,9 +391,16 @@ protected override void OnPaint(PaintEventArgs e)
g.TextRenderingHint = TextRenderingHint.ClearTypeGridFit;
Rectangle mainRect = new Rectangle(0, 0, Width - (ShowBorder ? 1 : 0), Height - (ShowBorder ? 1 : 0));

int lastItem = (_scrollBar.Value / _itemHeight) + (Height / _itemHeight) + 1 > Items.Count ? Items.Count : (_scrollBar.Value / _itemHeight) + (Height / _itemHeight) + 1;
int firstItem = _scrollBar.Value / _itemHeight < 0 ? 0 : (_scrollBar.Value / _itemHeight);

// Account for partially visible items.
int itemOffset = SmoothScrolling ? _scrollBar.Value - (firstItem * _itemHeight) : 0;

// Calculate the last item
int lastItem = (_scrollBar.Value / _itemHeight) + ((Height + itemOffset) / _itemHeight) + 1 > Items.Count ?
Items.Count :
(_scrollBar.Value / _itemHeight) + ((Height + itemOffset) / _itemHeight) + 1;

g.FillRectangle(Enabled ? SkinManager.BackgroundBrush : SkinManager.BackgroundDisabledBrush, mainRect);

//Set TextAlignFlags
Expand Down Expand Up @@ -409,7 +430,7 @@ protected override void OnPaint(PaintEventArgs e)
string itemText = Items[i].Text;
string itemSecondaryText = Items[i].SecondaryText;

Rectangle itemRect = new Rectangle(0, (i - firstItem) * _itemHeight, Width - (_showScrollBar && _scrollBar.Visible ? _scrollBar.Width : 0), _itemHeight);
Rectangle itemRect = new Rectangle(0, (i - firstItem) * _itemHeight - itemOffset, Width - (_showScrollBar && _scrollBar.Visible ? _scrollBar.Width : 0), _itemHeight);

if (MultiSelect && _indicates.Count != 0)
{
Expand Down Expand Up @@ -675,7 +696,8 @@ protected override void OnMouseDown(MouseEventArgs e)
Focus();
if (e.Button == MouseButtons.Left)
{
int index = _scrollBar.Value / _itemHeight + e.Location.Y / _itemHeight;
int itemOffset = SmoothScrolling ? _scrollBar.Value % _itemHeight: 0;
int index = _scrollBar.Value / _itemHeight + (e.Location.Y+itemOffset) / _itemHeight;
if (index >= 0 && index < _items.Count)
{
if (MultiSelect && _multiKeyDown)
Expand Down Expand Up @@ -798,7 +820,8 @@ protected override void OnMouseMove(MouseEventArgs e)

private void _updateHoveredItem(MouseEventArgs e)
{
int index = _scrollBar.Value / _itemHeight + e.Location.Y / _itemHeight;
int itemOffset = SmoothScrolling ? _scrollBar.Value % _itemHeight: 0;
int index = _scrollBar.Value / _itemHeight + (e.Location.Y+itemOffset) / _itemHeight;

if (index >= Items.Count)
{
Expand Down