Parcourir la source

Refineed accept logic

Tig il y a 1 an
Parent
commit
23cb8968cd

+ 5 - 1
Terminal.Gui/Views/RadioGroup.cs

@@ -355,7 +355,11 @@ public class RadioGroup : View
     /// <param name="selectedItem"></param>
     /// <param name="previousSelectedItem"></param>
     public virtual void OnSelectedItemChanged (int selectedItem, int previousSelectedItem)
-    {
+    { 
+        if (_selected == selectedItem)
+        {
+            return;
+        }
         _selected = selectedItem;
         SelectedItemChanged?.Invoke (this, new (selectedItem, previousSelectedItem));
     }

+ 2 - 4
Terminal.Gui/Views/Shortcut.cs

@@ -385,10 +385,8 @@ public class Shortcut : View
             Title = _commandView.Text;
             _commandView.TextChanged += CommandViewTextChanged;
 
-            Remove (HelpView);
-            Remove (KeyView);
-            Add (_commandView, HelpView, KeyView);
-
+            SetHelpViewDefaultLayout ();
+            SetKeyViewDefaultLayout();
             ShowHide ();
             UpdateKeyBinding ();
 

+ 79 - 84
UICatalog/Scenarios/Shortcuts.cs

@@ -1,8 +1,6 @@
 using System;
-using System.Collections.Generic;
 using System.Collections.ObjectModel;
 using System.Linq;
-using System.Text;
 using Terminal.Gui;
 
 namespace UICatalog.Scenarios;
@@ -23,7 +21,6 @@ public class Shortcuts : Scenario
         Application.Shutdown ();
     }
 
-
     // Setting everything up in Loaded handler because we change the
     // QuitKey and it only sticks if changed after init
     private void App_Loaded (object sender, EventArgs e)
@@ -32,7 +29,8 @@ public class Shortcuts : Scenario
         Application.Top.Title = $"{Application.QuitKey} to Quit - Scenario: {GetName ()}";
 
         ObservableCollection<string> eventSource = new ();
-        ListView eventLog = new ListView ()
+
+        var eventLog = new ListView
         {
             X = Pos.AnchorEnd (),
             Width = 40,
@@ -45,19 +43,14 @@ public class Shortcuts : Scenario
         var shortcut1 = new Shortcut
         {
             X = 20,
-            Width = 30,
-            Title = "Zi_gzag",
+            Width = 35,
+            Title = "A_pp Shortcut",
             Key = Key.F1,
             Text = "Width is 30",
             KeyBindingScope = KeyBindingScope.Application,
             BorderStyle = LineStyle.Dotted
         };
-        shortcut1.Border.Thickness = new Thickness (1, 0, 1, 0);
-        shortcut1.Accept += (s, e) =>
-                            {
-                                eventSource.Add ($"Accept: {s}");
-                                eventLog.MoveDown ();
-                            };
+        shortcut1.Border.Thickness = new (1, 0, 1, 0);
         Application.Top.Add (shortcut1);
 
         var shortcut2 = new Shortcut
@@ -69,67 +62,70 @@ public class Shortcuts : Scenario
             Text = "Width is ^",
             KeyBindingScope = KeyBindingScope.HotKey,
             BorderStyle = LineStyle.Dotted,
-            CommandView = new RadioGroup ()
+            CommandView = new RadioGroup
             {
                 Orientation = Orientation.Vertical,
-                RadioLabels = ["One", "Two", "Three", "Four"],
-            },
+                RadioLabels = ["One", "Two", "Three", "Four"]
+            }
         };
 
-        shortcut2.Border.Thickness = new Thickness (1, 0, 1, 0);
-        shortcut2.Accept += (s, e) =>
+        ((RadioGroup)shortcut2.CommandView).SelectedItemChanged += (o, args) =>
+                                                                   {
+                                                                       eventSource.Add ($"SelectedItemChanged: {o.GetType ().Name} - {args.SelectedItem}");
+                                                                       eventLog.MoveDown ();
+                                                                   };
+
+        shortcut2.Accept += (o, args) =>
                             {
-                                eventSource.Add ($"Accept: {s}");
-                                eventLog.MoveDown ();
+                                // Cycle to next item. If at end, set 0
+                                if (((RadioGroup)shortcut2.CommandView).SelectedItem < ((RadioGroup)shortcut2.CommandView).RadioLabels.Length-1)
+                                {
+                                    ((RadioGroup)shortcut2.CommandView).SelectedItem++;
+                                }
+                                else
+                                {
+                                    ((RadioGroup)shortcut2.CommandView).SelectedItem = 0;
+                                }
                             };
+        shortcut2.Border.Thickness = new (1, 0, 1, 0);
         Application.Top.Add (shortcut2);
 
         var shortcut3 = new Shortcut
         {
             X = 20,
             Y = Pos.Bottom (shortcut2),
-            CommandView = new CheckBox () { Text = "_Align" },
+            CommandView = new CheckBox { Text = "_Align" },
             Key = Key.F3,
             HelpText = "Width is Fill",
             Width = Dim.Fill () - Dim.Width (eventLog),
             KeyBindingScope = KeyBindingScope.HotKey,
             BorderStyle = LineStyle.Dotted
         };
-        shortcut3.Border.Thickness = new Thickness (1, 0, 1, 0);
+        shortcut3.Border.Thickness = new (1, 0, 1, 0);
 
         ((CheckBox)shortcut3.CommandView).Toggled += (s, e) =>
