浏览代码

KeyDown API usage cleanup

Tig 9 月之前
父节点
当前提交
bc51f8868b

+ 1 - 1
Terminal.Gui/Application/Application.Initialization.cs

@@ -179,7 +179,7 @@ public static partial class Application // Initialization (Init/Shutdown)
 
     private static void Driver_SizeChanged (object? sender, SizeChangedEventArgs e) { OnSizeChanging (e); }
     private static void Driver_KeyDown (object? sender, Key e) { RaiseKeyDownEvent (e); }
-    private static void Driver_KeyUp (object? sender, Key e) { OnKeyUp (e); }
+    private static void Driver_KeyUp (object? sender, Key e) { RaiseKeyUpEvent (e); }
     private static void Driver_MouseEvent (object? sender, MouseEvent e) { OnMouseEvent (e); }
 
     /// <summary>Gets of list of <see cref="ConsoleDriver"/> types that are available.</summary>

+ 79 - 0
Terminal.Gui/Application/Application.Navigation.cs

@@ -7,4 +7,83 @@ public static partial class Application // Navigation stuff
     ///     Gets the <see cref="ApplicationNavigation"/> instance for the current <see cref="Application"/>.
     /// </summary>
     public static ApplicationNavigation? Navigation { get; internal set; }
+
+    private static Key _nextTabGroupKey = Key.F6; // Resources/config.json overrides
+    private static Key _nextTabKey = Key.Tab; // Resources/config.json overrides
+    private static Key _prevTabGroupKey = Key.F6.WithShift; // Resources/config.json overrides
+    private static Key _prevTabKey = Key.Tab.WithShift; // Resources/config.json overrides
+
+    /// <summary>Alternative key to navigate forwards through views. Ctrl+Tab is the primary key.</summary>
+    [SerializableConfigurationProperty (Scope = typeof (SettingsScope))]
+    public static Key NextTabGroupKey
+    {
+        get => _nextTabGroupKey;
+        set
+        {
+            if (_nextTabGroupKey != value)
+            {
+                ReplaceKey (_nextTabGroupKey, value);
+                _nextTabGroupKey = value;
+            }
+        }
+    }
+
+    /// <summary>Alternative key to navigate forwards through views. Ctrl+Tab is the primary key.</summary>
+    [SerializableConfigurationProperty (Scope = typeof (SettingsScope))]
+    public static Key NextTabKey
+    {
+        get => _nextTabKey;
+        set
+        {
+            if (_nextTabKey != value)
+            {
+                ReplaceKey (_nextTabKey, value);
+                _nextTabKey = value;
+            }
+        }
+    }
+
+
+    /// <summary>
+    ///     Raised when the user releases a key.
+    ///     <para>
+    ///         Set <see cref="Key.Handled"/> to <see langword="true"/> to indicate the key was handled and to prevent
+    ///         additional processing.
+    ///     </para>
+    /// </summary>
+    /// <remarks>
+    ///     All drivers support firing the <see cref="KeyDown"/> event. Some drivers (Curses) do not support firing the
+    ///     <see cref="KeyDown"/> and <see cref="KeyUp"/> events.
+    ///     <para>Fired after <see cref="KeyDown"/>.</para>
+    /// </remarks>
+    public static event EventHandler<Key>? KeyUp;
+    /// <summary>Alternative key to navigate backwards through views. Shift+Ctrl+Tab is the primary key.</summary>
+    [SerializableConfigurationProperty (Scope = typeof (SettingsScope))]
+    public static Key PrevTabGroupKey
+    {
+        get => _prevTabGroupKey;
+        set
+        {
+            if (_prevTabGroupKey != value)
+            {
+                ReplaceKey (_prevTabGroupKey, value);
+                _prevTabGroupKey = value;
+            }
+        }
+    }
+
+    /// <summary>Alternative key to navigate backwards through views. Shift+Ctrl+Tab is the primary key.</summary>
+    [SerializableConfigurationProperty (Scope = typeof (SettingsScope))]
+    public static Key PrevTabKey
+    {
+        get => _prevTabKey;
+        set
+        {
+            if (_prevTabKey != value)
+            {
+                ReplaceKey (_prevTabKey, value);
+                _prevTabKey = value;
+            }
+        }
+    }
 }

+ 2 - 5
Terminal.Gui/View/View.Keyboard.cs

@@ -265,7 +265,7 @@ public partial class View // Keyboard APIs
     ///     <para>
     ///         Calling this method for a key bound to the view via an Application-scoped keybinding will have no effect.
     ///         Instead,
-    ///         use <see cref="Application.NewKeyDown"/>.
+    ///         use <see cref="Application.RaiseKeyDownEvent"/>.
     ///     </para>
     ///     <para>See <see href="../docs/keyboard.md">for an overview of Terminal.Gui keyboard APIs.</see></para>
     /// </remarks>
@@ -530,7 +530,6 @@ public partial class View // Keyboard APIs
             return true;
         }
 
-        // BUGBUG: The proper pattern is for the v-method (OnInvokingKeyBindings) to be called first, then the event
         InvokingKeyBindings?.Invoke (this, key);
 
         if (key.Handled)
@@ -538,8 +537,6 @@ public partial class View // Keyboard APIs
             return true;
         }
 
-        bool? handled;
-
         // After
 
         // * If no key binding was found, `InvokeKeyBindings` returns `null`.
@@ -548,7 +545,7 @@ public partial class View // Keyboard APIs
         //   `InvokeKeyBindings` returns `false`. Continue passing the event (return `false` from `OnInvokeKeyBindings`)..
         // * If key bindings were found, and any handled the key (at least one `Command` returned `true`),
         //   `InvokeKeyBindings` returns `true`. Continue passing the event (return `false` from `OnInvokeKeyBindings`).
-        handled = InvokeCommands (key, scope);
+        bool?  handled = InvokeCommands (key, scope);
 
         if (handled is { } && (bool)handled)
         {

+ 10 - 10
Terminal.Gui/Views/TextField.cs

@@ -1014,27 +1014,27 @@ public class TextField : View
     }
 
     /// <inheritdoc/>
