Skip to content

Commit 0c4b29e

Browse files
committed
Modified labels creators to accept a list of chord parts.
It was difficult to refactor the ResolvedKeybindings without also refactoring label creators, as the null checks where handled in the label generation.
1 parent 28f0b4b commit 0c4b29e

6 files changed

Lines changed: 139 additions & 79 deletions

File tree

src/vs/base/common/keyCodes.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -583,12 +583,12 @@ export abstract class ResolvedKeybinding {
583583
public abstract isChord(): boolean;
584584

585585
/**
586-
* Returns the firstPart, chordPart that should be used for dispatching.
586+
* Returns the parts that should be used for dispatching.
587587
*/
588-
public abstract getDispatchParts(): string[];
588+
public abstract getDispatchParts(): (string | null)[];
589589
/**
590-
* Returns the firstPart, chordPart of the keybinding.
591-
* For simple keybindings, the second element will be null.
590+
* Returns the parts that comprise of the keybinding.
591+
* Simple keybindings return one element.
592592
*/
593593
public abstract getParts(): ResolvedKeybindingPart[];
594594
}

src/vs/base/common/keybindingLabels.ts

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,8 @@ export class ModifierLabelProvider {
3232
this.modifierLabels[OperatingSystem.Linux] = linux;
3333
}
3434

35-
public toLabel(firstPartMod: Modifiers | null, firstPartKey: string | null, chordPartMod: Modifiers | null, chordPartKey: string | null, OS: OperatingSystem): string | null {
36-
if (firstPartMod === null || firstPartKey === null) {
37-
return null;
38-
}
39-
return _asString(firstPartMod, firstPartKey, chordPartMod, chordPartKey, this.modifierLabels[OS]);
35+
public toLabel(partModifiers: Modifiers[], partKeys: string[], OS: OperatingSystem): string | null {
36+
return _asString(partModifiers, partKeys, this.modifierLabels[OS]);
4037
}
4138
}
4239

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

