Skip to content

Format keyboard shortcuts according to OS conventions #258

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

Merged
merged 5 commits into from
Nov 3, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
148 changes: 62 additions & 86 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,23 @@ export namespace CommandRegistry {
* Format a keystroke for display on the local system.
*/
export function formatKeystroke(keystroke: string): string {
let mods = '';
let mods = [];
let separator = Platform.IS_MAC ? ' ' : '+';
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+';
}
if (parts.ctrl) {
mods.push('Ctrl');
}
return mods + parts.key;
if (parts.alt) {
mods.push('Alt');
}
if (parts.shift) {
mods.push('Shift');
}
if (Platform.IS_MAC && parts.cmd) {
mods.push('Cmd');
}
mods.push(parts.key);
return mods.map(Private.formatKey).join(separator);
}

/**
Expand Down Expand Up @@ -1282,20 +1218,21 @@ export namespace CommandRegistry {
if (!key || layout.isModifierKey(key)) {
return '';
}
let mods = '';
let mods = [];
if (event.ctrlKey) {
mods += 'Ctrl ';
mods.push('Ctrl');
}
if (event.altKey) {
mods += 'Alt ';
mods.push('Alt');
}
if (event.shiftKey) {
mods += 'Shift ';
mods.push('Shift');
}
if (event.metaKey && Platform.IS_MAC) {
mods += 'Cmd ';
mods.push('Cmd');
}
return mods + key;
mods.push(key);
return mods.join(' ');
}
}

Expand Down Expand Up @@ -1492,6 +1429,45 @@ namespace Private {
event.target!.dispatchEvent(cloneKeyboardEvent(event));
}

export function formatKey(key: string): string {
if (Platform.IS_MAC) {
return MAC_DISPLAY.hasOwnProperty(key) ? MAC_DISPLAY[key] : key;
} else {
return WIN_DISPLAY.hasOwnProperty(key) ? WIN_DISPLAY[key] : key;
}
}

const MAC_DISPLAY: { [key: string]: string } = {
Backspace: '⌫',
Tab: '⇥',
Enter: '↩',
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⏎ might have a closer semantic meaning, but will often render as a less familiar icon

Copy link
Contributor Author

@jasongrout jasongrout Nov 3, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In VoiceOver, your character ⏎ is read as "return symbol", whereas the one in the PR (↩) is read as "return" (when they are in the place of keyboard shortcuts on a menu). I think the one in the PR is the official one to use.

Shift: '⇧',
Ctrl: '⌃',
Alt: '⌥',
Escape: '⎋',
PageUp: '⇞',
PageDown: '⇟',
End: '↘',
Home: '↖',
ArrowLeft: '←',
ArrowUp: '↑',
ArrowRight: '→',
ArrowDown: '↓',
Delete: '⌦',
Cmd: '⌘'
};

const WIN_DISPLAY: { [key: string]: string } = {
Escape: 'Esc',
PageUp: 'Page Up',
PageDown: 'Page Down',
ArrowLeft: 'Left',
ArrowUp: 'Right',
ArrowRight: 'Up',
ArrowDown: 'Down',
Delete: 'Del'
};

/**
* A singleton empty string function.
*/
Expand Down
25 changes: 25 additions & 0 deletions packages/commands/tests/src/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1068,6 +1068,11 @@ describe('@lumino/commands', () => {
expect(parts.key).to.equal('S');
});

it('should preserve arrow names in key without formatting', () => {
let parts = CommandRegistry.parseKeystroke('ArrowRight');
expect(parts.key).to.equal('ArrowRight');
});

it('should be a tolerant parse', () => {
let parts = CommandRegistry.parseKeystroke('G Ctrl Shift S Shift K');
expect(parts.cmd).to.equal(false);
Expand All @@ -1078,6 +1083,26 @@ describe('@lumino/commands', () => {
});
});

describe('.formatKeystroke()', () => {
it('should prepend modifiers', () => {
let label = CommandRegistry.formatKeystroke('Ctrl Alt Shift S');
if (Platform.IS_MAC) {
expect(label).to.equal('\u2303 \u2325 \u21E7 S');
} else {
expect(label).to.equal('Ctrl+Alt+Shift+S');
}
});

it('should format arrow', () => {
let label = CommandRegistry.formatKeystroke('Alt ArrowDown');
if (Platform.IS_MAC) {
expect(label).to.equal('\u2325 \u2193');
} else {
expect(label).to.equal('Alt+Down');
}
});
});

describe('.normalizeKeystroke()', () => {
it('should normalize and validate a keystroke', () => {
let stroke = CommandRegistry.normalizeKeystroke('Ctrl S');
Expand Down