-
Notifications
You must be signed in to change notification settings - Fork 28k
/
extHostWindow.ts
67 lines (55 loc) · 2.48 KB
/
extHostWindow.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import { Event, Emitter } from 'vs/base/common/event';
import { ExtHostWindowShape, MainContext, MainThreadWindowShape, IMainContext, IOpenUriOptions } from './extHost.protocol';
import { WindowState } from 'vscode';
import { URI } from 'vs/base/common/uri';
import { Schemas } from 'vs/base/common/network';
import { isFalsyOrWhitespace } from 'vs/base/common/strings';
export class ExtHostWindow implements ExtHostWindowShape {
private static InitialState: WindowState = {
focused: true
};
private _proxy: MainThreadWindowShape;
private _onDidChangeWindowState = new Emitter<WindowState>();
readonly onDidChangeWindowState: Event<WindowState> = this._onDidChangeWindowState.event;
private _state = ExtHostWindow.InitialState;
get state(): WindowState { return this._state; }
constructor(mainContext: IMainContext) {
this._proxy = mainContext.getProxy(MainContext.MainThreadWindow);
this._proxy.$getWindowVisibility().then(isFocused => this.$onDidChangeWindowFocus(isFocused));
}
$onDidChangeWindowFocus(focused: boolean): void {
if (focused === this._state.focused) {
return;
}
this._state = { ...this._state, focused };
this._onDidChangeWindowState.fire(this._state);
}
openUri(stringOrUri: string | URI, options: IOpenUriOptions): Promise<boolean> {
if (typeof stringOrUri === 'string') {
try {
stringOrUri = URI.parse(stringOrUri);
} catch (e) {
return Promise.reject(`Invalid uri - '${stringOrUri}'`);
}
}
if (isFalsyOrWhitespace(stringOrUri.scheme)) {
return Promise.reject('Invalid scheme - cannot be empty');
} else if (stringOrUri.scheme === Schemas.command) {
return Promise.reject(`Invalid scheme '${stringOrUri.scheme}'`);
}
return this._proxy.$openUri(stringOrUri, options);
}
async resolveExternalUri(uri: URI, options: IOpenUriOptions): Promise<URI> {
if (isFalsyOrWhitespace(uri.scheme)) {
return Promise.reject('Invalid scheme - cannot be empty');
} else if (!new Set([Schemas.http, Schemas.https]).has(uri.scheme)) {
return Promise.reject(`Invalid scheme '${uri.scheme}'`);
}
const result = await this._proxy.$resolveExternalUri(uri, options);
return URI.from(result);
}
}