浏览代码

Made Buttons.NumericUpDown even cooler. Updated Adornments scenario to use it.

Tig 1 年之前
父节点
当前提交
085fd6316d
共有 2 个文件被更改,包括 136 次插入84 次删除
  1. 89 70
      UICatalog/Scenarios/Adornments.cs
  2. 47 14
      UICatalog/Scenarios/Buttons.cs

+ 89 - 70
UICatalog/Scenarios/Adornments.cs

@@ -13,13 +13,11 @@ public class Adornments : Scenario
 {
     private ViewDiagnosticFlags _diagnosticFlags;
 
-    public override void Init ()
+    public override void Main ()
     {
         Application.Init ();
-        ConfigurationManager.Themes.Theme = Theme;
-        ConfigurationManager.Apply ();
-        Top = new ();
-        Top.ColorScheme = Colors.ColorSchemes [TopLevelColorScheme];
+
+        _diagnosticFlags = View.Diagnostics;
 
         var view = new Window { Title = "The _Window" };
         var tf1 = new TextField { Width = 10, Text = "TextField" };
@@ -78,18 +76,15 @@ public class Adornments : Scenario
             X = 40, Y = 5, Title = "This is long text (in a label) that should clip.",
 
         };
-        longLabel.TextFormatter.WordWrap =true;
+        longLabel.TextFormatter.WordWrap = true;
         view.Add (tf1, color, button, label, btnButtonInWindow, tv, longLabel);
 
         var editor = new AdornmentsEditor
         {
             Title = $"{Application.QuitKey} to Quit - Scenario: {GetName ()}",
-            ColorScheme = Colors.ColorSchemes [TopLevelColorScheme]
-
-            //BorderStyle = LineStyle.None,
         };
         view.Width = Dim.Percent (60);
-        view.Height = Dim.Percent (80); 
+        view.Height = Dim.Percent (80);
 
         editor.Initialized += (s, e) => { editor.ViewToEdit = view; };
 
@@ -122,7 +117,7 @@ public class Adornments : Scenario
 #endif
                             };
 
-        Top.Closed += (s, e) => View.Diagnostics = _diagnosticFlags;
+        editor.Closed += (s, e) => View.Diagnostics = _diagnosticFlags;
 
         Application.Run (editor);
         editor.Dispose ();
@@ -130,8 +125,9 @@ public class Adornments : Scenario
         Application.Shutdown ();
     }
 
-    public override void Run () { }
-
+    /// <summary>
+    /// Provides a composable UI for editing the settings of an Adornment.
+    /// </summary>
     public class AdornmentEditor : View
     {
         private readonly ColorPicker _backgroundColorPicker = new ()
@@ -152,12 +148,12 @@ public class Adornments : Scenario
             SuperViewRendersLineCanvas = true
         };
 
-        private TextField _bottomEdit;
-        private bool _isUpdating;
-        private TextField _leftEdit;
-        private TextField _rightEdit;
+        private Buttons.NumericUpDown<int> _topEdit;
+        private Buttons.NumericUpDown<int> _leftEdit;
+        private Buttons.NumericUpDown<int> _bottomEdit;
+        private Buttons.NumericUpDown<int> _rightEdit;
         private Thickness _thickness;
-        private TextField _topEdit;
+        private bool _isUpdating;
 
         public AdornmentEditor ()
         {
@@ -165,7 +161,6 @@ public class Adornments : Scenario
             BorderStyle = LineStyle.Double;
             Initialized += AdornmentEditor_Initialized;
         }
