keybindingLabels.js 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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 * as nls from '../../nls.js';
  6. export class ModifierLabelProvider {
  7. constructor(mac, windows, linux = windows) {
  8. this.modifierLabels = [null]; // index 0 will never me accessed.
  9. this.modifierLabels[2 /* Macintosh */] = mac;
  10. this.modifierLabels[1 /* Windows */] = windows;
  11. this.modifierLabels[3 /* Linux */] = linux;
  12. }
  13. toLabel(OS, parts, keyLabelProvider) {
  14. if (parts.length === 0) {
  15. return null;
  16. }
  17. const result = [];
  18. for (let i = 0, len = parts.length; i < len; i++) {
  19. const part = parts[i];
  20. const keyLabel = keyLabelProvider(part);
  21. if (keyLabel === null) {
  22. // this keybinding cannot be expressed...
  23. return null;
  24. }
  25. result[i] = _simpleAsString(part, keyLabel, this.modifierLabels[OS]);
  26. }
  27. return result.join(' ');
  28. }
  29. }
  30. /**
  31. * A label provider that prints modifiers in a suitable format for displaying in the UI.
  32. */
  33. export const UILabelProvider = new ModifierLabelProvider({
  34. ctrlKey: '\u2303',
  35. shiftKey: '⇧',
  36. altKey: '⌥',
  37. metaKey: '⌘',
  38. separator: '',
  39. }, {
  40. ctrlKey: nls.localize({ key: 'ctrlKey', comment: ['This is the short form for the Control key on the keyboard'] }, "Ctrl"),
  41. shiftKey: nls.localize({ key: 'shiftKey', comment: ['This is the short form for the Shift key on the keyboard'] }, "Shift"),
  42. altKey: nls.localize({ key: 'altKey', comment: ['This is the short form for the Alt key on the keyboard'] }, "Alt"),
  43. metaKey: nls.localize({ key: 'windowsKey', comment: ['This is the short form for the Windows key on the keyboard'] }, "Windows"),
  44. separator: '+',
  45. }, {
  46. ctrlKey: nls.localize({ key: 'ctrlKey', comment: ['This is the short form for the Control key on the keyboard'] }, "Ctrl"),
  47. shiftKey: nls.localize({ key: 'shiftKey', comment: ['This is the short form for the Shift key on the keyboard'] }, "Shift"),
  48. altKey: nls.localize({ key: 'altKey', comment: ['This is the short form for the Alt key on the keyboard'] }, "Alt"),
  49. metaKey: nls.localize({ key: 'superKey', comment: ['This is the short form for the Super key on the keyboard'] }, "Super"),
  50. separator: '+',
  51. });
  52. /**
  53. * A label provider that prints modifiers in a suitable format for ARIA.
  54. */
  55. export const AriaLabelProvider = new ModifierLabelProvider({
  56. ctrlKey: nls.localize({ key: 'ctrlKey.long', comment: ['This is the long form for the Control key on the keyboard'] }, "Control"),
  57. shiftKey: nls.localize({ key: 'shiftKey.long', comment: ['This is the long form for the Shift key on the keyboard'] }, "Shift"),
  58. altKey: nls.localize({ key: 'optKey.long', comment: ['This is the long form for the Alt/Option key on the keyboard'] }, "Option"),
  59. metaKey: nls.localize({ key: 'cmdKey.long', comment: ['This is the long form for the Command key on the keyboard'] }, "Command"),
  60. separator: '+',
  61. }, {
  62. ctrlKey: nls.localize({ key: 'ctrlKey.long', comment: ['This is the long form for the Control key on the keyboard'] }, "Control"),
  63. shiftKey: nls.localize({ key: 'shiftKey.long', comment: ['This is the long form for the Shift key on the keyboard'] }, "Shift"),
  64. altKey: nls.localize({ key: 'altKey.long', comment: ['This is the long form for the Alt key on the keyboard'] }, "Alt"),
  65. metaKey: nls.localize({ key: 'windowsKey.long', comment: ['This is the long form for the Windows key on the keyboard'] }, "Windows"),
  66. separator: '+',
  67. }, {
  68. ctrlKey: nls.localize({ key: 'ctrlKey.long', comment: ['This is the long form for the Control key on the keyboard'] }, "Control"),
  69. shiftKey: nls.localize({ key: 'shiftKey.long', comment: ['This is the long form for the Shift key on the keyboard'] }, "Shift"),
  70. altKey: nls.localize({ key: 'altKey.long', comment: ['This is the long form for the Alt key on the keyboard'] }, "Alt"),
  71. metaKey: nls.localize({ key: 'superKey.long', comment: ['This is the long form for the Super key on the keyboard'] }, "Super"),
  72. separator: '+',
  73. });
  74. /**
  75. * A label provider that prints modifiers in a suitable format for Electron Accelerators.
  76. * See https://github.com/electron/electron/blob/master/docs/api/accelerator.md
  77. */
  78. export const ElectronAcceleratorLabelProvider = new ModifierLabelProvider({
  79. ctrlKey: 'Ctrl',
  80. shiftKey: 'Shift',
  81. altKey: 'Alt',
  82. metaKey: 'Cmd',
  83. separator: '+',
  84. }, {
  85. ctrlKey: 'Ctrl',
  86. shiftKey: 'Shift',
  87. altKey: 'Alt',
  88. metaKey: 'Super',
  89. separator: '+',
  90. });
  91. function _simpleAsString(modifiers, key, labels) {
  92. if (key === null) {
  93. return '';
  94. }
  95. const result = [];
  96. // translate modifier keys: Ctrl-Shift-Alt-Meta
  97. if (modifiers.ctrlKey) {
  98. result.push(labels.ctrlKey);
  99. }
  100. if (modifiers.shiftKey) {
  101. result.push(labels.shiftKey);
  102. }
  103. if (modifiers.altKey) {
  104. result.push(labels.altKey);
  105. }
  106. if (modifiers.metaKey) {
  107. result.push(labels.metaKey);
  108. }
  109. // the actual key
  110. if (key !== '') {
  111. result.push(key);
  112. }
  113. return result.join(labels.separator);
  114. }