Browse Source

Fixes #2453. TextField OnEnter throws exception if IsInitialized is false.

BDisp 2 years ago
parent
commit
f0f0d008ca
2 changed files with 26 additions and 13 deletions
  1. 11 9
      Terminal.Gui/Views/TextField.cs
  2. 15 4
      UnitTests/Views/TextFieldTests.cs

+ 11 - 9
Terminal.Gui/Views/TextField.cs

@@ -249,6 +249,16 @@ namespace Terminal.Gui {
 			Autocomplete.PopupInsideContainer = false;
 		}
 
+		///<inheritdoc/>
+		public override bool OnEnter (View view)
+		{
+			if (IsInitialized) {
+				Application.Driver.SetCursorVisibility (DesiredCursorVisibility);
+			}
+
+			return base.OnEnter (view);
+		}
+
 		///<inheritdoc/>
 		public override bool OnLeave (View view)
 		{
@@ -308,7 +318,7 @@ namespace Terminal.Gui {
 						, HistoryText.LineStatus.Replaced);
 				}
 
-				TextChanged?.Invoke (this, new TextChangedEventArgs(oldText));
+				TextChanged?.Invoke (this, new TextChangedEventArgs (oldText));
 
 				if (point > text.Count) {
 					point = Math.Max (TextModel.DisplaySize (text, 0).size - 1, 0);
@@ -1269,14 +1279,6 @@ namespace Terminal.Gui {
 			}
 		}
 
-		///<inheritdoc/>
-		public override bool OnEnter (View view)
-		{
-			Application.Driver.SetCursorVisibility (DesiredCursorVisibility);
-
-			return base.OnEnter (view);
-		}
-
 		/// <summary>
 		/// Inserts the given <paramref name="toAdd"/> text at the current cursor position
 		/// exactly as if the user had just typed it

+ 15 - 4
UnitTests/Views/TextFieldTests.cs

@@ -663,7 +663,7 @@ namespace Terminal.Gui.ViewTests {
 		{
 			bool cancel = true;
 
-			_textField.TextChanging += (s,e) => {
+			_textField.TextChanging += (s, e) => {
 				Assert.Equal ("changing", e.NewText);
 				if (cancel) {
 					e.Cancel = true;
@@ -681,7 +681,7 @@ namespace Terminal.Gui.ViewTests {
 		[TextFieldTestsAutoInitShutdown]
 		public void TextChanged_Event ()
 		{
-			_textField.TextChanged += (s,e) => {
+			_textField.TextChanged += (s, e) => {
 				Assert.Equal ("TAB to jump between text fields.", e.OldValue);
 			};
 
@@ -781,7 +781,7 @@ namespace Terminal.Gui.ViewTests {
 			Assert.Equal ("A", tf.Text.ToString ());
 
 			// cancel the next keystroke
-			tf.TextChanging += (s,e) => e.Cancel = e.NewText == "AB";
+			tf.TextChanging += (s, e) => e.Cancel = e.NewText == "AB";
 			tf.ProcessKey (new KeyEvent (Key.B, new KeyModifiers ()));
 
 			// B was canceled so should just be A
@@ -1137,7 +1137,7 @@ namespace Terminal.Gui.ViewTests {
 			var oldText = "";
 			var tf = new TextField () { Width = 10, Text = "-1" };
 
-			tf.TextChanging += (s,e) => newText = e.NewText.ToString ();
+			tf.TextChanging += (s, e) => newText = e.NewText.ToString ();
 			tf.TextChanged += (s, e) => oldText = e.OldValue.ToString ();
 
 			Application.Top.Add (tf);
@@ -1440,5 +1440,16 @@ Les Miśerables", output);
 			TestHelpers.AssertDriverContentsWithFrameAre (@"
 ắ", output);
 		}
+
+		[Fact]
+		public void OnEnter_Does_Not_Throw_If_Not_IsInitialized_SetCursorVisibility ()
+		{
+			var top = new Toplevel ();
+			var tf = new TextField () { Width = 10 };
+			top.Add (tf);
+
+			var exception = Record.Exception (tf.SetFocus);
+			Assert.Null (exception);
+		}
 	}
 }