Bläddra i källkod

Fixed TextView

Tig 10 månader sedan
förälder
incheckning
a33ef5ad9a
3 ändrade filer med 133 tillägg och 125 borttagningar
  1. 92 109
      Terminal.Gui/Views/TextField.cs
  2. 3 4
      Terminal.Gui/Views/TextView.cs
  3. 38 12
      UnitTests/Views/TextViewTests.cs

+ 92 - 109
Terminal.Gui/Views/TextField.cs

@@ -1,4 +1,3 @@
-using System.Data;
 using System.Globalization;
 using Terminal.Gui.Resources;
 
@@ -25,14 +24,14 @@ public class TextField : View
     /// </summary>
     public TextField ()
     {
-        _historyText = new HistoryText ();
+        _historyText = new ();
         _isButtonReleased = true;
         _selectedStart = -1;
-        _text = new List<Rune> ();
-        CaptionColor = new Color (Color.DarkGray);
+        _text = new ();
+        CaptionColor = new (Color.DarkGray);
         ReadOnly = false;
         Autocomplete = new TextFieldAutocomplete ();
-        Height = Dim.Auto (DimAutoStyle.Text, minimumContentDim: 1);
+        Height = Dim.Auto (DimAutoStyle.Text, 1);
 
         CanFocus = true;
         CursorVisibility = CursorVisibility.Default;
@@ -405,13 +404,12 @@ public class TextField : View
 
         _currentCulture = Thread.CurrentThread.CurrentUICulture;
 
-        ContextMenu = new ContextMenu { Host = this };
+        ContextMenu = new() { Host = this };
         ContextMenu.KeyChanged += ContextMenu_KeyChanged;
 
         KeyBindings.Add (ContextMenu.Key, KeyBindingScope.HotKey, Command.Context);
     }
 
-
     /// <summary>
     ///     Provides autocomplete context menu based on suggestions at the current cursor position. Configure
     ///     <see cref="ISuggestionGenerator"/> to enable this feature.
@@ -545,13 +543,13 @@ public class TextField : View
             if (!Secret && !_historyText.IsFromHistory)
             {
                 _historyText.Add (
-                                  new List<List<RuneCell>> { TextModel.ToRuneCellList (oldText) },
-                                  new Point (_cursorPosition, 0)
+                                  new() { TextModel.ToRuneCellList (oldText) },
+                                  new (_cursorPosition, 0)
                                  );
 
                 _historyText.Add (
-                                  new List<List<RuneCell>> { TextModel.ToRuneCells (_text) },
-                                  new Point (_cursorPosition, 0),
+                                  new() { TextModel.ToRuneCells (_text) },
+                                  new (_cursorPosition, 0),
                                   HistoryText.LineStatus.Replaced
                                  );
             }
@@ -647,8 +645,8 @@ public class TextField : View
         }
 
         _historyText.Add (
-                          new List<List<RuneCell>> { TextModel.ToRuneCells (_text) },
-                          new Point (_cursorPosition, 0)
+                          new() { TextModel.ToRuneCells (_text) },
+                          new (_cursorPosition, 0)
                          );
 
         if (SelectedLength == 0)
@@ -701,8 +699,8 @@ public class TextField : View
         }
 
         _historyText.Add (
-                          new List<List<RuneCell>> { TextModel.ToRuneCells (_text) },
-                          new Point (_cursorPosition, 0)
+                          new() { TextModel.ToRuneCells (_text) },
+                          new (_cursorPosition, 0)
                          );
 
         if (SelectedLength == 0)
@@ -727,10 +725,7 @@ public class TextField : View
     }
 
     /// <inheritdoc/>
-    public override Attribute GetNormalColor ()
-    {
-        return GetFocusColor ();
-    }
+    public override Attribute GetNormalColor () { return GetFocusColor (); }
 
     /// <summary>
     ///     Inserts the given <paramref name="toAdd"/> text at the current cursor position exactly as if the user had just
@@ -1042,8 +1037,6 @@ public class TextField : View
 
         //if (SelectedLength != 0 && !(Application.MouseGrabView is MenuBar))
         //	ClearAllSelection ();
-
-        return;
     }
 
     /// TODO: Flush out these docs
@@ -1144,6 +1137,7 @@ public class TextField : View
 
         int pos = _cursorPosition - ScrollOffset + Math.Min (Frame.X, 0);
         Move (pos, 0);
