瀏覽代碼

Merged v2_develop.
Code cleanup

Tig 7 月之前
父節點
當前提交
5a0b3507a0

+ 1 - 1
Terminal.Gui/ConsoleDrivers/AnsiEscapeSequenceRequest.cs

@@ -21,7 +21,7 @@ public class AnsiEscapeSequenceRequest : AnsiEscapeSequence
 
 
 
 
     /// <summary>
     /// <summary>
-    ///     Sends the <see cref="Request"/> to the raw output stream of the current <see cref="ConsoleDriver"/>.
+    ///     Sends the <see cref="AnsiEscapeSequence.Request"/> to the raw output stream of the current <see cref="ConsoleDriver"/>.
     ///     Only call this method from the main UI thread. You should use <see cref="AnsiRequestScheduler"/> if
     ///     Only call this method from the main UI thread. You should use <see cref="AnsiRequestScheduler"/> if
     ///     sending many requests.
     ///     sending many requests.
     /// </summary>
     /// </summary>

+ 3 - 1
Terminal.Gui/Input/ICommandContext.cs

@@ -1,12 +1,14 @@
 #nullable enable
 #nullable enable
 namespace Terminal.Gui;
 namespace Terminal.Gui;
 
 
+#pragma warning disable CS1574 // XML comment has cref attribute that could not be resolved
 /// <summary>
 /// <summary>
 ///     Describes the context in which a <see cref="Command"/> is being invoked. <see cref="CommandContext{TBindingType}"/> inherits from this interface.
 ///     Describes the context in which a <see cref="Command"/> is being invoked. <see cref="CommandContext{TBindingType}"/> inherits from this interface.
 ///     When a <see cref="Command"/> is invoked,
 ///     When a <see cref="Command"/> is invoked,
 ///     a context object is passed to Command handlers as an <see cref="ICommandContext"/> reference.
 ///     a context object is passed to Command handlers as an <see cref="ICommandContext"/> reference.
 /// </summary>
 /// </summary>
-/// <seealso cref="View.AddCommand(Command, CommandImplementation)"/>.
+/// <seealso cref="View.AddCommand(Command, View.CommandImplementation)"/>.
+#pragma warning restore CS1574 // XML comment has cref attribute that could not be resolved
 public interface ICommandContext
 public interface ICommandContext
 {
 {
     /// <summary>
     /// <summary>

+ 0 - 1
Terminal.Gui/Input/Keyboard/KeyBinding.cs

@@ -24,7 +24,6 @@ public record struct KeyBinding
 
 
     /// <summary>Initializes a new instance.</summary>
     /// <summary>Initializes a new instance.</summary>
     /// <param name="commands">The commands this key binding will invoke.</param>
     /// <param name="commands">The commands this key binding will invoke.</param>
-    /// <param name="scope">The scope of the <see cref="Commands"/>.</param>
     /// <param name="boundView">The view the key binding is bound to.</param>
     /// <param name="boundView">The view the key binding is bound to.</param>
     /// <param name="data">Arbitrary data that can be associated with this key binding.</param>
     /// <param name="data">Arbitrary data that can be associated with this key binding.</param>
     public KeyBinding (Command [] commands, View? boundView, object? data = null)
     public KeyBinding (Command [] commands, View? boundView, object? data = null)

+ 3 - 7
Terminal.Gui/Input/Keyboard/KeyBindings.cs

@@ -48,17 +48,13 @@ public class KeyBindings
         Bindings.Add (new (key), binding);
         Bindings.Add (new (key), binding);
     }
     }
 
 
+#pragma warning disable CS1574 // XML comment has cref attribute that could not be resolved
     /// <summary>
     /// <summary>
     ///     <para>
     ///     <para>
     ///         Adds a new key combination that will trigger the commands in <paramref name="commands"/> (if supported by the
     ///         Adds a new key combination that will trigger the commands in <paramref name="commands"/> (if supported by the
     ///         View - see <see cref="View.GetSupportedCommands"/>).
     ///         View - see <see cref="View.GetSupportedCommands"/>).
     ///     </para>
     ///     </para>
     ///     <para>
     ///     <para>
