KeyBindingsDialog.cs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using Terminal.Gui;
  7. namespace UICatalog {
  8. class KeyBindingsDialog : Dialog {
  9. static Dictionary<Command,Key> CurrentBindings = new Dictionary<Command,Key>();
  10. private Command[] commands;
  11. private ListView commandsListView;
  12. private Label keyLabel;
  13. /// <summary>
  14. /// Tracks views as they are created in UICatalog so that their keybindings can
  15. /// be managed.
  16. /// </summary>
  17. private class ViewTracker {
  18. public static ViewTracker Instance;
  19. /// <summary>
  20. /// All views seen so far and a bool to indicate if we have applied keybindings to them
  21. /// </summary>
  22. Dictionary<View, bool> knownViews = new Dictionary<View, bool> ();
  23. private object lockKnownViews = new object ();
  24. private Dictionary<Command, Key> keybindings;
  25. public ViewTracker (View top)
  26. {
  27. RecordView (top);
  28. // Refresh known windows
  29. Application.MainLoop.AddTimeout (TimeSpan.FromMilliseconds (100), (m) => {
  30. lock (lockKnownViews) {
  31. RecordView (Application.Top);
  32. ApplyKeyBindingsToAllKnownViews ();
  33. }
  34. return true;
  35. });
  36. }
  37. private void RecordView (View view)
  38. {
  39. if (!knownViews.ContainsKey (view)) {
  40. knownViews.Add (view, false);
  41. }
  42. // may already have subviews that were added to it
  43. // before we got to it
  44. foreach (var sub in view.Subviews) {
  45. RecordView (sub);
  46. }
  47. view.Added += RecordView;
  48. }
  49. internal static void Initialize ()
  50. {
  51. Instance = new ViewTracker (Application.Top);
  52. }
  53. internal void StartUsingNewKeyMap (Dictionary<Command, Key> currentBindings)
  54. {
  55. lock (lockKnownViews) {
  56. // change our knowledge of what keys to bind
  57. this.keybindings = currentBindings;
  58. // Mark that we have not applied the key bindings yet to any views
  59. foreach (var view in knownViews.Keys) {
  60. knownViews [view] = false;
  61. }
  62. }
  63. }
  64. private void ApplyKeyBindingsToAllKnownViews ()
  65. {
  66. if(keybindings == null) {
  67. return;
  68. }
  69. // Key is the view Value is whether we have already done it
  70. foreach (var viewDone in knownViews) {
  71. var view = viewDone.Key;
  72. var done = viewDone.Value;
  73. if (done) {
  74. // we have already applied keybindings to this view
  75. continue;
  76. }
  77. var supported = new HashSet<Command>(view.GetSupportedCommands ());
  78. foreach (var kvp in keybindings) {
  79. // if the view supports the keybinding
  80. if(supported.Contains(kvp.Key))
  81. {
  82. // if the key was bound to any other commands clear that
  83. view.ClearKeybinding (kvp.Key);
  84. view.AddKeyBinding (kvp.Value,kvp.Key);
  85. }
  86. // mark that we have done this view so don't need to set keybindings again on it
  87. knownViews [view] = true;
  88. }
  89. }
  90. }
  91. }
  92. public KeyBindingsDialog () : base("Keybindings", 50,10)
  93. {
  94. if(ViewTracker.Instance == null) {
  95. ViewTracker.Initialize ();
  96. }
  97. // known commands that views can support
  98. commands = Enum.GetValues (typeof (Command)).Cast<Command>().ToArray();
  99. commandsListView = new ListView (commands) {
  100. Width = Dim.Percent (50),
  101. Height = Dim.Percent (100) - 1,
  102. };
  103. Add (commandsListView);
  104. keyLabel = new Label () {
  105. Text = "Key: None",
  106. Width = Dim.Fill(),
  107. X = Pos.Percent(50),
  108. Y = 0
  109. };
  110. Add (keyLabel);
  111. var btnChange = new Button ("Ch_ange") {
  112. X = Pos.Percent (50),
  113. Y = 1,
  114. };
  115. Add (btnChange);
  116. btnChange.Clicked += RemapKey;
  117. var close = new Button ("Ok");
  118. close.Clicked += () => {
  119. Application.RequestStop ();
  120. ViewTracker.Instance.StartUsingNewKeyMap (CurrentBindings);
  121. };
  122. AddButton (close);
  123. var cancel = new Button ("Cancel");
  124. cancel.Clicked += ()=>Application.RequestStop();
  125. AddButton (cancel);
  126. // Register event handler as the last thing in constructor to prevent early calls
  127. // before it is even shown (e.g. OnEnter)
  128. commandsListView.SelectedItemChanged += CommandsListView_SelectedItemChanged;
  129. // Setup to show first ListView entry
  130. SetTextBoxToShowBinding (commands.First());
  131. }
  132. private void RemapKey ()
  133. {
  134. var cmd = commands [commandsListView.SelectedItem];
  135. Key? key = null;
  136. // prompt user to hit a key
  137. var dlg = new Dialog ("Enter Key");
  138. dlg.KeyPress += (k) => {
  139. key = k.KeyEvent.Key;
  140. Application.RequestStop ();
  141. };
  142. Application.Run (dlg);
  143. if(key.HasValue) {
  144. CurrentBindings [cmd] = key.Value;
  145. SetTextBoxToShowBinding (cmd);
  146. }
  147. }
  148. private void SetTextBoxToShowBinding (Command cmd)
  149. {
  150. if (CurrentBindings.ContainsKey (cmd)) {
  151. keyLabel.Text = "Key: " + CurrentBindings [cmd].ToString ();
  152. } else {
  153. keyLabel.Text = "Key: None";
  154. }
  155. SetNeedsDisplay ();
  156. }
  157. private void CommandsListView_SelectedItemChanged (ListViewItemEventArgs obj)
  158. {
  159. SetTextBoxToShowBinding ((Command)obj.Value);
  160. }
  161. }
  162. }