-
         public Attribute Color
         {
             get => new (_foregroundColorPicker.SelectedColor, _backgroundColorPicker.SelectedColor);
@@ -192,26 +187,10 @@ public class Adornments : Scenario
                 if (IsInitialized)
                 {
                     _isUpdating = true;
-
-                    if (_topEdit.Text != _thickness.Top.ToString ())
-                    {
-                        _topEdit.Text = _thickness.Top.ToString ();
-                    }
-
-                    if (_leftEdit.Text != _thickness.Left.ToString ())
-                    {
-                        _leftEdit.Text = _thickness.Left.ToString ();
-                    }
-
-                    if (_rightEdit.Text != _thickness.Right.ToString ())
-                    {
-                        _rightEdit.Text = _thickness.Right.ToString ();
-                    }
-
-                    if (_bottomEdit.Text != _thickness.Bottom.ToString ())
-                    {
-                        _bottomEdit.Text = _thickness.Bottom.ToString ();
-                    }
+                    _topEdit.Value = _thickness.Top;
+                    _leftEdit.Value = _thickness.Left;
+                    _rightEdit.Value = _thickness.Right;
+                    _bottomEdit.Value = _thickness.Bottom;
 
                     _isUpdating = false;
                 }
@@ -223,49 +202,44 @@ public class Adornments : Scenario
 
         private void AdornmentEditor_Initialized (object sender, EventArgs e)
         {
-            var editWidth = 3;
-
-            _topEdit = new () { X = Pos.Center (), Y = 0, Width = editWidth };
+            _topEdit = new ()
+            {
+                X = Pos.Center (), Y = 0
+            };
 
-            _topEdit.Accept += Edit_Accept;
+            _topEdit.ValueChanging += Top_ValueChanging;
             Add (_topEdit);
 
             _leftEdit = new ()
             {
-                X = Pos.Left (_topEdit) - editWidth, Y = Pos.Bottom (_topEdit), Width = editWidth
+                X = Pos.Left (_topEdit) - Pos.Function (() => _topEdit.Digits) - 2, Y = Pos.Bottom (_topEdit)
             };
 
-            _leftEdit.Accept += Edit_Accept;
+            _leftEdit.ValueChanging += Left_ValueChanging;
             Add (_leftEdit);
 
-            _rightEdit = new () { X = Pos.Right (_topEdit), Y = Pos.Bottom (_topEdit), Width = editWidth };
+            _rightEdit = new () { X = Pos.Right (_leftEdit) + 5, Y = Pos.Bottom (_topEdit) };
 
-            _rightEdit.Accept += Edit_Accept;
+            _rightEdit.ValueChanging += Right_ValueChanging;
             Add (_rightEdit);
 
-            _bottomEdit = new () { X = Pos.Center (), Y = Pos.Bottom (_leftEdit), Width = editWidth };
+            _bottomEdit = new () { X = Pos.Center (), Y = Pos.Bottom (_leftEdit) };
 
-            _bottomEdit.Accept += Edit_Accept;
+            _bottomEdit.ValueChanging += Bottom_ValueChanging;
             Add (_bottomEdit);
 
-            var copyTop = new Button { X = Pos.Center () + 1, Y = Pos.Bottom (_bottomEdit), Text = "Cop_y Top" };
+            var copyTop = new Button { X = Pos.Center (), Y = Pos.Bottom (_bottomEdit), Text = "Cop_y Top" };
 
             copyTop.Accept += (s, e) =>
                               {
                                   Thickness = new (Thickness.Top);
-
-                                  if (string.IsNullOrEmpty (_topEdit.Text))
-                                  {
-                                      _topEdit.Text = "0";
-                                  }
-
-                                  _bottomEdit.Text = _leftEdit.Text = _rightEdit.Text = _topEdit.Text;
+                                  _leftEdit.Value = _rightEdit.Value = _bottomEdit.Value = _topEdit.Value;
                               };
             Add (copyTop);
 
             // Foreground ColorPicker.
             _foregroundColorPicker.X = -1;
-            _foregroundColorPicker.Y = Pos.Bottom (copyTop) + 1;
+            _foregroundColorPicker.Y = Pos.Bottom (copyTop);
             _foregroundColorPicker.SelectedColor = Color.Foreground.GetClosestNamedColor ();
 
             _foregroundColorPicker.ColorChanged += (o, a) =>
@@ -293,28 +267,68 @@ public class Adornments : Scenario
                                                                                 );
             Add (_backgroundColorPicker);
 
-            _topEdit.Text = $"{Thickness.Top}";
-            _leftEdit.Text = $"{Thickness.Left}";
-            _rightEdit.Text = $"{Thickness.Right}";
-            _bottomEdit.Text = $"{Thickness.Bottom}";
+            _topEdit.Value = Thickness.Top;
+            _leftEdit.Value = Thickness.Left;
+            _rightEdit.Value = Thickness.Right;
+            _bottomEdit.Value = Thickness.Bottom;
 
             LayoutSubviews ();
-            Height = GetAdornmentsThickness ().Vertical + 4 + 4;
+            Height = GetAdornmentsThickness ().Vertical + 4 + 3;
             Width = GetAdornmentsThickness ().Horizontal + _foregroundColorPicker.Frame.Width * 2 - 3;
         }
 
