From 112d04390978d4bc94ef99cd58b9cd1fccf72b16 Mon Sep 17 00:00:00 2001 From: Tig Date: Mon, 22 Jul 2024 12:48:55 -0600 Subject: [PATCH] Upgraded Slider --- Terminal.Gui/Views/Slider.cs | 87 ++++++++++++++------------- UICatalog/Scenarios/AllViewsTester.cs | 10 ++- 2 files changed, 48 insertions(+), 49 deletions(-) diff --git a/Terminal.Gui/Views/Slider.cs b/Terminal.Gui/Views/Slider.cs index a613ce7d5..ee0f2f4d0 100644 --- a/Terminal.Gui/Views/Slider.cs +++ b/Terminal.Gui/Views/Slider.cs @@ -21,7 +21,7 @@ public class Slider : Slider /// keyboard or mouse. /// /// -public class Slider : View +public class Slider : View, IOrientation { private readonly SliderConfiguration _config = new (); @@ -31,6 +31,8 @@ public class Slider : View // Options private List> _options; + private OrientationHelper _orientationHelper; + #region Initialize private void SetInitialProperties ( @@ -45,11 +47,13 @@ public class Slider : View _options = options ?? new List> (); - _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 : View } } - /// Slider Orientation. + + /// + /// Gets or sets the . The default is . + /// public Orientation Orientation { - get => _config._sliderOrientation; - set => OnOrientationChanged (value); + get => _orientationHelper.Orientation; + set => _orientationHelper.Orientation = value; } + #region IOrientation members + + /// + public event EventHandler> OrientationChanging; + + /// + public event EventHandler> OrientationChanged; + + /// + 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 + /// Legends Orientation. public Orientation LegendsOrientation { @@ -309,42 +346,6 @@ public class Slider : View #region Events - /// - /// Fired when the slider orientation has changed. Can be cancelled. - /// - public event EventHandler> OrientationChanged; - - /// Called when the slider orientation has changed. Invokes the event. - /// - /// True of the event was cancelled. - public virtual bool OnOrientationChanged (Orientation newOrientation) - { - var args = new CancelEventArgs (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; - } - /// Event raised when the slider option/s changed. The dictionary contains: key = option index, value = T public event EventHandler> OptionsChanged; @@ -1737,7 +1738,7 @@ public class Slider : View internal bool Select () { - SetFocusedOption(); + SetFocusedOption (); return true; } diff --git a/UICatalog/Scenarios/AllViewsTester.cs b/UICatalog/Scenarios/AllViewsTester.cs index cc440b820..ed5d74bf6 100644 --- a/UICatalog/Scenarios/AllViewsTester.cs +++ b/UICatalog/Scenarios/AllViewsTester.cs @@ -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