浏览代码

Improved KeyBindings scenario.
Made QuitKey and Refresh app scoped

Tig 1 年之前
父节点
当前提交
891eb29660
共有 3 个文件被更改,包括 83 次插入20 次删除
  1. 5 0
      Terminal.Gui/Application/ApplicationKeyboard.cs
  2. 3 2
      Terminal.Gui/Views/Toplevel.cs
  3. 75 18
      UICatalog/Scenarios/KeyBindings.cs

+ 5 - 0
Terminal.Gui/Application/ApplicationKeyboard.cs

@@ -209,6 +209,11 @@ partial class Application
     /// </summary>
     /// </summary>
     private static readonly Dictionary<Key, List<View>> _keyBindings = new ();
     private static readonly Dictionary<Key, List<View>> _keyBindings = new ();
 
 
+    /// <summary>
+    /// Gets the list of <see cref="KeyBindingScope.Application"/> key bindings.
+    /// </summary>
+    public static Dictionary<Key, List<View>> GetKeyBindings () { return _keyBindings; }
+
     /// <summary>
     /// <summary>
     ///     Adds an  <see cref="KeyBindingScope.Application"/> scoped key binding.
     ///     Adds an  <see cref="KeyBindingScope.Application"/> scoped key binding.
     /// </summary>
     /// </summary>

+ 3 - 2
Terminal.Gui/Views/Toplevel.cs

@@ -108,7 +108,7 @@ public partial class Toplevel : View
                    );
                    );
 
 
         // Default keybindings for this view
         // Default keybindings for this view
-        KeyBindings.Add (Application.QuitKey, Command.QuitToplevel);
+        KeyBindings.Add (Application.QuitKey, KeyBindingScope.Application, Command.QuitToplevel);
 
 
         KeyBindings.Add (Key.CursorRight, Command.NextView);
         KeyBindings.Add (Key.CursorRight, Command.NextView);
         KeyBindings.Add (Key.CursorDown, Command.NextView);
         KeyBindings.Add (Key.CursorDown, Command.NextView);
@@ -120,7 +120,8 @@ public partial class Toplevel : View
         KeyBindings.Add (Key.Tab.WithCtrl, Command.NextViewOrTop);
         KeyBindings.Add (Key.Tab.WithCtrl, Command.NextViewOrTop);
         KeyBindings.Add (Key.Tab.WithShift.WithCtrl, Command.PreviousViewOrTop);
         KeyBindings.Add (Key.Tab.WithShift.WithCtrl, Command.PreviousViewOrTop);
 
 
-        KeyBindings.Add (Key.F5, Command.Refresh);
+        // TODO: Refresh Key should be configurable
+        KeyBindings.Add (Key.F5, KeyBindingScope.Application, Command.Refresh);
         KeyBindings.Add (Application.AlternateForwardKey, Command.NextViewOrTop); // Needed on Unix
         KeyBindings.Add (Application.AlternateForwardKey, Command.NextViewOrTop); // Needed on Unix
         KeyBindings.Add (Application.AlternateBackwardKey, Command.PreviousViewOrTop); // Needed on Unix
         KeyBindings.Add (Application.AlternateBackwardKey, Command.PreviousViewOrTop); // Needed on Unix
 
 

+ 75 - 18
UICatalog/Scenarios/KeyBindings.cs