-        private void Edit_Accept (object sender, CancelEventArgs e)
+        private void Top_ValueChanging (object sender, StateEventArgs<int> e)
         {
-            e.Cancel = true;
+            if (e.NewValue < 0)
+            {
+                e.Cancel = true;
 
-            Thickness = new (
-                             int.Parse (_leftEdit.Text),
-                             int.Parse (_topEdit.Text),
-                             int.Parse (_rightEdit.Text),
-                             int.Parse (_bottomEdit.Text));
+                return;
+            }
+
+            Thickness.Top = e.NewValue;
+        }
+
+        private void Left_ValueChanging (object sender, StateEventArgs<int> e)
+        {
+            if (e.NewValue < 0)
+            {
+                e.Cancel = true;
+
+                return;
+            }
+
+            Thickness.Left = e.NewValue;
+        }
+
+        private void Right_ValueChanging (object sender, StateEventArgs<int> e)
+        {
+            if (e.NewValue < 0)
+            {
+                e.Cancel = true;
+
+                return;
+            }
+
+            Thickness.Right = e.NewValue;
+        }
+
+        private void Bottom_ValueChanging (object sender, StateEventArgs<int> e)
+        {
+            if (e.NewValue < 0)
+            {
+                e.Cancel = true;
+
+                return;
+            }
+
+            Thickness.Bottom = e.NewValue;
         }
     }
 