-                            {
-                                eventSource.Add ($"Toggled: {s}");
-                                eventLog.MoveDown ();
-
-                                if (shortcut3.CommandView is CheckBox cb)
-                                {
-                                    int max = 0;
-                                    if (e.NewValue == true)
-                                    {
-                                        foreach (Shortcut peer in Application.Top.Subviews.Where (v => v is Shortcut)!)
-                                        {
-                                            max = Math.Max (max, peer.KeyView.Text.GetColumns ());
-                                        }
-
-                                    }
-                                    foreach (Shortcut peer in Application.Top.Subviews.Where (v => v is Shortcut)!)
-                                    {
-                                        peer.MinimumKeyViewSize = max;
-                                    }
-                                }
-
-                                //Application.Top.SetNeedsDisplay ();
-                                //Application.Top.LayoutSubviews ();
-                                //Application.Top.SetNeedsDisplay ();
-                                //Application.Top.Draw ();
-                            };
-        shortcut3.Accept += (s, e) =>
-                            {
-                                eventSource.Add ($"Accept: {s}");
-                                eventLog.MoveDown ();
-
-                            };
+                                                     {
+                                                         if (shortcut3.CommandView is CheckBox cb)
+                                                         {
+                                                             eventSource.Add ($"Toggled: {cb.Text}");
+                                                             eventLog.MoveDown ();
+
+                                                             var max = 0;
+
+                                                             if (e.NewValue == true)
+                                                             {
+                                                                 foreach (Shortcut peer in Application.Top.Subviews.Where (v => v is Shortcut)!)
+                                                                 {
+                                                                     max = Math.Max (max, peer.KeyView.Text.GetColumns ());
+                                                                 }
+                                                             }
+
+                                                             foreach (Shortcut peer in Application.Top.Subviews.Where (v => v is Shortcut)!)
+                                                             {
+                                                                 peer.MinimumKeyViewSize = max;
+                                                             }
+                                                         }
+                                                     };
         Application.Top.Add (shortcut3);
 
         var shortcut4 = new Shortcut
@@ -137,21 +133,19 @@ public class Shortcuts : Scenario
             X = 20,
             Y = Pos.Bottom (shortcut3),
             Width = Dim.Width (shortcut3),
-            Title = "C",
+            CommandView = new Button
+            {
+                Title = "_Button"
+            },
             HelpText = "Width is Fill",
             Key = Key.K,
             KeyBindingScope = KeyBindingScope.HotKey,
-            //           Command = Command.Accept,
             BorderStyle = LineStyle.Dotted
         };
-        shortcut4.Border.Thickness = new Thickness (1, 0, 1, 0);
 
-        shortcut4.Accept += (s, e) =>
-                            {
-                                eventSource.Add ($"Accept: {s}");
-                                eventLog.MoveDown ();
-                                MessageBox.Query ("Hi", $"You clicked {s}");
-                            };
+        shortcut4.CommandView.Accept += Button_Clicked;
+        shortcut4.Border.Thickness = new (1, 0, 1, 0);
+
         Application.Top.Add (shortcut4);
 
         var shortcut5 = new Shortcut
@@ -166,16 +160,10 @@ public class Shortcuts : Scenario
             KeyBindingScope = KeyBindingScope.HotKey,
             BorderStyle = LineStyle.Dotted
         };
-        shortcut5.Border.Thickness = new Thickness (1, 0, 1, 0);
+        shortcut5.Border.Thickness = new (1, 0, 1, 0);
 
-        shortcut5.Accept += (s, e) =>
-                            {
-                                eventSource.Add ($"Accept: {s}");
-                                eventLog.MoveDown ();
-                            };
         Application.Top.Add (shortcut5);
 
-
         var shortcutSlider = new Shortcut
         {
             X = 20,
@@ -186,33 +174,40 @@ public class Shortcuts : Scenario
 
             KeyBindingScope = KeyBindingScope.HotKey,
             BorderStyle = LineStyle.Dotted,
-            CommandView = new Slider<string> ()
+            CommandView = new Slider<string>
             {
                 Orientation = Orientation.Vertical,
-                AllowEmpty = false,
+                AllowEmpty = false
             }
         };
 
-
-        ((Slider<string>)shortcutSlider.CommandView).Options = new List<SliderOption<string>> ()
-            { new () { Legend = "A" }, new () { Legend = "B" }, new () { Legend = "C" } };
+        ((Slider<string>)shortcutSlider.CommandView).Options = new() { new () { Legend = "A" }, new () { Legend = "B" }, new () { Legend = "C" } };
         ((Slider<string>)shortcutSlider.CommandView).SetOption (0);
-        shortcutSlider.Border.Thickness = new Thickness (1, 0, 1, 0);
+        shortcutSlider.Border.Thickness = new (1, 0, 1, 0);
+
+        ((Slider<string>)shortcutSlider.CommandView).OptionsChanged += (o, args) =>
+                                                                       {
+                                                                           eventSource.Add ($"OptionsChanged: {o.GetType ().Name} - {args.Options}");
+                                                                           eventLog.MoveDown ();
+                                                                       };
 
-        shortcutSlider.Accept += (s, e) =>
-                            {
-                                eventSource.Add ($"Accept: {s}");
-                                eventLog.MoveDown ();
-                            };
         Application.Top.Add (shortcutSlider);
-        ;
-        ((CheckBox)shortcut3.CommandView).OnToggled ();
+
+        foreach (View sh in Application.Top.Subviews.Where (v => v is Shortcut)!)
+        {
+            if (sh is Shortcut shortcut)
+            {
+                shortcut.Accept += (o, args) =>
+                                   {
+                                       eventSource.Add ($"Accept: {shortcut!.CommandView.Text}");
+                                       eventLog.MoveDown ();
+                                   };
+            }
+        }
 
         //shortcut1.SetFocus ();
         //View.Diagnostics = ViewDiagnosticFlags.Ruler;
-
     }
 
     private void Button_Clicked (object sender, EventArgs e) { MessageBox.Query ("Hi", $"You clicked {sender}"); }
-
 }