diff --git a/Terminal.Gui/Views/RadioGroup.cs b/Terminal.Gui/Views/RadioGroup.cs index 24a0b8992..bc6efb85a 100644 --- a/Terminal.Gui/Views/RadioGroup.cs +++ b/Terminal.Gui/Views/RadioGroup.cs @@ -8,7 +8,8 @@ namespace Terminal.Gui { /// shows a group of radio labels, only one of those can be selected at a given time /// public class RadioGroup : View { - int selected, cursor; + int selected = -1; + int cursor; void Init(Rect rect, ustring [] radioLabels, int selected) { @@ -113,7 +114,7 @@ namespace Terminal.Gui { if (prevCount != radioLabels.Count) { SetWidthHeight (radioLabels); } - Selected = 0; + SelectedItem = 0; cursor = 0; SetNeedsDisplay (); } @@ -152,24 +153,61 @@ namespace Terminal.Gui { Move (0, cursor); } + // TODO: Make this a global class /// - /// Invoked when the selected radio label has changed. The passed int indicates the newly selected item. + /// Event arguments for the SelectedItemChagned event. /// - public Action SelectedItemChanged; + public class SelectedItemChangedEventArgs : EventArgs { + /// + /// Gets the index of the item that was previously selected. -1 if there was no previous selection. + /// + public int PreviousSelectedItem { get; } + + /// + /// Gets the index of the item that is now selected. -1 if there is no selection. + /// + public int SelectedItem { get; } + + /// + /// Initializes a new class. + /// + /// + /// + public SelectedItemChangedEventArgs(int selectedItem, int previousSelectedItem) + { + PreviousSelectedItem = previousSelectedItem; + SelectedItem = selectedItem; + } + } + + /// + /// Invoked when the selected radio label has changed. + /// + public Action SelectedItemChanged; /// /// The currently selected item from the list of radio labels /// /// The selected. - public int Selected { + public int SelectedItem { get => selected; set { - selected = value; - SelectedItemChanged?.Invoke (selected); + OnSelectedItemChanged (value, SelectedItem); SetNeedsDisplay (); } } + /// + /// Called whenever the current selected item changes. Invokes the event. + /// + /// + /// + public virtual void OnSelectedItemChanged (int selectedItem, int previousSelectedItem) + { + selected = selectedItem; + SelectedItemChanged?.Invoke (new SelectedItemChangedEventArgs (selectedItem, previousSelectedItem)); + } + /// public override bool ProcessColdKey (KeyEvent kb) { @@ -184,7 +222,7 @@ namespace Terminal.Gui { nextIsHot = true; else { if (nextIsHot && c == key) { - Selected = i; + SelectedItem = i; cursor = i; if (!HasFocus) SuperView.SetFocus (this); @@ -218,7 +256,7 @@ namespace Terminal.Gui { } break; case Key.Space: - Selected = cursor; + SelectedItem = cursor; return true; } return base.ProcessKey (kb); @@ -233,7 +271,7 @@ namespace Terminal.Gui { SuperView.SetFocus (this); if (me.Y < radioLabels.Count) { - cursor = Selected = me.Y; + cursor = SelectedItem = me.Y; SetNeedsDisplay (); } return true; diff --git a/UICatalog/Scenarios/AllViewsTester.cs b/UICatalog/Scenarios/AllViewsTester.cs index fceb45e6a..ff689f3b2 100644 --- a/UICatalog/Scenarios/AllViewsTester.cs +++ b/UICatalog/Scenarios/AllViewsTester.cs @@ -235,7 +235,7 @@ namespace UICatalog { return; } try { - switch (_xRadioGroup.Selected) { + switch (_xRadioGroup.SelectedItem) { case 0: view.X = Pos.Percent (_xVal); break; @@ -250,7 +250,7 @@ namespace UICatalog { break; } - switch (_yRadioGroup.Selected) { + switch (_yRadioGroup.SelectedItem) { case 0: view.Y = Pos.Percent (_yVal); break; @@ -265,7 +265,7 @@ namespace UICatalog { break; } - switch (_wRadioGroup.Selected) { + switch (_wRadioGroup.SelectedItem) { case 0: view.Width = Dim.Percent (_wVal); break; @@ -277,7 +277,7 @@ namespace UICatalog { break; } - switch (_hRadioGroup.Selected) { + switch (_hRadioGroup.SelectedItem) { case 0: view.Height = Dim.Percent (_hVal); break; @@ -301,15 +301,15 @@ namespace UICatalog { { var x = view.X.ToString (); var y = view.Y.ToString (); - _xRadioGroup.Selected = posNames.IndexOf (posNames.Where (s => x.Contains (s)).First ()); - _yRadioGroup.Selected = posNames.IndexOf (posNames.Where (s => y.Contains (s)).First ()); + _xRadioGroup.SelectedItem = posNames.IndexOf (posNames.Where (s => x.Contains (s)).First ()); + _yRadioGroup.SelectedItem = posNames.IndexOf (posNames.Where (s => y.Contains (s)).First ()); _xText.Text = $"{view.Frame.X}"; _yText.Text = $"{view.Frame.Y}"; var w = view.Width.ToString (); var h = view.Height.ToString (); - _wRadioGroup.Selected = dimNames.IndexOf (dimNames.Where (s => w.Contains (s)).First ()); - _hRadioGroup.Selected = dimNames.IndexOf (dimNames.Where (s => h.Contains (s)).First ()); + _wRadioGroup.SelectedItem = dimNames.IndexOf (dimNames.Where (s => w.Contains (s)).First ()); + _hRadioGroup.SelectedItem = dimNames.IndexOf (dimNames.Where (s => h.Contains (s)).First ()); _wText.Text = $"{view.Frame.Width}"; _hText.Text = $"{view.Frame.Height}"; } diff --git a/UICatalog/Scenarios/Buttons.cs b/UICatalog/Scenarios/Buttons.cs index f73db1263..be51b1d79 100644 --- a/UICatalog/Scenarios/Buttons.cs +++ b/UICatalog/Scenarios/Buttons.cs @@ -171,9 +171,9 @@ namespace UICatalog { var radioGroup = new RadioGroup (new ustring [] { "Left", "Right", "Centered", "Justified" }) { X = 4, Y = Pos.Bottom (label) + 1, - Selected = 2, - SelectedItemChanged = (selected) => { - switch (selected) { + SelectedItem = 2, + SelectedItemChanged = (args) => { + switch (args.SelectedItem) { case 0: moveBtn.TextAlignment = TextAlignment.Left; sizeBtn.TextAlignment = TextAlignment.Left; diff --git a/UICatalog/Scenarios/CharacterMap.cs b/UICatalog/Scenarios/CharacterMap.cs index 7cb6ae4e2..60d30ee83 100644 --- a/UICatalog/Scenarios/CharacterMap.cs +++ b/UICatalog/Scenarios/CharacterMap.cs @@ -56,8 +56,8 @@ namespace UICatalog { jumpList.X = Pos.X (label); jumpList.Y = Pos.Bottom (label); jumpList.Width = Dim.Fill (); - jumpList.SelectedItemChanged = (selected) => { - charMap.Start = radioItems[selected].start; + jumpList.SelectedItemChanged = (args) => { + charMap.Start = radioItems[args.SelectedItem].start; }; Win.Add (jumpList); diff --git a/UICatalog/Scenarios/MessageBoxes.cs b/UICatalog/Scenarios/MessageBoxes.cs index eaf1061a4..8075ba51a 100644 --- a/UICatalog/Scenarios/MessageBoxes.cs +++ b/UICatalog/Scenarios/MessageBoxes.cs @@ -158,7 +158,7 @@ namespace UICatalog { for (int i = 0; i < numButtons; i++) { btns.Add(btnText[i % 10]); } - if (styleRadioGroup.Selected == 0) { + if (styleRadioGroup.SelectedItem == 0) { buttonPressedLabel.Text = $"{MessageBox.Query (width, height, titleEdit.Text.ToString (), messageEdit.Text.ToString (), btns.ToArray ())}"; } else { buttonPressedLabel.Text = $"{MessageBox.ErrorQuery (width, height, titleEdit.Text.ToString (), messageEdit.Text.ToString (), btns.ToArray ())}";