123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195 |
- using System;
- using System.Collections.Generic;
- using System.Collections.ObjectModel;
- using System.Linq;
- using System.Text;
- using Terminal.Gui;
- namespace UICatalog.Scenarios;
- [ScenarioMetadata ("KeyBindings", "Illustrates the KeyBindings API.")]
- [ScenarioCategory ("Mouse and Keyboard")]
- public sealed class KeyBindings : Scenario
- {
- private readonly ObservableCollection<string> _focusedBindings = [];
- private ListView _focusedBindingsListView;
- public override void Main ()
- {
- // Init
- Application.Init ();
- // Setup - Create a top-level application window and configure it.
- Window appWindow = new ()
- {
- Title = GetQuitKeyAndName (),
- SuperViewRendersLineCanvas = true,
- };
- Label label = new ()
- {
- Title = "_Label:",
- };
- TextField textField = new ()
- {
- X = Pos.Right (label),
- Y = Pos.Top (label),
- Width = 20,
- };
- appWindow.Add (label, textField);
- Button button = new ()
- {
- X = Pos.Right (textField) + 1,
- Y = Pos.Top (label),
- Text = "_Button",
- };
- appWindow.Add (button);
- KeyBindingsDemo keyBindingsDemo = new ()
- {
- X = Pos.Right (button) + 1,
- Width = Dim.Auto (DimAutoStyle.Text),
- Height = Dim.Auto (DimAutoStyle.Text),
- HotKeySpecifier = (Rune)'_',
- Title = "_KeyBindingsDemo",
- Text = $"""
- These keys will cause this view to show a message box:
- - Hotkey: k, K, Alt-K, Alt-Shift-K
- - Focused: F3
- - Application: F4
- Pressing Esc or {Application.QuitKey} will cause it to quit the app.
- """,
- BorderStyle = LineStyle.Dashed
- };
- appWindow.Add (keyBindingsDemo);
- ObservableCollection<string> appBindings = new ();
- ListView appBindingsListView = new ()
- {
- Title = "_Application Bindings",
- BorderStyle = LineStyle.Single,
- X = -1,
- Y = Pos.Bottom (keyBindingsDemo) + 1,
- Width = Dim.Auto (),
- Height = Dim.Fill () + 1,
- CanFocus = true,
- Source = new ListWrapper<string> (appBindings),
- SuperViewRendersLineCanvas = true
- };
- appWindow.Add (appBindingsListView);
- foreach (var key in Application.KeyBindings.GetBoundKeys())
- {
- var binding = Application.KeyBindings.Get (key);
- appBindings.Add ($"{key} -> {binding.BoundView?.GetType ().Name} - {binding.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 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<string> (_focusedBindings),
- SuperViewRendersLineCanvas = true
- };
- appWindow.Add (_focusedBindingsListView);
- Application.Navigation!.FocusedChanged += Application_HasFocusChanged;
- // Run - Start the application.
- Application.Run (appWindow);
- Application.Navigation!.FocusedChanged -= Application_HasFocusChanged;
- appWindow.Dispose ();
- // Shutdown - Calling Application.Shutdown is required.
- Application.Shutdown ();
- }
- private void Application_HasFocusChanged (object sender, EventArgs e)
- {
- View focused = Application.Navigation!.GetFocused ();
- if (focused == null)
- {
- return;
- }
- _focusedBindingsListView.Title = $"_Focused ({focused?.GetType ().Name}) Bindings";
- _focusedBindings.Clear();
- foreach (var binding in focused?.KeyBindings!.Bindings.Where (b => b.Value.Scope == KeyBindingScope.Focused)!)
- {
- _focusedBindings.Add ($"{binding.Key} -> {binding.Value.Commands [0]}");
- }
- }
- }
- public class KeyBindingsDemo : View
- {
- public KeyBindingsDemo ()
- {
- CanFocus = true;
- AddCommand (Command.Save, ctx =>
- {
- MessageBox.Query ($"{ctx.KeyBinding?.Scope}", $"Key: {ctx.Key}\nCommand: {ctx.Command}", buttons: "Ok");
- return true;
- });
- AddCommand (Command.New, ctx =>
- {
- MessageBox.Query ($"{ctx.KeyBinding?.Scope}", $"Key: {ctx.Key}\nCommand: {ctx.Command}", buttons: "Ok");
- return true;
- });
- AddCommand (Command.HotKey, ctx =>
- {
- MessageBox.Query ($"{ctx.KeyBinding?.Scope}", $"Key: {ctx.Key}\nCommand: {ctx.Command}", buttons: "Ok");
- SetFocus ();
- return true;
- });
- KeyBindings.Add (Key.F2, KeyBindingScope.Focused, Command.Save);
- KeyBindings.Add (Key.F3, Command.New); // same as specifying KeyBindingScope.Focused
- Application.KeyBindings.Add (Key.F4, this, Command.New);
- AddCommand (Command.Quit, ctx =>
- {
- MessageBox.Query ($"{ctx.KeyBinding?.Scope}", $"Key: {ctx.Key}\nCommand: {ctx.Command}", buttons: "Ok");
- Application.RequestStop ();
- return true;
- });
- Application.KeyBindings.Add (Key.Q.WithAlt, this, Command.Quit);
- }
- }
|