Ver Fonte

Add TextChangedEventArgs (correctly this time)

tznind há 2 anos atrás
pai
commit
53597b7cec

+ 6 - 6
Terminal.Gui/Views/ComboBox.cs

@@ -188,7 +188,7 @@ namespace Terminal.Gui {
 				if (SuperView != null && SuperView.Subviews.Contains (this)) {
 					SelectedItem = -1;
 					search.Text = "";
-					Search_Changed ("");
+					Search_Changed (this, new TextChangedEventArgs(""));
 					SetNeedsDisplay ();
 				}
 			}
@@ -328,7 +328,7 @@ namespace Terminal.Gui {
 
 				SetNeedsLayout ();
 				SetNeedsDisplay ();
-				Search_Changed (Text);
+				Search_Changed (this, new TextChangedEventArgs( Text));
 			};
 
 			// Things this view knows how to do
@@ -750,7 +750,7 @@ namespace Terminal.Gui {
 
 			SetValue (searchset [listview.SelectedItem]);
 			search.CursorPosition = search.Text.ConsoleWidth;
-			Search_Changed (search.Text);
+			Search_Changed (this, new TextChangedEventArgs(search.Text));
 			OnOpenSelectedItem ();
 			Reset (keepSearchText: true);
 			HideList ();
@@ -806,15 +806,15 @@ namespace Terminal.Gui {
 			}
 		}
 
