KeyBindingsDialog.cs 6.8 KB

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