Skip to content

Commit

Permalink
Add contextKey (#29096)
Browse files Browse the repository at this point in the history
  • Loading branch information
chrmarti committed Aug 8, 2018
1 parent dcd17d8 commit 0659543
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 1 deletion.
7 changes: 7 additions & 0 deletions src/vs/platform/quickinput/common/quickInput.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,11 @@ export interface IPickOptions<T extends IQuickPickItem> {
*/
canPickMany?: boolean;

/**
* a context key to set when this picker is active
*/
contextKey?: string;

/**
* an optional property for the item to focus initially.
*/
Expand Down Expand Up @@ -104,6 +109,8 @@ export interface IQuickInput {

enabled: boolean;

contextKey: string | undefined;

busy: boolean;

ignoreFocusOut: boolean;
Expand Down
49 changes: 48 additions & 1 deletion src/vs/workbench/browser/parts/quickinput/quickInput.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ interface QuickInputUI {
show(controller: QuickInput): void;
setVisibilities(visibilities: Visibilities): void;
setEnabled(enabled: boolean): void;
setContextKey(contextKey?: string): void;
hide(): void;
}

Expand All @@ -94,6 +95,7 @@ class QuickInput implements IQuickInput {
private _totalSteps: number;
protected visible = false;
private _enabled = true;
private _contextKey: string;
private _busy = false;
private _ignoreFocusOut = false;
private _buttons: IQuickInputButton[] = [];
Expand Down Expand Up @@ -148,6 +150,15 @@ class QuickInput implements IQuickInput {
this.update();
}

get contextKey() {
return this._contextKey;
}

set contextKey(contextKey: string) {
this._contextKey = contextKey;
this.update();
}

get busy() {
return this._busy;
}
Expand Down Expand Up @@ -249,6 +260,7 @@ class QuickInput implements IQuickInput {
}
this.ui.ignoreFocusOut = this.ignoreFocusOut;
this.ui.setEnabled(this.enabled);
this.ui.setContextKey(this.contextKey);
}

private getTitle() {
Expand Down Expand Up @@ -740,6 +752,7 @@ export class QuickInputService extends Component implements IQuickInputService {
private enabled = true;
private inQuickOpenWidgets: Record<string, boolean> = {};
private inQuickOpenContext: IContextKey<boolean>;
private contexts: { [id: string]: IContextKey<boolean>; } = Object.create(null);
private onDidAcceptEmitter = this._register(new Emitter<void>());
private onDidTriggerButtonEmitter = this._register(new Emitter<IQuickInputButton>());

Expand All @@ -753,7 +766,7 @@ export class QuickInputService extends Component implements IQuickInputService {
@IQuickOpenService private quickOpenService: IQuickOpenService,
@IEditorGroupsService private editorGroupService: IEditorGroupsService,
@IKeybindingService private keybindingService: IKeybindingService,
@IContextKeyService contextKeyService: IContextKeyService,
@IContextKeyService private contextKeyService: IContextKeyService,
@IThemeService themeService: IThemeService
) {
super(QuickInputService.ID, themeService);
Expand All @@ -779,6 +792,36 @@ export class QuickInputService extends Component implements IQuickInputService {
}
}

private setContextKey(id?: string) {
let key: IContextKey<boolean>;
if (id) {
key = this.contexts[id];
if (!key) {
key = new RawContextKey<boolean>(id, false)
.bindTo(this.contextKeyService);
this.contexts[id] = key;
}
}

if (key && key.get()) {
return; // already active context
}

this.resetContextKeys();

if (key) {
key.set(true);
}
}

private resetContextKeys() {
for (const key in this.contexts) {
if (this.contexts[key].get()) {
this.contexts[key].reset();
}
}
}

private create() {
if (this.ui) {
return;
Expand Down Expand Up @@ -923,6 +966,7 @@ export class QuickInputService extends Component implements IQuickInputService {
hide: () => this.hide(),
setVisibilities: visibilities => this.setVisibilities(visibilities),
setEnabled: enabled => this.setEnabled(enabled),
setContextKey: contextKey => this.setContextKey(contextKey),
};
this.updateStyles();
}
Expand Down Expand Up @@ -976,6 +1020,7 @@ export class QuickInputService extends Component implements IQuickInputService {
input.ignoreFocusOut = options.ignoreFocusLost;
input.matchOnDescription = options.matchOnDescription;
input.matchOnDetail = options.matchOnDetail;
input.contextKey = options.contextKey;
input.busy = true;
TPromise.join([picks, options.activeItem])
.then(([items, activeItem]) => {
Expand Down Expand Up @@ -1096,6 +1141,7 @@ export class QuickInputService extends Component implements IQuickInputService {
backButton.tooltip = keybinding ? localize('quickInput.backWithKeybinding', "Back ({0})", keybinding.getLabel()) : localize('quickInput.back', "Back");

this.inQuickOpen('quickInput', true);
this.resetContextKeys();

this.ui.container.style.display = '';
this.updateLayout();
Expand Down Expand Up @@ -1136,6 +1182,7 @@ export class QuickInputService extends Component implements IQuickInputService {
if (controller) {
this.controller = null;
this.inQuickOpen('quickInput', false);
this.resetContextKeys();
this.ui.container.style.display = 'none';
if (!focusLost) {
this.editorGroupService.activeGroup.focus();
Expand Down

0 comments on commit 0659543

Please sign in to comment.