From 23cb8968cd04a3f64dcf0f207547fa0fc81b294e Mon Sep 17 00:00:00 2001 From: Tig Date: Wed, 12 Jun 2024 10:21:25 -0700 Subject: [PATCH] Refineed accept logic --- Terminal.Gui/Views/RadioGroup.cs | 6 +- Terminal.Gui/Views/Shortcut.cs | 6 +- UICatalog/Scenarios/Shortcuts.cs | 157 +++++++++++++++---------------- 3 files changed, 83 insertions(+), 86 deletions(-) diff --git a/Terminal.Gui/Views/RadioGroup.cs b/Terminal.Gui/Views/RadioGroup.cs index efdfa5260..26ffaffde 100644 --- a/Terminal.Gui/Views/RadioGroup.cs +++ b/Terminal.Gui/Views/RadioGroup.cs @@ -355,7 +355,11 @@ public class RadioGroup : View /// /// public virtual void OnSelectedItemChanged (int selectedItem, int previousSelectedItem) - { + { + if (_selected == selectedItem) + { + return; + } _selected = selectedItem; SelectedItemChanged?.Invoke (this, new (selectedItem, previousSelectedItem)); } diff --git a/Terminal.Gui/Views/Shortcut.cs b/Terminal.Gui/Views/Shortcut.cs index 526da816d..c850a600a 100644 --- a/Terminal.Gui/Views/Shortcut.cs +++ b/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 (); diff --git a/UICatalog/Scenarios/Shortcuts.cs b/UICatalog/Scenarios/Shortcuts.cs index 4ab677197..0a26298bb 100644 --- a/UICatalog/Scenarios/Shortcuts.cs +++ b/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 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) + { + eventSource.Add ($"Toggled: {cb.Text}"); + 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 ()); - } + var max = 0; - } - foreach (Shortcut peer in Application.Top.Subviews.Where (v => v is Shortcut)!) - { - peer.MinimumKeyViewSize = max; - } - } + if (e.NewValue == true) + { + foreach (Shortcut peer in Application.Top.Subviews.Where (v => v is Shortcut)!) + { + max = Math.Max (max, peer.KeyView.Text.GetColumns ()); + } + } - //Application.Top.SetNeedsDisplay (); - //Application.Top.LayoutSubviews (); - //Application.Top.SetNeedsDisplay (); - //Application.Top.Draw (); - }; - shortcut3.Accept += (s, e) => - { - eventSource.Add ($"Accept: {s}"); - eventLog.MoveDown (); - - }; + 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 () + CommandView = new Slider { Orientation = Orientation.Vertical, - AllowEmpty = false, + AllowEmpty = false } }; - - ((Slider)shortcutSlider.CommandView).Options = new List> () - { new () { Legend = "A" }, new () { Legend = "B" }, new () { Legend = "C" } }; + ((Slider)shortcutSlider.CommandView).Options = new() { new () { Legend = "A" }, new () { Legend = "B" }, new () { Legend = "C" } }; ((Slider)shortcutSlider.CommandView).SetOption (0); - shortcutSlider.Border.Thickness = new Thickness (1, 0, 1, 0); + shortcutSlider.Border.Thickness = new (1, 0, 1, 0); + + ((Slider)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}"); } - }