Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make keyboard layouts in charge of modifier keys and formatting of keys #257

Draft
wants to merge 11 commits into
base: main
Choose a base branch
from
Draft
130 changes: 37 additions & 93 deletions packages/commands/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1062,26 +1062,6 @@ export namespace CommandRegistry {
*/
shift: boolean;

/**
* Whether `'ArrowLeft'` appears in the keystroke.
*/
arrowLeft: boolean;

/**
* Whether `'ArrowUp'` appears in the keystroke.
*/
arrowUp: boolean;

/**
* Whether `'ArrowRight'` appears in the keystroke.
*/
arrowRight: boolean;

/**
* Whether `'ArrowDown'` appears in the keystroke.
*/
arrowDown: boolean;

/**
* The primary key for the keystroke.
*/
Expand Down Expand Up @@ -1116,10 +1096,6 @@ export namespace CommandRegistry {
let cmd = false;
let ctrl = false;
let shift = false;
let arrowLeft = false;
let arrowUp = false;
let arrowRight = false;
let arrowDown = false;
for (let token of keystroke.split(/\s+/)) {
if (token === 'Accel') {
if (Platform.IS_MAC) {
Expand All @@ -1135,29 +1111,11 @@ export namespace CommandRegistry {
ctrl = true;
} else if (token === 'Shift') {
shift = true;
} else if (token === 'ArrowLeft') {
arrowLeft = true;
} else if (token === 'ArrowUp') {
arrowUp = true;
} else if (token === 'ArrowRight') {
arrowRight = true;
} else if (token === 'ArrowDown') {
arrowDown = true;
} else if (token.length > 0) {
key = token;
}
}
return {
cmd,
ctrl,
alt,
shift,
key,
arrowLeft,
arrowUp,
arrowRight,
arrowDown
};
return { cmd, ctrl, alt, shift, key };
}

/**
Expand Down Expand Up @@ -1214,45 +1172,25 @@ export namespace CommandRegistry {
* Format a keystroke for display on the local system.
*/
export function formatKeystroke(keystroke: string): string {
let mods = '';
let parts = parseKeystroke(keystroke);
if (Platform.IS_MAC) {
if (parts.ctrl) {
mods += '\u2303 ';
}
if (parts.alt) {
mods += '\u2325 ';
}
if (parts.shift) {
mods += '\u21E7 ';
}
if (parts.cmd) {
mods += '\u2318 ';
}
if (parts.arrowLeft) {
mods += '\u2190 ';
}
if (parts.arrowUp) {
mods += '\u2191 ';
}
if (parts.arrowRight) {
mods += '\u2192 ';
}
if (parts.arrowDown) {
mods += '\u2193 ';
}
} else {
if (parts.ctrl) {
mods += 'Ctrl+';
}
if (parts.alt) {
mods += 'Alt+';
}
if (parts.shift) {
mods += 'Shift+';
}
let layout = getKeyboardLayout();
let label = [];
let separator = Platform.IS_MAC ? ' ' : '+';
if (parts.ctrl) {
label.push('Ctrl');
}
return mods + parts.key;
if (parts.alt) {
label.push('Alt');
}
if (parts.shift) {
label.push('Shift');
}
if (Platform.IS_MAC && parts.cmd) {
// Keyboard layouts label Command as Meta
label.push('Meta');
}
label.push(parts.key);
return label.map(k => layout.formatKey(k)).join(separator);
}

/**
Expand Down Expand Up @@ -1282,20 +1220,25 @@ export namespace CommandRegistry {
if (!key || layout.isModifierKey(key)) {
return '';
}
let mods = '';
if (event.ctrlKey) {
mods += 'Ctrl ';
}
if (event.altKey) {
mods += 'Alt ';
}
if (event.shiftKey) {
mods += 'Shift ';
}
if (event.metaKey && Platform.IS_MAC) {
mods += 'Cmd ';
// Loop through modifier keys in order to test them
let mods = [];
for (let mod of layout.modifierKeys()) {
// Special treatment for Meta (Cmd on macOS) for backwards compatibility
if (mod === 'Meta') {
if (Platform.IS_MAC && event.getModifierState(mod)) {
mods.push('Cmd');
}
} else if (mod === 'Ctrl') {
// For backwards compatibility, our keyboard layout still uses Ctrl.
if (event.getModifierState('Control')) {
mods.push('Ctrl');
}
} else if (event.getModifierState(mod)) {
mods.push(mod);
}
}
return mods + key;
mods.push(key);
return mods.join(' ');
}
}

Expand Down Expand Up @@ -1635,6 +1578,7 @@ namespace Private {
clone.shiftKey = event.shiftKey || false;
clone.metaKey = event.metaKey || false;
clone.view = event.view || window;
clone.getModifierState = (key: string) => event.getModifierState(key);
return clone as KeyboardEvent;
}
}
Loading