-    protected override bool OnInvokingKeyBindings (Key a, KeyBindingScope scope)
+    protected override void OnHasFocusChanged (bool newHasFocus, View previousFocusedView, View view)
     {
-        // Give autocomplete first opportunity to respond to key presses
-        if (SelectedLength == 0 && Autocomplete.Suggestions.Count > 0 && Autocomplete.ProcessKey (a))
+        if (Application.MouseGrabView is { } && Application.MouseGrabView == this)
         {
-            return true;
+            Application.UngrabMouse ();
         }
 
-        return base.OnInvokingKeyBindings (a, scope);
+        //if (SelectedLength != 0 && !(Application.MouseGrabView is MenuBar))
+        //	ClearAllSelection ();
     }
 
     /// <inheritdoc/>
-    protected override void OnHasFocusChanged (bool newHasFocus, View previousFocusedView, View view)
+    protected override bool OnKeyDown (Key key)
     {
-        if (Application.MouseGrabView is { } && Application.MouseGrabView == this)
+        // Give autocomplete first opportunity to respond to key presses
+        if (SelectedLength == 0 && Autocomplete.Suggestions.Count > 0 && Autocomplete.ProcessKey (key))
         {
-            Application.UngrabMouse ();
+            return true;
         }
 
-        //if (SelectedLength != 0 && !(Application.MouseGrabView is MenuBar))
-        //	ClearAllSelection ();
+        return false;
     }
 
     /// <inheritdoc />

+ 79 - 83
Terminal.Gui/Views/TextView.cs

@@ -1,10 +1,8 @@
 #nullable enable
 
 // TextView.cs: multi-line text editing
-using System.Diagnostics;
 using System.Globalization;
 using System.Runtime.CompilerServices;
-using System.Text.Json.Serialization;
 using Terminal.Gui.Resources;
 
 namespace Terminal.Gui;
@@ -106,7 +104,7 @@ internal class TextModel
 
     public void LoadCells (List<Cell> cells, Attribute? attribute)
     {
-        _lines = Cell.ToCells ((List<Cell>)cells);
+        _lines = Cell.ToCells (cells);
         SetAttributes (attribute);
         OnLinesLoaded ();
     }
@@ -180,7 +178,7 @@ internal class TextModel
     {
         if (_lines.Count > 0 && pos < _lines.Count)
         {
-            _lines [pos] = [..runes];
+            _lines [pos] = [.. runes];
         }
         else if (_lines.Count == 0 || (_lines.Count > 0 && pos >= _lines.Count))
         {
@@ -188,8 +186,6 @@ internal class TextModel
         }
     }
 