+
         return new Point (pos, 0);
     }
 
@@ -1252,73 +1246,70 @@ public class TextField : View
 
     private MenuBarItem BuildContextMenuBarItem ()
     {
-        return new MenuBarItem (
-                                new MenuItem []
-                                {
-                                    new (
-                                         Strings.ctxSelectAll,
-                                         "",
-                                         () => SelectAll (),
-                                         null,
-                                         null,
-                                         (KeyCode)KeyBindings.GetKeyFromCommands (Command.SelectAll)
-                                        ),
-                                    new (
-                                         Strings.ctxDeleteAll,
-                                         "",
-                                         () => DeleteAll (),
-                                         null,
-                                         null,
-                                         (KeyCode)KeyBindings.GetKeyFromCommands (Command.DeleteAll)
-                                        ),
-                                    new (
-                                         Strings.ctxCopy,
-                                         "",
-                                         () => Copy (),
-                                         null,
-                                         null,
-                                         (KeyCode)KeyBindings.GetKeyFromCommands (Command.Copy)
-                                        ),
-                                    new (
-                                         Strings.ctxCut,
-                                         "",
-                                         () => Cut (),
-                                         null,
-                                         null,
-                                         (KeyCode)KeyBindings.GetKeyFromCommands (Command.Cut)
-                                        ),
-                                    new (
-                                         Strings.ctxPaste,
-                                         "",
-                                         () => Paste (),
-                                         null,
-                                         null,
-                                         (KeyCode)KeyBindings.GetKeyFromCommands (Command.Paste)
-                                        ),
-                                    new (
-                                         Strings.ctxUndo,
-                                         "",
-                                         () => Undo (),
-                                         null,
-                                         null,
-                                         (KeyCode)KeyBindings.GetKeyFromCommands (Command.Undo)
-                                        ),
-                                    new (
-                                         Strings.ctxRedo,
-                                         "",
-                                         () => Redo (),
-                                         null,
-                                         null,
-                                         (KeyCode)KeyBindings.GetKeyFromCommands (Command.Redo)
-                                        )
-                                }
-                               );
+        return new (
+                    new MenuItem []
+                    {
+                        new (
+                             Strings.ctxSelectAll,
+                             "",
+                             () => SelectAll (),
+                             null,
+                             null,
+                             (KeyCode)KeyBindings.GetKeyFromCommands (Command.SelectAll)
+                            ),
+                        new (
+                             Strings.ctxDeleteAll,
+                             "",
+                             () => DeleteAll (),
+                             null,
+                             null,
+                             (KeyCode)KeyBindings.GetKeyFromCommands (Command.DeleteAll)
+                            ),
+                        new (
+                             Strings.ctxCopy,
+                             "",
+                             () => Copy (),
+                             null,
+                             null,
+                             (KeyCode)KeyBindings.GetKeyFromCommands (Command.Copy)
+                            ),
+                        new (
+                             Strings.ctxCut,
+                             "",
+                             () => Cut (),
+                             null,
+                             null,
+                             (KeyCode)KeyBindings.GetKeyFromCommands (Command.Cut)
+                            ),
+                        new (
+                             Strings.ctxPaste,
+                             "",
+                             () => Paste (),
+                             null,
+                             null,
+                             (KeyCode)KeyBindings.GetKeyFromCommands (Command.Paste)
+                            ),
+                        new (
+                             Strings.ctxUndo,
+                             "",
+                             () => Undo (),
+                             null,
+                             null,
+                             (KeyCode)KeyBindings.GetKeyFromCommands (Command.Undo)
+                            ),
+                        new (
+                             Strings.ctxRedo,
+                             "",
+                             () => Redo (),
+                             null,
+                             null,
+                             (KeyCode)KeyBindings.GetKeyFromCommands (Command.Redo)
+                            )
+                    }
+                   );
     }
 
