From b01d61491a93f3f10e42bd3ed969843790fa2b50 Mon Sep 17 00:00:00 2001 From: LadyDefile Date: Fri, 6 Jan 2023 22:49:17 -0600 Subject: [PATCH 1/2] Improved the scrolling functionality of the material listbox to allow for smoothly scrolling up and down (fixes items clipping off the bottom). --- MaterialSkin/Controls/MaterialListBox.cs | 31 +++++++++++++++++++++--- 1 file changed, 27 insertions(+), 4 deletions(-) diff --git a/MaterialSkin/Controls/MaterialListBox.cs b/MaterialSkin/Controls/MaterialListBox.cs index de629b9d..79a94f6b 100644 --- a/MaterialSkin/Controls/MaterialListBox.cs +++ b/MaterialSkin/Controls/MaterialListBox.cs @@ -37,6 +37,7 @@ public class MaterialListBox : Control, IMaterialControl private bool _multiKeyDown; private int _hoveredItem; private MaterialScrollBar _scrollBar; + private bool _smoothScrolling = false; private object _selectedValue; private bool _updating=false; @@ -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 @@ -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 @@ -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) { @@ -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) @@ -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) { From 11f3def79d41af8d3839c6b4b4ba47ee319ea8a7 Mon Sep 17 00:00:00 2001 From: LadyDefile Date: Fri, 6 Jan 2023 23:04:13 -0600 Subject: [PATCH 2/2] Fixed the default value --- MaterialSkin/Controls/MaterialListBox.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/MaterialSkin/Controls/MaterialListBox.cs b/MaterialSkin/Controls/MaterialListBox.cs index 79a94f6b..9704c4be 100644 --- a/MaterialSkin/Controls/MaterialListBox.cs +++ b/MaterialSkin/Controls/MaterialListBox.cs @@ -37,7 +37,7 @@ public class MaterialListBox : Control, IMaterialControl private bool _multiKeyDown; private int _hoveredItem; private MaterialScrollBar _scrollBar; - private bool _smoothScrolling = false; + private bool _smoothScrolling = true; private object _selectedValue; private bool _updating=false;