From dba3ce34e328e01d52f6e3f265bfd414804cd183 Mon Sep 17 00:00:00 2001 From: Tig Date: Sat, 8 Jun 2024 15:41:07 -0600 Subject: [PATCH] Adding KeyBindings scenario --- UICatalog/Scenarios/KeyBindings.cs | 76 ++++++++++++++++++++++++++++++ 1 file changed, 76 insertions(+) create mode 100644 UICatalog/Scenarios/KeyBindings.cs diff --git a/UICatalog/Scenarios/KeyBindings.cs b/UICatalog/Scenarios/KeyBindings.cs new file mode 100644 index 000000000..3f5e1da24 --- /dev/null +++ b/UICatalog/Scenarios/KeyBindings.cs @@ -0,0 +1,76 @@ +using System; +using System.Collections.Generic; +using Terminal.Gui; + +namespace UICatalog.Scenarios; + +[ScenarioMetadata ("KeyBindings", "Illustrates the KeyBindings API.")] +[ScenarioCategory ("Mouse and Keyboard")] +public sealed class KeyBindings : Scenario +{ + public override void Main () + { + // Init + Application.Init (); + + // Setup - Create a top-level application window and configure it. + Window appWindow = new () + { + Title = $"{Application.QuitKey} to Quit - Scenario: {GetName ()}", + }; + + + Label label = new () + { + Title = "_Label:", + }; + TextField textField = new () + { + X = Pos.Right (label), + Y = Pos.Top (label), + Width = 20, + }; + textField.KeyBindings.Add (Key.F4, KeyBindingScope.Application, Command.SelectAll); + + appWindow.Add (label, textField); + + List bindingList = new (); + ListView keyBindingsListView = new () + { + X = 0, + Y = Pos.Bottom (textField) + 1, + Width = 60, + Height = Dim.Fill (1), + CanFocus = true, + Source = new ListWrapper (bindingList), + }; + appWindow.Add (keyBindingsListView); + + foreach (var binding in appWindow.KeyBindings.Bindings) + { + 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) + { + bindingList.Add ($"{subview.GetType ().Name} - {binding.Key} - {binding.Value.Scope}: {binding.Value.Commands [0]}"); + } + } + + keyBindingsListView.SelectedItem = 0; + //keyBindingsListView.MoveEnd (); + + //appWindow.Initialized += (s, e) => + //{ + // keyBindingsListView.EnsureSelectedItemVisible (); + //}; + // Run - Start the application. + Application.Run (appWindow); + appWindow.Dispose (); + + // Shutdown - Calling Application.Shutdown is required. + Application.Shutdown (); + } +}