-
-
     public override string ToString ()
     {
         var sb = new StringBuilder ();
@@ -608,8 +604,7 @@ internal class TextModel
 
     internal Size GetDisplaySize ()
     {
-        Size size = Size.Empty;
-
+        var size = Size.Empty;
 
         return size;
     }
@@ -799,7 +794,7 @@ internal class TextModel
 
         string GetText (List<Cell> x)
         {
-            string txt = Cell.ToString (x);
+            var txt = Cell.ToString (x);
 
             if (!matchCase)
             {
@@ -872,7 +867,7 @@ internal class TextModel
         for (int i = start.Y; i < linesCount; i++)
         {
             List<Cell> x = _lines [i];
-            string txt = Cell.ToString (x);
+            var txt = Cell.ToString (x);
 
             if (!matchCase)
             {
@@ -912,7 +907,7 @@ internal class TextModel
         for (int i = linesCount; i >= 0; i--)
         {
             List<Cell> x = _lines [i];
-            string txt = Cell.ToString (x);
+            var txt = Cell.ToString (x);
 
             if (!matchCase)
             {
@@ -1075,7 +1070,7 @@ internal class TextModel
 
     private string ReplaceText (List<Cell> source, string textToReplace, string matchText, int col)
     {
-        string origTxt = Cell.ToString (source);
+        var origTxt = Cell.ToString (source);
         (_, int len) = DisplaySize (source, 0, col, false);
         (_, int len2) = DisplaySize (source, col, col + matchText.Length, false);
         (_, int len3) = DisplaySize (source, col + matchText.Length, origTxt.GetRuneCount (), false);
@@ -1171,10 +1166,11 @@ internal partial class HistoryText
         _historyTextItems.Clear ();
         _idxHistoryText = -1;
         _originalCellsList.Clear ();
+
         // Save a copy of the original, not the reference
         foreach (List<Cell> cells in cellsList)
         {
-            _originalCellsList.Add ([..cells]);
+            _originalCellsList.Add ([.. cells]);
         }
 
         OnChangeText (null);
@@ -1678,15 +1674,15 @@ internal class WordWrapManager
             List<Cell> line = Model.GetLine (i);
 
             List<List<Cell>> wrappedLines = ToListRune (
-                                                            TextFormatter.Format (
-                                                                                  Cell.ToString (line),
-                                                                                  width,
-                                                                                  Alignment.Start,
-                                                                                  true,
-                                                                                  preserveTrailingSpaces,
-                                                                                  tabWidth
-                                                                                 )
-                                                           );
+                                                        TextFormatter.Format (
+                                                                              Cell.ToString (line),
+                                                                              width,
+                                                                              Alignment.Start,
+                                                                              true,
+                                                                              preserveTrailingSpaces,
+                                                                              tabWidth
+                                                                             )
+                                                       );
             var sumColWidth = 0;
 
             for (var j = 0; j < wrappedLines.Count; j++)
@@ -1885,7 +1881,6 @@ public class TextView : View
     private WordWrapManager? _wrapManager;
     private bool _wrapNeeded;
 
-
     /// <summary>
     ///     Initializes a <see cref="TextView"/> on the specified area, with dimensions controlled with the X, Y, Width
     ///     and Height properties.
@@ -1911,7 +1906,7 @@ public class TextView : View
         // Things this view knows how to do
 
         // Note - NewLine is only bound to Enter if Multiline is true
-        AddCommand (Command.NewLine, (ctx) => ProcessEnterKey (ctx));
+        AddCommand (Command.NewLine, ctx => ProcessEnterKey (ctx));
 
         AddCommand (
                     Command.PageDown,
@@ -2376,7 +2371,7 @@ public class TextView : View
         KeyBindings.Add (Key.C.WithCtrl, Command.Copy);
 
         KeyBindings.Add (Key.W.WithCtrl, Command.Cut); // Move to Unix?
-        KeyBindings.Add (Key.X.WithCtrl, Command.Cut); 
+        KeyBindings.Add (Key.X.WithCtrl, Command.Cut);
 
         KeyBindings.Add (Key.CursorLeft.WithCtrl, Command.WordLeft);
 
@@ -2422,10 +2417,7 @@ public class TextView : View
         KeyBindings.Add ((KeyCode)ContextMenu.Key, KeyBindingScope.HotKey, Command.Context);
     }
 
-    private void TextView_Added1 (object? sender, SuperViewChangedEventArgs e)
-    {
-        throw new NotImplementedException ();
-    }
+    private void TextView_Added1 (object? sender, SuperViewChangedEventArgs e) { throw new NotImplementedException (); }
 
     // BUGBUG: AllowsReturn is mis-named. It should be EnterKeyAccepts.
     /// <summary>
@@ -2435,11 +2427,13 @@ public class TextView : View
     /// <remarks>
     ///     <para>
     ///         Setting this property alters <see cref="Multiline"/>.
-    ///         If <see cref="AllowsReturn"/> is set to <see langword="true"/>, then <see cref="Multiline"/> is also set to `true` and
+    ///         If <see cref="AllowsReturn"/> is set to <see langword="true"/>, then <see cref="Multiline"/> is also set to
+    ///         `true` and
     ///         vice-versa.
     ///     </para>
     ///     <para>
-    ///         If <see cref="AllowsReturn"/> is set to <see langword="false"/>, then <see cref="AllowsTab"/> gets set to <see langword="false"/>.
+    ///         If <see cref="AllowsReturn"/> is set to <see langword="false"/>, then <see cref="AllowsTab"/> gets set to
+    ///         <see langword="false"/>.
     ///     </para>
     /// </remarks>
     public bool AllowsReturn
@@ -2458,6 +2452,7 @@ public class TextView : View
             if (!_allowsReturn && _multiline)
             {
                 Multiline = false;
+
                 // BUGBUG: Setting properties should not have side-effects like this. Multiline and AllowsTab should be independent.
                 AllowsTab = false;
             }
@@ -2532,7 +2527,6 @@ public class TextView : View
         }
     }
 
-
     /// <summary>
     ///     Indicates whatever the text has history changes or not. <see langword="true"/> if the text has history changes
     ///     <see langword="false"/> otherwise.
@@ -2604,7 +2598,7 @@ public class TextView : View
                 CurrentRow = 0;
                 _savedHeight = Height;
 
-                Height = Dim.Auto (DimAutoStyle.Text, minimumContentDim: 1);
+                Height = Dim.Auto (DimAutoStyle.Text, 1);
 
                 if (!IsInitialized)
                 {
@@ -2805,7 +2799,6 @@ public class TextView : View
         }
     }
 
-
     /// <summary>Allows clearing the <see cref="HistoryText.HistoryTextItemEventArgs"/> items updating the original text.</summary>
     public void ClearHistoryChanges () { _historyText?.Clear (_model.GetAllLines ()); }
 
@@ -2855,7 +2848,7 @@ public class TextView : View
                     line [c] = cell; // Assign the modified copy back
                 }
 
-                selectedCellsChanged.Add ([..GetLine (r)]);
+                selectedCellsChanged.Add ([.. GetLine (r)]);
             }
 
             GetSelectedRegion ();
@@ -2906,10 +2899,10 @@ public class TextView : View
     public void PromptForColors ()
     {
         if (!ColorPicker.Prompt (
-                                     "Colors",
-                                     GetSelectedCellAttribute (),
-                                     out Attribute newAttribute
-                                    ))
+                                 "Colors",
+                                 GetSelectedCellAttribute (),
+                                 out Attribute newAttribute
+                                ))
         {
             return;
         }
@@ -3177,10 +3170,7 @@ public class TextView : View
     public List<Cell> GetLine (int line) { return _model.GetLine (line); }
 
     /// <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
@@ -3563,7 +3553,6 @@ public class TextView : View
 
         ProcessInheritsPreviousColorScheme (CurrentRow, CurrentColumn);
         ProcessAutocomplete ();
-
     }
 
     /// <inheritdoc/>
@@ -3628,6 +3617,7 @@ public class TextView : View
                 else
                 {
                     AddRune (col, row, rune);
+
                     // Ensures that cols less than 0 to be 1 because it will be converted to a printable rune
                     cols = Math.Max (cols, 1);
                 }
@@ -3664,42 +3654,29 @@ public class TextView : View
     }
 
     /// <inheritdoc/>
-    protected override bool OnInvokingKeyBindings (Key a, KeyBindingScope scope)
+    protected override void OnHasFocusChanged (bool newHasFocus, View? previousFocusedView, View? view)
     {
-        if (!a.IsValid)
-        {
-            return false;
-        }
-
-        // Give autocomplete first opportunity to respond to key presses
-        if (SelectedLength == 0 && Autocomplete.Suggestions.Count > 0 && Autocomplete.ProcessKey (a))
+        if (Application.MouseGrabView is { } && Application.MouseGrabView == this)
         {
-            return true;
+            Application.UngrabMouse ();
         }
-
-        return base.OnInvokingKeyBindings (a, scope);
     }
 
     /// <inheritdoc/>
-    public override bool OnKeyUp (Key key)
+    protected override bool OnKeyDown (Key key)
     {
-        if (key == Key.Space.WithCtrl)
+        if (!key.IsValid)
         {
-            return true;
+            return false;
         }
 
-        return false;
-    }
-
-    /// <inheritdoc/>
-    protected override void OnHasFocusChanged (bool newHasFocus, View? previousFocusedView, View? view)
-    {
-        if (Application.MouseGrabView is { } && Application.MouseGrabView == this)
+        // Give autocomplete first opportunity to respond to key presses
+        if (SelectedLength == 0 && Autocomplete.Suggestions.Count > 0 && Autocomplete.ProcessKey (key))
         {
-            Application.UngrabMouse ();
+            return true;
         }
 
-        return;
+        return false;
     }
 
     /// <inheritdoc/>
