KeyBindings.cs 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Collections.ObjectModel;
  4. using System.Linq;
  5. using System.Text;
  6. using Terminal.Gui;
  7. namespace UICatalog.Scenarios;
  8. [ScenarioMetadata ("KeyBindings", "Illustrates the KeyBindings API.")]
  9. [ScenarioCategory ("Mouse and Keyboard")]
  10. public sealed class KeyBindings : Scenario
  11. {
  12. private readonly ObservableCollection<string> _focusedBindings = [];
  13. private ListView _focusedBindingsListView;
  14. public override void Main ()
  15. {
  16. // Init
  17. Application.Init ();
  18. // Setup - Create a top-level application window and configure it.
  19. Window appWindow = new ()
  20. {
  21. Title = $"{Application.QuitKey} to Quit - Scenario: {GetName ()}",
  22. SuperViewRendersLineCanvas = true,
  23. };
  24. Label label = new ()
  25. {
  26. Title = "_Label:",
  27. };
  28. TextField textField = new ()
  29. {
  30. X = Pos.Right (label),
  31. Y = Pos.Top (label),
  32. Width = 20,
  33. };
  34. appWindow.Add (label, textField);
  35. Button button = new ()
  36. {
  37. X = Pos.Right (textField) + 1,
  38. Y = Pos.Top (label),
  39. Text = "_Button",
  40. };
  41. appWindow.Add (button);
  42. KeyBindingsDemo keyBindingsDemo = new ()
  43. {
  44. X = Pos.Right (button) + 1,
  45. Width = Dim.Auto (DimAutoStyle.Text),
  46. Height = Dim.Auto (DimAutoStyle.Text),
  47. HotKeySpecifier = (Rune)'_',
  48. Title = "_KeyBindingsDemo",
  49. Text = @"These keys will cause this view to show a message box:
  50. - Hotkey: k, K, Alt-K, Alt-Shift-K
  51. - Focused: F3
  52. - Application: F4
  53. Pressing Ctrl-Q will cause it to quit the app.",
  54. BorderStyle = LineStyle.Dashed
  55. };
  56. appWindow.Add (keyBindingsDemo);
  57. ObservableCollection<string> appBindings = new ();
  58. ListView appBindingsListView = new ()
  59. {
  60. Title = "_Application Bindings",
  61. BorderStyle = LineStyle.Single,
  62. X = -1,
  63. Y = Pos.Bottom (keyBindingsDemo) + 1,
  64. Width = Dim.Auto (),
  65. Height = Dim.Fill () + 1,
  66. CanFocus = true,
  67. Source = new ListWrapper<string> (appBindings),
  68. SuperViewRendersLineCanvas = true
  69. };
  70. appWindow.Add (appBindingsListView);
  71. foreach (var appBinding in Application.GetKeyBindings ())
  72. {
  73. foreach (var view in appBinding.Value)
  74. {
  75. var commands = view.KeyBindings.GetCommands (appBinding.Key);
  76. appBindings.Add ($"{appBinding.Key} -> {view.GetType ().Name} - {commands [0]}");
  77. }
  78. }
  79. ObservableCollection<string> hotkeyBindings = new ();
  80. ListView hotkeyBindingsListView = new ()
  81. {
  82. Title = "_Hotkey Bindings",
  83. BorderStyle = LineStyle.Single,
  84. X = Pos.Right (appBindingsListView) - 1,
  85. Y = Pos.Bottom (keyBindingsDemo) + 1,
  86. Width = Dim.Auto (),
  87. Height = Dim.Fill () + 1,
  88. CanFocus = true,
  89. Source = new ListWrapper<string> (hotkeyBindings),
  90. SuperViewRendersLineCanvas = true
  91. };
  92. appWindow.Add (hotkeyBindingsListView);
  93. foreach (var subview in appWindow.Subviews)
  94. {
  95. foreach (var binding in subview.KeyBindings.Bindings.Where (b => b.Value.Scope == KeyBindingScope.HotKey))
  96. {
  97. hotkeyBindings.Add ($"{binding.Key} -> {subview.GetType ().Name} - {binding.Value.Commands [0]}");
  98. }
  99. }
  100. _focusedBindingsListView = new ()
  101. {
  102. Title = "_Focused Bindings",
  103. BorderStyle = LineStyle.Single,
  104. X = Pos.Right (hotkeyBindingsListView) - 1,
  105. Y = Pos.Bottom (keyBindingsDemo) + 1,
  106. Width = Dim.Auto (),
  107. Height = Dim.Fill () + 1,
  108. CanFocus = true,
  109. Source = new ListWrapper<string> (_focusedBindings),
  110. SuperViewRendersLineCanvas = true
  111. };
  112. appWindow.Add (_focusedBindingsListView);
  113. appWindow.Leave += AppWindow_Leave;
  114. appWindow.Enter += AppWindow_Leave;
  115. appWindow.DrawContent += AppWindow_DrawContent;
  116. // Run - Start the application.
  117. Application.Run (appWindow);
  118. appWindow.Dispose ();
  119. // Shutdown - Calling Application.Shutdown is required.
  120. Application.Shutdown ();
  121. }
  122. private void AppWindow_DrawContent (object sender, DrawEventArgs e)
  123. {
  124. _focusedBindingsListView.Title = $"_Focused ({Application.Top.MostFocused.GetType ().Name}) Bindings";
  125. _focusedBindings.Clear ();
  126. foreach (var binding in Application.Top.MostFocused.KeyBindings.Bindings.Where (b => b.Value.Scope == KeyBindingScope.Focused))
  127. {
  128. _focusedBindings.Add ($"{binding.Key} -> {binding.Value.Commands [0]}");
  129. }
  130. }
  131. private void AppWindow_Leave (object sender, FocusEventArgs e)
  132. {
  133. //foreach (var binding in Application.Top.MostFocused.KeyBindings.Bindings.Where (b => b.Value.Scope == KeyBindingScope.Focused))
  134. //{
  135. // _focusedBindings.Add ($"{binding.Key} -> {binding.Value.Commands [0]}");
  136. //}
  137. }
  138. }
  139. public class KeyBindingsDemo : View
  140. {
  141. public KeyBindingsDemo ()
  142. {
  143. CanFocus = true;
  144. AddCommand (Command.New, ctx =>
  145. {
  146. MessageBox.Query ("Hi", $"Key: {ctx.Key}\nCommand: {ctx.Command}", buttons: "Ok");
  147. return true;
  148. });
  149. AddCommand (Command.HotKey, ctx =>
  150. {
  151. MessageBox.Query ("Hi", $"Key: {ctx.Key}\nCommand: {ctx.Command}", buttons: "Ok");
  152. SetFocus ();
  153. return true;
  154. });
  155. KeyBindings.Add (Key.F3, KeyBindingScope.Focused, Command.New);
  156. KeyBindings.Add (Key.F4, KeyBindingScope.Application, Command.New);
  157. AddCommand (Command.QuitToplevel, ctx =>
  158. {
  159. Application.RequestStop ();
  160. return true;
  161. });
  162. KeyBindings.Add (Key.Q.WithCtrl, KeyBindingScope.Application, Command.QuitToplevel);
  163. }
  164. }