mainThreadQuickOpen.ts 3.5 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';
9
import { IPickOptions, IInputOptions, IQuickInputService } from 'vs/platform/quickinput/common/quickInput';
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
	private _quickInputService: IQuickInputService;
19 20 21 22 23
	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,
26
		@IQuickInputService quickInputService: IQuickInputService
A
Alex Dima 已提交
27
	) {
28
		this._proxy = extHostContext.getProxy(ExtHostContext.ExtHostQuickOpen);
29
		this._quickInputService = quickInputService;
30 31
	}

32 33 34
	public dispose(): void {
	}

C
Christof Marti 已提交
35
	$show(options: IPickOptions): TPromise<number | 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);
				}
			};
		});

C
Christof Marti 已提交
53
		if (options.canPickMany) {
54
			return asWinJsPromise(token => this._quickInputService.pick(this._contents, options as { canPickMany: true }, token)).then(items => {
C
Christof Marti 已提交
55 56 57 58 59 60 61 62 63 64
				if (items) {
					return items.map(item => item.handle);
				}
				return undefined;
			}, undefined, progress => {
				if (progress) {
					this._proxy.$onItemSelected((<MyQuickPickItems>progress).handle);
				}
			});
		} else {
65
			return asWinJsPromise(token => this._quickInputService.pick(this._contents, options, token)).then(item => {
C
Christof Marti 已提交
66 67 68 69 70 71 72 73 74 75
				if (item) {
					return item.handle;
				}
				return undefined;
			}, undefined, progress => {
				if (progress) {
					this._proxy.$onItemSelected((<MyQuickPickItems>progress).handle);
				}
			});
		}
76 77
	}

J
Johannes Rieken 已提交
78
	$setItems(items: MyQuickPickItems[]): TPromise<any> {
79 80 81
		if (this._doSetItems) {
			this._doSetItems(items);
		}
M
Matt Bierner 已提交
82
		return undefined;
83 84
	}

J
Johannes Rieken 已提交
85
	$setError(error: Error): TPromise<any> {
86 87 88
		if (this._doSetError) {
			this._doSetError(error);
		}
M
Matt Bierner 已提交
89
		return undefined;
90 91 92 93
	}

	// ---- input

94
	$input(options: InputBoxOptions, validateInput: boolean): TPromise<string> {
95 96 97 98 99 100

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

		if (options) {
			inputOptions.password = options.password;
			inputOptions.placeHolder = options.placeHolder;
101
			inputOptions.valueSelection = options.valueSelection;
102 103
			inputOptions.prompt = options.prompt;
			inputOptions.value = options.value;
104
			inputOptions.ignoreFocusLost = options.ignoreFocusOut;
105 106 107 108 109 110 111 112
		}

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

113
		return asWinJsPromise(token => this._quickInputService.input(inputOptions, token));
114 115
	}
}