mainThreadQuickOpen.ts 3.1 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
import { TPromise } from 'vs/base/common/winjs.base';
import { asWinJsPromise } from 'vs/base/common/async';
J
Johannes Rieken 已提交
9
import { IQuickOpenService, IPickOptions, IInputOptions } from 'vs/platform/quickOpen/common/quickOpen';
J
Johannes Rieken 已提交
10
import { InputBoxOptions } from 'vscode';
11
import { ExtHostContext, MainThreadQuickOpenShape, ExtHostQuickOpenShape, MyQuickPickItems, MainContext, IExtHostContext } from '../node/extHost.protocol';
12
import { extHostNamedCustomer } from 'vs/workbench/api/electron-browser/extHostCustomers';
13

14
@extHostNamedCustomer(MainContext.MainThreadQuickOpen)
15
export class MainThreadQuickOpen implements MainThreadQuickOpenShape {
16

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

A
Alex Dima 已提交
24
	constructor(
25
		extHostContext: IExtHostContext,
A
Alex Dima 已提交
26 27
		@IQuickOpenService quickOpenService: IQuickOpenService
	) {
28
		this._proxy = extHostContext.get(ExtHostContext.ExtHostQuickOpen);
29 30 31
		this._quickOpenService = quickOpenService;
	}

32 33 34
	public dispose(): void {
	}

J
Johannes Rieken 已提交
35
	$show(options: IPickOptions): TPromise<number> {
36 37 38

		const myToken = ++this._token;

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

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

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

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

J
Johannes Rieken 已提交
72
	$setError(error: Error): TPromise<any> {
73 74 75
		if (this._doSetError) {
			this._doSetError(error);
		}
M
Matt Bierner 已提交
76
		return undefined;
77 78 79 80
	}

	// ---- input

81
	$input(options: InputBoxOptions, validateInput: boolean): TPromise<string> {
82 83 84 85 86 87

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

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

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

100
		return asWinJsPromise(token => this._quickOpenService.input(inputOptions, token));
101 102
	}
}