@@ -3724,6 +3701,17 @@ public class TextView : View
         return true;
     }
 
+    /// <inheritdoc/>
+    public override bool OnKeyUp (Key key)
+    {
+        if (key == Key.Space.WithCtrl)
+        {
+            return true;
+        }
+
+        return false;
+    }
+
     /// <summary>Invoke the <see cref="UnwrappedCursorPosition"/> event with the unwrapped <see cref="CursorPosition"/>.</summary>
     public virtual void OnUnwrappedCursorPosition (int? cRow = null, int? cCol = null)
     {
@@ -3760,7 +3748,7 @@ public class TextView : View
             List<List<Cell>> addedLine = [new (currentLine), runeList];
 
             _historyText.Add (
-                              [..addedLine],
+                              [.. addedLine],
                               CursorPosition,
                               HistoryText.LineStatus.Added
                              );
@@ -3862,6 +3850,7 @@ public class TextView : View
         if (posX > -1 && col >= posX && posX < Viewport.Width && _topRow <= CurrentRow && posY < Viewport.Height)
         {
             Move (col, CurrentRow - _topRow);
+
             return new (col, CurrentRow - _topRow);
         }
 
@@ -4481,12 +4470,12 @@ public class TextView : View
         }
         else
         {
-            _historyText.Add ([ [.. currentLine]], CursorPosition);
+            _historyText.Add ([[.. currentLine]], CursorPosition);
 
             currentLine.RemoveAt (CurrentColumn);
 
             _historyText.Add (
-                              [ [.. currentLine]],
+                              [[.. currentLine]],
                               CursorPosition,
                               HistoryText.LineStatus.Replaced
                              );
@@ -4656,6 +4645,7 @@ public class TextView : View
         {
             cells = line.GetRange (startCol, endCol - startCol);
             cellsList.Add (cells);
+
             return StringFromRunes (cells);
         }
 
@@ -4668,6 +4658,7 @@ public class TextView : View
             cellsList.AddRange ([]);
             cells = model == null ? _model.GetLine (row) : model.GetLine (row);
             cellsList.Add (cells);
+
             res = res
                   + Environment.NewLine
                   + StringFromRunes (cells);
@@ -4703,7 +4694,7 @@ public class TextView : View
 
         OnUnwrappedCursorPosition (cRow, cCol);
 
-        return GetRegion (out _, sRow: startRow, sCol: startCol, cRow: cRow, cCol: cCol, model: model);
+        return GetRegion (out _, startRow, startCol, cRow, cCol, model);
     }
 
     private (int Row, int Col) GetUnwrappedPosition (int line, int col)
@@ -4897,7 +4888,7 @@ public class TextView : View
         {
             _model.AddLine (CurrentRow + i, lines [i]);
 
-            addedLines.Add ([..lines [i]]);
+            addedLines.Add ([.. lines [i]]);
         }
 
         if (rest is { })
@@ -5071,7 +5062,7 @@ public class TextView : View
         }
 
         _historyText.Add (
-                          [ [.. GetCurrentLine ()]],
+                          [[.. GetCurrentLine ()]],
                           CursorPosition,
                           HistoryText.LineStatus.Replaced
                          );
@@ -5111,7 +5102,7 @@ public class TextView : View
             return;
         }
 
-        _historyText.Add ([ [.. currentLine]], CursorPosition);
+        _historyText.Add ([[.. currentLine]], CursorPosition);
 
         if (currentLine.Count == 0)
         {
@@ -5178,7 +5169,7 @@ public class TextView : View
         }
 
         _historyText.Add (
-                          [ [.. GetCurrentLine ()]],
+                          [[.. GetCurrentLine ()]],
                           CursorPosition,
                           HistoryText.LineStatus.Replaced
                          );
@@ -5202,14 +5193,14 @@ public class TextView : View
 
         List<Cell> currentLine = GetCurrentLine ();
 
-        _historyText.Add ([ [.. GetCurrentLine ()]], CursorPosition);
+        _historyText.Add ([[.. GetCurrentLine ()]], CursorPosition);
 
         if (CurrentColumn == 0)
         {
             DeleteTextBackwards ();
 
             _historyText.ReplaceLast (
-                                      [ [.. GetCurrentLine ()]],
+                                      [[.. GetCurrentLine ()]],
                                       CursorPosition,
                                       HistoryText.LineStatus.Replaced
                                      );
@@ -5248,7 +5239,7 @@ public class TextView : View
         }
 
         _historyText.Add (
-                          [ [.. GetCurrentLine ()]],
+                          [[.. GetCurrentLine ()]],
                           CursorPosition,
                           HistoryText.LineStatus.Replaced
                          );
@@ -5270,14 +5261,14 @@ public class TextView : View
 
         List<Cell> currentLine = GetCurrentLine ();
 
-        _historyText.Add ([ [.. GetCurrentLine ()]], CursorPosition);
+        _historyText.Add ([[.. GetCurrentLine ()]], CursorPosition);
 
         if (currentLine.Count == 0 || CurrentColumn == currentLine.Count)
         {
             DeleteTextForwards ();
 
             _historyText.ReplaceLast (
-                                      [ [.. GetCurrentLine ()]],
+                                      [[.. GetCurrentLine ()]],
                                       CursorPosition,
                                       HistoryText.LineStatus.Replaced
                                      );
@@ -5307,7 +5298,7 @@ public class TextView : View
         }
 
         _historyText.Add (
-                          [ [.. GetCurrentLine ()]],
+                          [[.. GetCurrentLine ()]],
                           CursorPosition,
                           HistoryText.LineStatus.Replaced
                          );
@@ -5579,6 +5570,7 @@ public class TextView : View
         }
 
         DoNeededAction ();
+
         return true;
     }
 
@@ -5919,6 +5911,7 @@ public class TextView : View
     private bool ProcessMoveDown ()
     {
         ResetContinuousFindTrack ();
+
         if (_shiftSelecting && IsSelecting)
         {
             StopSelecting ();
@@ -5961,8 +5954,10 @@ public class TextView : View
             if (IsSelecting)
             {
                 StopSelecting ();
+
                 return true;
             }
+
             // do not respond (this lets the key press fall through to navigation system - which usually changes focus backward)
             return false;
         }
@@ -6001,8 +5996,10 @@ public class TextView : View
             {
                 // In which case clear
                 StopSelecting ();
+
                 return true;
             }
+
             return false;
         }
 
