Skip to content

Commit

Permalink
Modified labels creators to accept a list of chord parts.
Browse files Browse the repository at this point in the history
It was difficult to refactor the ResolvedKeybindings without
also refactoring label creators, as the null checks where handled in the
label generation.
  • Loading branch information
toumorokoshi committed Jan 13, 2019
1 parent 28f0b4b commit 0c4b29e
Show file tree
Hide file tree
Showing 6 changed files with 139 additions and 79 deletions.
8 changes: 4 additions & 4 deletions src/vs/base/common/keyCodes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -583,12 +583,12 @@ export abstract class ResolvedKeybinding {
public abstract isChord(): boolean;

/**
* Returns the firstPart, chordPart that should be used for dispatching.
* Returns the parts that should be used for dispatching.
*/
public abstract getDispatchParts(): string[];
public abstract getDispatchParts(): (string | null)[];
/**
* Returns the firstPart, chordPart of the keybinding.
* For simple keybindings, the second element will be null.
* Returns the parts that comprise of the keybinding.
* Simple keybindings return one element.
*/
public abstract getParts(): ResolvedKeybindingPart[];
}
20 changes: 7 additions & 13 deletions src/vs/base/common/keybindingLabels.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,8 @@ export class ModifierLabelProvider {
this.modifierLabels[OperatingSystem.Linux] = linux;
}

public toLabel(firstPartMod: Modifiers | null, firstPartKey: string | null, chordPartMod: Modifiers | null, chordPartKey: string | null, OS: OperatingSystem): string | null {
if (firstPartMod === null || firstPartKey === null) {
return null;
}
return _asString(firstPartMod, firstPartKey, chordPartMod, chordPartKey, this.modifierLabels[OS]);
public toLabel(partModifiers: Modifiers[], partKeys: string[], OS: OperatingSystem): string | null {
return _asString(partModifiers, partKeys, this.modifierLabels[OS]);
}
}

Expand Down Expand Up @@ -172,13 +169,10 @@ function _simpleAsString(modifiers: Modifiers, key: string, labels: ModifierLabe
return result.join(labels.separator);
}

