mainThreadQuickOpen.ts 3.0 KB
Newer Older
1 2 3 4 5 6
/*---------------------------------------------------------------------------------------------
 *  Copyright (c) Microsoft Corporation. All rights reserved.
 *  Licensed under the MIT License. See License.txt in the project root for license information.
 *--------------------------------------------------------------------------------------------*/
'use strict';

J
Johannes Rieken 已提交
7 8 9
import { TPromise } from 'vs/base/common/winjs.base';
import { asWinJsPromise } from 'vs/base/common/async';
import { IThreadService } from 'vs/workbench/services/thread/common/threadService';
J
Johannes Rieken 已提交
10
import { IQuickOpenService, IPickOptions, IInputOptions } from 'vs/platform/quickOpen/common/quickOpen';
J
Johannes Rieken 已提交
11
import { InputBoxOptions } from 'vscode';
12
import { ExtHostContext, MainThreadQuickOpenShape, ExtHostQuickOpenShape, MyQuickPickItems } from '../node/extHost.protocol';
13

A
Alex Dima 已提交
14
export class MainThreadQuickOpen extends MainThreadQuickOpenShape {
15

16
	private _proxy: ExtHostQuickOpenShape;
17 18 19 20 21 22
	private _quickOpenService: IQuickOpenService;
	private _doSetItems: (items: MyQuickPickItems[]) => any;
	private _doSetError: (error: Error) => any;
	private _contents: TPromise<MyQuickPickItems[]>;
	private _token: number = 0;

A
Alex Dima 已提交
23 24 25 26 27
	constructor(
		@IThreadService threadService: IThreadService,
		@IQuickOpenService quickOpenService: IQuickOpenService
	) {
		super();
28 29 30 31 32 33 34 35
		this._proxy = threadService.get(ExtHostContext.ExtHostQuickOpen);
		this._quickOpenService = quickOpenService;
	}

	$show(options: IPickOptions): Thenable<number> {

		const myToken = ++this._token;

36
		this._contents = new TPromise<MyQuickPickItems[]>((c, e) => {
37 38 39 40 41 42 43 44 45 46 47 48 49
			this._doSetItems = (items) => {
				if (myToken === this._token) {
					c(items);
				}
			};

			this._doSetError = (error) => {
				if (myToken === this._token) {
					e(error);
				}
			};
		});

50
		return asWinJsPromise(token => this._quickOpenService.pick(this._contents, options, token)).then(item => {
51 52 53
			if (item) {
				return item.handle;
			}
M
Matt Bierner 已提交
54
			return undefined;
55 56 57 58 59 60 61 62 63 64 65
		}, undefined, progress => {
			if (progress) {
				this._proxy.$onItemSelected((<MyQuickPickItems>progress).handle);
			}
		});
	}

	$setItems(items: MyQuickPickItems[]): Thenable<any> {
		if (this._doSetItems) {
			this._doSetItems(items);
		}
M
Matt Bierner 已提交
66
		return undefined;
67 68 69 70 71 72
	}

	$setError(error: Error): Thenable<any> {
		if (this._doSetError) {
			this._doSetError(error);
		}
M
Matt Bierner 已提交
73
		return undefined;
74 75 76 77
	}

	// ---- input

78
	$input(options: InputBoxOptions, validateInput: boolean): TPromise<string> {
79 80 81 82 83 84

		const inputOptions: IInputOptions = Object.create(null);

		if (options) {
			inputOptions.password = options.password;
			inputOptions.placeHolder = options.placeHolder;
85
			inputOptions.valueSelection = options.valueSelection;
86 87
			inputOptions.prompt = options.prompt;
			inputOptions.value = options.value;
88
			inputOptions.ignoreFocusLost = options.ignoreFocusOut;
89 90 91 92 93 94 95 96
		}

		if (validateInput) {
			inputOptions.validateInput = (value) => {
				return this._proxy.$validateInput(value);
			};
		}

97
		return asWinJsPromise(token => this._quickOpenService.input(inputOptions, token));
98 99
	}
}