Tig пре 1 година
родитељ
комит
625aa90503

+ 2 - 0
Terminal.Gui/Input/KeyBindings.cs

@@ -1,5 +1,7 @@
 #nullable enable
 
+using System.Diagnostics;
+
 namespace Terminal.Gui;
 
 /// <summary>

+ 1 - 0
Terminal.Gui/View/View.cs

@@ -230,6 +230,7 @@ public partial class View : Responder, ISupportInitializeNotification
         }
 
         Initialized?.Invoke (this, EventArgs.Empty);
+
     }
 
     #endregion Constructors and Initialization

+ 60 - 2
Terminal.Gui/View/ViewKeyboard.cs

@@ -1,4 +1,5 @@
 using System.ComponentModel;
+using System.Diagnostics;
 
 namespace Terminal.Gui;
 
@@ -702,7 +703,7 @@ public partial class View
         return false;
     }
 
-    private bool ProcessSubViewKeyBindings (Key keyEvent, KeyBindingScope scope, ref bool? handled)
+    private bool ProcessSubViewKeyBindings (Key keyEvent, KeyBindingScope scope, ref bool? handled, bool invoke = true)
     {
         // Now, process any key bindings in the subviews that are tagged to KeyBindingScope.HotKey.
         foreach (View subview in Subviews)
@@ -713,6 +714,12 @@ public partial class View
                 {
                     continue;
                 }
+
+                if (!invoke)
+                {
+                    return true;
+                }
+
                 handled = subview.OnInvokingKeyBindings (keyEvent, scope);
 
                 if (handled is { } && (bool)handled)
@@ -721,7 +728,7 @@ public partial class View
                 }
             }
 
-            bool recurse = subview.ProcessSubViewKeyBindings (keyEvent, scope, ref handled);
+            bool recurse = subview.ProcessSubViewKeyBindings (keyEvent, scope, ref handled, invoke);
             if (recurse || (handled is { } && (bool)handled))
             {
                 return true;
@@ -731,6 +738,36 @@ public partial class View
         return false;
     }
 
+    // TODO: This is a "prototype" debug check. It may be too annyoing vs. useful.
+    // TODO: A better approach would be have Application hold a list of bound Hotkeys, similar to
+    // TODO: how Application holds a list of Application Scoped key bindings and then check that list.
+    /// <summary>
+    /// Returns true if Key is bound in this view heirarchy. For debugging
+    /// </summary>
+    /// <param name="key"></param>
+    /// <returns></returns>
+    public bool IsHotKeyKeyBound (Key key, out View boundView)
+    {
+        // recurse through the subviews to find the views that has the key bound
+        boundView = null;
+
+        foreach (View subview in Subviews)
+        {
+            if (subview.KeyBindings.TryGet (key, KeyBindingScope.HotKey, out _))
+            {
+                boundView = subview;
+                return true;
+            }
+
+            if (subview.IsHotKeyKeyBound (key, out boundView))
+            {
+                return true;
+            }
+
+        }
+        return false;
+    }
+
     /// <summary>
     ///     Invoked when a key is pressed that may be mapped to a key binding. Set <see cref="Key.Handled"/> to true to
     ///     stop the key from being processed by other views.
@@ -756,6 +793,27 @@ public partial class View
             return null;
         }
 
+#if DEBUG
+
+        // TODO: Determine if App scope bindings should be fired first or last (currently last).
+        if (Application.TryGetKeyBindings (key, out List<View> views))
+        {
+            var boundView = views [0];
+            var commandBinding = boundView.KeyBindings.Get (key);
+            Debug.WriteLine ($"WARNING: InvokeKeyBindings ({key}) - An Application scope binding exists for this key. The registered view will not invoke Command.{commandBinding.Commands [0]}: {boundView}.");
+        }
+
+        // TODO: This is a "prototype" debug check. It may be too annyoing vs. useful.
+        // Scour the bindings up our View heirarchy
+        // to ensure that the key is not already bound to a different set of commands.
+        if (SuperView?.IsHotKeyKeyBound (key, out View previouslyBoundView) ?? false)
+        {
+            Debug.WriteLine ($"WARNING: InvokeKeyBindings ({key}) - A subview or peer has bound this Key and will not see it: {previouslyBoundView}.");
+        }
+
+#endif
+
+
         foreach (Command command in binding.Commands)
         {
             if (!CommandImplementations.ContainsKey (command))

+ 2 - 0
Terminal.Gui/View/ViewSubViews.cs

@@ -1,3 +1,5 @@
+using System.Diagnostics;
+
 namespace Terminal.Gui;
 
 public partial class View

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

@@ -119,7 +119,7 @@ public partial class Toplevel : View
         KeyBindings.Add (Key.Tab.WithShift.WithCtrl, Command.PreviousViewOrTop);
 
         // TODO: Refresh Key should be configurable
-        KeyBindings.Add (Key.F5, Command.Refresh);
+        KeyBindings.Add (Key.F5, KeyBindingScope.Application, Command.Refresh);
         KeyBindings.Add (Application.AlternateForwardKey, Command.NextViewOrTop); // Needed on Unix
         KeyBindings.Add (Application.AlternateBackwardKey, Command.PreviousViewOrTop); // Needed on Unix
 

+ 6 - 3
UICatalog/Scenarios/Shortcuts.cs

@@ -1,6 +1,8 @@
 using System;
 using System.Collections.ObjectModel;
+using System.Diagnostics;
 using System.Linq;
+using System.Text;
 using System.Threading.Tasks;
 using System.Timers;
 using Terminal.Gui;
@@ -38,7 +40,9 @@ public class Shortcuts : Scenario
             Width = 40,
             Height = Dim.Fill (4),
             ColorScheme = Colors.ColorSchemes ["Toplevel"],
-            Source = new ListWrapper<string> (eventSource)
+            Source = new ListWrapper<string> (eventSource),
+            BorderStyle = LineStyle.Double,
+            Title = "E_vents"
         };
         Application.Top.Add (eventLog);
 
@@ -228,7 +232,7 @@ public class Shortcuts : Scenario
             Y = Pos.Bottom (vShortcut6),
             Width = Dim.Width (vShortcutSlider),
             Key = Key.F6,
-            Title = "No _Help",
+            Title = "Not _very much help",
             HelpText = "",
         };
 
@@ -348,7 +352,6 @@ public class Shortcuts : Scenario
 
         Application.Top.Add (hShortcut3);
 
-
         foreach (View sh in Application.Top.Subviews.Where (v => v is Shortcut)!)
         {
             if (sh is Shortcut shortcut)

+ 0 - 1
UnitTests/View/NavigationTests.cs

@@ -1535,7 +1535,6 @@ public class NavigationTests (ITestOutputHelper output)
     }
 
     [Fact]
-    [AutoInitShutdown]
     public void WindowDispose_CanFocusProblem ()
     {
         // Arrange