function _asString(firstPartMod: Modifiers, firstPartKey: string, chordPartMod: Modifiers | null, chordPartKey: string | null, labels: ModifierLabels): string {
let result = _simpleAsString(firstPartMod, firstPartKey, labels);

if (chordPartMod !== null && chordPartKey !== null) {
result += ' ';
result += _simpleAsString(chordPartMod, chordPartKey, labels);
function _asString(partModifiers: Modifiers[], partKeys: string[], labels: ModifierLabels): string {
let results: string[] = [];
for (let i = 0; i < partModifiers.length; i++) {
results.push(_simpleAsString(partModifiers[i], partKeys[i], labels));
}

return result;
return results.join(' ');
}
57 changes: 39 additions & 18 deletions src/vs/platform/keybinding/common/usLayoutResolvedKeybinding.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,15 @@ import { OperatingSystem } from 'vs/base/common/platform';
export class USLayoutResolvedKeybinding extends ResolvedKeybinding {

private readonly _os: OperatingSystem;
private readonly _chords: SimpleKeybinding[];
private readonly _parts: SimpleKeybinding[];

constructor(actual: Keybinding, OS: OperatingSystem) {
super();
this._os = OS;
if (!actual) {
throw new Error(`Invalid USLayoutResolvedKeybinding`);
} else {
this._chords = actual.parts;
this._parts = actual.parts;
}
}

Expand Down Expand Up @@ -52,9 +52,15 @@ export class USLayoutResolvedKeybinding extends ResolvedKeybinding {
}

public getLabel(): string | null {
let firstPart = this._getUILabelForKeybinding(this._chords[0]);
let chordPart = this._getUILabelForKeybinding(this._chords[1]);
return UILabelProvider.toLabel(this._chords[0], firstPart, this._chords[1], chordPart, this._os);
let partKeys: string[] = [];
for (let part of this._parts) {
let key = this._getUILabelForKeybinding(part);
if (key === null) {
return null;
}
partKeys.push(key);
}
return UILabelProvider.toLabel(this._parts, partKeys, this._os);
}

private _getAriaLabelForKeybinding(keybinding: SimpleKeybinding | null): string | null {
Expand All @@ -68,9 +74,15 @@ export class USLayoutResolvedKeybinding extends ResolvedKeybinding {
}

public getAriaLabel(): string | null {
let firstPart = this._getAriaLabelForKeybinding(this._chords[0]);
let chordPart = this._getAriaLabelForKeybinding(this._chords[1]);
return AriaLabelProvider.toLabel(this._chords[0], firstPart, this._chords[1], chordPart, this._os);
let partKeys: string[] = [];
for (let part of this._parts) {
let key = this._getAriaLabelForKeybinding(part);
if (key === null) {
return null;
}
partKeys.push(key);
}
return AriaLabelProvider.toLabel(this._parts, partKeys, this._os);
}

private _keyCodeToElectronAccelerator(keyCode: KeyCode): string | null {
Expand Down Expand Up @@ -104,13 +116,16 @@ export class USLayoutResolvedKeybinding extends ResolvedKeybinding {
}

public getElectronAccelerator(): string | null {
if (this._chords.length > 1) {
if (this._parts.length > 1) {
// Electron cannot handle chords
return null;
}

let firstPart = this._getElectronAcceleratorLabelForKeybinding(this._chords[0]);
return ElectronAcceleratorLabelProvider.toLabel(this._chords[0], firstPart, null, null, this._os);
let firstPart = this._getElectronAcceleratorLabelForKeybinding(this._parts[0]);
if (firstPart === null) {
return null;
}
return ElectronAcceleratorLabelProvider.toLabel(this._parts, [firstPart], this._os);
}

private _getUserSettingsLabelForKeybinding(keybinding: SimpleKeybinding | null): string | null {
Expand All @@ -124,9 +139,15 @@ export class USLayoutResolvedKeybinding extends ResolvedKeybinding {
}

public getUserSettingsLabel(): string | null {
let firstPart = this._getUserSettingsLabelForKeybinding(this._chords[0]);
let chordPart = this._getUserSettingsLabelForKeybinding(this._chords[1]);
let result = UserSettingsLabelProvider.toLabel(this._chords[0], firstPart, this._chords[1], chordPart, this._os);
let partKeys: string[] = [];
for (let part of this._parts) {
let key = this._getUserSettingsLabelForKeybinding(part);
if (key === null) {
return null;
}
partKeys.push(key);
}
let result = UserSettingsLabelProvider.toLabel(this._parts, partKeys, this._os);
return (result ? result.toLowerCase() : result);
}

Expand All @@ -135,11 +156,11 @@ export class USLayoutResolvedKeybinding extends ResolvedKeybinding {
}

public isChord(): boolean {
return this._chords.length > 1;
return this._parts.length > 1;
}

public getParts(): ResolvedKeybindingPart[] {
return this._chords.map(this._toResolvedKeybindingPart);
return this._parts.map(this._toResolvedKeybindingPart);
}

private _toResolvedKeybindingPart(keybinding: SimpleKeybinding): ResolvedKeybindingPart {
Expand All @@ -153,8 +174,8 @@ export class USLayoutResolvedKeybinding extends ResolvedKeybinding {
);
}

public getDispatchParts(): string[] {
return this._chords.map((chord) => USLayoutResolvedKeybinding.getDispatchStr(chord));
public getDispatchParts(): (string | null)[] {
return this._parts.map((chord) => USLayoutResolvedKeybinding.getDispatchStr(chord));
}

public static getDispatchStr(keybinding: SimpleKeybinding): string | null {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,44 +67,65 @@ export class NativeResolvedKeybinding extends ResolvedKeybinding {

private readonly _mapper: MacLinuxKeyboardMapper;
private readonly _OS: OperatingSystem;
private readonly _chords: ScanCodeBinding[];
private readonly _parts: ScanCodeBinding[];

constructor(mapper: MacLinuxKeyboardMapper, OS: OperatingSystem, chords: ScanCodeBinding[]) {
constructor(mapper: MacLinuxKeyboardMapper, OS: OperatingSystem, parts: ScanCodeBinding[]) {
super();
if (chords.length === 0) {
if (parts.length === 0) {
throw new Error(`Invalid USLayoutResolvedKeybinding`);
}
this._mapper = mapper;
this._OS = OS;
this._chords = chords;
this._parts = parts;
}

public getLabel(): string | null {
let firstPart = this._mapper.getUILabelForScanCodeBinding(this._chords[0]);
let chordPart = this._mapper.getUILabelForScanCodeBinding(this._chords[1]);
return UILabelProvider.toLabel(this._chords[0], firstPart, this._chords[1], chordPart, this._OS);
let partKeys: string[] = [];
for (let part of this._parts) {
let key = this._mapper.getUILabelForScanCodeBinding(part);
if (key === null) {
return null;
}
partKeys.push(key);
}
return UILabelProvider.toLabel(this._parts, partKeys, this._OS);
}

public getAriaLabel(): string | null {
let firstPart = this._mapper.getAriaLabelForScanCodeBinding(this._chords[0]);
let chordPart = this._mapper.getAriaLabelForScanCodeBinding(this._chords[1]);
return AriaLabelProvider.toLabel(this._chords[0], firstPart, this._chords[1], chordPart, this._OS);
let partKeys: string[] = [];
for (let part of this._parts) {
let key = this._mapper.getAriaLabelForScanCodeBinding(part);
if (key === null) {
return null;
}
partKeys.push(key);
}
return AriaLabelProvider.toLabel(this._parts, partKeys, this._OS);
}

public getElectronAccelerator(): string | null {
if (this._chords.length > 1) {
if (this._parts.length > 1) {
// Electron cannot handle chords
return null;
}

let firstPart = this._mapper.getElectronAcceleratorLabelForScanCodeBinding(this._chords[0]);
return ElectronAcceleratorLabelProvider.toLabel(this._chords[0], firstPart, null, null, this._OS);
let firstPart = this._mapper.getElectronAcceleratorLabelForScanCodeBinding(this._parts[0]);
if (firstPart === null) {
return null;
}
return ElectronAcceleratorLabelProvider.toLabel(this._parts, [firstPart], this._OS);
}

public getUserSettingsLabel(): string | null {
let firstPart = this._mapper.getUserSettingsLabelForScanCodeBinding(this._chords[0]);
let chordPart = this._mapper.getUserSettingsLabelForScanCodeBinding(this._chords[1]);
return UserSettingsLabelProvider.toLabel(this._chords[0], firstPart, this._chords[1], chordPart, this._OS);
let partKeys: string[] = [];
for (let part of this._parts) {
let key = this._mapper.getUserSettingsLabelForScanCodeBinding(part);
if (key === null) {
return null;
}
partKeys.push(key);
}
return UserSettingsLabelProvider.toLabel(this._parts, partKeys, this._OS);
}

private _isWYSIWYG(binding: ScanCodeBinding | null): boolean {
Expand All @@ -127,15 +148,15 @@ export class NativeResolvedKeybinding extends ResolvedKeybinding {
}

public isWYSIWYG(): boolean {
return this._chords.every(this._isWYSIWYG);
return this._parts.every(this._isWYSIWYG);
}

public isChord(): boolean {
return this._chords.length > 1;
return this._parts.length > 1;
}

public getParts(): ResolvedKeybindingPart[] {
return this._chords.map(this._toResolvedKeybindingPart);
return this._parts.map(this._toResolvedKeybindingPart);
}

private _toResolvedKeybindingPart(binding: ScanCodeBinding): ResolvedKeybindingPart {
Expand All @@ -149,8 +170,8 @@ export class NativeResolvedKeybinding extends ResolvedKeybinding {
);
}

public getDispatchParts(): string[] {
return this._chords.map(this._mapper.getDispatchStrForScanCodeBinding);
public getDispatchParts(): (string | null)[] {
return this._parts.map(this._mapper.getDispatchStrForScanCodeBinding);
}
}

Expand Down Expand Up @@ -1093,7 +1114,7 @@ export class MacLinuxKeyboardMapper implements IKeyboardMapper {
}

public resolveUserBinding(_firstPart: SimpleKeybinding | ScanCodeBinding | null, _chordPart: SimpleKeybinding | ScanCodeBinding | null): ResolvedKeybinding[] {
let chordParts = [];
let chordParts: ScanCodeBinding[][] = [];
if (_firstPart) {
chordParts.push(this._resolveSimpleUserBinding(_firstPart));
}
Expand Down
Loading

0 comments on commit 0c4b29e

Please sign in to comment.