-    ///         This is a helper function for <see cref="Add(Key,KeyBinding,View?)"/>. If used for a View (
-    ///         <see cref="BoundView"/> is set), the scope will be set to <see cref="KeyBindingScope.Focused"/>.
-    ///         Otherwise, it will be set to <see cref="KeyBindingScope.Application"/>.
-    ///     </para>
-    ///     <para>
     ///         If the key is already bound to a different array of <see cref="Command"/>s it will be rebound
     ///         If the key is already bound to a different array of <see cref="Command"/>s it will be rebound
     ///         <paramref name="commands"/>.
     ///         <paramref name="commands"/>.
     ///     </para>
     ///     </para>
@@ -74,6 +70,7 @@ public class KeyBindings
     ///     consumed if any took effect.
     ///     consumed if any took effect.
     /// </param>
     /// </param>
     /// <exception cref="ArgumentException">If <paramref name="commands"/> is empty.</exception>
     /// <exception cref="ArgumentException">If <paramref name="commands"/> is empty.</exception>
+#pragma warning restore CS1574 // XML comment has cref attribute that could not be resolved
     public void Add (Key key, params Command [] commands)
     public void Add (Key key, params Command [] commands)
     {
     {
         Add (key, new KeyBinding (commands));
         Add (key, new KeyBinding (commands));
@@ -168,8 +165,7 @@ public class KeyBindings
 
 
     /// <summary>Removes a <see cref="KeyBinding"/> from the collection.</summary>
     /// <summary>Removes a <see cref="KeyBinding"/> from the collection.</summary>
     /// <param name="key"></param>
     /// <param name="key"></param>
-    /// <param name="boundViewForAppScope">Optional View for <see cref="KeyBindingScope.Application"/> bindings.</param>
-    public void Remove (Key key, View? boundViewForAppScope = null)
+    public void Remove (Key key)
     {
     {
         if (!TryGet (key, out KeyBinding _))
         if (!TryGet (key, out KeyBinding _))
         {
         {

+ 18 - 12
Terminal.Gui/View/View.Keyboard.cs

@@ -299,13 +299,13 @@ public partial class View // Keyboard APIs
         // During (this is what can be cancelled)
         // During (this is what can be cancelled)
 
 
         // TODO: NewKeyDownEvent returns bool. It should be bool? so state of InvokeCommands can be reflected up stack
         // TODO: NewKeyDownEvent returns bool. It should be bool? so state of InvokeCommands can be reflected up stack
-        if (InvokeCommandsBoundToKey (key) is true || key.Handled)
+        if (InvokeCommands (key) is true || key.Handled)
         {
         {
             return true;
             return true;
         }
         }
 
 
         bool? handled = false;
         bool? handled = false;
-        if (InvokeCommandsBoundToHotKeyOnSubviews (key, ref handled))
+        if (InvokeCommandsBoundToHotKey (key, ref handled))
         {
         {
             return true;
             return true;
         }
         }
@@ -516,7 +516,7 @@ public partial class View // Keyboard APIs
     /// <summary>Gets the bindings for this view that will be invoked only if this view has focus.</summary>
     /// <summary>Gets the bindings for this view that will be invoked only if this view has focus.</summary>
     public KeyBindings KeyBindings { get; internal set; } = null!;
     public KeyBindings KeyBindings { get; internal set; } = null!;
 
 
-    /// <summary>Gets the bindings for this view that will be invoked regardless of whehter this view has focus or not.</summary>
+    /// <summary>Gets the bindings for this view that will be invoked regardless of whether this view has focus or not.</summary>
     public KeyBindings HotKeyBindings { get; internal set; } = null!;
     public KeyBindings HotKeyBindings { get; internal set; } = null!;
 
 
     /// <summary>
     /// <summary>
@@ -531,7 +531,7 @@ public partial class View // Keyboard APIs
     ///     <see langword="true"/> if at least one command was invoked and handled (or
     ///     <see langword="true"/> if at least one command was invoked and handled (or
     ///     cancelled); input processing should stop.
     ///     cancelled); input processing should stop.
     /// </returns>
     /// </returns>
-    internal bool? InvokeCommandsBoundToKey (Key key)
+    internal bool? InvokeCommands (Key key)
     {
     {
         // * If no key binding was found, `InvokeKeyBindings` returns `null`.
         // * If no key binding was found, `InvokeKeyBindings` returns `null`.
         //   Continue passing the event (return `false` from `OnInvokeKeyBindings`).
         //   Continue passing the event (return `false` from `OnInvokeKeyBindings`).
@@ -539,7 +539,7 @@ public partial class View // Keyboard APIs
         //   `InvokeKeyBindings` returns `false`. Continue passing the event (return `false` from `OnInvokeKeyBindings`)..
         //   `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`),
         // * 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`).
         //   `InvokeKeyBindings` returns `true`. Continue passing the event (return `false` from `OnInvokeKeyBindings`).
-        bool? handled = InvokeCommandsBoundToFocusedKey (key);
+        bool? handled = DoInvokeCommands (key);
 
 
         if (handled is true)
         if (handled is true)
         {
         {
@@ -568,7 +568,7 @@ public partial class View // Keyboard APIs
 
 
     private static bool InvokeCommandsBoundToKeyOnAdornment (Adornment adornment, Key key, ref bool? handled)
     private static bool InvokeCommandsBoundToKeyOnAdornment (Adornment adornment, Key key, ref bool? handled)
     {
     {
-        bool? adornmentHandled = adornment.InvokeCommandsBoundToKey (key);
+        bool? adornmentHandled = adornment.InvokeCommands (key);
 
 
         if (adornmentHandled is true)
         if (adornmentHandled is true)
         {
         {
@@ -582,7 +582,7 @@ public partial class View // Keyboard APIs
 
 
         foreach (View subview in adornment.Subviews)
         foreach (View subview in adornment.Subviews)
         {
         {
-            bool? subViewHandled = subview.InvokeCommandsBoundToKey (key);
+            bool? subViewHandled = subview.InvokeCommands (key);
 
 
             if (subViewHandled is { })
             if (subViewHandled is { })
             {
             {
@@ -598,7 +598,14 @@ public partial class View // Keyboard APIs
         return false;
         return false;
     }
     }
 
 
-    internal bool InvokeCommandsBoundToHotKeyOnSubviews (Key key, ref bool? handled, bool invoke = true)
+    // BUGBUG: This will miss any hotkeys in subviews of Adornments.
+    /// <summary>
+    ///     Invokes any commands bound to <paramref name="key"/> on this view and subviews.
+    /// </summary>
+    /// <param name="key"></param>
+    /// <param name="handled"></param>
+    /// <returns></returns>
+    internal bool InvokeCommandsBoundToHotKey (Key key, ref bool? handled)
     {
     {
         bool? weHandled = InvokeCommandsBoundToHotKey (key);
         bool? weHandled = InvokeCommandsBoundToHotKey (key);
         if (weHandled is true)
         if (weHandled is true)
@@ -606,7 +613,7 @@ public partial class View // Keyboard APIs
             return true;
             return true;
         }
         }
 
 
-        // Now, process any key bindings in the subviews that are tagged to KeyBindingScope.HotKey.
+        // Now, process any HotKey bindings in the subviews
         foreach (View subview in Subviews)
         foreach (View subview in Subviews)
         {
         {
             if (subview == Focused)
             if (subview == Focused)
@@ -614,7 +621,7 @@ public partial class View // Keyboard APIs
                 continue;
                 continue;
             }
             }
 
 
-            bool recurse = subview.InvokeCommandsBoundToHotKeyOnSubviews (key, ref handled, invoke);
+            bool recurse = subview.InvokeCommandsBoundToHotKey (key, ref handled);
 
 
             if (recurse || (handled is { } && (bool)handled))
             if (recurse || (handled is { } && (bool)handled))
             {
             {
@@ -669,7 +676,7 @@ public partial class View // Keyboard APIs
     ///     <see langword="true"/> if at least one command was invoked and handled (or cancelled); input processing should
     ///     <see langword="true"/> if at least one command was invoked and handled (or cancelled); input processing should
     ///     stop.
     ///     stop.
     /// </returns>
     /// </returns>
-    protected bool? InvokeCommandsBoundToFocusedKey (Key key)
+    protected bool? DoInvokeCommands (Key key)
     {
     {
         if (!KeyBindings.TryGet (key, out KeyBinding binding))
         if (!KeyBindings.TryGet (key, out KeyBinding binding))
         {
         {
@@ -702,7 +709,6 @@ public partial class View // Keyboard APIs
     ///     <para>See <see href="../docs/keyboard.md">for an overview of Terminal.Gui keyboard APIs.</see></para>
     ///     <para>See <see href="../docs/keyboard.md">for an overview of Terminal.Gui keyboard APIs.</see></para>
     /// </summary>
     /// </summary>
     /// <param name="key">The key event passed.</param>
     /// <param name="key">The key event passed.</param>
-    /// <param name="scope">The scope.</param>
     /// <returns>
     /// <returns>
     ///     <see langword="null"/> if no command was invoked; input processing should continue.
     ///     <see langword="null"/> if no command was invoked; input processing should continue.
     ///     <see langword="false"/> if at least one command was invoked and was not handled (or cancelled); input processing
     ///     <see langword="false"/> if at least one command was invoked and was not handled (or cancelled); input processing

+ 3 - 0
Terminal.Gui/Views/CharMap/CharMap.cs

@@ -20,9 +20,12 @@ public class CharMap : View, IDesignable
 
 
     private ContextMenu _contextMenu = new ();
     private ContextMenu _contextMenu = new ();
 
 
+
     /// <summary>
     /// <summary>
     ///     Initializes a new instance.
     ///     Initializes a new instance.
     /// </summary>
     /// </summary>
+    [RequiresUnreferencedCode ("AOT")]
+    [RequiresDynamicCode ("AOT")]
     public CharMap ()
     public CharMap ()
     {
     {
         base.ColorScheme = Colors.ColorSchemes ["Dialog"];
         base.ColorScheme = Colors.ColorSchemes ["Dialog"];

+ 1 - 1
Terminal.Gui/Views/Menu/Menu.cs

@@ -251,7 +251,7 @@ internal sealed class Menu : View
     {
     {
         // We didn't handle the key, pass it on to host
         // We didn't handle the key, pass it on to host
         bool? handled = null;
         bool? handled = null;
-        return _host.InvokeCommandsBoundToHotKeyOnSubviews (keyEvent, ref handled, true) == true;
+        return _host.InvokeCommandsBoundToHotKey (keyEvent, ref handled) == true;
     }
     }
 
 
     protected override bool OnMouseEvent (MouseEventArgs me)
     protected override bool OnMouseEvent (MouseEventArgs me)

+ 1 - 1
Terminal.Gui/Views/Shortcut.cs

@@ -18,7 +18,7 @@ namespace Terminal.Gui;
 ///         - Pressing the HotKey specified by <see cref="CommandView"/>.
 ///         - Pressing the HotKey specified by <see cref="CommandView"/>.
 ///     </para>
 ///     </para>
 ///     <para>
 ///     <para>
-///         If <see cref="BindKeyToApplication"/> is <see cref="KeyBindingScope.Application"/>, <see cref="Key"/> will invoke
+///         If <see cref="BindKeyToApplication"/> is <see langword="true"/>, <see cref="Key"/> will invoke
 ///         <see cref="Command.Accept"/>
 ///         <see cref="Command.Accept"/>
 ///         regardless of what View has focus, enabling an application-wide keyboard shortcut.
 ///         regardless of what View has focus, enabling an application-wide keyboard shortcut.
 ///     </para>
 ///     </para>

+ 0 - 2
UnitTests/ConsoleDrivers/AnsiResponseParserTests.cs

@@ -438,8 +438,6 @@ public class AnsiResponseParserTests (ITestOutputHelper output)
     [Fact]
     [Fact]
     public void UnknownResponses_ParameterShouldMatch ()
     public void UnknownResponses_ParameterShouldMatch ()
     {
     {
-        int i = 0;
-
         // Track unknown responses passed to the UnexpectedResponseHandler
         // Track unknown responses passed to the UnexpectedResponseHandler
         var unknownResponses = new List<string> ();
         var unknownResponses = new List<string> ();
 
 

+ 2 - 2
UnitTests/View/Keyboard/KeyboardEventTests.cs

@@ -273,12 +273,12 @@ public class KeyboardEventTests (ITestOutputHelper output) : TestsAllViews
     [InlineData (null, null)]
     [InlineData (null, null)]
     [InlineData (true, true)]
     [InlineData (true, true)]
     [InlineData (false, false)]
     [InlineData (false, false)]
-    public void InvokeCommandsBoundToKey_Returns_Nullable_Properly (bool? toReturn, bool? expected)
+    public void InvokeCommands_Returns_Nullable_Properly (bool? toReturn, bool? expected)
     {
     {
         var view = new KeyBindingsTestView ();
         var view = new KeyBindingsTestView ();
         view.CommandReturns = toReturn;
         view.CommandReturns = toReturn;
 
 
-        bool? result = view.InvokeCommandsBoundToKey (Key.A);
+        bool? result = view.InvokeCommands (Key.A);
         Assert.Equal (expected, result);
         Assert.Equal (expected, result);
     }
     }
 
 

+ 21 - 27
UnitTests/View/ViewCommandTests.cs

@@ -1,12 +1,9 @@
-using System.ComponentModel;
-using System.Text;
-using Xunit.Abstractions;
-
 namespace Terminal.Gui.ViewTests;
 namespace Terminal.Gui.ViewTests;
 
 
-public class ViewCommandTests (ITestOutputHelper output)
+public class ViewCommandTests
 {
 {
     #region OnAccept/Accept tests
     #region OnAccept/Accept tests
+
     [Fact]
     [Fact]
     public void Accept_Command_Raises_NoFocus ()
     public void Accept_Command_Raises_NoFocus ()
     {
     {
@@ -77,8 +74,8 @@ public class ViewCommandTests (ITestOutputHelper output)
     [Fact]
     [Fact]
     public void Accept_Command_Bubbles_Up_To_SuperView ()
     public void Accept_Command_Bubbles_Up_To_SuperView ()
     {
     {
-        var view = new ViewEventTester () { Id = "view" };
-        var subview = new ViewEventTester () { Id = "subview" };
+        var view = new ViewEventTester { Id = "view" };
+        var subview = new ViewEventTester { Id = "subview" };
         view.Add (subview);
         view.Add (subview);
 
 
         subview.InvokeCommand (Command.Accept);
         subview.InvokeCommand (Command.Accept);
@@ -97,7 +94,7 @@ public class ViewCommandTests (ITestOutputHelper output)
         Assert.Equal (1, view.OnAcceptedCount);
         Assert.Equal (1, view.OnAcceptedCount);
 
 
         // Add a super view to test deeper hierarchy
         // Add a super view to test deeper hierarchy
-        var superView = new ViewEventTester () { Id = "superView" };
+        var superView = new ViewEventTester { Id = "superView" };
         superView.Add (view);
         superView.Add (view);
 
 
         subview.InvokeCommand (Command.Accept);
         subview.InvokeCommand (Command.Accept);
@@ -135,7 +132,7 @@ public class ViewCommandTests (ITestOutputHelper output)
     [CombinatorialData]
     [CombinatorialData]
     public void Select_Command_Raises_SetsFocus (bool canFocus)
     public void Select_Command_Raises_SetsFocus (bool canFocus)
     {
     {
-        var view = new ViewEventTester ()
+        var view = new ViewEventTester
         {
         {
             CanFocus = canFocus
             CanFocus = canFocus
         };
         };
@@ -236,30 +233,29 @@ public class ViewCommandTests (ITestOutputHelper output)
             CanFocus = true;
             CanFocus = true;
 
 
             Accepting += (s, a) =>
             Accepting += (s, a) =>
-                      {
-                          a.Cancel = HandleAccepted;
-                          AcceptedCount++;
-                      };
+                         {
+                             a.Cancel = HandleAccepted;
+                             AcceptedCount++;
+                         };
 
 
             HandlingHotKey += (s, a) =>
             HandlingHotKey += (s, a) =>
-                             {
-                                 a.Cancel = HandleHandlingHotKey;
-                                 HandlingHotKeyCount++;
-                             };
-
+                              {
+                                  a.Cancel = HandleHandlingHotKey;
+                                  HandlingHotKeyCount++;
+                              };
 
 
             Selecting += (s, a) =>
             Selecting += (s, a) =>
-                             {
-                                 a.Cancel = HandleSelecting;
-                                 SelectingCount++;
-                             };
+                         {
+                             a.Cancel = HandleSelecting;
+                             SelectingCount++;
+                         };
         }
         }
 
 
         public int OnAcceptedCount { get; set; }
         public int OnAcceptedCount { get; set; }
         public int AcceptedCount { get; set; }
         public int AcceptedCount { get; set; }
         public bool HandleOnAccepted { get; set; }
         public bool HandleOnAccepted { get; set; }
 
 
-        /// <inheritdoc />
+        /// <inheritdoc/>
         protected override bool OnAccepting (CommandEventArgs args)
         protected override bool OnAccepting (CommandEventArgs args)
         {
         {
             OnAcceptedCount++;
             OnAcceptedCount++;
@@ -273,7 +269,7 @@ public class ViewCommandTests (ITestOutputHelper output)
         public int HandlingHotKeyCount { get; set; }
         public int HandlingHotKeyCount { get; set; }
         public bool HandleOnHandlingHotKey { get; set; }
         public bool HandleOnHandlingHotKey { get; set; }
 
 
-        /// <inheritdoc />
+        /// <inheritdoc/>
         protected override bool OnHandlingHotKey (CommandEventArgs args)
         protected override bool OnHandlingHotKey (CommandEventArgs args)
         {
         {
             OnHandlingHotKeyCount++;
             OnHandlingHotKeyCount++;
@@ -283,12 +279,11 @@ public class ViewCommandTests (ITestOutputHelper output)
 
 
         public bool HandleHandlingHotKey { get; set; }
         public bool HandleHandlingHotKey { get; set; }
 
 
-
         public int OnSelectingCount { get; set; }
         public int OnSelectingCount { get; set; }
         public int SelectingCount { get; set; }
         public int SelectingCount { get; set; }
         public bool HandleOnSelecting { get; set; }
         public bool HandleOnSelecting { get; set; }
 
 
-        /// <inheritdoc />
+        /// <inheritdoc/>
         protected override bool OnSelecting (CommandEventArgs args)
         protected override bool OnSelecting (CommandEventArgs args)
         {
         {
             OnSelectingCount++;
             OnSelectingCount++;
@@ -297,6 +292,5 @@ public class ViewCommandTests (ITestOutputHelper output)
         }
         }
 
 
         public bool HandleSelecting { get; set; }
         public bool HandleSelecting { get; set; }
-
     }
     }
 }
 }