Skip to content

Commit

Permalink
Merge pull request #2454 from BDisp/v2_textfield-onenter-fix_2453
Browse files Browse the repository at this point in the history
Fixes #2453. TextField OnEnter throws exception if IsInitialized is false.
  • Loading branch information
tig authored Mar 27, 2023
2 parents f518ddf + f0f0d00 commit 33c9a5f
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 13 deletions.
20 changes: 11 additions & 9 deletions Terminal.Gui/Views/TextField.cs
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,16 @@ void TextField_Initialized (object sender, EventArgs e)
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)
{
Expand Down Expand Up @@ -308,7 +318,7 @@ public override Rect Frame {
, 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);
Expand Down Expand Up @@ -1269,14 +1279,6 @@ public CursorVisibility DesiredCursorVisibility {
}
}

///<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
Expand Down
19 changes: 15 additions & 4 deletions UnitTests/Views/TextFieldTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -663,7 +663,7 @@ public void TextChanging_Event ()
{
bool cancel = true;

_textField.TextChanging += (s,e) => {
_textField.TextChanging += (s, e) => {
Assert.Equal ("changing", e.NewText);
if (cancel) {
e.Cancel = true;
Expand All @@ -681,7 +681,7 @@ public void TextChanging_Event ()
[TextFieldTestsAutoInitShutdown]
public void TextChanged_Event ()
{
_textField.TextChanged += (s,e) => {
_textField.TextChanged += (s, e) => {
Assert.Equal ("TAB to jump between text fields.", e.OldValue);
};

Expand Down Expand Up @@ -781,7 +781,7 @@ public void Cancel_TextChanging_ThenBackspace ()
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
Expand Down Expand Up @@ -1137,7 +1137,7 @@ public void DeleteSelectedText_InsertText_DeleteCharLeft_DeleteCharRight_Cut ()
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);
Expand Down Expand Up @@ -1440,5 +1440,16 @@ public void Accented_Letter_With_Three_Combining_Unicode_Chars ()
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);
}
}
}

0 comments on commit 33c9a5f

Please sign in to comment.