소스 검색

Upgraded Slider

Tig 1 년 전
부모
커밋
112d043909
2개의 변경된 파일48개의 추가작업 그리고 49개의 파일을 삭제
  1. 44 43
      Terminal.Gui/Views/Slider.cs
  2. 4 6
      UICatalog/Scenarios/AllViewsTester.cs

+ 44 - 43
Terminal.Gui/Views/Slider.cs

@@ -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,12 +226,45 @@ 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;
     }

+ 4 - 6
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