Skip to content

Commit

Permalink
Improve key binding rendering (eclipse-theia#10801)
Browse files Browse the repository at this point in the history
  • Loading branch information
colin-grant-work authored Feb 25, 2022
1 parent f0cdf69 commit 6b45b27
Show file tree
Hide file tree
Showing 3 changed files with 169 additions and 160 deletions.
15 changes: 10 additions & 5 deletions packages/core/src/browser/keybinding.ts
Original file line number Diff line number Diff line change
Expand Up @@ -337,23 +337,28 @@ export class KeybindingRegistry {
* @param asciiOnly if `true`, no special characters will be substituted into the string returned. Ensures correct keyboard shortcuts in Electron menus.
*/
acceleratorForKeyCode(keyCode: KeyCode, separator: string = ' ', asciiOnly = false): string {
return this.componentsForKeyCode(keyCode, asciiOnly).join(separator);
}

componentsForKeyCode(keyCode: KeyCode, asciiOnly = false): string[] {
const keyCodeResult = [];
const useSymbols = isOSX && !asciiOnly;
if (keyCode.meta && isOSX) {
keyCodeResult.push('Cmd');
keyCodeResult.push(useSymbols ? '⌘' : 'Cmd');
}
if (keyCode.ctrl) {
keyCodeResult.push('Ctrl');
keyCodeResult.push(useSymbols ? '⌃' : 'Ctrl');
}
if (keyCode.alt) {
keyCodeResult.push('Alt');
keyCodeResult.push(useSymbols ? '⌥' : 'Alt');
}
if (keyCode.shift) {
keyCodeResult.push('Shift');
keyCodeResult.push(useSymbols ? '⇧' : 'Shift');
}
if (keyCode.key) {
keyCodeResult.push(this.acceleratorForKey(keyCode.key, asciiOnly));
}
return keyCodeResult.join(separator);
return keyCodeResult;
}

/**
Expand Down
14 changes: 12 additions & 2 deletions packages/core/src/browser/keyboard/keyboard-layout-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,8 +98,18 @@ export class KeyboardLayoutService {
getKeyboardCharacter(key: Key): string {
const layout = this.currentLayout;
if (layout) {
const value = layout.code2Character[key.code];
if (value && value.replace(/[\n\r\t]/g, '')) {
const value = layout.code2Character[key.code]?.trim();
// Special cases from native keymap
if (value === '\u001b') {
return 'escape';
}
if (value === '\u007f') {
return 'delete';
}
if (value === '\u0008') {
return 'backspace';
}
if (value?.replace(/[\n\r\t]/g, '')) {
return value;
}
}
Expand Down
Loading

0 comments on commit 6b45b27

Please sign in to comment.