@@ -6296,7 +6293,6 @@ public class TextView : View
         _continuousFind = false;
     }
 
-
     private void ResetPosition ()
     {
         _topRow = _leftColumn = CurrentRow = CurrentColumn = 0;
@@ -6461,13 +6457,13 @@ public class TextView : View
         }
     }
 
-
     private void TextView_Initialized (object sender, EventArgs e)
     {
         if (Autocomplete.HostControl is null)
         {
             Autocomplete.HostControl = this;
         }
+
         OnContentsChanged ();
     }
 

+ 1 - 4
Terminal.Gui/Views/TreeView/TreeView.cs

@@ -1189,15 +1189,12 @@ public class TreeView<T> : View, ITreeView where T : class
             return false;
         }
 
-        // BUGBUG: this should move to OnInvokingKeyBindings
         // If not a keybinding, is the key a searchable key press?
         if (CollectionNavigatorBase.IsCompatibleKey (keyEvent) && AllowLetterBasedNavigation)
         {
-            IReadOnlyCollection<Branch<T>> map;
-
             // If there has been a call to InvalidateMap since the last time
             // we need a new one to reflect the new exposed tree state
-            map = BuildLineMap ();
+            IReadOnlyCollection<Branch<T>> map = BuildLineMap ();
 
             // Find the current selected object within the tree
             int current = map.IndexOf (b => b.Model == SelectedObject);

+ 29 - 29
UnitTests/Application/KeyboardTests.cs

@@ -64,14 +64,14 @@ public class KeyboardTests
         Assert.True (win2.HasFocus);
         Assert.Equal ("win2", ((Window)top.Subviews [^1]).Title);
 
-        Application.NewKeyDown (Key.F6);
+        Application.RaiseKeyDownEvent (Key.F6);
         Assert.True (win2.CanFocus);
         Assert.False (win.HasFocus);
         Assert.True (win2.CanFocus);
         Assert.True (win2.HasFocus);
         Assert.Equal ("win2", ((Window)top.Subviews [^1]).Title);
 
-        Application.NewKeyDown (Key.F6);
+        Application.RaiseKeyDownEvent (Key.F6);
         Assert.False (win.CanFocus);
         Assert.False (win.HasFocus);
         Assert.True (win2.CanFocus);
@@ -117,14 +117,14 @@ public class KeyboardTests
         Assert.False (win2.HasFocus);
         Assert.Equal ("win", ((Window)top.Subviews [^1]).Title);
 
-        Application.NewKeyDown (Key.F6);
+        Application.RaiseKeyDownEvent (Key.F6);
         Assert.True (win.CanFocus);
         Assert.False (win.HasFocus);
         Assert.True (win2.CanFocus);
         Assert.True (win2.HasFocus);
         Assert.Equal ("win2", ((Window)top.Subviews [^1]).Title);
 
-        Application.NewKeyDown (Key.F6);
+        Application.RaiseKeyDownEvent (Key.F6);
         Assert.True (win.CanFocus);
         Assert.True (win.HasFocus);
         Assert.True (win2.CanFocus);
@@ -170,31 +170,31 @@ public class KeyboardTests
         top.Add (view);
         Application.Begin (top);
 
-        Application.NewKeyDown (Key.A);
+        Application.RaiseKeyDownEvent (Key.A);
         Assert.False (invoked);
         Assert.True (view.ApplicationCommand);
 
         invoked = false;
         view.ApplicationCommand = false;
         Application.KeyBindings.Remove (KeyCode.A);
-        Application.NewKeyDown (Key.A); // old
+        Application.RaiseKeyDownEvent (Key.A); // old
         Assert.False (invoked);
         Assert.False (view.ApplicationCommand);
         Application.KeyBindings.Add (Key.A.WithCtrl, view, Command.Save);
-        Application.NewKeyDown (Key.A); // old
+        Application.RaiseKeyDownEvent (Key.A); // old
         Assert.False (invoked);
         Assert.False (view.ApplicationCommand);
-        Application.NewKeyDown (Key.A.WithCtrl); // new
+        Application.RaiseKeyDownEvent (Key.A.WithCtrl); // new
         Assert.False (invoked);
         Assert.True (view.ApplicationCommand);
 
         invoked = false;
-        Application.NewKeyDown (Key.H);
+        Application.RaiseKeyDownEvent (Key.H);
         Assert.True (invoked);
 
         invoked = false;
         Assert.False (view.HasFocus);
-        Application.NewKeyDown (Key.F);
+        Application.RaiseKeyDownEvent (Key.F);
         Assert.False (invoked);
 
         Assert.True (view.ApplicationCommand);
@@ -215,7 +215,7 @@ public class KeyboardTests
         top.Add (view);
         Application.Begin (top);
 
-        Application.NewKeyDown (Key.A.WithCtrl);
+        Application.RaiseKeyDownEvent (Key.A.WithCtrl);
         Assert.False (invoked);
         Assert.False (view.ApplicationCommand);
         Assert.False (view.HotKeyCommand);
@@ -223,7 +223,7 @@ public class KeyboardTests
 
         invoked = false;
         Assert.False (view.HasFocus);
-        Application.NewKeyDown (Key.Z);
+        Application.RaiseKeyDownEvent (Key.Z);
         Assert.False (invoked);
         Assert.False (view.ApplicationCommand);
         Assert.False (view.HotKeyCommand);
@@ -399,7 +399,7 @@ public class KeyboardTests
         Assert.True (subView1.HasFocus);
 
         // Act
-        Application.NewKeyDown (Application.NextTabGroupKey);
+        Application.RaiseKeyDownEvent (Application.NextTabGroupKey);
 
         // Assert
         Assert.True (view2.HasFocus);
@@ -432,24 +432,24 @@ public class KeyboardTests
                                      Assert.True (v1.HasFocus);
 
                                      // Across TabGroups
-                                     Application.NewKeyDown (Key.F6);
+                                     Application.RaiseKeyDownEvent (Key.F6);
                                      Assert.True (v3.HasFocus);
-                                     Application.NewKeyDown (Key.F6);
+                                     Application.RaiseKeyDownEvent (Key.F6);
                                      Assert.True (v1.HasFocus);
 
-                                     Application.NewKeyDown (Key.F6.WithShift);
+                                     Application.RaiseKeyDownEvent (Key.F6.WithShift);
                                      Assert.True (v3.HasFocus);
-                                     Application.NewKeyDown (Key.F6.WithShift);
+                                     Application.RaiseKeyDownEvent (Key.F6.WithShift);
                                      Assert.True (v1.HasFocus);
 
                                      // Restore?
-                                     Application.NewKeyDown (Key.Tab);
+                                     Application.RaiseKeyDownEvent (Key.Tab);
                                      Assert.True (v2.HasFocus);
 
-                                     Application.NewKeyDown (Key.F6);
+                                     Application.RaiseKeyDownEvent (Key.F6);
                                      Assert.True (v3.HasFocus);
 
-                                     Application.NewKeyDown (Key.F6);
+                                     Application.RaiseKeyDownEvent (Key.F6);
                                      Assert.True (v1.HasFocus);
 
                                      Application.RequestStop ();
@@ -485,7 +485,7 @@ public class KeyboardTests
         view1.SetFocus ();
 
         // Act
-        Application.NewKeyDown (Application.NextTabKey);
+        Application.RaiseKeyDownEvent (Application.NextTabKey);
 
         // Assert
         Assert.True (view2.HasFocus);
@@ -539,7 +539,7 @@ public class KeyboardTests
         Assert.True (subView1.HasFocus);
 
         // Act
-        Application.NewKeyDown (Application.PrevTabGroupKey);
+        Application.RaiseKeyDownEvent (Application.PrevTabGroupKey);
 
         // Assert
         Assert.True (view2.HasFocus);
@@ -562,7 +562,7 @@ public class KeyboardTests
         view1.SetFocus ();
 
         // Act
-        Application.NewKeyDown (Application.NextTabKey);
+        Application.RaiseKeyDownEvent (Application.NextTabKey);
 
         // Assert
         Assert.True (view2.HasFocus);
@@ -605,21 +605,21 @@ public class KeyboardTests
 
         Key prevKey = Application.QuitKey;
 
-        Application.NewKeyDown (Application.QuitKey);
+        Application.RaiseKeyDownEvent (Application.QuitKey);
         Assert.True (isQuiting);
 
         isQuiting = false;
-        Application.NewKeyDown (Application.QuitKey);
+        Application.RaiseKeyDownEvent (Application.QuitKey);
         Assert.True (isQuiting);
 
         isQuiting = false;
         Application.QuitKey = Key.C.WithCtrl;
-        Application.NewKeyDown (prevKey); // Should not quit
+        Application.RaiseKeyDownEvent (prevKey); // Should not quit
         Assert.False (isQuiting);
-        Application.NewKeyDown (Key.Q.WithCtrl); // Should not quit
+        Application.RaiseKeyDownEvent (Key.Q.WithCtrl); // Should not quit
         Assert.False (isQuiting);
 
-        Application.NewKeyDown (Application.QuitKey);
+        Application.RaiseKeyDownEvent (Application.QuitKey);
         Assert.True (isQuiting);
 
         // Reset the QuitKey to avoid throws errors on another tests
@@ -728,7 +728,7 @@ public class KeyboardTests
             if (Application.IsInitialized)
             {
                 _output.WriteLine ("  Pressing QuitKey");
-                Application.NewKeyDown (Application.QuitKey);
+                Application.RaiseKeyDownEvent (Application.QuitKey);
             }
         }
     }