175-
function _asString(firstPartMod: Modifiers, firstPartKey: string, chordPartMod: Modifiers | null, chordPartKey: string | null, labels: ModifierLabels): string {
176-
let result = _simpleAsString(firstPartMod, firstPartKey, labels);
177-
178-
if (chordPartMod !== null && chordPartKey !== null) {
179-
result += ' ';
180-
result += _simpleAsString(chordPartMod, chordPartKey, labels);
172+
function _asString(partModifiers: Modifiers[], partKeys: string[], labels: ModifierLabels): string {
173+
let results: string[] = [];
174+
for (let i = 0; i < partModifiers.length; i++) {
175+
results.push(_simpleAsString(partModifiers[i], partKeys[i], labels));
181176
}
182-
183-
return result;
177+
return results.join(' ');
184178
}

src/vs/platform/keybinding/common/usLayoutResolvedKeybinding.ts

Lines changed: 39 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,15 @@ import { OperatingSystem } from 'vs/base/common/platform';
1313
export class USLayoutResolvedKeybinding extends ResolvedKeybinding {
1414

1515
private readonly _os: OperatingSystem;
16-
private readonly _chords: SimpleKeybinding[];
16+
private readonly _parts: SimpleKeybinding[];
1717

1818
constructor(actual: Keybinding, OS: OperatingSystem) {
1919
super();
2020
this._os = OS;
2121
if (!actual) {
2222
throw new Error(`Invalid USLayoutResolvedKeybinding`);
2323
} else {
24-
this._chords = actual.parts;
24+
this._parts = actual.parts;
2525
}
2626
}
2727

@@ -52,9 +52,15 @@ export class USLayoutResolvedKeybinding extends ResolvedKeybinding {
5252
}
5353

5454
public getLabel(): string | null {
55-
let firstPart = this._getUILabelForKeybinding(this._chords[0]);
56-
let chordPart = this._getUILabelForKeybinding(this._chords[1]);
57-
return UILabelProvider.toLabel(this._chords[0], firstPart, this._chords[1], chordPart, this._os);
55+
let partKeys: string[] = [];
56+
for (let part of this._parts) {
57+
let key = this._getUILabelForKeybinding(part);
58+
if (key === null) {
59+
return null;
60+
}
61+
partKeys.push(key);
62+
}
63+
return UILabelProvider.toLabel(this._parts, partKeys, this._os);
5864
}
5965

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

7076
public getAriaLabel(): string | null {
71-
let firstPart = this._getAriaLabelForKeybinding(this._chords[0]);
72-
let chordPart = this._getAriaLabelForKeybinding(this._chords[1]);
73-
return AriaLabelProvider.toLabel(this._chords[0], firstPart, this._chords[1], chordPart, this._os);
77+
let partKeys: string[] = [];
78+
for (let part of this._parts) {
79+
let key = this._getAriaLabelForKeybinding(part);
80+
if (key === null) {
81+
return null;
82+
}
83+
partKeys.push(key);
84+
}
85+
return AriaLabelProvider.toLabel(this._parts, partKeys, this._os);
7486
}
7587

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

106118
public getElectronAccelerator(): string | null {
107-
if (this._chords.length > 1) {
119+
if (this._parts.length > 1) {
108120
// Electron cannot handle chords
109121
return null;
110122
}
111123

112-
let firstPart = this._getElectronAcceleratorLabelForKeybinding(this._chords[0]);
113-
return ElectronAcceleratorLabelProvider.toLabel(this._chords[0], firstPart, null, null, this._os);
124+
let firstPart = this._getElectronAcceleratorLabelForKeybinding(this._parts[0]);
125+
if (firstPart === null) {
126+
return null;
127+
}
128+
return ElectronAcceleratorLabelProvider.toLabel(this._parts, [firstPart], this._os);
114129
}
115130

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

126141
public getUserSettingsLabel(): string | null {
127-
let firstPart = this._getUserSettingsLabelForKeybinding(this._chords[0]);
128-
let chordPart = this._getUserSettingsLabelForKeybinding(this._chords[1]);
129-
let result = UserSettingsLabelProvider.toLabel(this._chords[0], firstPart, this._chords[1], chordPart, this._os);
142+
let partKeys: string[] = [];
143+
for (let part of this._parts) {
144+
let key = this._getUserSettingsLabelForKeybinding(part);
145+
if (key === null) {
146+
return null;
147+
}
148+
partKeys.push(key);
149+
}
150+
let result = UserSettingsLabelProvider.toLabel(this._parts, partKeys, this._os);
130151
return (result ? result.toLowerCase() : result);
131152
}
132153

@@ -135,11 +156,11 @@ export class USLayoutResolvedKeybinding extends ResolvedKeybinding {
135156
}
136157

137158
public isChord(): boolean {
138-
return this._chords.length > 1;
159+
return this._parts.length > 1;
139160
}
140161

141162
public getParts(): ResolvedKeybindingPart[] {
142-
return this._chords.map(this._toResolvedKeybindingPart);
163+
return this._parts.map(this._toResolvedKeybindingPart);
143164
}
144165

145166
private _toResolvedKeybindingPart(keybinding: SimpleKeybinding): ResolvedKeybindingPart {
@@ -153,8 +174,8 @@ export class USLayoutResolvedKeybinding extends ResolvedKeybinding {
153174
);
154175
}
155176

156-
public getDispatchParts(): string[] {
157-
return this._chords.map((chord) => USLayoutResolvedKeybinding.getDispatchStr(chord));
177+
public getDispatchParts(): (string | null)[] {
178+
return this._parts.map((chord) => USLayoutResolvedKeybinding.getDispatchStr(chord));
158179
}
159180

160181
public static getDispatchStr(keybinding: SimpleKeybinding): string | null {

src/vs/workbench/services/keybinding/common/macLinuxKeyboardMapper.ts

Lines changed: 43 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -67,44 +67,65 @@ export class NativeResolvedKeybinding extends ResolvedKeybinding {
6767

6868
private readonly _mapper: MacLinuxKeyboardMapper;
6969
private readonly _OS: OperatingSystem;
70-
private readonly _chords: ScanCodeBinding[];
70+
private readonly _parts: ScanCodeBinding[];
7171

72-
constructor(mapper: MacLinuxKeyboardMapper, OS: OperatingSystem, chords: ScanCodeBinding[]) {
72+
constructor(mapper: MacLinuxKeyboardMapper, OS: OperatingSystem, parts: ScanCodeBinding[]) {
7373
super();
74-
if (chords.length === 0) {
74+
if (parts.length === 0) {
7575
throw new Error(`Invalid USLayoutResolvedKeybinding`);
7676
}
7777
this._mapper = mapper;
7878
this._OS = OS;
79-
this._chords = chords;
79+
this._parts = parts;
8080
}
8181

8282
public getLabel(): string | null {
83-
let firstPart = this._mapper.getUILabelForScanCodeBinding(this._chords[0]);
84-
let chordPart = this._mapper.getUILabelForScanCodeBinding(this._chords[1]);
85-
return UILabelProvider.toLabel(this._chords[0], firstPart, this._chords[1], chordPart, this._OS);
83+
let partKeys: string[] = [];
84+
for (let part of this._parts) {
85+
let key = this._mapper.getUILabelForScanCodeBinding(part);
86+
if (key === null) {
87+
return null;
88+
}
89+
partKeys.push(key);
90+
}
91+
return UILabelProvider.toLabel(this._parts, partKeys, this._OS);
8692
}
8793

8894
public getAriaLabel(): string | null {
89-
let firstPart = this._mapper.getAriaLabelForScanCodeBinding(this._chords[0]);
90-
let chordPart = this._mapper.getAriaLabelForScanCodeBinding(this._chords[1]);
91-
return AriaLabelProvider.toLabel(this._chords[0], firstPart, this._chords[1], chordPart, this._OS);
95+
let partKeys: string[] = [];
96+
for (let part of this._parts) {
97+
let key = this._mapper.getAriaLabelForScanCodeBinding(part);
98+
if (key === null) {
99+
return null;
100+
}
101+
partKeys.push(key);
102+
}
103+
return AriaLabelProvider.toLabel(this._parts, partKeys, this._OS);
92104
}
93105

94106
public getElectronAccelerator(): string | null {
95-
if (this._chords.length > 1) {
107+
if (this._parts.length > 1) {
96108
// Electron cannot handle chords
97109
return null;
98110
}
99111

100-
let firstPart = this._mapper.getElectronAcceleratorLabelForScanCodeBinding(this._chords[0]);
101-
return ElectronAcceleratorLabelProvider.toLabel(this._chords[0], firstPart, null, null, this._OS);
112+
let firstPart = this._mapper.getElectronAcceleratorLabelForScanCodeBinding(this._parts[0]);
113+
if (firstPart === null) {
114+
return null;
115+
}
116+
return ElectronAcceleratorLabelProvider.toLabel(this._parts, [firstPart], this._OS);
102117
}
103118

104119
public getUserSettingsLabel(): string | null {
105-
let firstPart = this._mapper.getUserSettingsLabelForScanCodeBinding(this._chords[0]);
106-
let chordPart = this._mapper.getUserSettingsLabelForScanCodeBinding(this._chords[1]);
107-
return UserSettingsLabelProvider.toLabel(this._chords[0], firstPart, this._chords[1], chordPart, this._OS);
120+
let partKeys: string[] = [];
121+
for (let part of this._parts) {
122+
let key = this._mapper.getUserSettingsLabelForScanCodeBinding(part);
123+
if (key === null) {
124+
return null;
125+
}
126+
partKeys.push(key);
127+
}
128+
return UserSettingsLabelProvider.toLabel(this._parts, partKeys, this._OS);
108129
}
109130

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

129150
public isWYSIWYG(): boolean {
130-
return this._chords.every(this._isWYSIWYG);
151+
return this._parts.every(this._isWYSIWYG);
131152
}
132153

133154
public isChord(): boolean {
134-
return this._chords.length > 1;
155+
return this._parts.length > 1;
135156
}
136157

137158
public getParts(): ResolvedKeybindingPart[] {
138-
return this._chords.map(this._toResolvedKeybindingPart);
159+
return this._parts.map(this._toResolvedKeybindingPart);
139160
}
140161

141162
private _toResolvedKeybindingPart(binding: ScanCodeBinding): ResolvedKeybindingPart {
@@ -149,8 +170,8 @@ export class NativeResolvedKeybinding extends ResolvedKeybinding {
149170
);
150171
}
151172

152-
public getDispatchParts(): string[] {
153-
return this._chords.map(this._mapper.getDispatchStrForScanCodeBinding);
173+
public getDispatchParts(): (string | null)[] {
174+
return this._parts.map(this._mapper.getDispatchStrForScanCodeBinding);
154175
}
155176
}
156177

@@ -1093,7 +1114,7 @@ export class MacLinuxKeyboardMapper implements IKeyboardMapper {
10931114
}
10941115

10951116
public resolveUserBinding(_firstPart: SimpleKeybinding | ScanCodeBinding | null, _chordPart: SimpleKeybinding | ScanCodeBinding | null): ResolvedKeybinding[] {
1096-
let chordParts = [];
1117+
let chordParts: ScanCodeBinding[][] = [];
10971118
if (_firstPart) {
10981119
chordParts.push(this._resolveSimpleUserBinding(_firstPart));
10991120
}

0 commit comments

Comments
 (0)