diff --git a/MaterialSkin/Controls/MaterialSlider.cs b/MaterialSkin/Controls/MaterialSlider.cs index 8cd327c9..cafc0e32 100644 --- a/MaterialSkin/Controls/MaterialSlider.cs +++ b/MaterialSkin/Controls/MaterialSlider.cs @@ -8,8 +8,10 @@ namespace MaterialSkin.Controls { public class MaterialSlider : Control, IMaterialControl { - #region "Private members" - private bool _mousePressed; + public enum Directions { Normal, Reverse } + + #region "Private members" + private bool _mousePressed; private int _mouseX; //private int _indicatorSize; private bool _hovered = false; @@ -182,12 +184,26 @@ public MaterialSkinManager.fontType FontType } } + protected int _stepChange; + [DefaultValue(2)] + [Category("Material Skin")] + [Description("Define control step change value")] + public int StepChange + { + get => _stepChange; + set => _stepChange = value.Clamp(1, RangeMax); + } - #endregion + [DefaultValue(Directions.Normal)] + [Category("Material Skin")] + [Description("Define control direction change value with mouse wheel")] + public Directions ScrollDirection { get; set; } + + #endregion - #region "Events" + #region "Events" - [Category("Behavior")] + [Category("Behavior")] [Description("Occurs when value change.")] public delegate void ValueChanged(object sender, int newValue); public event ValueChanged onValueChanged; @@ -255,14 +271,13 @@ protected override void OnMouseDown(MouseEventArgs e) protected override void OnMouseWheel(MouseEventArgs e) { base.OnMouseWheel(e); - if (_valueMax != 0 && (Value + e.Delta / -40) > _valueMax) - Value = _valueMax; - else - Value += e.Delta/-40; - onValueChanged?.Invoke(this, _value); + int scrollLines = SystemInformation.MouseWheelScrollLines; + Value += e.Delta / 40 / scrollLines * StepChange * (ScrollDirection == Directions.Normal ? 1 : -1); + Value = Value.Clamp(RangeMin, RangeMax); + onValueChanged?.Invoke(this, _value); } - protected override void OnMouseEnter(EventArgs e) + protected override void OnMouseEnter(EventArgs e) { base.OnMouseEnter(e); _hovered = true; diff --git a/MaterialSkin/Extensions.cs b/MaterialSkin/Extensions.cs index 10fe207a..83419487 100644 --- a/MaterialSkin/Extensions.cs +++ b/MaterialSkin/Extensions.cs @@ -85,5 +85,26 @@ public static int PercentageToColorComponent(this int percentage) { return (int)((percentage / 100d) * 255d); } + + // Simulate Clamp function from .NET Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8 + // https://source.dot.net/#System.Private.CoreLib/src/libraries/System.Private.CoreLib/src/System/Math.cs,538 + internal static int Clamp(this int value, int min, int max) + { + if (min > max) + { + throw new ArgumentException(string.Format("'{0}' cannot be greater than {1}.", min, max)); + } + + if (value < min) + { + return min; + } + else if (value > max) + { + return max; + } + + return value; + } } } \ No newline at end of file