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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
|
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import { isSafari, setFullscreen } from 'vs/base/browser/browser';
import { addDisposableListener, addDisposableThrottledListener, detectFullscreen, EventHelper, EventType, windowOpenNoOpener, windowOpenPopup, windowOpenWithSuccess } from 'vs/base/browser/dom';
import { DomEmitter } from 'vs/base/browser/event';
import { timeout } from 'vs/base/common/async';
import { Event } from 'vs/base/common/event';
import { Disposable } from 'vs/base/common/lifecycle';
import { Schemas } from 'vs/base/common/network';
import { isIOS, isMacintosh } from 'vs/base/common/platform';
import Severity from 'vs/base/common/severity';
import { URI } from 'vs/base/common/uri';
import { localize } from 'vs/nls';
import { IDialogService } from 'vs/platform/dialogs/common/dialogs';
import { registerWindowDriver } from 'vs/platform/driver/browser/driver';
import { ILabelService } from 'vs/platform/label/common/label';
import { IOpenerService, matchesScheme } from 'vs/platform/opener/common/opener';
import { IProductService } from 'vs/platform/product/common/productService';
import { IBrowserWorkbenchEnvironmentService } from 'vs/workbench/services/environment/browser/environmentService';
import { IWorkbenchLayoutService } from 'vs/workbench/services/layout/browser/layoutService';
import { BrowserLifecycleService } from 'vs/workbench/services/lifecycle/browser/lifecycleService';
import { ILifecycleService } from 'vs/workbench/services/lifecycle/common/lifecycle';
export class BrowserWindow extends Disposable {
constructor(
@IOpenerService private readonly openerService: IOpenerService,
@ILifecycleService private readonly lifecycleService: BrowserLifecycleService,
@IDialogService private readonly dialogService: IDialogService,
@ILabelService private readonly labelService: ILabelService,
@IProductService private readonly productService: IProductService,
@IBrowserWorkbenchEnvironmentService private readonly environmentService: IBrowserWorkbenchEnvironmentService,
@IWorkbenchLayoutService private readonly layoutService: IWorkbenchLayoutService
) {
super();
this.registerListeners();
this.create();
}
private registerListeners(): void {
// Lifecycle
this._register(this.lifecycleService.onWillShutdown(() => this.onWillShutdown()));
// Layout
const viewport = isIOS && window.visualViewport ? window.visualViewport /** Visual viewport */ : window /** Layout viewport */;
this._register(addDisposableListener(viewport, EventType.RESIZE, () => {
this.layoutService.layout();
// Sometimes the keyboard appearing scrolls the whole workbench out of view, as a workaround scroll back into view #121206
if (isIOS) {
window.scrollTo(0, 0);
}
}));
// Prevent the back/forward gestures in macOS
this._register(addDisposableListener(this.layoutService.container, EventType.WHEEL, e => e.preventDefault(), { passive: false }));
// Prevent native context menus in web
this._register(addDisposableListener(this.layoutService.container, EventType.CONTEXT_MENU, e => EventHelper.stop(e, true)));
// Prevent default navigation on drop
this._register(addDisposableListener(this.layoutService.container, EventType.DROP, e => EventHelper.stop(e, true)));
// Fullscreen (Browser)
for (const event of [EventType.FULLSCREEN_CHANGE, EventType.WK_FULLSCREEN_CHANGE]) {
this._register(addDisposableListener(document, event, () => setFullscreen(!!detectFullscreen())));
}
// Fullscreen (Native)
this._register(addDisposableThrottledListener(viewport, EventType.RESIZE, () => {
setFullscreen(!!detectFullscreen());
}, undefined, isMacintosh ? 2000 /* adjust for macOS animation */ : 800 /* can be throttled */));
}
private onWillShutdown(): void {
// Try to detect some user interaction with the workbench
// when shutdown has happened to not show the dialog e.g.
// when navigation takes a longer time.
Event.toPromise(Event.any(
Event.once(new DomEmitter(document.body, EventType.KEY_DOWN, true).event),
Event.once(new DomEmitter(document.body, EventType.MOUSE_DOWN, true).event)
)).then(async () => {
// Delay the dialog in case the user interacted
// with the page before it transitioned away
await timeout(3000);
// This should normally not happen, but if for some reason
// the workbench was shutdown while the page is still there,
// inform the user that only a reload can bring back a working
// state.
const res = await this.dialogService.show(
Severity.Error,
localize('shutdownError', "An unexpected error occurred that requires a reload of this page."),
[
localize('reload', "Reload")
],
{
detail: localize('shutdownErrorDetail', "The workbench was unexpectedly disposed while running.")
}
);
if (res.choice === 0) {
window.location.reload(); // do not use any services at this point since they are likely not functional at this point
}
});
}
private create(): void {
// Handle open calls
this.setupOpenHandlers();
// Label formatting
this.registerLabelFormatters();
// Smoke Test Driver
this.setupDriver();
}
private setupDriver(): void {
if (this.environmentService.enableSmokeTestDriver) {
registerWindowDriver();
}
}
private setupOpenHandlers(): void {
// We need to ignore the `beforeunload` event while
// we handle external links to open specifically for
// the case of application protocols that e.g. invoke
// vscode itself. We do not want to open these links
// in a new window because that would leave a blank
// window to the user, but using `window.location.href`
// will trigger the `beforeunload`.
this.openerService.setDefaultExternalOpener({
openExternal: async (href: string) => {
let isAllowedOpener = false;
if (this.environmentService.options?.openerAllowedExternalUrlPrefixes) {
for (const trustedPopupPrefix of this.environmentService.options.openerAllowedExternalUrlPrefixes) {
if (href.startsWith(trustedPopupPrefix)) {
isAllowedOpener = true;
break;
}
}
}
// HTTP(s): open in new window and deal with potential popup blockers
if (matchesScheme(href, Schemas.http) || matchesScheme(href, Schemas.https)) {
if (isSafari) {
const opened = windowOpenWithSuccess(href, !isAllowedOpener);
if (!opened) {
const showResult = await this.dialogService.show(
Severity.Warning,
localize('unableToOpenExternal', "The browser interrupted the opening of a new tab or window. Press 'Open' to open it anyway."),
[
localize('open', "Open"),
localize('learnMore', "Learn More"),
localize('cancel', "Cancel")
],
{
cancelId: 2,
detail: href
}
);
if (showResult.choice === 0) {
isAllowedOpener
? windowOpenPopup(href)
: windowOpenNoOpener(href);
}
if (showResult.choice === 1) {
await this.openerService.open(URI.parse('https://aka.ms/allow-vscode-popup'));
}
}
} else {
isAllowedOpener
? windowOpenPopup(href)
: windowOpenNoOpener(href);
}
}
// Anything else: set location to trigger protocol handler in the browser
// but make sure to signal this as an expected unload and disable unload
// handling explicitly to prevent the workbench from going down.
else {
const invokeProtocolHandler = () => {
this.lifecycleService.withExpectedShutdown({ disableShutdownHandling: true }, () => window.location.href = href);
};
invokeProtocolHandler();
// We cannot know whether the protocol handler succeeded.
// Display guidance in case it did not, e.g. the app is not installed locally.
if (matchesScheme(href, this.productService.urlProtocol)) {
const showResult = await this.dialogService.show(
Severity.Info,
localize('openExternalDialogTitle', "All done. You can close this tab now."),
[
localize('openExternalDialogButtonRetry', "Try again"),
localize('openExternalDialogButtonInstall', "Install {0}", this.productService.nameLong),
localize('openExternalDialogButtonContinue', "Continue here")
],
{
cancelId: 2,
detail: localize('openExternalDialogDetail', "We tried opening {0} on your computer.", this.productService.nameLong)
},
);
if (showResult.choice === 0) {
invokeProtocolHandler();
} else if (showResult.choice === 1) {
await this.openerService.open(URI.parse(`http://aka.ms/vscode-install`));
}
}
}
return true;
}
});
}
private registerLabelFormatters() {
this._register(this.labelService.registerFormatter({
scheme: Schemas.vscodeUserData,
priority: true,
formatting: {
label: '(Settings) ${path}',
separator: '/',
}
}));
}
}
|