Skip to content

Commit 02adcfd

Browse files
committed
make alt command behaviour smooth
1 parent 15addfc commit 02adcfd

1 file changed

Lines changed: 42 additions & 11 deletions

File tree

src/vs/platform/actions/browser/menuItemActionItem.ts

Lines changed: 42 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,10 @@ import {localize} from 'vs/nls';
99
import {IKeybindingService} from 'vs/platform/keybinding/common/keybindingService';
1010
import {IMenu, MenuItemAction} from 'vs/platform/actions/common/actions';
1111
import {IAction} from 'vs/base/common/actions';
12+
import {IDisposable, dispose} from 'vs/base/common/lifecycle';
1213
import {ActionItem, Separator} from 'vs/base/browser/ui/actionbar/actionbar';
1314
import {domEvent} from 'vs/base/browser/event';
15+
import {Emitter} from 'vs/base/common/event';
1416

1517

1618
export function fillInActions(menu: IMenu, target: IAction[] | { primary: IAction[]; secondary: IAction[];}): void {
@@ -44,6 +46,28 @@ export function createActionItem(action: IAction, keybindingService: IKeybinding
4446
}
4547
}
4648

49+
50+
const _altKey = new class extends Emitter<boolean> {
51+
52+
private _subscriptions: IDisposable[] = [];
53+
54+
constructor() {
55+
super({
56+
onFirstListenerAdd: () => {
57+
domEvent(document.body, 'keydown')(this._key, this, this._subscriptions);
58+
domEvent(document.body, 'keyup')(this._key, this, this._subscriptions);
59+
},
60+
onLastListenerRemove: () => {
61+
this._subscriptions = dispose(this._subscriptions);
62+
}
63+
});
64+
}
65+
66+
private _key(e: KeyboardEvent) {
67+
this.fire(e.type === 'keydown' && e.altKey);
68+
}
69+
};
70+
4771
class MenuItemActionItem extends ActionItem {
4872

4973
private _altKeyDown: boolean = false;
@@ -55,7 +79,7 @@ class MenuItemActionItem extends ActionItem {
5579
super(undefined, action, { icon: !!action.command.iconClass, label: !action.command.iconClass });
5680
}
5781

58-
private get command() {
82+
private get _command() {
5983
const {command, altCommand} = <MenuItemAction>this._action;
6084
return this._altKeyDown && altCommand || command;
6185
}
@@ -70,38 +94,45 @@ class MenuItemActionItem extends ActionItem {
7094
render(container: HTMLElement): void {
7195
super.render(container);
7296

73-
this._callOnDispose.push(domEvent(container, 'mousemove')(e => {
74-
if (this._altKeyDown !== e.altKey) {
75-
this._altKeyDown = e.altKey;
97+
let altSubscription: IDisposable;
98+
this._callOnDispose.push(domEvent(container, 'mouseleave')(_ => dispose(altSubscription)));
99+
this._callOnDispose.push(domEvent(container, 'mouseenter')(e => {
100+
altSubscription = _altKey.event(value => {
76101

102+
this._altKeyDown = value;
77103
this._updateLabel();
78104
this._updateTooltip();
79105
this._updateClass();
80-
}
106+
});
81107
}));
82108
}
83109

84110
_updateLabel(): void {
85111
if (this.options.label) {
86-
this.$e.text(this.command.title);
112+
this.$e.text(this._command.title);
87113
}
88114
}
89115

90116
_updateTooltip(): void {
91117
const element = this.$e.getHTMLElement();
92-
const keybinding = this._keybindingService.lookupKeybindings(this.command.id)[0];
118+
const keybinding = this._keybindingService.lookupKeybindings(this._command.id)[0];
93119
const keybindingLabel = keybinding && this._keybindingService.getLabelFor(keybinding);
94120

95121
element.title = keybindingLabel
96-
? localize('titleAndKb', "{0} ({1})", this.command.title, keybindingLabel)
97-
: this.command.title;
122+
? localize('titleAndKb', "{0} ({1})", this._command.title, keybindingLabel)
123+
: this._command.title;
98124
}
99125

100126
_updateClass(): void {
101127
if (this.options.icon) {
102128
const element = this.$e.getHTMLElement();
103-
const {iconClass} = this.command;
104-
element.classList.add('icon', iconClass);
129+
const {command, altCommand} = (<MenuItemAction>this._action);
130+
if (this._command !== command) {
131+
element.classList.remove(command.iconClass);
132+
} else if (altCommand) {
133+
element.classList.remove(altCommand.iconClass);
134+
}
135+
element.classList.add('icon', this._command.iconClass);
105136
}
106137
}
107138
}

0 commit comments

Comments
 (0)