+    /// <summary>
+    /// Provides an editor UI for the Margin, Border, and Padding of a View.
+    /// </summary>
     public class AdornmentsEditor : Window
     {
         private AdornmentEditor _borderEditor;
@@ -324,6 +338,11 @@ public class Adornments : Scenario
         private AdornmentEditor _paddingEditor;
         private View _viewToEdit;
 
+        public AdornmentsEditor ()
+        {
+            ColorScheme = Colors.ColorSchemes ["Dialog"];
+        }
+
         public View ViewToEdit
         {
             get => _viewToEdit;

+ 47 - 14
UICatalog/Scenarios/Buttons.cs

@@ -79,7 +79,7 @@ public class Buttons : Scenario
         Button button;
 
         main.Add (
-                  button = new()
+                  button = new ()
                   {
                       X = 2,
                       Y = Pos.Bottom (colorButtonsLabel) + 1,
@@ -91,7 +91,7 @@ public class Buttons : Scenario
 
         // Note the 'N' in 'Newline' will be the hotkey
         main.Add (
-                  button = new() { X = 2, Y = Pos.Bottom (button) + 1, Text = "a Newline\nin the button" }
+                  button = new () { X = 2, Y = Pos.Bottom (button) + 1, Text = "a Newline\nin the button" }
                  );
         button.Accept += (s, e) => MessageBox.Query ("Message", "Question?", "Yes", "No");
 
@@ -100,7 +100,7 @@ public class Buttons : Scenario
         textChanger.Accept += (s, e) => textChanger.Text += "!";
 
         main.Add (
-                  button = new()
+                  button = new ()
                   {
                       X = Pos.Right (textChanger) + 2,
                       Y = Pos.Y (textChanger),
@@ -335,7 +335,7 @@ public class Buttons : Scenario
                                               }
                                           };
 
-        label = new()
+        label = new ()
         {
             X = 0,
             Y = Pos.Bottom (moveUnicodeHotKeyBtn) + 1,
@@ -356,7 +356,7 @@ public class Buttons : Scenario
 
         main.Add (label, numericUpDown);
 
-        label = new()
+        label = new ()
         {
             X = 0,
             Y = Pos.Bottom (numericUpDown) + 1,
@@ -374,7 +374,7 @@ public class Buttons : Scenario
         noRepeatButton.Accept += (s, e) => { noRepeatButton.Title = $"Accept Cou_nt: {++noRepeatAcceptCount}"; };
         main.Add (label, noRepeatButton);
 
-        label = new()
+        label = new ()
         {
             X = 0,
             Y = Pos.Bottom (label) + 1,
@@ -430,7 +430,9 @@ public class Buttons : Scenario
 
             // TODO: Use Dim.Auto for the Width and Height
             Height = 1;
-            Width = 5; // button + 3 for number + button
+            Width = Dim.Function (() => Digits + 2); // button + 3 for number + button
+
+            CanFocus = true;
 
             _down = new ()
             {
@@ -444,18 +446,23 @@ public class Buttons : Scenario
                 WantContinuousButtonPressed = true
             };
 
-            _number = new()
+            _number = new ()
             {
                 Text = Value.ToString (),
+                AutoSize = false,
                 X = Pos.Right (_down),
                 Y = Pos.Top (_down),
+                Width = Dim.Function (() => Digits),
+                Height = 1,
+                TextAlignment = TextAlignment.Centered,
+                CanFocus = true
             };
 
             _up = new ()
             {
                 CanFocus = false,
                 AutoSize = false,
-                X = Pos.Right (_number),
+                X = Pos.AnchorEnd (1),
                 Y = Pos.Top (_number),
                 Height = 1,
                 Width = 1,
@@ -470,19 +477,37 @@ public class Buttons : Scenario
 
             Add (_down, _number, _up);
 
+
+            AddCommand (Command.ScrollUp, () =>
+                                          {
+                                              Value = (dynamic)Value + 1;
+                                              _number.Text = Value.ToString ();
+
+                                              return true;
+                                          });
+            AddCommand (Command.ScrollDown, () =>
+                                            {
+                                                Value = (dynamic)Value - 1;
+                                                _number.Text = Value.ToString ();
+
+                                                return true;
+                                            });
+
+            KeyBindings.Add (Key.CursorUp, Command.ScrollUp);
+            KeyBindings.Add (Key.CursorDown, Command.ScrollDown);
+
             return;
 
             void OnDownButtonOnAccept (object s, CancelEventArgs e)
             {
-                Value = (dynamic)Value - 1;
-                _number.Text = Value.ToString();
+                InvokeCommand (Command.ScrollDown);
             }
 
             void OnUpButtonOnAccept (object s, CancelEventArgs e)
             {
-                Value = (dynamic)Value + 1;
-                _number.Text = Value.ToString ();
+                InvokeCommand (Command.ScrollUp);
             }
+
         }
 
         private T _value;
@@ -495,7 +520,7 @@ public class Buttons : Scenario
             get => _value;
             set
             {
-                if (_value.Equals(value))
+                if (_value.Equals (value))
                 {
                     return;
                 }
@@ -526,5 +551,13 @@ public class Buttons : Scenario
         /// </summary>
         [CanBeNull]
         public event EventHandler<StateEventArgs<T>> ValueChanged;
+
+        /// <summary>
+        /// The number of digits to display. The <see cref="View.Viewport"/> will be resized to fit this number of characters plus the buttons. The default is 3.
+        /// </summary>
+        public int Digits { get; set; } = 3;
+
+
     }
 }
+