Browse Source

RadioGroup event fix

Charlie Kindel 5 years ago
parent
commit
f00cdb0258

+ 48 - 10
Terminal.Gui/Views/RadioGroup.cs

@@ -8,7 +8,8 @@ namespace Terminal.Gui {
 	/// <see cref="RadioGroup"/> shows a group of radio labels, only one of those can be selected at a given time
 	/// </summary>
 	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
 		/// <summary>
-		/// Invoked when the selected radio label has changed. The passed <c>int</c> indicates the newly selected item.
+		/// Event arguments for the SelectedItemChagned event.
 		/// </summary>
-		public Action<int> SelectedItemChanged;
+		public class SelectedItemChangedEventArgs : EventArgs {
+			/// <summary>
+			/// Gets the index of the item that was previously selected. -1 if there was no previous selection.
+			/// </summary>
+			public int PreviousSelectedItem { get;  }
+
+			/// <summary>
+			/// Gets the index of the item that is now selected. -1 if there is no selection.
+			/// </summary>
+			public int SelectedItem { get; }
+
+			/// <summary>
+			/// Initializes a new <see cref="SelectedItemChangedEventArgs"/> class.
+			/// </summary>
+			/// <param name="selectedItem"></param>
+			/// <param name="previousSelectedItem"></param>
+			public SelectedItemChangedEventArgs(int selectedItem, int previousSelectedItem)
+			{
+				PreviousSelectedItem = previousSelectedItem;
+				SelectedItem = selectedItem;
+			}
+		}
+
+		/// <summary>
+		/// Invoked when the selected radio label has changed.
+		/// </summary>
+		public Action<SelectedItemChangedEventArgs> SelectedItemChanged;
 
 		/// <summary>
 		/// The currently selected item from the list of radio labels
 		/// </summary>
 		/// <value>The selected.</value>
-		public int Selected {
+		public int SelectedItem {
 			get => selected;
 			set {
-				selected = value;
-				SelectedItemChanged?.Invoke (selected);
+				OnSelectedItemChanged (value, SelectedItem);
 				SetNeedsDisplay ();
 			}
 		}
 
+		/// <summary>
+		/// Called whenever the current selected item changes. Invokes the <see cref="SelectedItemChanged"/> event.
+		/// </summary>
+		/// <param name="selectedItem"></param>
+		/// <param name="previousSelectedItem"></param>
+		public virtual void OnSelectedItemChanged (int selectedItem, int previousSelectedItem)
+		{
+			selected = selectedItem;
+			SelectedItemChanged?.Invoke (new SelectedItemChangedEventArgs (selectedItem, previousSelectedItem));
+		}
+
 		///<inheritdoc/>
 		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;

+ 8 - 8
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}";
 		}

+ 3 - 3
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;

+ 2 - 2
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);

+ 1 - 1
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 ())}";