-
Notifications
You must be signed in to change notification settings - Fork 29.8k
/
keybindingLabel.ts
185 lines (154 loc) · 6.16 KB
/
keybindingLabel.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import * as dom from '../../dom.js';
import type { IManagedHover } from '../hover/hover.js';
import { getBaseLayerHoverDelegate } from '../hover/hoverDelegate2.js';
import { getDefaultHoverDelegate } from '../hover/hoverDelegateFactory.js';
import { UILabelProvider } from '../../../common/keybindingLabels.js';
import { ResolvedKeybinding, ResolvedChord } from '../../../common/keybindings.js';
import { Disposable } from '../../../common/lifecycle.js';
import { equals } from '../../../common/objects.js';
import { OperatingSystem } from '../../../common/platform.js';
import './keybindingLabel.css';
import { localize } from '../../../../nls.js';
const $ = dom.$;
export interface ChordMatches {
ctrlKey?: boolean;
shiftKey?: boolean;
altKey?: boolean;
metaKey?: boolean;
keyCode?: boolean;
}
export interface Matches {
firstPart: ChordMatches;
chordPart: ChordMatches;
}
export interface KeybindingLabelOptions extends IKeybindingLabelStyles {
renderUnboundKeybindings?: boolean;
/**
* Default false.
*/
disableTitle?: boolean;
}
export interface IKeybindingLabelStyles {
keybindingLabelBackground: string | undefined;
keybindingLabelForeground: string | undefined;
keybindingLabelBorder: string | undefined;
keybindingLabelBottomBorder: string | undefined;
keybindingLabelShadow: string | undefined;
}
export const unthemedKeybindingLabelOptions: KeybindingLabelOptions = {
keybindingLabelBackground: undefined,
keybindingLabelForeground: undefined,
keybindingLabelBorder: undefined,
keybindingLabelBottomBorder: undefined,
keybindingLabelShadow: undefined
};
export class KeybindingLabel extends Disposable {
private domNode: HTMLElement;
private options: KeybindingLabelOptions;
private readonly keyElements = new Set<HTMLSpanElement>();
private hover: IManagedHover;
private keybinding: ResolvedKeybinding | undefined;
private matches: Matches | undefined;
private didEverRender: boolean;
constructor(container: HTMLElement, private os: OperatingSystem, options?: KeybindingLabelOptions) {
super();
this.options = options || Object.create(null);
const labelForeground = this.options.keybindingLabelForeground;
this.domNode = dom.append(container, $('.monaco-keybinding'));
if (labelForeground) {
this.domNode.style.color = labelForeground;
}
this.hover = this._register(getBaseLayerHoverDelegate().setupManagedHover(getDefaultHoverDelegate('mouse'), this.domNode, ''));
this.didEverRender = false;
container.appendChild(this.domNode);
}
get element(): HTMLElement {
return this.domNode;
}
set(keybinding: ResolvedKeybinding | undefined, matches?: Matches) {
if (this.didEverRender && this.keybinding === keybinding && KeybindingLabel.areSame(this.matches, matches)) {
return;
}
this.keybinding = keybinding;
this.matches = matches;
this.render();
}
private render() {
this.clear();
if (this.keybinding) {
const chords = this.keybinding.getChords();
if (chords[0]) {
this.renderChord(this.domNode, chords[0], this.matches ? this.matches.firstPart : null);
}
for (let i = 1; i < chords.length; i++) {
dom.append(this.domNode, $('span.monaco-keybinding-key-chord-separator', undefined, ' '));
this.renderChord(this.domNode, chords[i], this.matches ? this.matches.chordPart : null);
}
const title = (this.options.disableTitle ?? false) ? undefined : this.keybinding.getAriaLabel() || undefined;
this.hover.update(title);
this.domNode.setAttribute('aria-label', title || '');
} else if (this.options && this.options.renderUnboundKeybindings) {
this.renderUnbound(this.domNode);
}
this.didEverRender = true;
}
private clear(): void {
dom.clearNode(this.domNode);
this.keyElements.clear();
}
private renderChord(parent: HTMLElement, chord: ResolvedChord, match: ChordMatches | null) {
const modifierLabels = UILabelProvider.modifierLabels[this.os];
if (chord.ctrlKey) {
this.renderKey(parent, modifierLabels.ctrlKey, Boolean(match?.ctrlKey), modifierLabels.separator);
}
if (chord.shiftKey) {
this.renderKey(parent, modifierLabels.shiftKey, Boolean(match?.shiftKey), modifierLabels.separator);
}
if (chord.altKey) {
this.renderKey(parent, modifierLabels.altKey, Boolean(match?.altKey), modifierLabels.separator);
}
if (chord.metaKey) {
this.renderKey(parent, modifierLabels.metaKey, Boolean(match?.metaKey), modifierLabels.separator);
}
const keyLabel = chord.keyLabel;
if (keyLabel) {
this.renderKey(parent, keyLabel, Boolean(match?.keyCode), '');
}
}
private renderKey(parent: HTMLElement, label: string, highlight: boolean, separator: string): void {
dom.append(parent, this.createKeyElement(label, highlight ? '.highlight' : ''));
if (separator) {
dom.append(parent, $('span.monaco-keybinding-key-separator', undefined, separator));
}
}
private renderUnbound(parent: HTMLElement): void {
dom.append(parent, this.createKeyElement(localize('unbound', "Unbound")));
}
private createKeyElement(label: string, extraClass = ''): HTMLElement {
const keyElement = $('span.monaco-keybinding-key' + extraClass, undefined, label);
this.keyElements.add(keyElement);
if (this.options.keybindingLabelBackground) {
keyElement.style.backgroundColor = this.options.keybindingLabelBackground;
}
if (this.options.keybindingLabelBorder) {
keyElement.style.borderColor = this.options.keybindingLabelBorder;
}
if (this.options.keybindingLabelBottomBorder) {
keyElement.style.borderBottomColor = this.options.keybindingLabelBottomBorder;
}
if (this.options.keybindingLabelShadow) {
keyElement.style.boxShadow = `inset 0 -1px 0 ${this.options.keybindingLabelShadow}`;
}
return keyElement;
}
private static areSame(a: Matches | undefined, b: Matches | undefined): boolean {
if (a === b || (!a && !b)) {
return true;
}
return !!a && !!b && equals(a.firstPart, b.firstPart) && equals(a.chordPart, b.chordPart);
}
}