KeyBindings.cs 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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 = GetQuitKeyAndName (),
  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 = $"""
  50. These keys will cause this view to show a message box:
  51. - Hotkey: k, K, Alt-K, Alt-Shift-K
  52. - Focused: F3
  53. - Application: F4
  54. Pressing Esc or {Application.QuitKey} will cause it to quit the app.
  55. """,
  56. BorderStyle = LineStyle.Dashed
  57. };
  58. appWindow.Add (keyBindingsDemo);
  59. ObservableCollection<string> appBindings = new ();
  60. ListView appBindingsListView = new ()
  61. {
  62. Title = "_Application Bindings",
  63. BorderStyle = LineStyle.Single,
  64. X = -1,
  65. Y = Pos.Bottom (keyBindingsDemo) + 1,
  66. Width = Dim.Auto (),
  67. Height = Dim.Fill () + 1,
  68. CanFocus = true,
  69. Source = new ListWrapper<string> (appBindings),
  70. SuperViewRendersLineCanvas = true
  71. };
  72. appWindow.Add (appBindingsListView);
  73. foreach (var appBinding in Application.KeyBindings.Bindings)
  74. {
  75. var commands = Application.KeyBindings.GetCommands (appBinding.Key);
  76. appBindings.Add ($"{appBinding.Key} -> {appBinding.Value.BoundView?.GetType ().Name} - {commands [0]}");
  77. }
  78. ObservableCollection<string> hotkeyBindings = new ();
  79. ListView hotkeyBindingsListView = new ()
  80. {
  81. Title = "_Hotkey Bindings",
  82. BorderStyle = LineStyle.Single,
  83. X = Pos.Right (appBindingsListView) - 1,
  84. Y = Pos.Bottom (keyBindingsDemo) + 1,
  85. Width = Dim.Auto (),
  86. Height = Dim.Fill () + 1,
  87. CanFocus = true,
  88. Source = new ListWrapper<string> (hotkeyBindings),
  89. SuperViewRendersLineCanvas = true
  90. };
  91. appWindow.Add (hotkeyBindingsListView);
  92. foreach (var subview in appWindow.Subviews)
  93. {
  94. foreach (var binding in subview.KeyBindings.Bindings.Where (b => b.Value.Scope == KeyBindingScope.HotKey))
  95. {
  96. hotkeyBindings.Add ($"{binding.Key} -> {subview.GetType ().Name} - {binding.Value.Commands [0]}");
  97. }
  98. }
  99. _focusedBindingsListView = new ()
  100. {
  101. Title = "_Focused Bindings",
  102. BorderStyle = LineStyle.Single,
  103. X = Pos.Right (hotkeyBindingsListView) - 1,
  104. Y = Pos.Bottom (keyBindingsDemo) + 1,
  105. Width = Dim.Auto (),
  106. Height = Dim.Fill () + 1,
  107. CanFocus = true,
  108. Source = new ListWrapper<string> (_focusedBindings),
  109. SuperViewRendersLineCanvas = true
  110. };
  111. appWindow.Add (_focusedBindingsListView);
  112. appWindow.Leave += AppWindow_Leave;
  113. appWindow.Enter += AppWindow_Leave;
  114. appWindow.DrawContent += AppWindow_DrawContent;
  115. // Run - Start the application.
  116. Application.Run (appWindow);
  117. appWindow.Dispose ();
  118. // Shutdown - Calling Application.Shutdown is required.
  119. Application.Shutdown ();
  120. }
  121. private void AppWindow_DrawContent (object sender, DrawEventArgs e)
  122. {
  123. _focusedBindingsListView.Title = $"_Focused ({Application.Top.MostFocused.GetType ().Name}) Bindings";
  124. _focusedBindings.Clear ();
  125. foreach (var binding in Application.Top.MostFocused.KeyBindings.Bindings.Where (b => b.Value.Scope == KeyBindingScope.Focused))
  126. {
  127. _focusedBindings.Add ($"{binding.Key} -> {binding.Value.Commands [0]}");
  128. }
  129. }
  130. private void AppWindow_Leave (object sender, FocusEventArgs e)
  131. {
  132. foreach (var binding in Application.Top.MostFocused.KeyBindings.Bindings.Where (b => b.Value.Scope == KeyBindingScope.Focused))
  133. {
  134. _focusedBindings.Add ($"{binding.Key} -> {binding.Value.Commands [0]}");
  135. }
  136. }
  137. }
  138. public class KeyBindingsDemo : View
  139. {
  140. public KeyBindingsDemo ()
  141. {
  142. CanFocus = true;
  143. AddCommand (Command.Save, ctx =>
  144. {
  145. MessageBox.Query ($"{ctx.KeyBinding?.Scope}", $"Key: {ctx.Key}\nCommand: {ctx.Command}", buttons: "Ok");
  146. return true;
  147. });
  148. AddCommand (Command.New, ctx =>
  149. {
  150. MessageBox.Query ($"{ctx.KeyBinding?.Scope}", $"Key: {ctx.Key}\nCommand: {ctx.Command}", buttons: "Ok");
  151. return true;
  152. });
  153. AddCommand (Command.HotKey, ctx =>
  154. {
  155. MessageBox.Query ($"{ctx.KeyBinding?.Scope}", $"Key: {ctx.Key}\nCommand: {ctx.Command}", buttons: "Ok");
  156. SetFocus ();
  157. return true;
  158. });
  159. KeyBindings.Add (Key.F2, KeyBindingScope.Focused, Command.Save);
  160. KeyBindings.Add (Key.F3, Command.New); // same as specifying KeyBindingScope.Focused
  161. Application.KeyBindings.Add (Key.F4, this, Command.New);
  162. AddCommand (Command.QuitToplevel, ctx =>
  163. {
  164. MessageBox.Query ($"{ctx.KeyBinding?.Scope}", $"Key: {ctx.Key}\nCommand: {ctx.Command}", buttons: "Ok");
  165. Application.RequestStop ();
  166. return true;
  167. });
  168. Application.KeyBindings.Add (Key.Q.WithAlt, this, Command.QuitToplevel);
  169. }
  170. }