KeyBindingsDialog.cs 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  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. // TODO: BUG: Based on my new understanding of Added event I think this is wrong
  48. // (and always was wrong). Parents don't get to be told when new views are added
  49. // to them
  50. view.Added += (s,e)=>RecordView(e.Child);
  51. }
  52. internal static void Initialize ()
  53. {
  54. Instance = new ViewTracker (Application.Top);
  55. }
  56. internal void StartUsingNewKeyMap (Dictionary<Command, Key> currentBindings)
  57. {
  58. lock (lockKnownViews) {
  59. // change our knowledge of what keys to bind
  60. this.keybindings = currentBindings;
  61. // Mark that we have not applied the key bindings yet to any views
  62. foreach (var view in knownViews.Keys) {
  63. knownViews [view] = false;
  64. }
  65. }
  66. }
  67. private void ApplyKeyBindingsToAllKnownViews ()
  68. {
  69. if(keybindings == null) {
  70. return;
  71. }
  72. // Key is the view Value is whether we have already done it
  73. foreach (var viewDone in knownViews) {
  74. var view = viewDone.Key;
  75. var done = viewDone.Value;
  76. if (done) {
  77. // we have already applied keybindings to this view
  78. continue;
  79. }
  80. var supported = new HashSet<Command>(view.GetSupportedCommands ());
  81. foreach (var kvp in keybindings) {
  82. // if the view supports the keybinding
  83. if(supported.Contains(kvp.Key))
  84. {
  85. // if the key was bound to any other commands clear that
  86. view.ClearKeybinding (kvp.Key);
  87. view.AddKeyBinding (kvp.Value,kvp.Key);
  88. }
  89. // mark that we have done this view so don't need to set keybindings again on it
  90. knownViews [view] = true;
  91. }
  92. }
  93. }
  94. }
  95. public KeyBindingsDialog () : base("Keybindings", 50,10)
  96. {
  97. if(ViewTracker.Instance == null) {
  98. ViewTracker.Initialize ();
  99. }
  100. // known commands that views can support
  101. commands = Enum.GetValues (typeof (Command)).Cast<Command>().ToArray();
  102. commandsListView = new ListView (commands) {
  103. Width = Dim.Percent (50),
  104. Height = Dim.Percent (100) - 1,
  105. };
  106. Add (commandsListView);
  107. keyLabel = new Label () {
  108. Text = "Key: None",
  109. Width = Dim.Fill(),
  110. X = Pos.Percent(50),
  111. Y = 0
  112. };
  113. Add (keyLabel);
  114. var btnChange = new Button ("Ch_ange") {
  115. X = Pos.Percent (50),
  116. Y = 1,
  117. };
  118. Add (btnChange);
  119. btnChange.Clicked += RemapKey;
  120. var close = new Button ("Ok");
  121. close.Clicked += (s,e) => {
  122. Application.RequestStop ();
  123. ViewTracker.Instance.StartUsingNewKeyMap (CurrentBindings);
  124. };
  125. AddButton (close);
  126. var cancel = new Button ("Cancel");
  127. cancel.Clicked += (s,e)=>Application.RequestStop();
  128. AddButton (cancel);
  129. // Register event handler as the last thing in constructor to prevent early calls
  130. // before it is even shown (e.g. OnEnter)
  131. commandsListView.SelectedItemChanged += CommandsListView_SelectedItemChanged;
  132. // Setup to show first ListView entry
  133. SetTextBoxToShowBinding (commands.First());
  134. }
  135. private void RemapKey (object sender, EventArgs e)
  136. {
  137. var cmd = commands [commandsListView.SelectedItem];
  138. Key? key = null;
  139. // prompt user to hit a key
  140. var dlg = new Dialog ("Enter Key");
  141. dlg.KeyPress += (s, k) => {
  142. key = k.KeyEvent.Key;
  143. Application.RequestStop ();
  144. };
  145. Application.Run (dlg);
  146. if(key.HasValue) {
  147. CurrentBindings [cmd] = key.Value;
  148. SetTextBoxToShowBinding (cmd);
  149. }
  150. }
  151. private void SetTextBoxToShowBinding (Command cmd)
  152. {
  153. if (CurrentBindings.ContainsKey (cmd)) {
  154. keyLabel.Text = "Key: " + CurrentBindings [cmd].ToString ();
  155. } else {
  156. keyLabel.Text = "Key: None";
  157. }
  158. SetNeedsDisplay ();
  159. }
  160. private void CommandsListView_SelectedItemChanged (object sender, ListViewItemEventArgs obj)
  161. {
  162. SetTextBoxToShowBinding ((Command)obj.Value);
  163. }
  164. }
  165. }