@@ -11,6 +11,9 @@ namespace UICatalog.Scenarios;
 [ScenarioCategory ("Mouse and Keyboard")]
 [ScenarioCategory ("Mouse and Keyboard")]
 public sealed class KeyBindings : Scenario
 public sealed class KeyBindings : Scenario
 {
 {
+    private readonly ObservableCollection<string> _focusedBindings = [];
+    private ListView _focusedBindingsListView;
+
     public override void Main ()
     public override void Main ()
     {
     {
         // Init
         // Init
@@ -20,9 +23,9 @@ public sealed class KeyBindings : Scenario
         Window appWindow = new ()
         Window appWindow = new ()
         {
         {
             Title = $"{Application.QuitKey} to Quit - Scenario: {GetName ()}",
             Title = $"{Application.QuitKey} to Quit - Scenario: {GetName ()}",
+            SuperViewRendersLineCanvas = true,
         };
         };
 
 
-
         Label label = new ()
         Label label = new ()
         {
         {
             Title = "_Label:",
             Title = "_Label:",
@@ -60,38 +63,73 @@ Pressing Ctrl-Q will cause it to quit the app.",
         };
         };
         appWindow.Add (keyBindingsDemo);
         appWindow.Add (keyBindingsDemo);
 
 
-        ObservableCollection<string> bindingList = new ();
-        ListView keyBindingsListView = new ()
+        ObservableCollection<string> appBindings = new ();
+        ListView appBindingsListView = new ()
         {
         {
-            X = 0,
+            Title = "_Application Bindings",
+            BorderStyle = LineStyle.Single,
+            X = -1,
             Y = Pos.Bottom (keyBindingsDemo) + 1,
             Y = Pos.Bottom (keyBindingsDemo) + 1,
-            Width = 60,
-            Height = Dim.Fill (1),
+            Width = Dim.Auto (),
+            Height = Dim.Fill () + 1,
             CanFocus = true,
             CanFocus = true,
-            Source = new ListWrapper<string> (bindingList),
+            Source = new ListWrapper<string> (appBindings),
+            SuperViewRendersLineCanvas = true
         };
         };
-        appWindow.Add (keyBindingsListView);
+        appWindow.Add (appBindingsListView);
 
 
-        foreach (var binding in appWindow.KeyBindings.Bindings)
+        foreach (var appBinding in Application.GetKeyBindings ())
         {
         {
-            bindingList.Add ($"{appWindow.GetType ().Name} - {binding.Key} - {binding.Value.Scope}: {binding.Value.Commands [0]}");
+            foreach (var view in appBinding.Value)
+            {
+                var commands = view.KeyBindings.GetCommands (appBinding.Key);
+                appBindings.Add ($"{appBinding.Key} -> {view.GetType ().Name} - {commands [0]}");
+            }
         }
         }
 
 
+        ObservableCollection<string> hotkeyBindings = new ();
+        ListView hotkeyBindingsListView = new ()
+        {
+            Title = "_Hotkey Bindings",
+            BorderStyle = LineStyle.Single,
+            X = Pos.Right (appBindingsListView) - 1,
+            Y = Pos.Bottom (keyBindingsDemo) + 1,
+            Width = Dim.Auto (),
+            Height = Dim.Fill () + 1,
+            CanFocus = true,
+            Source = new ListWrapper<string> (hotkeyBindings),
+            SuperViewRendersLineCanvas = true
+
+        };
+        appWindow.Add (hotkeyBindingsListView);
+
         foreach (var subview in appWindow.Subviews)
         foreach (var subview in appWindow.Subviews)
         {
         {
-            foreach (var binding in subview.KeyBindings.Bindings)
+            foreach (var binding in subview.KeyBindings.Bindings.Where (b => b.Value.Scope == KeyBindingScope.HotKey))
             {
             {
-                bindingList.Add ($"{subview.GetType ().Name} - {binding.Key} - {binding.Value.Scope}: {binding.Value.Commands [0]}");
+                hotkeyBindings.Add ($"{binding.Key} -> {subview.GetType ().Name} - {binding.Value.Commands [0]}");
             }
             }
         }
         }
 
 
-        keyBindingsListView.SelectedItem = 0;
-        //keyBindingsListView.MoveEnd ();
+        _focusedBindingsListView = new ()
+        {
+            Title = "_Focused Bindings",
+            BorderStyle = LineStyle.Single,
+            X = Pos.Right (hotkeyBindingsListView) - 1,
+            Y = Pos.Bottom (keyBindingsDemo) + 1,
+            Width = Dim.Auto (),
+            Height = Dim.Fill () + 1,
+            CanFocus = true,
+            Source = new ListWrapper<string> (_focusedBindings),
+            SuperViewRendersLineCanvas = true
+
+        };
+        appWindow.Add (_focusedBindingsListView);
+
+        appWindow.Leave += AppWindow_Leave;
+        appWindow.Enter += AppWindow_Leave;
+        appWindow.DrawContent += AppWindow_DrawContent;
 
 
-        //appWindow.Initialized += (s, e) =>
-        //{
-        //    keyBindingsListView.EnsureSelectedItemVisible ();
-        //};
         // Run - Start the application.
         // Run - Start the application.
         Application.Run (appWindow);
         Application.Run (appWindow);
         appWindow.Dispose ();
         appWindow.Dispose ();
@@ -99,6 +137,25 @@ Pressing Ctrl-Q will cause it to quit the app.",
         // Shutdown - Calling Application.Shutdown is required.
         // Shutdown - Calling Application.Shutdown is required.
         Application.Shutdown ();
         Application.Shutdown ();
     }
     }
+
+    private void AppWindow_DrawContent (object sender, DrawEventArgs e)
+    {
+        _focusedBindingsListView.Title = $"_Focused ({Application.Top.MostFocused.GetType ().Name}) Bindings";
+
+        _focusedBindings.Clear ();
+        foreach (var binding in Application.Top.MostFocused.KeyBindings.Bindings.Where (b => b.Value.Scope == KeyBindingScope.Focused))
+        {
+            _focusedBindings.Add ($"{binding.Key} -> {binding.Value.Commands [0]}");
+        }
+    }
+
+    private void AppWindow_Leave (object sender, FocusEventArgs e)
+    {
+        //foreach (var binding in Application.Top.MostFocused.KeyBindings.Bindings.Where (b => b.Value.Scope == KeyBindingScope.Focused))
+        //{
+        //    _focusedBindings.Add ($"{binding.Key} -> {binding.Value.Commands [0]}");
+        //}
+    }
 }
 }
 
 
 public class KeyBindingsDemo : View
 public class KeyBindingsDemo : View