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

Fixes #3829 - Slider -> LinearRange #3830

Draft
wants to merge 7 commits into
base: v2_develop
Choose a base branch
from
Draft
Show file tree
Hide file tree
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
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@
namespace Terminal.Gui;

/// <summary>Slider control.</summary>
public class Slider : Slider<object>
public class LinearRange : LinearRange<object>
{
/// <summary>Initializes a new instance of the <see cref="Slider"/> class.</summary>
public Slider () { }
/// <summary>Initializes a new instance of the <see cref="LinearRange"/> class.</summary>
public LinearRange () { }

/// <summary>Initializes a new instance of the <see cref="Slider"/> class.</summary>
/// <summary>Initializes a new instance of the <see cref="LinearRange"/> class.</summary>
/// <param name="options">Initial slider options.</param>
/// <param name="orientation">Initial slider options.</param>
public Slider (List<object> options, Orientation orientation = Orientation.Horizontal) :
public LinearRange (List<object> options, Orientation orientation = Orientation.Horizontal) :
base (options, orientation)
{ }
}
Expand All @@ -21,22 +21,22 @@ public Slider (List<object> options, Orientation orientation = Orientation.Horiz
/// keyboard or mouse.
/// </summary>
/// <typeparam name="T"></typeparam>
public class Slider<T> : View, IOrientation
public class LinearRange<T> : View, IOrientation
{
private readonly SliderConfiguration _config = new ();
private readonly LinearRangeConfiguration _config = new ();

// List of the current set options.
private readonly List<int> _setOptions = new ();

// Options
private List<SliderOption<T>> _options;
private List<LinearRangeOption<T>> _options;

private OrientationHelper _orientationHelper;

#region Initialize

private void SetInitialProperties (
List<SliderOption<T>> options,
List<LinearRangeOption<T>> options,
Orientation orientation = Orientation.Horizontal
)
{
Expand All @@ -45,7 +45,7 @@ private void SetInitialProperties (
CanFocus = true;
CursorVisibility = CursorVisibility.Default;

_options = options ?? new List<SliderOption<T>> ();
_options = options ?? new List<LinearRangeOption<T>> ();

_orientationHelper = new (this); // Do not use object initializer!
_orientationHelper.Orientation = _config._sliderOrientation = orientation;
Expand Down Expand Up @@ -117,13 +117,13 @@ private void SetDefaultStyle ()

#region Constructors

/// <summary>Initializes a new instance of the <see cref="Slider"/> class.</summary>
public Slider () : this (new ()) { }
/// <summary>Initializes a new instance of the <see cref="LinearRange"/> class.</summary>
public LinearRange () : this (new ()) { }

/// <summary>Initializes a new instance of the <see cref="Slider"/> class.</summary>
/// <summary>Initializes a new instance of the <see cref="LinearRange"/> class.</summary>
/// <param name="options">Initial slider options.</param>
/// <param name="orientation">Initial slider orientation.</param>
public Slider (List<T> options, Orientation orientation = Orientation.Horizontal)
public LinearRange (List<T> options, Orientation orientation = Orientation.Horizontal)
{
if (options is null)
{
Expand All @@ -137,7 +137,7 @@ public Slider (List<T> options, Orientation orientation = Orientation.Horizontal
{
var legend = e.ToString ();

return new SliderOption<T>
return new LinearRangeOption<T>
{
Data = e,
Legend = legend,
Expand Down Expand Up @@ -180,7 +180,7 @@ public override string Text
else
{
IEnumerable<string> list = value.Split (',').Select (x => x.Trim ());
Options = list.Select (x => new SliderOption<T> { Legend = x }).ToList ();
Options = list.Select (x => new LinearRangeOption<T> { Legend = x }).ToList ();
}
}
}
Expand Down Expand Up @@ -212,8 +212,8 @@ public int MinimumInnerSpacing
}
}

/// <summary>Slider Type. <see cref="SliderType"></see></summary>
public SliderType Type
/// <summary>Slider Type. <see cref="LinearRangeType"></see></summary>
public LinearRangeType Type
{
get => _config._type;
set
Expand Down Expand Up @@ -278,11 +278,11 @@ public Orientation LegendsOrientation
}
}

/// <summary>Slider styles. <see cref="SliderStyle"></see></summary>
public SliderStyle Style { get; set; } = new ();
/// <summary>Slider styles. <see cref="LinearRangeStyle"></see></summary>
public LinearRangeStyle Style { get; set; } = new ();

/// <summary>Set the slider options.</summary>
public List<SliderOption<T>> Options
public List<LinearRangeOption<T>> Options
{
get =>
_options;
Expand Down Expand Up @@ -347,7 +347,7 @@ public bool UseMinimumSize
#region Events

/// <summary>Event raised when the slider option/s changed. The dictionary contains: key = option index, value = T</summary>
public event EventHandler<SliderEventArgs<T>> OptionsChanged;
public event EventHandler<LinearRangeEventArgs<T>> OptionsChanged;

/// <summary>Overridable method called when the slider options have changed. Raises the <see cref="OptionsChanged"/> event.</summary>
public virtual void OnOptionsChanged ()
Expand All @@ -357,7 +357,7 @@ public virtual void OnOptionsChanged ()
}

/// <summary>Event raised When the option is hovered with the keys or the mouse.</summary>
public event EventHandler<SliderEventArgs<T>> OptionFocused;
public event EventHandler<LinearRangeEventArgs<T>> OptionFocused;

private int
_lastFocusedOption; // for Range type; the most recently focused option. Used to determine shrink direction
Expand All @@ -366,7 +366,7 @@ private int
/// <param name="args"></param>
/// <returns><see langword="true"/> if the focus change was cancelled.</returns>
/// <param name="newFocusedOption"></param>
public virtual bool OnOptionFocused (int newFocusedOption, SliderEventArgs<T> args)
public virtual bool OnOptionFocused (int newFocusedOption, LinearRangeEventArgs<T> args)
{
if (newFocusedOption > _options.Count - 1 || newFocusedOption < 0)
{
Expand Down Expand Up @@ -500,7 +500,7 @@ void CalcSpacingConfig (int size)
{
_config._showLegendsAbbr = true;

foreach (SliderOption<T> o in _options.Where (op => op.LegendAbbr == default (Rune)))
foreach (LinearRangeOption<T> o in _options.Where (op => op.LegendAbbr == default (Rune)))
{
o.LegendAbbr = (Rune)(o.Legend?.GetColumns () > 0 ? o.Legend [0] : ' ');
}
Expand Down Expand Up @@ -841,7 +841,7 @@ private string AlignText (string text, int width, Alignment alignment)
private void DrawSlider ()
{
// TODO: be more surgical on clear
ClearViewport ();
// ClearViewport ();

// Attributes

Expand All @@ -867,11 +867,11 @@ private void DrawSlider ()
if (_config._showEndSpacing && _config._startSpacing > 0)
{
SetAttribute (
isSet && _config._type == SliderType.LeftRange
isSet && _config._type == LinearRangeType.LeftRange
? Style.RangeChar.Attribute ?? normalAttr
: Style.SpaceChar.Attribute ?? normalAttr
);
Rune rune = isSet && _config._type == SliderType.LeftRange ? Style.RangeChar.Rune : Style.SpaceChar.Rune;
Rune rune = isSet && _config._type == LinearRangeType.LeftRange ? Style.RangeChar.Rune : Style.SpaceChar.Rune;

for (var i = 0; i < _config._startSpacing; i++)
{
Expand Down Expand Up @@ -917,19 +917,19 @@ private void DrawSlider ()
{
switch (_config._type)
{
case SliderType.LeftRange when i <= _setOptions [0]:
case LinearRangeType.LeftRange when i <= _setOptions [0]:
drawRange = i < _setOptions [0];

break;
case SliderType.RightRange when i >= _setOptions [0]:
case LinearRangeType.RightRange when i >= _setOptions [0]:
drawRange = i >= _setOptions [0];

break;
case SliderType.Range when _setOptions.Count == 1:
case LinearRangeType.Range when _setOptions.Count == 1:
drawRange = false;

break;
case SliderType.Range when _setOptions.Count == 2:
case LinearRangeType.Range when _setOptions.Count == 2:
if ((i >= _setOptions [0] && i <= _setOptions [1])
|| (i >= _setOptions [1] && i <= _setOptions [0]))
{
Expand Down Expand Up @@ -1009,11 +1009,11 @@ private void DrawSlider ()
if (_config._showEndSpacing)
{
SetAttribute (
isSet && _config._type == SliderType.RightRange
isSet && _config._type == LinearRangeType.RightRange
? Style.RangeChar.Attribute ?? normalAttr
: Style.SpaceChar.Attribute ?? normalAttr
);
Rune rune = isSet && _config._type == SliderType.RightRange ? Style.RangeChar.Rune : Style.SpaceChar.Rune;
Rune rune = isSet && _config._type == LinearRangeType.RightRange ? Style.RangeChar.Rune : Style.SpaceChar.Rune;

for (var i = 0; i < remaining; i++)
{
Expand Down Expand Up @@ -1100,36 +1100,36 @@ private void DrawLegends ()
// Check if the Option is Set.
switch (_config._type)
{
case SliderType.Single:
case SliderType.Multiple:
case LinearRangeType.Single:
case LinearRangeType.Multiple:
if (isSet && _setOptions.Contains (i))
{
isOptionSet = true;
}

break;
case SliderType.LeftRange:
case LinearRangeType.LeftRange:
if (isSet && i <= _setOptions [0])
{
isOptionSet = true;
}

break;
case SliderType.RightRange:
case LinearRangeType.RightRange:
if (isSet && i >= _setOptions [0])
{
isOptionSet = true;
}

break;
case SliderType.Range when _setOptions.Count == 1:
case LinearRangeType.Range when _setOptions.Count == 1:
if (isSet && i == _setOptions [0])
{
isOptionSet = true;
}

break;
case SliderType.Range:
case LinearRangeType.Range:
if (isSet
&& ((i >= _setOptions [0] && i <= _setOptions [1])
|| (i >= _setOptions [1] && i <= _setOptions [0])))
Expand Down Expand Up @@ -1172,7 +1172,7 @@ private void DrawLegends ()
switch (_config._legendsOrientation)
{
case Orientation.Horizontal:
x = 1;
x = 2;

break;
case Orientation.Vertical:
Expand Down Expand Up @@ -1466,7 +1466,7 @@ private void SetKeyBindings ()
KeyBindings.Add (Key.Space, Command.Select);
}

private Dictionary<int, SliderOption<T>> GetSetOptionDictionary () { return _setOptions.ToDictionary (e => e, e => _options [e]); }
private Dictionary<int, LinearRangeOption<T>> GetSetOptionDictionary () { return _setOptions.ToDictionary (e => e, e => _options [e]); }

/// <summary>
/// Sets or unsets <paramref name="optionIndex"/> based on <paramref name="set"/>.
Expand Down Expand Up @@ -1505,9 +1505,9 @@ private bool SetFocusedOption ()
bool changed = false;
switch (_config._type)
{
case SliderType.Single:
case SliderType.LeftRange:
case SliderType.RightRange:
case LinearRangeType.Single:
case LinearRangeType.LeftRange:
case LinearRangeType.RightRange:

if (_setOptions.Count == 1)
{
Expand Down Expand Up @@ -1538,7 +1538,7 @@ private bool SetFocusedOption ()
changed = true;

break;
case SliderType.Multiple:
case LinearRangeType.Multiple:
if (_setOptions.Contains (FocusedOption))
{
if (!_config._allowEmpty && _setOptions.Count () == 1)
Expand All @@ -1560,7 +1560,7 @@ private bool SetFocusedOption ()

break;

case SliderType.Range:
case LinearRangeType.Range:
if (_config._rangeAllowSingle)
{
if (_setOptions.Count == 1)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
namespace Terminal.Gui;

/// <summary><see cref="Slider{T}"/> Legend Style</summary>
public class SliderAttributes
/// <summary><see cref="LinearRange{T}"/> Legend Style</summary>
public class LinearRangeAttributes
{
/// <summary>Attribute for the Legends Container.</summary>
public Attribute? EmptyAttribute { get; set; }
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
namespace Terminal.Gui;

/// <summary>All <see cref="Slider{T}"/> configuration are grouped in this class.</summary>
internal class SliderConfiguration
/// <summary>All <see cref="LinearRange{T}"/> configuration are grouped in this class.</summary>
internal class LinearRangeConfiguration
{
internal bool _allowEmpty;
internal int _endSpacing;
Expand All @@ -14,6 +14,6 @@ internal class SliderConfiguration
internal bool _showLegendsAbbr;
internal Orientation _sliderOrientation = Orientation.Horizontal;
internal int _startSpacing;
internal SliderType _type = SliderType.Single;
internal LinearRangeType _type = LinearRangeType.Single;
internal bool _useMinimumSize;
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
namespace Terminal.Gui;

/// <summary><see cref="EventArgs"/> for <see cref="Slider{T}"/> events.</summary>
public class SliderEventArgs<T> : EventArgs
/// <summary><see cref="EventArgs"/> for <see cref="LinearRange{T}"/> events.</summary>
public class LinearRangeEventArgs<T> : EventArgs
{
/// <summary>Initializes a new instance of <see cref="SliderEventArgs{T}"/></summary>
/// <summary>Initializes a new instance of <see cref="LinearRangeEventArgs{T}"/></summary>
/// <param name="options">The current options.</param>
/// <param name="focused">Index of the option that is focused. -1 if no option has the focus.</param>
public SliderEventArgs (Dictionary<int, SliderOption<T>> options, int focused = -1)
public LinearRangeEventArgs (Dictionary<int, LinearRangeOption<T>> options, int focused = -1)
{
Options = options;
Focused = focused;
Expand All @@ -20,5 +20,5 @@ public SliderEventArgs (Dictionary<int, SliderOption<T>> options, int focused =
public int Focused { get; set; }

/// <summary>Gets/sets whether the option is set or not.</summary>
public Dictionary<int, SliderOption<T>> Options { get; set; }
public Dictionary<int, LinearRangeOption<T>> Options { get; set; }
}
Loading
Loading