Upgraded Slider

This commit is contained in:
Tig
2024-07-22 12:48:55 -06:00
parent 3b89159dfe
commit 112d043909
2 changed files with 48 additions and 49 deletions

View File

@@ -21,7 +21,7 @@ public class Slider : Slider<object>
/// keyboard or mouse.
/// </summary>
/// <typeparam name="T"></typeparam>
public class Slider<T> : View
public class Slider<T> : View, IOrientation
{
private readonly SliderConfiguration _config = new ();
@@ -31,6 +31,8 @@ public class Slider<T> : View
// Options
private List<SliderOption<T>> _options;
private OrientationHelper _orientationHelper;
#region Initialize
private void SetInitialProperties (
@@ -45,11 +47,13 @@ public class Slider<T> : View
_options = options ?? new List<SliderOption<T>> ();
_config._sliderOrientation = orientation;
_orientationHelper = new (this);
_orientationHelper.Orientation = _config._sliderOrientation = orientation;
_orientationHelper.OrientationChanging += (sender, e) => OrientationChanging?.Invoke (this, e);
_orientationHelper.OrientationChanged += (sender, e) => OrientationChanged?.Invoke (this, e);
SetDefaultStyle ();
SetCommands ();
SetContentSize ();
// BUGBUG: This should not be needed - Need to ensure SetRelativeLayout gets called during EndInit
@@ -222,13 +226,46 @@ public class Slider<T> : View
}
}
/// <summary>Slider Orientation. <see cref="Gui.Orientation"></see></summary>
/// <summary>
/// Gets or sets the <see cref="Orientation"/>. The default is <see cref="Orientation.Horizontal"/>.
/// </summary>
public Orientation Orientation
{
get => _config._sliderOrientation;
set => OnOrientationChanged (value);
get => _orientationHelper.Orientation;
set => _orientationHelper.Orientation = value;
}
#region IOrientation members
/// <inheritdoc />
public event EventHandler<CancelEventArgs<Orientation>> OrientationChanging;
/// <inheritdoc />
public event EventHandler<CancelEventArgs<Orientation>> OrientationChanged;
/// <inheritdoc />
public void OnOrientationChanged (Orientation oldOrientation, Orientation newOrientation)
{
_config._sliderOrientation = newOrientation;
switch (_config._sliderOrientation)
{
case Orientation.Horizontal:
Style.SpaceChar = new () { Rune = Glyphs.HLine }; // '─'
break;
case Orientation.Vertical:
Style.SpaceChar = new () { Rune = Glyphs.VLine };
break;
}
SetKeyBindings ();
SetContentSize ();
}
#endregion
/// <summary>Legends Orientation. <see cref="Gui.Orientation"></see></summary>
public Orientation LegendsOrientation
{
@@ -309,42 +346,6 @@ public class Slider<T> : View
#region Events
/// <summary>
/// Fired when the slider orientation has changed. Can be cancelled.
/// </summary>
public event EventHandler<CancelEventArgs<Orientation>> OrientationChanged;
/// <summary>Called when the slider orientation has changed. Invokes the <see cref="OrientationChanged"/> event.</summary>
/// <param name="newOrientation"></param>
/// <returns>True of the event was cancelled.</returns>
public virtual bool OnOrientationChanged (Orientation newOrientation)
{
var args = new CancelEventArgs<Orientation> (in _config._sliderOrientation, ref newOrientation);
OrientationChanged?.Invoke (this, args);
if (!args.Cancel)
{
_config._sliderOrientation = newOrientation;
switch (_config._sliderOrientation)
{
case Orientation.Horizontal:
Style.SpaceChar = new () { Rune = Glyphs.HLine }; // '─'
break;
case Orientation.Vertical:
Style.SpaceChar = new () { Rune = Glyphs.VLine };
break;
}
SetKeyBindings ();
SetContentSize ();
}
return args.Cancel;
}
/// <summary>Event raised when the slider option/s changed. The dictionary contains: key = option index, value = T</summary>
public event EventHandler<SliderEventArgs<T>> OptionsChanged;
@@ -1737,7 +1738,7 @@ public class Slider<T> : View
internal bool Select ()
{
SetFocusedOption();
SetFocusedOption ();
return true;
}

View File

@@ -272,9 +272,9 @@ public class AllViewsTester : Scenario
_orientation.SelectedItemChanged += (s, selected) =>
{
if (_curView?.GetType ().GetProperty ("Orientation") is { } prop)
if (_curView is IOrientation orientatedView)
{
prop.GetSetMethod ()?.Invoke (_curView, new object [] { _orientation.SelectedItem });
orientatedView.Orientation = (Orientation)_orientation.SelectedItem;
}
};
_settingsPane.Add (label, _orientation);
@@ -358,11 +358,9 @@ public class AllViewsTester : Scenario
view.Title = "_Test Title";
}
// TODO: Add IOrientation so this doesn't require reflection
// If the view supports a Title property, set it so we have something to look at
if (view?.GetType ().GetProperty ("Orientation") is { } prop)
if (view is IOrientation orientatedView)
{
_orientation.SelectedItem = (int)prop.GetGetMethod ()!.Invoke (view, null)!;
_orientation.SelectedItem = (int)orientatedView.Orientation;
_orientation.Enabled = true;
}
else