-    private void ContextMenu_KeyChanged (object sender, KeyChangedEventArgs e)
-    {
-        KeyBindings.ReplaceKey (e.OldKey.KeyCode, e.NewKey.KeyCode);
-    }
+    private void ContextMenu_KeyChanged (object sender, KeyChangedEventArgs e) { KeyBindings.ReplaceKey (e.OldKey.KeyCode, e.NewKey.KeyCode); }
 
     private List<Rune> DeleteSelectedText ()
     {
@@ -1344,13 +1335,13 @@ public class TextField : View
         List<RuneCell> currentLine = TextModel.ToRuneCellList (Text);
         int cursorPosition = Math.Min (CursorPosition, currentLine.Count);
 
-        Autocomplete.Context = new AutocompleteContext (
-                                                        currentLine,
-                                                        cursorPosition,
-                                                        Autocomplete.Context != null
-                                                            ? Autocomplete.Context.Canceled
-                                                            : false
-                                                       );
+        Autocomplete.Context = new (
+                                    currentLine,
+                                    cursorPosition,
+                                    Autocomplete.Context != null
+                                        ? Autocomplete.Context.Canceled
+                                        : false
+                                   );
 
         Autocomplete.GenerateSuggestions (
                                           Autocomplete.Context
@@ -1371,15 +1362,15 @@ public class TextField : View
 
         if (ColorScheme is null)
         {
-            cs = new ColorScheme ();
+            cs = new ();
         }
 
         if (cs.Disabled.Foreground == cs.Focus.Background)
         {
-            return new Attribute (cs.Focus.Foreground, cs.Focus.Background);
+            return new (cs.Focus.Foreground, cs.Focus.Background);
         }
 
-        return new Attribute (cs.Disabled.Foreground, cs.Focus.Background);
+        return new (cs.Disabled.Foreground, cs.Focus.Background);
     }
 
     private void HistoryText_ChangeText (object sender, HistoryText.HistoryTextItemEventArgs obj)
@@ -1397,8 +1388,8 @@ public class TextField : View
     private void InsertText (Key a, bool usePreTextChangedCursorPos)
     {
         _historyText.Add (
-                          new List<List<RuneCell>> { TextModel.ToRuneCells (_text) },
-                          new Point (_cursorPosition, 0)
+                          new() { TextModel.ToRuneCells (_text) },
+                          new (_cursorPosition, 0)
                          );
 
         List<Rune> newText = _text;
@@ -1532,7 +1523,6 @@ public class TextField : View
 
     private bool MoveLeft ()
     {
-
         if (_cursorPosition > 0)
         {
             ClearAllSelection ();
@@ -1671,13 +1661,10 @@ public class TextField : View
             offB = SuperView.Frame.Right - Frame.Right - 1;
         }
 
-        return 0;//offB;
+        return 0; //offB;
     }
 
-    private int PositionCursor (MouseEvent ev)
-    {
-        return PositionCursor (TextModel.GetColFromX (_text, ScrollOffset, ev.Position.X), false);
-    }
+    private int PositionCursor (MouseEvent ev) { return PositionCursor (TextModel.GetColFromX (_text, ScrollOffset, ev.Position.X), false); }
 
     private int PositionCursor (int x, bool getX = true)
     {
@@ -1771,7 +1758,6 @@ public class TextField : View
 
     private void DrawAutocomplete ()
     {
-
         if (SelectedLength > 0)
         {
             return;
@@ -1862,10 +1848,7 @@ public class TextField : View
         }
     }
 
-    private void TextField_Removed (object sender, SuperViewChangedEventArgs e)
-    {
-        Autocomplete.HostControl = null;
-    }
+    private void TextField_Removed (object sender, SuperViewChangedEventArgs e) { Autocomplete.HostControl = null; }
 
     private void TextField_Initialized (object sender, EventArgs e)
     {

+ 3 - 4
Terminal.Gui/Views/TextView.cs

@@ -2276,8 +2276,7 @@ public class TextView : View
                     }
                    );
 
-        // BUGBUG: If AllowsReturn is false, Key.Enter should not be bound (so that Toplevel can cause Command.Accept).
-        AddCommand (Command.Accept, () => ProcessReturn ());
+        AddCommand (Command.Accept, () => ProcessEnterKey ());
 
         AddCommand (
                     Command.End,
@@ -6033,7 +6032,7 @@ public class TextView : View
         Paste ();
     }
 
-    private bool ProcessReturn ()
+    private bool ProcessEnterKey ()
     {
         ResetColumnTrack ();
 
@@ -6046,7 +6045,7 @@ public class TextView : View
         {
             // By Default pressing ENTER should be ignored (OnAccept will return false or null). Only cancel if the
             // event was fired and set Cancel = true.
-            return RaiseAcceptEvent () == false;
+            return RaiseAcceptEvent () is null or false;
         }
 
         SetWrapModel ();

+ 38 - 12
UnitTests/Views/TextViewTests.cs

@@ -5334,16 +5334,18 @@ This is the second line.
         Assert.Equal ("", tv.SelectedText);
         Assert.False (tv.Selecting);
         Assert.True (tv.AllowsReturn);
+
         tv.AllowsReturn = false;
         Assert.Equal (Point.Empty, tv.CursorPosition);
         Assert.False (tv.Selecting);
-        Assert.False (tv.NewKeyDownEvent (Key.Enter));
+        Assert.True (tv.NewKeyDownEvent (Key.Enter));
         Assert.Equal ($"This is the second line.{Environment.NewLine}This is the third ", tv.Text);
         Assert.Equal (Point.Empty, tv.CursorPosition);
         Assert.Equal (0, tv.SelectedLength);
         Assert.Equal ("", tv.SelectedText);
         Assert.False (tv.Selecting);
         Assert.False (tv.AllowsReturn);
+
         tv.AllowsReturn = true;
         Assert.Equal (Point.Empty, tv.CursorPosition);
         Assert.True (tv.NewKeyDownEvent (Key.Enter));
@@ -8419,7 +8421,7 @@ line.
     // This is necessary because a) Application is a singleton and Init/Shutdown must be called
     // as a pair, and b) all unit test functions should be atomic.
     [AttributeUsage (AttributeTargets.Class | AttributeTargets.Method)]
-    public class TextViewTestsAutoInitShutdown : AutoInitShutdownAttribute 
+    public class TextViewTestsAutoInitShutdown : AutoInitShutdownAttribute
     {
         public static string Txt = "TAB to jump between text fields.";
 
@@ -8472,26 +8474,50 @@ line.
         void OnAccept (object sender, HandledEventArgs e) { accepted = true; }
     }
 
-    [Fact]
-    public void Accept_Command_Fires_Accept ()
+    [Theory]
+    [InlineData (false, 1)]
+    [InlineData (true, 0)]
+    public void Accept_Command_Fires_Accept_Based_On_AllowsReturn (bool allowsReturn, int expectedAcceptEvents)
     {
-        var view = new TextView ();
+        var view = new TextView ()
+        {
+            AllowsReturn = allowsReturn,
+        };
 
-        var accepted = false;
+        int acceptEvents = 0;
         view.Accept += Accept;
         view.InvokeCommand (Command.Accept);
-        Assert.True (accepted);
+        Assert.Equal (expectedAcceptEvents, acceptEvents);
 
         return;
 
-        void Accept (object sender, HandledEventArgs e) { accepted = true; }
+        void Accept (object sender, HandledEventArgs e) { acceptEvents++; }
     }
 
+    [Theory]
+    [InlineData (false, 1)]
+    [InlineData (true, 0)]
+    public void Enter_Key_Fires_Accept_BasedOn_AllowsReturn (bool allowsReturn, int expectedAccepts)
+    {
+        var view = new TextView ()
+        {
+            Multiline = allowsReturn,
+        };
+
+        int accepted = 0;
+        view.Accept += Accept;
+        view.NewKeyDownEvent (Key.Enter);
+        Assert.Equal (expectedAccepts, accepted);
+
+        return;
+
+        void Accept (object sender, HandledEventArgs e) { accepted++; }
+    }
 
     [Theory]
     [InlineData (false, 1)]
     [InlineData (true, 0)]
-    public void Enter_Key_Fires_Accept (bool multiline, int expectedAccepts)
+    public void Enter_Key_Fires_Accept_BasedOn_Multiline (bool multiline, int expectedAccepts)
     {
         var view = new TextView ()
         {
@@ -8509,11 +8535,11 @@ line.
     }
 
     [Theory]
-    [InlineData (false, false, 1, 0)]
-    [InlineData (false, true, 1, 1)]
+    [InlineData (false, false, 1, 1)]
+    [InlineData (false, true, 1, 0)]
     [InlineData (true, false, 0, 0)]
     [InlineData (true, true, 0, 0)]
-    public void Accept_Handler_Handled_Prevents_Default_Button_Accept (bool multiline, bool handleAccept, int expectedAccepts, int expectedButtonAccepts)
+    public void Accept_Event_Handled_Prevents_Default_Button_Accept (bool multiline, bool handleAccept, int expectedAccepts, int expectedButtonAccepts)
     {
         var superView = new Window ();
         var tv = new TextView ()