+ 1 - 1
UnitTests/UICatalog/ScenarioTests.cs

@@ -132,7 +132,7 @@ public class ScenarioTests : TestsAllViews
             {
                 // Press QuitKey 
                 //_output.WriteLine ($"Forcing Quit with {Application.QuitKey}");
-                Application.NewKeyDown (Application.QuitKey);
+                Application.RaiseKeyDownEvent (Application.QuitKey);
             }
         }
     }

+ 2 - 2
UnitTests/Views/MenuBarTests.cs

@@ -2666,7 +2666,7 @@ Edit
         top.Draw ();
         TestHelpers.AssertDriverContentsAre (expectedMenu.ExpectedSubMenuOpen (0), output);
 
-        Assert.True (Application.NewKeyDown (menu.Key));
+        Assert.True (Application.RaiseKeyDownEvent (menu.Key));
         Assert.False (menu.IsMenuOpen);
         Assert.True (tf.HasFocus);
         top.Draw ();
@@ -2949,7 +2949,7 @@ Edit
         top.Add (menu);
         Application.Begin (top);
 
-        Application.NewKeyDown (Key.S.WithCtrl);
+        Application.RaiseKeyDownEvent (Key.S.WithCtrl);
         Application.MainLoop.RunIteration ();
 
         Assert.True (saveAction);

+ 33 - 33
UnitTests/Views/RadioGroupTests.cs

@@ -57,7 +57,7 @@ public class RadioGroupTests (ITestOutputHelper output)
         rg.SetFocus ();
 
         Assert.Equal (-1, rg.SelectedItem);
-        Application.NewKeyDown (Key.Space);
+        Application.RaiseKeyDownEvent (Key.Space);
         Assert.Equal (0, rg.SelectedItem);
 
         Application.Top.Dispose ();
@@ -105,21 +105,21 @@ public class RadioGroupTests (ITestOutputHelper output)
 
         // With HasFocus
         // Test up/down without Select
-        Assert.False (Application.NewKeyDown (Key.CursorUp)); // Should not change (should focus prev view if there was one, which there isn't)
+        Assert.False (Application.RaiseKeyDownEvent (Key.CursorUp)); // Should not change (should focus prev view if there was one, which there isn't)
         Assert.Equal (0, rg.SelectedItem);
         Assert.Equal (0, rg.Cursor);
         Assert.Equal (0, selectedItemChangedCount);
         Assert.Equal (0, selectingCount);
         Assert.Equal (0, acceptedCount);
 
