From 891eb296609923a8ee707f7550e5e21f2644f54a Mon Sep 17 00:00:00 2001 From: Tig Date: Sun, 9 Jun 2024 10:39:55 -0600 Subject: [PATCH] Improved KeyBindings scenario. Made QuitKey and Refresh app scoped --- .../Application/ApplicationKeyboard.cs | 5 + Terminal.Gui/Views/Toplevel.cs | 5 +- UICatalog/Scenarios/KeyBindings.cs | 101 ++++++++++++++---- 3 files changed, 87 insertions(+), 24 deletions(-) diff --git a/Terminal.Gui/Application/ApplicationKeyboard.cs b/Terminal.Gui/Application/ApplicationKeyboard.cs index 984d711c8..0a56ce712 100644 --- a/Terminal.Gui/Application/ApplicationKeyboard.cs +++ b/Terminal.Gui/Application/ApplicationKeyboard.cs @@ -209,6 +209,11 @@ partial class Application /// private static readonly Dictionary> _keyBindings = new (); + /// + /// Gets the list of key bindings. + /// + public static Dictionary> GetKeyBindings () { return _keyBindings; } + /// /// Adds an scoped key binding. /// diff --git a/Terminal.Gui/Views/Toplevel.cs b/Terminal.Gui/Views/Toplevel.cs index 3cbfb6d58..fece9dcc4 100644 --- a/Terminal.Gui/Views/Toplevel.cs +++ b/Terminal.Gui/Views/Toplevel.cs @@ -108,7 +108,7 @@ public partial class Toplevel : 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.CursorDown, Command.NextView); @@ -120,7 +120,8 @@ public partial class Toplevel : View KeyBindings.Add (Key.Tab.WithCtrl, Command.NextViewOrTop); 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.AlternateBackwardKey, Command.PreviousViewOrTop); // Needed on Unix diff --git a/UICatalog/Scenarios/KeyBindings.cs b/UICatalog/Scenarios/KeyBindings.cs index 0cca44d3c..3daa8c3c9 100644 --- a/UICatalog/Scenarios/KeyBindings.cs +++ b/UICatalog/Scenarios/KeyBindings.cs @@ -11,6 +11,9 @@ namespace UICatalog.Scenarios; [ScenarioCategory ("Mouse and Keyboard")] public sealed class KeyBindings : Scenario { + private readonly ObservableCollection _focusedBindings = []; + private ListView _focusedBindingsListView; + public override void Main () { // Init @@ -20,9 +23,9 @@ public sealed class KeyBindings : Scenario Window appWindow = new () { Title = $"{Application.QuitKey} to Quit - Scenario: {GetName ()}", + SuperViewRendersLineCanvas = true, }; - Label label = new () { Title = "_Label:", @@ -60,38 +63,73 @@ Pressing Ctrl-Q will cause it to quit the app.", }; appWindow.Add (keyBindingsDemo); - ObservableCollection bindingList = new (); - ListView keyBindingsListView = new () + ObservableCollection appBindings = new (); + ListView appBindingsListView = new () { - X = 0, + Title = "_Application Bindings", + BorderStyle = LineStyle.Single, + X = -1, Y = Pos.Bottom (keyBindingsDemo) + 1, - Width = 60, - Height = Dim.Fill (1), + Width = Dim.Auto (), + Height = Dim.Fill () + 1, CanFocus = true, - Source = new ListWrapper (bindingList), + Source = new ListWrapper (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 subview in appWindow.Subviews) - { - foreach (var binding in subview.KeyBindings.Bindings) + foreach (var view in appBinding.Value) { - bindingList.Add ($"{subview.GetType ().Name} - {binding.Key} - {binding.Value.Scope}: {binding.Value.Commands [0]}"); + var commands = view.KeyBindings.GetCommands (appBinding.Key); + appBindings.Add ($"{appBinding.Key} -> {view.GetType ().Name} - {commands [0]}"); } } - keyBindingsListView.SelectedItem = 0; - //keyBindingsListView.MoveEnd (); + ObservableCollection 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 (hotkeyBindings), + SuperViewRendersLineCanvas = true + + }; + appWindow.Add (hotkeyBindingsListView); + + foreach (var subview in appWindow.Subviews) + { + foreach (var binding in subview.KeyBindings.Bindings.Where (b => b.Value.Scope == KeyBindingScope.HotKey)) + { + hotkeyBindings.Add ($"{binding.Key} -> {subview.GetType ().Name} - {binding.Value.Commands [0]}"); + } + } + + _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 (_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. Application.Run (appWindow); appWindow.Dispose (); @@ -99,6 +137,25 @@ Pressing Ctrl-Q will cause it to quit the app.", // Shutdown - Calling Application.Shutdown is required. 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