-		private void Search_Changed (ustring text)
+		private void Search_Changed (object sender, TextChangedEventArgs e)
 		{
 			if (source == null) { // Object initialization		
 				return;
 			}
 
-			if (ustring.IsNullOrEmpty (search.Text) && ustring.IsNullOrEmpty (text)) {
+			if (ustring.IsNullOrEmpty (search.Text) && ustring.IsNullOrEmpty (e.OldValue)) {
 				ResetSearchSet ();
-			} else if (search.Text != text) {
+			} else if (search.Text != e.OldValue) {
 				isShow = true;
 				ResetSearchSet (noCopy: true);
 

+ 3 - 3
Terminal.Gui/Views/DateField.cs

@@ -106,13 +106,13 @@ namespace Terminal.Gui {
 			AddKeyBinding (Key.F | Key.CtrlMask, Command.Right);
 		}
 
-		void DateField_Changed (ustring e)
+		void DateField_Changed (object sender, TextChangedEventArgs e)
 		{
 			try {
 				if (!DateTime.TryParseExact (GetDate (Text).ToString (), GetInvarianteFormat (), CultureInfo.CurrentCulture, DateTimeStyles.None, out DateTime result))
-					Text = e;
+					Text = e.OldValue;
 			} catch (Exception) {
-				Text = e;
+				Text = e.OldValue;
 			}
 		}
 

+ 20 - 2
Terminal.Gui/Views/TextField.cs

@@ -53,7 +53,7 @@ namespace Terminal.Gui {
 		/// <remarks>
 		///   The passed <see cref="EventArgs"/> is a <see cref="NStack.ustring"/> containing the old value. 
 		/// </remarks>
-		public event Action<ustring> TextChanged;
+		public event EventHandler<TextChangedEventArgs> TextChanged;
 
 		/// <summary>
 		/// Initializes a new instance of the <see cref="TextField"/> class using <see cref="LayoutStyle.Computed"/> positioning.
@@ -308,7 +308,7 @@ namespace Terminal.Gui {
 						, HistoryText.LineStatus.Replaced);
 				}
 
-				TextChanged?.Invoke (oldText);
+				TextChanged?.Invoke (this, new TextChangedEventArgs(oldText));
 
 				if (point > text.Count) {
 					point = Math.Max (TextModel.DisplaySize (text, 0).size - 1, 0);
@@ -1331,7 +1331,25 @@ namespace Terminal.Gui {
 			NewText = newText;
 		}
 	}
+	/// <summary>
+	/// Event args for the <see cref="TextField.TextChanged"/> event
+	/// </summary>
+	public class TextChangedEventArgs : EventArgs {
+
+		/// <summary>
+		/// Creates a new instance of the <see cref="TextChangedEventArgs"/> class
+		/// </summary>
+		/// <param name="oldValue"></param>
+		public TextChangedEventArgs (ustring oldValue)
+		{
+			OldValue = oldValue;
+		}
 
+		/// <summary>
+		/// The old value before the text changed
+		/// </summary>
+		public ustring OldValue { get; }
+	}
 	/// <summary>
 	/// Renders an overlay on another view at a given point that allows selecting
 	/// from a range of 'autocomplete' options.

+ 3 - 3
Terminal.Gui/Views/TimeField.cs

@@ -106,13 +106,13 @@ namespace Terminal.Gui {
 			AddKeyBinding (Key.F | Key.CtrlMask, Command.Right);
 		}
 
-		void TextField_TextChanged (ustring e)
+		void TextField_TextChanged (object sender, TextChangedEventArgs e)
 		{
 			try {
 				if (!TimeSpan.TryParseExact (Text.ToString ().Trim (), format.Trim (), CultureInfo.CurrentCulture, TimeSpanStyles.None, out TimeSpan result))
-					Text = e;
+					Text = e.OldValue;
 			} catch (Exception) {
-				Text = e;
+				Text = e.OldValue;
 			}
 		}
 

+ 2 - 2
Terminal.Gui/Windows/FileDialog.cs

@@ -649,7 +649,7 @@ namespace Terminal.Gui {
 				Y = 1 + msgLines,
 				Width = Dim.Fill () - 1,
 			};
-			dirEntry.TextChanged += (e) => {
+			dirEntry.TextChanged += (s, e) => {
 				DirectoryPath = dirEntry.Text;
 				nameEntry.Text = ustring.Empty;
 			};
@@ -731,7 +731,7 @@ namespace Terminal.Gui {
 			};
 			AddButton (this.prompt);
 
-			nameEntry.TextChanged += (e) => {
+			nameEntry.TextChanged += (s,e) => {
 				if (nameEntry.Text.IsEmpty) {
 					this.prompt.Enabled = false;
 				} else {

+ 4 - 4
UICatalog/Scenarios/AllViewsTester.cs

@@ -126,7 +126,7 @@ namespace UICatalog.Scenarios {
 			};
 			_xRadioGroup.SelectedItemChanged += (s, selected) => DimPosChanged (_curView);
 			_xText = new TextField ($"{_xVal}") { X = Pos.Right (label) + 1, Y = 0, Width = 4 };
-			_xText.TextChanged += (args) => {
+			_xText.TextChanged += (s, args) => {
 				try {
 					_xVal = int.Parse (_xText.Text.ToString ());
 					DimPosChanged (_curView);
@@ -142,7 +142,7 @@ namespace UICatalog.Scenarios {
 			label = new Label ("y:") { X = Pos.Right (_xRadioGroup) + 1, Y = 0 };
 			_locationFrame.Add (label);
 			_yText = new TextField ($"{_yVal}") { X = Pos.Right (label) + 1, Y = 0, Width = 4 };
-			_yText.TextChanged += (args) => {
+			_yText.TextChanged += (s,args) => {
 				try {
 					_yVal = int.Parse (_yText.Text.ToString ());
 					DimPosChanged (_curView);
@@ -174,7 +174,7 @@ namespace UICatalog.Scenarios {
 			};
 			_wRadioGroup.SelectedItemChanged += (s, selected) => DimPosChanged (_curView);
 			_wText = new TextField ($"{_wVal}") { X = Pos.Right (label) + 1, Y = 0, Width = 4 };
-			_wText.TextChanged += (args) => {
+			_wText.TextChanged += (s,args) => {
 				try {
 					switch (_wRadioGroup.SelectedItem) {
 					case 0:
@@ -197,7 +197,7 @@ namespace UICatalog.Scenarios {
 			label = new Label ("height:") { X = Pos.Right (_wRadioGroup) + 1, Y = 0 };
 			_sizeFrame.Add (label);
 			_hText = new TextField ($"{_hVal}") { X = Pos.Right (label) + 1, Y = 0, Width = 4 };
-			_hText.TextChanged += (args) => {
+			_hText.TextChanged += (s, args) => {
 				try {
 					switch (_hRadioGroup.SelectedItem) {
 					case 0:

+ 1 - 1
UICatalog/Scenarios/CharacterMap.cs

@@ -39,7 +39,7 @@ namespace UICatalog.Scenarios {
 			Win.Add (jumpEdit);
 			var unicodeLabel = new Label ("") { X = Pos.Right (jumpEdit) + 1, Y = Pos.Y (_charMap) };
 			Win.Add (unicodeLabel);
-			jumpEdit.TextChanged += (s) => {
+			jumpEdit.TextChanged += (s, e) => {
 				uint result = 0;
 				if (jumpEdit.Text.Length == 0) return;
 				try {

+ 1 - 1
UICatalog/Scenarios/CsvEditor.cs

@@ -99,7 +99,7 @@ namespace UICatalog.Scenarios {
 			SetupScrollBar ();
 		}
 
-		private void SelectedCellLabel_TextChanged (ustring last)
+		private void SelectedCellLabel_TextChanged (object sender, TextChangedEventArgs e)
 		{
 			// if user is in the text control and editing the selected cell
 			if (!selectedCellLabel.HasFocus)

+ 1 - 1
UICatalog/Scenarios/DynamicMenuBar.cs

@@ -89,7 +89,7 @@ namespace UICatalog.Scenarios {
 					X = Pos.Center (),
 					Width = 2,
 				};
-				_txtDelimiter.TextChanged += (_) => MenuBar.ShortcutDelimiter = _txtDelimiter.Text;
+				_txtDelimiter.TextChanged += (s, _) => MenuBar.ShortcutDelimiter = _txtDelimiter.Text;
 				_frmDelimiter.Add (_txtDelimiter);
 
 				Add (_frmDelimiter);

+ 1 - 1
UICatalog/Scenarios/DynamicStatusBar.cs

@@ -77,7 +77,7 @@ namespace UICatalog.Scenarios {
 					X = Pos.Center (),
 					Width = 2,
 				};
-				_txtDelimiter.TextChanged += (_) => StatusBar.ShortcutDelimiter = _txtDelimiter.Text;
+				_txtDelimiter.TextChanged += (s, _) => StatusBar.ShortcutDelimiter = _txtDelimiter.Text;
 				_frmDelimiter.Add (_txtDelimiter);
 
 				Add (_frmDelimiter);

+ 3 - 3
UICatalog/Scenarios/Editor.cs

@@ -813,7 +813,7 @@ namespace UICatalog.Scenarios {
 			btnFindPrevious.Clicked += (s,e) => FindPrevious ();
 			d.Add (btnFindPrevious);
 
-			txtToFind.TextChanged += (e) => {
+			txtToFind.TextChanged += (s, e) => {
 				_textToFind = txtToFind.Text.ToString ();
 				_textView.FindTextChanged ();
 				btnFindNext.Enabled = !txtToFind.Text.IsEmpty;
@@ -908,7 +908,7 @@ namespace UICatalog.Scenarios {
 				Y = Pos.Top (label),
 				Width = 20
 			};
-			txtToReplace.TextChanged += (e) => _textToReplace = txtToReplace.Text.ToString ();
+			txtToReplace.TextChanged += (s, e) => _textToReplace = txtToReplace.Text.ToString ();
 			d.Add (txtToReplace);
 
 			var btnFindPrevious = new Button ("Replace _Previous") {
@@ -933,7 +933,7 @@ namespace UICatalog.Scenarios {
 			btnReplaceAll.Clicked += (s,e) => ReplaceAll ();
 			d.Add (btnReplaceAll);
 
-			txtToFind.TextChanged += (e) => {
+			txtToFind.TextChanged += (s, e) => {
 				_textToFind = txtToFind.Text.ToString ();
 				_textView.FindTextChanged ();
 				btnFindNext.Enabled = !txtToFind.Text.IsEmpty;

+ 2 - 2
UICatalog/Scenarios/Progress.cs

@@ -167,7 +167,7 @@ namespace UICatalog.Scenarios {
 				systemTimerDemo.PulseProgressBar.Fraction = 1F;
 			};
 			systemTimerDemo.Speed.Text = $"{_systemTimerTick}";
-			systemTimerDemo.Speed.TextChanged += (a) => {
+			systemTimerDemo.Speed.TextChanged += (s, a) => {
 				uint result;
 				if (uint.TryParse (systemTimerDemo.Speed.Text.ToString(), out result)) {
 					_systemTimerTick = result;
@@ -210,7 +210,7 @@ namespace UICatalog.Scenarios {
 			};
 
 			mainLoopTimeoutDemo.Speed.Text = $"{_mainLooopTimeoutTick}";
-			mainLoopTimeoutDemo.Speed.TextChanged += (a) => {
+			mainLoopTimeoutDemo.Speed.TextChanged += (s, a) => {
 				uint result;
 				if (uint.TryParse (mainLoopTimeoutDemo.Speed.Text.ToString (), out result)) {
 					_mainLooopTimeoutTick = result;

+ 2 - 2
UICatalog/Scenarios/Text.cs

@@ -41,7 +41,7 @@ namespace UICatalog.Scenarios {
 			};
 			Win.Add (labelMirroringTextField);
 
-			textField.TextChanged += (prev) => {
+			textField.TextChanged += (s, prev) => {
 				labelMirroringTextField.Text = textField.Text;
 			};
 
@@ -159,7 +159,7 @@ namespace UICatalog.Scenarios {
 			};
 			Win.Add (labelMirroringDateField);
 
-			dateField.TextChanged += (prev) => {
+			dateField.TextChanged += (s, prev) => {
 				labelMirroringDateField.Text = dateField.Text;
 			};
 

+ 1 - 1
UICatalog/Scenarios/TileViewNesting.cs

@@ -36,7 +36,7 @@ namespace UICatalog.Scenarios {
 				Text = "2",
 			};
 
-			textField.TextChanged += (s) => SetupTileView ();
+			textField.TextChanged += (s,e) => SetupTileView ();
 
 
 			cbHorizontal = new CheckBox ("Horizontal") {

+ 4 - 4
UnitTests/UICatalog/ScenarioTests.cs

@@ -312,7 +312,7 @@ namespace UICatalog.Tests {
 
 			_xRadioGroup.SelectedItemChanged += (s, selected) => DimPosChanged (_curView);
 
-			_xText.TextChanged += (args) => {
+			_xText.TextChanged += (s, args) => {
 				try {
 					_xVal = int.Parse (_xText.Text.ToString ());
 					DimPosChanged (_curView);
@@ -321,7 +321,7 @@ namespace UICatalog.Tests {
 				}
 			};
 
-			_yText.TextChanged += (args) => {
+			_yText.TextChanged += (s,e) => {
 				try {
 					_yVal = int.Parse (_yText.Text.ToString ());
 					DimPosChanged (_curView);
@@ -334,7 +334,7 @@ namespace UICatalog.Tests {
 
 			_wRadioGroup.SelectedItemChanged += (s, selected) => DimPosChanged (_curView);
 
-			_wText.TextChanged += (args) => {
+			_wText.TextChanged += (s, args) => {
 				try {
 					_wVal = int.Parse (_wText.Text.ToString ());
 					DimPosChanged (_curView);
@@ -343,7 +343,7 @@ namespace UICatalog.Tests {
 				}
 			};
 
-			_hText.TextChanged += (args) => {
+			_hText.TextChanged += (s, args) => {
 				try {
 					_hVal = int.Parse (_hText.Text.ToString ());
 					DimPosChanged (_curView);

+ 3 - 3
UnitTests/Views/TextFieldTests.cs

@@ -681,8 +681,8 @@ namespace Terminal.Gui.ViewTests {
 		[TextFieldTestsAutoInitShutdown]
 		public void TextChanged_Event ()
 		{
-			_textField.TextChanged += (e) => {
-				Assert.Equal ("TAB to jump between text fields.", e);
+			_textField.TextChanged += (s,e) => {
+				Assert.Equal ("TAB to jump between text fields.", e.OldValue);
 			};
 
 			_textField.Text = "changed";
@@ -1138,7 +1138,7 @@ namespace Terminal.Gui.ViewTests {
 			var tf = new TextField () { Width = 10, Text = "-1" };
 
 			tf.TextChanging += (s,e) => newText = e.NewText.ToString ();
-			tf.TextChanged += (e) => oldText = e.ToString ();
+			tf.TextChanged += (s, e) => oldText = e.OldValue.ToString ();
 
 			Application.Top.Add (tf);
 			Application.Begin (Application.Top);