keybindingLabels.js 4.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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: '⌃',
  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: 'altKey.long', comment: ['This is the long form for the Alt key on the keyboard'] }, "Alt"),
  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. function _simpleAsString(modifiers, key, labels) {
  75. if (key === null) {
  76. return '';
  77. }
  78. const result = [];
  79. // translate modifier keys: Ctrl-Shift-Alt-Meta
  80. if (modifiers.ctrlKey) {
  81. result.push(labels.ctrlKey);
  82. }
  83. if (modifiers.shiftKey) {
  84. result.push(labels.shiftKey);
  85. }
  86. if (modifiers.altKey) {
  87. result.push(labels.altKey);
  88. }
  89. if (modifiers.metaKey) {
  90. result.push(labels.metaKey);
  91. }
  92. // the actual key
  93. if (key !== '') {
  94. result.push(key);
  95. }
  96. return result.join(labels.separator);
  97. }