Skip to content

Commit

Permalink
Cache debug viewlet actions
Browse files Browse the repository at this point in the history
fixes #46867
  • Loading branch information
isidorn committed Apr 23, 2018
1 parent 21700be commit 9e500bb
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 24 deletions.
40 changes: 22 additions & 18 deletions src/vs/workbench/parts/debug/browser/debugActionItems.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
*--------------------------------------------------------------------------------------------*/

import * as nls from 'vs/nls';
import * as lifecycle from 'vs/base/common/lifecycle';
import * as errors from 'vs/base/common/errors';
import { IAction, IActionRunner } from 'vs/base/common/actions';
import { KeyCode } from 'vs/base/common/keyCodes';
Expand All @@ -21,6 +20,7 @@ import { SIDE_BAR_BACKGROUND } from 'vs/workbench/common/theme';
import { selectBorder } from 'vs/platform/theme/common/colorRegistry';
import { IContextViewService } from 'vs/platform/contextview/browser/contextView';
import { IWorkspaceContextService, WorkbenchState } from 'vs/platform/workspace/common/workspace';
import { IDisposable, dispose } from 'vs/base/common/lifecycle';

const $ = dom.$;

Expand All @@ -33,7 +33,8 @@ export class StartDebugActionItem implements IActionItem {
private start: HTMLElement;
private selectBox: SelectBox;
private options: { label: string, handler: (() => boolean) }[];
private toDispose: lifecycle.IDisposable[];
private toDispose: IDisposable[];
private toDisposeOnRender: IDisposable[];
private selected: number;

constructor(
Expand All @@ -47,6 +48,7 @@ export class StartDebugActionItem implements IActionItem {
@IContextViewService contextViewService: IContextViewService,
) {
this.toDispose = [];
this.toDisposeOnRender = [];
this.selectBox = new SelectBox([], -1, contextViewService);
this.toDispose.push(attachSelectBoxStyler(this.selectBox, themeService, {
selectBackground: SIDE_BAR_BACKGROUND
Expand All @@ -61,44 +63,37 @@ export class StartDebugActionItem implements IActionItem {
this.updateOptions();
}
}));
this.toDispose.push(this.selectBox.onDidSelect(e => {
if (this.options[e.index].handler()) {
this.selected = e.index;
} else {
// Some select options should not remain selected https://github.com/Microsoft/vscode/issues/31526
this.selectBox.select(this.selected);
}
}));
this.toDispose.push(this.debugService.getConfigurationManager().onDidSelectConfiguration(() => {
this.updateOptions();
}));
}

public render(container: HTMLElement): void {
this.toDisposeOnRender = dispose(this.toDisposeOnRender);
this.container = container;
dom.addClass(container, 'start-debug-action-item');
this.start = dom.append(container, $('.icon'));
this.start.title = this.action.label;
this.start.tabIndex = 0;

this.toDispose.push(dom.addDisposableListener(this.start, dom.EventType.CLICK, () => {
this.toDisposeOnRender.push(dom.addDisposableListener(this.start, dom.EventType.CLICK, () => {
this.start.blur();
this.actionRunner.run(this.action, this.context).done(null, errors.onUnexpectedError);
}));

this.toDispose.push(dom.addDisposableListener(this.start, dom.EventType.MOUSE_DOWN, (e: MouseEvent) => {
this.toDisposeOnRender.push(dom.addDisposableListener(this.start, dom.EventType.MOUSE_DOWN, (e: MouseEvent) => {
if (this.action.enabled && e.button === 0) {
dom.addClass(this.start, 'active');
}
}));
this.toDispose.push(dom.addDisposableListener(this.start, dom.EventType.MOUSE_UP, () => {
this.toDisposeOnRender.push(dom.addDisposableListener(this.start, dom.EventType.MOUSE_UP, () => {
dom.removeClass(this.start, 'active');
}));
this.toDispose.push(dom.addDisposableListener(this.start, dom.EventType.MOUSE_OUT, () => {
this.toDisposeOnRender.push(dom.addDisposableListener(this.start, dom.EventType.MOUSE_OUT, () => {
dom.removeClass(this.start, 'active');
}));

this.toDispose.push(dom.addDisposableListener(this.start, dom.EventType.KEY_DOWN, (e: KeyboardEvent) => {
this.toDisposeOnRender.push(dom.addDisposableListener(this.start, dom.EventType.KEY_DOWN, (e: KeyboardEvent) => {
const event = new StandardKeyboardEvent(e);
if (event.equals(KeyCode.Enter)) {
this.actionRunner.run(this.action, this.context).done(null, errors.onUnexpectedError);
Expand All @@ -108,17 +103,26 @@ export class StartDebugActionItem implements IActionItem {
event.stopPropagation();
}
}));
this.toDisposeOnRender.push(this.selectBox.onDidSelect(e => {
const shouldBeSelected = this.options[e.index].handler();
if (shouldBeSelected) {
this.selected = e.index;
} else {
// Some select options should not remain selected https://github.com/Microsoft/vscode/issues/31526
this.selectBox.select(this.selected);
}
}));

const selectBoxContainer = $('.configuration');
this.selectBox.render(dom.append(container, selectBoxContainer));
this.toDispose.push(dom.addDisposableListener(selectBoxContainer, dom.EventType.KEY_DOWN, (e: KeyboardEvent) => {
this.toDisposeOnRender.push(dom.addDisposableListener(selectBoxContainer, dom.EventType.KEY_DOWN, (e: KeyboardEvent) => {
const event = new StandardKeyboardEvent(e);
if (event.equals(KeyCode.LeftArrow)) {
this.start.focus();
event.stopPropagation();
}
}));
this.toDispose.push(attachStylerCallback(this.themeService, { selectBorder }, colors => {
this.toDisposeOnRender.push(attachStylerCallback(this.themeService, { selectBorder }, colors => {
this.container.style.border = colors.selectBorder ? `1px solid ${colors.selectBorder}` : null;
selectBoxContainer.style.borderLeft = colors.selectBorder ? `1px solid ${colors.selectBorder}` : null;
}));
Expand Down Expand Up @@ -147,7 +151,7 @@ export class StartDebugActionItem implements IActionItem {
}

public dispose(): void {
this.toDispose = lifecycle.dispose(this.toDispose);
this.toDispose = dispose(this.toDispose);
}

private updateOptions(): void {
Expand Down
21 changes: 15 additions & 6 deletions src/vs/workbench/parts/debug/browser/debugViewlet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import { IContextKeyService } from 'vs/platform/contextkey/common/contextkey';
import { IContextMenuService } from 'vs/platform/contextview/browser/contextView';
import { IDisposable, dispose } from 'vs/base/common/lifecycle';
import { IPartService } from 'vs/workbench/services/part/common/partService';
import { memoize } from 'vs/base/common/decorators';

export class DebugViewlet extends PersistentViewsViewlet {

Expand Down Expand Up @@ -69,12 +70,17 @@ export class DebugViewlet extends PersistentViewsViewlet {
}
}

@memoize
private get actions(): IAction[] {
return [
this._register(this.instantiationService.createInstance(StartAction, StartAction.ID, StartAction.LABEL)),
this._register(this.instantiationService.createInstance(ConfigureAction, ConfigureAction.ID, ConfigureAction.LABEL)),
this._register(this.instantiationService.createInstance(ToggleReplAction, ToggleReplAction.ID, ToggleReplAction.LABEL))
];
}

public getActions(): IAction[] {
const actions = [];
actions.push(this.instantiationService.createInstance(StartAction, StartAction.ID, StartAction.LABEL));
actions.push(this.instantiationService.createInstance(ConfigureAction, ConfigureAction.ID, ConfigureAction.LABEL));
actions.push(this._register(this.instantiationService.createInstance(ToggleReplAction, ToggleReplAction.ID, ToggleReplAction.LABEL)));
return actions;
return this.actions;
}

public getSecondaryActions(): IAction[] {
Expand All @@ -83,7 +89,10 @@ export class DebugViewlet extends PersistentViewsViewlet {

public getActionItem(action: IAction): IActionItem {
if (action.id === StartAction.ID) {
this.startDebugActionItem = this.instantiationService.createInstance(StartDebugActionItem, null, action);
if (!this.startDebugActionItem) {
this.startDebugActionItem = this._register(this.instantiationService.createInstance(StartDebugActionItem, null, action));
}

return this.startDebugActionItem;
}

Expand Down

0 comments on commit 9e500bb

Please sign in to comment.