From febb868041025e12063a13219c0e839e23d559e8 Mon Sep 17 00:00:00 2001 From: tznind Date: Sun, 6 Oct 2024 10:26:24 +0100 Subject: [PATCH 1/6] When pressing right clear the selection instead of move tab - TextField --- Terminal.Gui/Views/TextField.cs | 40 +++++++++++++++---------------- UnitTests/Views/TextFieldTests.cs | 21 ++++++++++++++++ 2 files changed, 40 insertions(+), 21 deletions(-) diff --git a/Terminal.Gui/Views/TextField.cs b/Terminal.Gui/Views/TextField.cs index 64e3d0482e..c5a5c28a27 100644 --- a/Terminal.Gui/Views/TextField.cs +++ b/Terminal.Gui/Views/TextField.cs @@ -1531,19 +1531,27 @@ private void MoveHomeExtend () } } - private bool MoveLeft () + /// + /// Moves the cursor +/- the given , clearing + /// any selection and returning true if any meaningful changes were made. + /// + /// Distance to move the cursor, will be clamped to + /// text length. Positive for right, Negative for left. + /// + private bool Move (int distance) { + var oldCursorPosition = _cursorPosition; + var hadSelection = _selectedText != null && _selectedText.Length > 0; - if (_cursorPosition > 0) - { - ClearAllSelection (); - _cursorPosition--; - Adjust (); - - return true; - } + _cursorPosition = Math.Min (_text.Count, Math.Max (0, _cursorPosition + distance)); + ClearAllSelection (); + Adjust (); - return false; + return _cursorPosition != oldCursorPosition || hadSelection; + } + private bool MoveLeft () + { + return Move (-1); } private void MoveLeftExtend () @@ -1556,17 +1564,7 @@ private void MoveLeftExtend () private bool MoveRight () { - if (_cursorPosition == _text.Count) - { - return false; - } - - ClearAllSelection (); - - _cursorPosition++; - Adjust (); - - return true; + return Move (1); } private void MoveRightExtend () diff --git a/UnitTests/Views/TextFieldTests.cs b/UnitTests/Views/TextFieldTests.cs index a5c6fe4e1e..d9e8467e6f 100644 --- a/UnitTests/Views/TextFieldTests.cs +++ b/UnitTests/Views/TextFieldTests.cs @@ -2027,6 +2027,27 @@ public void Autocomplete__Added_To_SuperView_On_Add () Assert.Equal (2, superView.Subviews.Count); } + [Fact] + public void Right_CursorAtEnd_WithSelection_ShouldClearSelection () + { + var tf = new TextField + { + Text = "Hello", + }; + tf.SetFocus (); + tf.SelectAll (); + tf.CursorPosition = 5; + + // When there is selected text and the cursor is at the end of the text field + Assert.Equal ("Hello",tf.SelectedText); + + // Pressing right should not move focus, instead it should clear selection + Assert.True(tf.NewKeyDownEvent (Key.CursorRight)); + Assert.Null (tf.SelectedText); + + // Now that the selection is cleared another right keypress should move focus + Assert.False (tf.NewKeyDownEvent (Key.CursorRight)); + } [Fact] public void Autocomplete_Visible_False_By_Default () From 3e1e15199f3065841ccd6bc092c0ee5c9995e8f1 Mon Sep 17 00:00:00 2001 From: tznind Date: Sun, 6 Oct 2024 11:17:37 +0100 Subject: [PATCH 2/6] Add tests and start on TextView --- Terminal.Gui/Views/TextView.cs | 5 +++++ UnitTests/Views/TextFieldTests.cs | 25 +++++++++++++++++++++++++ 2 files changed, 30 insertions(+) diff --git a/Terminal.Gui/Views/TextView.cs b/Terminal.Gui/Views/TextView.cs index b81877b531..acef4f5ab8 100644 --- a/Terminal.Gui/Views/TextView.cs +++ b/Terminal.Gui/Views/TextView.cs @@ -5852,6 +5852,11 @@ private bool ProcessMoveLeft () // if the user presses Left (without any control keys) and they are at the start of the text if (CurrentColumn == 0 && CurrentRow == 0) { + if (Selecting) + { + StopSelecting (); + return true; + } // do not respond (this lets the key press fall through to navigation system - which usually changes focus backward) return false; } diff --git a/UnitTests/Views/TextFieldTests.cs b/UnitTests/Views/TextFieldTests.cs index d9e8467e6f..93658f5db0 100644 --- a/UnitTests/Views/TextFieldTests.cs +++ b/UnitTests/Views/TextFieldTests.cs @@ -2048,7 +2048,32 @@ public void Right_CursorAtEnd_WithSelection_ShouldClearSelection () // Now that the selection is cleared another right keypress should move focus Assert.False (tf.NewKeyDownEvent (Key.CursorRight)); } + [Fact] + public void Left_CursorAtStart_WithSelection_ShouldClearSelection () + { + var tf = new TextField + { + Text = "Hello", + }; + tf.SetFocus (); + + tf.CursorPosition = 2; + Assert.True (tf.NewKeyDownEvent (Key.CursorLeft.WithShift)); + Assert.True (tf.NewKeyDownEvent (Key.CursorLeft.WithShift)); + // When there is selected text and the cursor is at the end of the text field + Assert.Equal ("He", tf.SelectedText); + + // Pressing left should not move focus, instead it should clear selection + Assert.True (tf.NewKeyDownEvent (Key.CursorLeft)); + Assert.Null (tf.SelectedText); + + // When clearing selected text with left the cursor should be at the start of the selection + Assert.Equal (0,tf.CursorPosition); + + // Now that the selection is cleared another left keypress should move focus + Assert.False (tf.NewKeyDownEvent (Key.CursorLeft)); + } [Fact] public void Autocomplete_Visible_False_By_Default () { From 1afbaa617c155cdd47742dff279a95b3e0440f4d Mon Sep 17 00:00:00 2001 From: tznind Date: Sun, 6 Oct 2024 11:27:20 +0100 Subject: [PATCH 3/6] Finish tests for TextView --- Terminal.Gui/Views/TextView.cs | 7 +++++ UnitTests/Views/TextViewTests.cs | 54 ++++++++++++++++++++++++++++++++ 2 files changed, 61 insertions(+) diff --git a/Terminal.Gui/Views/TextView.cs b/Terminal.Gui/Views/TextView.cs index acef4f5ab8..493062aa0c 100644 --- a/Terminal.Gui/Views/TextView.cs +++ b/Terminal.Gui/Views/TextView.cs @@ -5890,6 +5890,13 @@ private bool ProcessMoveRight () // if they are at the very end of all the text do not respond (this lets the key press fall through to navigation system - which usually changes focus forward) if (CurrentColumn == lastCol && CurrentRow == lastRow) { + // Unless they have text selected + if (Selecting) + { + // In which case clear + StopSelecting (); + return true; + } return false; } diff --git a/UnitTests/Views/TextViewTests.cs b/UnitTests/Views/TextViewTests.cs index 2ab55e7aea..82498bf13f 100644 --- a/UnitTests/Views/TextViewTests.cs +++ b/UnitTests/Views/TextViewTests.cs @@ -8660,4 +8660,58 @@ public void Autocomplete_Visible_False_By_Default () Assert.True (t.Visible); Assert.False (t.Autocomplete.Visible); } + + + [Fact] + public void Right_CursorAtEnd_WithSelection_ShouldClearSelection () + { + var tv = new TextView + { + Text = "Hello", + }; + tv.SetFocus (); + + tv.NewKeyDownEvent (Key.End.WithShift); + Assert.Equal (5,tv.CursorPosition.X); + + // When there is selected text and the cursor is at the end of the text field + Assert.Equal ("Hello", tv.SelectedText); + + // Pressing right should not move focus, instead it should clear selection + Assert.True (tv.NewKeyDownEvent (Key.CursorRight)); + Assert.Empty (tv.SelectedText); + + // Now that the selection is cleared another right keypress should move focus + Assert.False (tv.NewKeyDownEvent (Key.CursorRight)); + } + [Fact] + public void Left_CursorAtStart_WithSelection_ShouldClearSelection () + { + var tv = new TextView + { + Text = "Hello", + }; + tv.SetFocus (); + + tv.NewKeyDownEvent (Key.CursorRight); + tv.NewKeyDownEvent (Key.CursorRight); + + Assert.Equal (2,tv.CursorPosition.X); + + Assert.True (tv.NewKeyDownEvent (Key.CursorLeft.WithShift)); + Assert.True (tv.NewKeyDownEvent (Key.CursorLeft.WithShift)); + + // When there is selected text and the cursor is at the end of the text field + Assert.Equal ("He", tv.SelectedText); + + // Pressing left should not move focus, instead it should clear selection + Assert.True (tv.NewKeyDownEvent (Key.CursorLeft)); + Assert.Empty (tv.SelectedText); + + // When clearing selected text with left the cursor should be at the start of the selection + Assert.Equal (0, tv.CursorPosition.X); + + // Now that the selection is cleared another left keypress should move focus + Assert.False (tv.NewKeyDownEvent (Key.CursorLeft)); + } } From 80cd3d7ca8d0bc3f301a12345b2769f984388a60 Mon Sep 17 00:00:00 2001 From: tznind Date: Sun, 6 Oct 2024 11:28:29 +0100 Subject: [PATCH 4/6] Fix typos --- Terminal.Gui/Views/TextField.cs | 1 + UnitTests/Views/TextFieldTests.cs | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/Terminal.Gui/Views/TextField.cs b/Terminal.Gui/Views/TextField.cs index c5a5c28a27..1b87f8591c 100644 --- a/Terminal.Gui/Views/TextField.cs +++ b/Terminal.Gui/Views/TextField.cs @@ -1549,6 +1549,7 @@ private bool Move (int distance) return _cursorPosition != oldCursorPosition || hadSelection; } + private bool MoveLeft () { return Move (-1); diff --git a/UnitTests/Views/TextFieldTests.cs b/UnitTests/Views/TextFieldTests.cs index 93658f5db0..c6caa944b5 100644 --- a/UnitTests/Views/TextFieldTests.cs +++ b/UnitTests/Views/TextFieldTests.cs @@ -2061,7 +2061,7 @@ public void Left_CursorAtStart_WithSelection_ShouldClearSelection () Assert.True (tf.NewKeyDownEvent (Key.CursorLeft.WithShift)); Assert.True (tf.NewKeyDownEvent (Key.CursorLeft.WithShift)); - // When there is selected text and the cursor is at the end of the text field + // When there is selected text and the cursor is at the start of the text field Assert.Equal ("He", tf.SelectedText); // Pressing left should not move focus, instead it should clear selection From f82b135ef6b1f10d93fb183432292581bb3489ba Mon Sep 17 00:00:00 2001 From: tznind Date: Sun, 6 Oct 2024 12:31:27 +0100 Subject: [PATCH 5/6] typo fix x2 --- UnitTests/Views/TextViewTests.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/UnitTests/Views/TextViewTests.cs b/UnitTests/Views/TextViewTests.cs index 82498bf13f..cecaa1ecf0 100644 --- a/UnitTests/Views/TextViewTests.cs +++ b/UnitTests/Views/TextViewTests.cs @@ -8701,7 +8701,7 @@ public void Left_CursorAtStart_WithSelection_ShouldClearSelection () Assert.True (tv.NewKeyDownEvent (Key.CursorLeft.WithShift)); Assert.True (tv.NewKeyDownEvent (Key.CursorLeft.WithShift)); - // When there is selected text and the cursor is at the end of the text field + // When there is selected text and the cursor is at the start of the text field Assert.Equal ("He", tv.SelectedText); // Pressing left should not move focus, instead it should clear selection From 10cf49fe8b03e3fdbee3084696cdc01653017eac Mon Sep 17 00:00:00 2001 From: tznind Date: Fri, 11 Oct 2024 10:58:55 +0100 Subject: [PATCH 6/6] Fix for Selecting being renamed IsSelecting --- Terminal.Gui/Views/TextView.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Terminal.Gui/Views/TextView.cs b/Terminal.Gui/Views/TextView.cs index 7c571d0344..674d8328ed 100644 --- a/Terminal.Gui/Views/TextView.cs +++ b/Terminal.Gui/Views/TextView.cs @@ -5958,7 +5958,7 @@ private bool ProcessMoveLeft () // if the user presses Left (without any control keys) and they are at the start of the text if (CurrentColumn == 0 && CurrentRow == 0) { - if (Selecting) + if (IsSelecting) { StopSelecting (); return true; @@ -5997,7 +5997,7 @@ private bool ProcessMoveRight () if (CurrentColumn == lastCol && CurrentRow == lastRow) { // Unless they have text selected - if (Selecting) + if (IsSelecting) { // In which case clear StopSelecting ();