-        Assert.True (Application.NewKeyDown (Key.CursorDown));
+        Assert.True (Application.RaiseKeyDownEvent (Key.CursorDown));
         Assert.Equal (0, rg.SelectedItem); // Cursor changed, but selection didnt
         Assert.Equal (1, rg.Cursor);
         Assert.Equal (0, selectedItemChangedCount);
         Assert.Equal (0, selectingCount);
         Assert.Equal (0, acceptedCount);
 
-        Assert.False (Application.NewKeyDown (Key.CursorDown)); // Should not change selection (should focus next view if there was one, which there isn't)
+        Assert.False (Application.RaiseKeyDownEvent (Key.CursorDown)); // Should not change selection (should focus next view if there was one, which there isn't)
         Assert.Equal (0, rg.SelectedItem);
         Assert.Equal (1, rg.Cursor);
         Assert.Equal (0, selectedItemChangedCount);
@@ -127,7 +127,7 @@ public class RadioGroupTests (ITestOutputHelper output)
         Assert.Equal (0, acceptedCount);
 
         // Test Select (Space) when Cursor != SelectedItem - Should select cursor
-        Assert.True (Application.NewKeyDown (Key.Space));
+        Assert.True (Application.RaiseKeyDownEvent (Key.Space));
         Assert.Equal (1, rg.SelectedItem);
         Assert.Equal (1, rg.Cursor);
         Assert.Equal (1, selectedItemChangedCount);
@@ -135,34 +135,34 @@ public class RadioGroupTests (ITestOutputHelper output)
         Assert.Equal (0, acceptedCount);
 
         // Test Select (Space) when Cursor == SelectedItem - Should cycle
-        Assert.True (Application.NewKeyDown (Key.Space));
+        Assert.True (Application.RaiseKeyDownEvent (Key.Space));
         Assert.Equal (0, rg.SelectedItem);
         Assert.Equal (0, rg.Cursor);
         Assert.Equal (2, selectedItemChangedCount);
         Assert.Equal (2, selectingCount);
         Assert.Equal (0, acceptedCount);
 
-        Assert.True (Application.NewKeyDown (Key.Space));
+        Assert.True (Application.RaiseKeyDownEvent (Key.Space));
         Assert.Equal (1, rg.SelectedItem);
         Assert.Equal (1, rg.Cursor);
-        Assert.True (Application.NewKeyDown (Key.Space));
+        Assert.True (Application.RaiseKeyDownEvent (Key.Space));
         Assert.Equal (0, rg.SelectedItem);
         Assert.Equal (0, rg.Cursor);
-        Assert.True (Application.NewKeyDown (Key.Space));
+        Assert.True (Application.RaiseKeyDownEvent (Key.Space));
         Assert.Equal (1, rg.SelectedItem);
         Assert.Equal (1, rg.Cursor);
 
-        Assert.True (Application.NewKeyDown (Key.Home));
+        Assert.True (Application.RaiseKeyDownEvent (Key.Home));
         Assert.Equal (1, rg.SelectedItem);
         Assert.Equal (0, rg.Cursor);
-        Assert.True (Application.NewKeyDown (Key.Space));
+        Assert.True (Application.RaiseKeyDownEvent (Key.Space));
         Assert.Equal (0, rg.SelectedItem);
         Assert.Equal (0, rg.Cursor);
 
-        Assert.True (Application.NewKeyDown (Key.End));
+        Assert.True (Application.RaiseKeyDownEvent (Key.End));
         Assert.Equal (0, rg.SelectedItem);
         Assert.Equal (1, rg.Cursor);
-        Assert.True (Application.NewKeyDown (Key.Space));
+        Assert.True (Application.RaiseKeyDownEvent (Key.Space));
         Assert.Equal (1, rg.SelectedItem);
         Assert.Equal (1, rg.Cursor);
         Assert.Equal (7, selectedItemChangedCount);
@@ -174,7 +174,7 @@ public class RadioGroupTests (ITestOutputHelper output)
 
         rg.HotKey = Key.L;
         Assert.Equal (Key.L, rg.HotKey);
-        Assert.True (Application.NewKeyDown (rg.HotKey));
+        Assert.True (Application.RaiseKeyDownEvent (rg.HotKey));
         Assert.Equal (0, rg.SelectedItem);
         Assert.Equal (0, rg.Cursor);
         Assert.Equal (8, selectedItemChangedCount);
@@ -182,12 +182,12 @@ public class RadioGroupTests (ITestOutputHelper output)
         Assert.Equal (0, acceptedCount);
 
         //     Make Selected != Cursor
-        Assert.True (Application.NewKeyDown (Key.CursorDown));
+        Assert.True (Application.RaiseKeyDownEvent (Key.CursorDown));
         Assert.Equal (0, rg.SelectedItem);
         Assert.Equal (1, rg.Cursor);
 
         //    Selected != Cursor - Raise HotKey event - Since we're focused, this should just advance
-        Assert.True (Application.NewKeyDown (rg.HotKey));
+        Assert.True (Application.RaiseKeyDownEvent (rg.HotKey));
         Assert.Equal (1, rg.SelectedItem);
         Assert.Equal (1, rg.Cursor);
         Assert.Equal (9, selectedItemChangedCount);
@@ -239,7 +239,7 @@ public class RadioGroupTests (ITestOutputHelper output)
         //    Selected (0) == Cursor (0) - SetFocus
         rg.HotKey = Key.L;
         Assert.Equal (Key.L, rg.HotKey);
-        Assert.True (Application.NewKeyDown (rg.HotKey));
+        Assert.True (Application.RaiseKeyDownEvent (rg.HotKey));
         Assert.True (rg.HasFocus);
         Assert.Equal (0, rg.SelectedItem);
         Assert.Equal (0, rg.Cursor);
@@ -248,14 +248,14 @@ public class RadioGroupTests (ITestOutputHelper output)
         Assert.Equal (0, acceptCount);
 
         //     Make Selected != Cursor
-        Assert.True (Application.NewKeyDown (Key.CursorDown));
+        Assert.True (Application.RaiseKeyDownEvent (Key.CursorDown));
         Assert.Equal (0, rg.SelectedItem);
         Assert.Equal (1, rg.Cursor);
 
         otherView.SetFocus ();
 
         //    Selected != Cursor - SetFocus
