Skip to content

Commit

Permalink
toolbar: resolve keybindings for primary actions
Browse files Browse the repository at this point in the history
  • Loading branch information
bpasero committed Oct 19, 2016
1 parent b8c98e6 commit c9e82c9
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 19 deletions.
21 changes: 17 additions & 4 deletions src/vs/base/browser/ui/toolbar/toolbar.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ export interface IToolBarOptions {
actionItemProvider?: IActionItemProvider;
ariaLabel?: string;
getKeyBinding?: (action: IAction) => Keybinding;
getKeyBindingLabel?: (key: Keybinding) => string;
}

/**
Expand All @@ -34,9 +35,12 @@ export class ToolBar {
private toggleMenuAction: ToggleMenuAction;
private toggleMenuActionItem: DropdownMenuActionItem;
private hasSecondaryActions: boolean;
private lookupKeybindings: boolean;

constructor(container: HTMLElement, contextMenuProvider: IContextMenuProvider, options: IToolBarOptions = { orientation: ActionsOrientation.HORIZONTAL }) {
this.options = options;
this.lookupKeybindings = typeof this.options.getKeyBinding === 'function' && typeof this.options.getKeyBindingLabel === 'function';

this.toggleMenuAction = new ToggleMenuAction(() => this.toggleMenuActionItem && this.toggleMenuActionItem.show());

let element = document.createElement('div');
Expand Down Expand Up @@ -111,22 +115,31 @@ export class ToolBar {
}

this.actionBar.clear();
this.actionBar.push(primaryActionsToSet, { icon: true, label: false });

primaryActionsToSet.forEach(action => {
this.actionBar.push(action, { icon: true, label: false, keybinding: this.getKeybindingLabel(action) });
});
};
}

public addPrimaryAction(primaryActions: IAction): () => void {
private getKeybindingLabel(action: IAction): string {
const key = this.lookupKeybindings ? this.options.getKeyBinding(action) : void 0;

return key ? this.options.getKeyBindingLabel(key) : void 0;
}

public addPrimaryAction(primaryAction: IAction): () => void {
return () => {

// Add after the "..." action if we have secondary actions
if (this.hasSecondaryActions) {
let itemCount = this.actionBar.length();
this.actionBar.push(primaryActions, { icon: true, label: false, index: itemCount });
this.actionBar.push(primaryAction, { icon: true, label: false, index: itemCount, keybinding: this.getKeybindingLabel(primaryAction) });
}

// Otherwise just add to the end
else {
this.actionBar.push(primaryActions, { icon: true, label: false });
this.actionBar.push(primaryAction, { icon: true, label: false, keybinding: this.getKeybindingLabel(primaryAction) });
}
};
}
Expand Down
3 changes: 2 additions & 1 deletion src/vs/workbench/browser/parts/compositePart.ts
Original file line number Diff line number Diff line change
Expand Up @@ -433,7 +433,8 @@ export abstract class CompositePart<T extends Composite> extends Part {
}

return null;
}
},
getKeyBindingLabel: (key) => this.keybindingService.getLabelFor(key)
});
});

Expand Down
3 changes: 2 additions & 1 deletion src/vs/workbench/browser/parts/editor/titleControl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,8 @@ export abstract class TitleControl implements ITitleAreaControl {
}

return null;
}
},
getKeyBindingLabel: (key) => this.keybindingService.getLabelFor(key)
});

// Action Run Handling
Expand Down
6 changes: 4 additions & 2 deletions src/vs/workbench/browser/viewlet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -334,7 +334,8 @@ export abstract class AdaptiveCollapsibleViewletView extends FixedCollapsibleVie
}

return null;
}
},
getKeyBindingLabel: (key) => this.keybindingService.getLabelFor(key)
});
this.toolBar.actionRunner = this.actionRunner;
this.toolBar.setActions(prepareActions(this.getActions()), prepareActions(this.getSecondaryActions()))();
Expand Down Expand Up @@ -470,7 +471,8 @@ export abstract class CollapsibleViewletView extends CollapsibleView implements
}

return null;
}
},
getKeyBindingLabel: (key) => this.keybindingService.getLabelFor(key)
});
this.toolBar.actionRunner = this.actionRunner;
this.toolBar.setActions(prepareActions(this.getActions()), prepareActions(this.getSecondaryActions()))();
Expand Down
12 changes: 1 addition & 11 deletions src/vs/workbench/parts/debug/browser/debugActions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ import EditorContextKeys = editorCommon.EditorContextKeys;
export class AbstractDebugAction extends Action {

protected toDispose: lifecycle.IDisposable[];
private keybinding: string;

constructor(
id: string, label: string, cssClass: string,
Expand All @@ -42,11 +41,6 @@ export class AbstractDebugAction extends Action {
this.toDispose = [];
this.toDispose.push(this.debugService.onDidChangeState((state) => this.updateEnablement(state)));

const keys = this.keybindingService.lookupKeybindings(id).map(k => this.keybindingService.getLabelFor(k));
if (keys && keys.length) {
this.keybinding = keys[0];
}

this.updateLabel(label);
this.updateEnablement(this.debugService.state);
}
Expand All @@ -56,11 +50,7 @@ export class AbstractDebugAction extends Action {
}

protected updateLabel(newLabel: string): void {
if (this.keybinding) {
this.label = nls.localize('debugActionLabelAndKeybinding', "{0} ({1})", newLabel, this.keybinding);
} else {
this.label = newLabel;
}
this.label = newLabel;
}

protected updateEnablement(state: debug.State): void {
Expand Down

0 comments on commit c9e82c9

Please sign in to comment.