keybindingsRegistry.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. /*---------------------------------------------------------------------------------------------
  2. * Copyright (c) Microsoft Corporation. All rights reserved.
  3. * Licensed under the MIT License. See License.txt in the project root for license information.
  4. *--------------------------------------------------------------------------------------------*/
  5. import { createKeybinding } from '../../../base/common/keybindings.js';
  6. import { OS } from '../../../base/common/platform.js';
  7. import { CommandsRegistry } from '../../commands/common/commands.js';
  8. import { Registry } from '../../registry/common/platform.js';
  9. class KeybindingsRegistryImpl {
  10. constructor() {
  11. this._coreKeybindings = [];
  12. this._extensionKeybindings = [];
  13. this._cachedMergedKeybindings = null;
  14. }
  15. /**
  16. * Take current platform into account and reduce to primary & secondary.
  17. */
  18. static bindToCurrentPlatform(kb) {
  19. if (OS === 1 /* Windows */) {
  20. if (kb && kb.win) {
  21. return kb.win;
  22. }
  23. }
  24. else if (OS === 2 /* Macintosh */) {
  25. if (kb && kb.mac) {
  26. return kb.mac;
  27. }
  28. }
  29. else {
  30. if (kb && kb.linux) {
  31. return kb.linux;
  32. }
  33. }
  34. return kb;
  35. }
  36. registerKeybindingRule(rule) {
  37. const actualKb = KeybindingsRegistryImpl.bindToCurrentPlatform(rule);
  38. if (actualKb && actualKb.primary) {
  39. const kk = createKeybinding(actualKb.primary, OS);
  40. if (kk) {
  41. this._registerDefaultKeybinding(kk, rule.id, rule.args, rule.weight, 0, rule.when);
  42. }
  43. }
  44. if (actualKb && Array.isArray(actualKb.secondary)) {
  45. for (let i = 0, len = actualKb.secondary.length; i < len; i++) {
  46. const k = actualKb.secondary[i];
  47. const kk = createKeybinding(k, OS);
  48. if (kk) {
  49. this._registerDefaultKeybinding(kk, rule.id, rule.args, rule.weight, -i - 1, rule.when);
  50. }
  51. }
  52. }
  53. }
  54. registerCommandAndKeybindingRule(desc) {
  55. this.registerKeybindingRule(desc);
  56. CommandsRegistry.registerCommand(desc);
  57. }
  58. static _mightProduceChar(keyCode) {
  59. if (keyCode >= 21 /* Digit0 */ && keyCode <= 30 /* Digit9 */) {
  60. return true;
  61. }
  62. if (keyCode >= 31 /* KeyA */ && keyCode <= 56 /* KeyZ */) {
  63. return true;
  64. }
  65. return (keyCode === 80 /* Semicolon */
  66. || keyCode === 81 /* Equal */
  67. || keyCode === 82 /* Comma */
  68. || keyCode === 83 /* Minus */
  69. || keyCode === 84 /* Period */
  70. || keyCode === 85 /* Slash */
  71. || keyCode === 86 /* Backquote */
  72. || keyCode === 110 /* ABNT_C1 */
  73. || keyCode === 111 /* ABNT_C2 */
  74. || keyCode === 87 /* BracketLeft */
  75. || keyCode === 88 /* Backslash */
  76. || keyCode === 89 /* BracketRight */
  77. || keyCode === 90 /* Quote */
  78. || keyCode === 91 /* OEM_8 */
  79. || keyCode === 92 /* IntlBackslash */);
  80. }
  81. _assertNoCtrlAlt(keybinding, commandId) {
  82. if (keybinding.ctrlKey && keybinding.altKey && !keybinding.metaKey) {
  83. if (KeybindingsRegistryImpl._mightProduceChar(keybinding.keyCode)) {
  84. console.warn('Ctrl+Alt+ keybindings should not be used by default under Windows. Offender: ', keybinding, ' for ', commandId);
  85. }
  86. }
  87. }
  88. _registerDefaultKeybinding(keybinding, commandId, commandArgs, weight1, weight2, when) {
  89. if (OS === 1 /* Windows */) {
  90. this._assertNoCtrlAlt(keybinding.parts[0], commandId);
  91. }
  92. this._coreKeybindings.push({
  93. keybinding: keybinding.parts,
  94. command: commandId,
  95. commandArgs: commandArgs,
  96. when: when,
  97. weight1: weight1,
  98. weight2: weight2,
  99. extensionId: null,
  100. isBuiltinExtension: false
  101. });
  102. this._cachedMergedKeybindings = null;
  103. }
  104. getDefaultKeybindings() {
  105. if (!this._cachedMergedKeybindings) {
  106. this._cachedMergedKeybindings = [].concat(this._coreKeybindings).concat(this._extensionKeybindings);
  107. this._cachedMergedKeybindings.sort(sorter);
  108. }
  109. return this._cachedMergedKeybindings.slice(0);
  110. }
  111. }
  112. export const KeybindingsRegistry = new KeybindingsRegistryImpl();
  113. // Define extension point ids
  114. export const Extensions = {
  115. EditorModes: 'platform.keybindingsRegistry'
  116. };
  117. Registry.add(Extensions.EditorModes, KeybindingsRegistry);
  118. function sorter(a, b) {
  119. if (a.weight1 !== b.weight1) {
  120. return a.weight1 - b.weight1;
  121. }
  122. if (a.command < b.command) {
  123. return -1;
  124. }
  125. if (a.command > b.command) {
  126. return 1;
  127. }
  128. return a.weight2 - b.weight2;
  129. }