-        Assert.True (Application.NewKeyDown (rg.HotKey));
+        Assert.True (Application.RaiseKeyDownEvent (rg.HotKey));
         Assert.True (rg.HasFocus);
         Assert.Equal (0, rg.SelectedItem);
         Assert.Equal (1, rg.Cursor);
@@ -263,7 +263,7 @@ public class RadioGroupTests (ITestOutputHelper output)
         Assert.Equal (0, selectCount);
         Assert.Equal (0, acceptCount);
 
-        Assert.True (Application.NewKeyDown (rg.HotKey));
+        Assert.True (Application.RaiseKeyDownEvent (rg.HotKey));
         Assert.True (rg.HasFocus);
         Assert.Equal (1, rg.SelectedItem);
         Assert.Equal (1, rg.Cursor);
@@ -314,7 +314,7 @@ public class RadioGroupTests (ITestOutputHelper output)
 
         // Test RadioTitem.HotKey - Should never SetFocus
         //    Selected (0) == Cursor (0) 
-        Assert.True (Application.NewKeyDown (Key.A));
+        Assert.True (Application.RaiseKeyDownEvent (Key.A));
         Assert.False (rg.HasFocus);
         Assert.Equal (0, rg.SelectedItem);
         Assert.Equal (0, rg.Cursor);
@@ -325,14 +325,14 @@ public class RadioGroupTests (ITestOutputHelper output)
         rg.SetFocus ();
 
         //     Make Selected != Cursor
-        Assert.True (Application.NewKeyDown (Key.CursorDown));
+        Assert.True (Application.RaiseKeyDownEvent (Key.CursorDown));
         Assert.Equal (0, rg.SelectedItem);
         Assert.Equal (1, rg.Cursor);
 
         otherView.SetFocus ();
 
         //    Selected != Cursor
-        Assert.True (Application.NewKeyDown (Key.A));
+        Assert.True (Application.RaiseKeyDownEvent (Key.A));
         Assert.False (rg.HasFocus);
         Assert.Equal (0, rg.SelectedItem);
         Assert.Equal (1, rg.Cursor);
@@ -341,7 +341,7 @@ public class RadioGroupTests (ITestOutputHelper output)
         Assert.Equal (0, acceptCount);
 
         //    Selected != Cursor - Should not set focus
-        Assert.True (Application.NewKeyDown (Key.B));
+        Assert.True (Application.RaiseKeyDownEvent (Key.B));
         Assert.False (rg.HasFocus);
         Assert.Equal (1, rg.SelectedItem);
         Assert.Equal (1, rg.Cursor);
@@ -349,7 +349,7 @@ public class RadioGroupTests (ITestOutputHelper output)
         Assert.Equal (1, selectCount);
         Assert.Equal (0, acceptCount);
 
-        Assert.True (Application.NewKeyDown (Key.B));
+        Assert.True (Application.RaiseKeyDownEvent (Key.B));
         Assert.False (rg.HasFocus);
         Assert.Equal (1, rg.SelectedItem);
         Assert.Equal (1, rg.Cursor);
@@ -372,22 +372,22 @@ public class RadioGroupTests (ITestOutputHelper output)
         Assert.NotEmpty (rg.KeyBindings.GetCommands (KeyCode.L | KeyCode.ShiftMask));
         Assert.NotEmpty (rg.KeyBindings.GetCommands (KeyCode.L | KeyCode.AltMask));
 
-        Assert.True (Application.NewKeyDown (Key.T));
+        Assert.True (Application.RaiseKeyDownEvent (Key.T));
         Assert.Equal (2, rg.SelectedItem);
-        Assert.True (Application.NewKeyDown (Key.L));
+        Assert.True (Application.RaiseKeyDownEvent (Key.L));
         Assert.Equal (0, rg.SelectedItem);
-        Assert.True (Application.NewKeyDown (Key.J));
+        Assert.True (Application.RaiseKeyDownEvent (Key.J));
         Assert.Equal (3, rg.SelectedItem);
-        Assert.True (Application.NewKeyDown (Key.R));
+        Assert.True (Application.RaiseKeyDownEvent (Key.R));
         Assert.Equal (1, rg.SelectedItem);
 
-        Assert.True (Application.NewKeyDown (Key.T.WithAlt));
+        Assert.True (Application.RaiseKeyDownEvent (Key.T.WithAlt));
         Assert.Equal (2, rg.SelectedItem);
-        Assert.True (Application.NewKeyDown (Key.L.WithAlt));
+        Assert.True (Application.RaiseKeyDownEvent (Key.L.WithAlt));
         Assert.Equal (0, rg.SelectedItem);
-        Assert.True (Application.NewKeyDown (Key.J.WithAlt));
+        Assert.True (Application.RaiseKeyDownEvent (Key.J.WithAlt));
         Assert.Equal (3, rg.SelectedItem);
-        Assert.True (Application.NewKeyDown (Key.R.WithAlt));
+        Assert.True (Application.RaiseKeyDownEvent (Key.R.WithAlt));
         Assert.Equal (1, rg.SelectedItem);
 
         var superView = new View ();

+ 3 - 3
UnitTests/Views/TextFieldTests.cs

@@ -524,7 +524,7 @@ public class TextFieldTests (ITestOutputHelper output)
         Application.Top = new ();
         Application.Top.Add (tf);
         tf.SetFocus ();
-        Application.NewKeyDown (Key.Space);
+        Application.RaiseKeyDownEvent (Key.Space);
 
         Application.Top.Dispose ();
         Application.ResetState (true);
@@ -541,7 +541,7 @@ public class TextFieldTests (ITestOutputHelper output)
         Application.Top = new ();
         Application.Top.Add (tf);
         tf.SetFocus ();
-        Application.NewKeyDown (Key.Enter);
+        Application.RaiseKeyDownEvent (Key.Enter);
 
         Assert.Equal (0, selectingCount);
 
@@ -560,7 +560,7 @@ public class TextFieldTests (ITestOutputHelper output)
         Application.Top = new ();
         Application.Top.Add (tf);
         tf.SetFocus ();
-        Application.NewKeyDown (Key.Enter);
+        Application.RaiseKeyDownEvent (Key.Enter);
 
         Assert.Equal (1, acceptedCount);