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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
|
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import { localize } from 'vs/nls';
import { URI } from 'vs/base/common/uri';
import { onUnexpectedError } from 'vs/base/common/errors';
import { equals } from 'vs/base/common/objects';
import { EventType, EventHelper, addDisposableListener, scheduleAtNextAnimationFrame, ModifierKeyEmitter } from 'vs/base/browser/dom';
import { Separator, WorkbenchActionExecutedClassification, WorkbenchActionExecutedEvent } from 'vs/base/common/actions';
import { IFileService } from 'vs/platform/files/common/files';
import { EditorResourceAccessor, IUntitledTextResourceEditorInput, SideBySideEditor, pathsToEditors, IResourceDiffEditorInput, IUntypedEditorInput } from 'vs/workbench/common/editor';
import { IEditorService } from 'vs/workbench/services/editor/common/editorService';
import { ITelemetryService } from 'vs/platform/telemetry/common/telemetry';
import { WindowMinimumSize, IOpenFileRequest, IWindowsConfiguration, getTitleBarStyle, IAddFoldersRequest, INativeRunActionInWindowRequest, INativeRunKeybindingInWindowRequest, INativeOpenFileRequest } from 'vs/platform/window/common/window';
import { ITitleService } from 'vs/workbench/services/title/common/titleService';
import { IWorkbenchThemeService } from 'vs/workbench/services/themes/common/workbenchThemeService';
import { applyZoom } from 'vs/platform/window/electron-sandbox/window';
import { setFullscreen, getZoomLevel } from 'vs/base/browser/browser';
import { ICommandService, CommandsRegistry } from 'vs/platform/commands/common/commands';
import { IResourceEditorInput } from 'vs/platform/editor/common/editor';
import { ipcRenderer } from 'vs/base/parts/sandbox/electron-sandbox/globals';
import { IWorkspaceEditingService } from 'vs/workbench/services/workspaces/common/workspaceEditing';
import { IMenuService, MenuId, IMenu, MenuItemAction, MenuRegistry } from 'vs/platform/actions/common/actions';
import { ICommandAction } from 'vs/platform/action/common/action';
import { createAndFillInActionBarActions } from 'vs/platform/actions/browser/menuEntryActionViewItem';
import { RunOnceScheduler } from 'vs/base/common/async';
import { Disposable, DisposableStore } from 'vs/base/common/lifecycle';
import { LifecyclePhase, ILifecycleService, WillShutdownEvent, ShutdownReason, BeforeShutdownErrorEvent, BeforeShutdownEvent } from 'vs/workbench/services/lifecycle/common/lifecycle';
import { IWorkspaceFolderCreationData } from 'vs/platform/workspaces/common/workspaces';
import { IIntegrityService } from 'vs/workbench/services/integrity/common/integrity';
import { isWindows, isMacintosh, isCI } from 'vs/base/common/platform';
import { IProductService } from 'vs/platform/product/common/productService';
import { INotificationService, Severity } from 'vs/platform/notification/common/notification';
import { IKeybindingService } from 'vs/platform/keybinding/common/keybinding';
import { INativeWorkbenchEnvironmentService } from 'vs/workbench/services/environment/electron-sandbox/environmentService';
import { IAccessibilityService, AccessibilitySupport } from 'vs/platform/accessibility/common/accessibility';
import { WorkbenchState, IWorkspaceContextService } from 'vs/platform/workspace/common/workspace';
import { coalesce } from 'vs/base/common/arrays';
import { IConfigurationService } from 'vs/platform/configuration/common/configuration';
import { IStorageService, StorageScope, StorageTarget } from 'vs/platform/storage/common/storage';
import { assertIsDefined, isArray } from 'vs/base/common/types';
import { IOpenerService, OpenOptions } from 'vs/platform/opener/common/opener';
import { Schemas } from 'vs/base/common/network';
import { INativeHostService } from 'vs/platform/native/electron-sandbox/native';
import { posix } from 'vs/base/common/path';
import { ITunnelService, extractLocalHostUriMetaDataForPortMapping } from 'vs/platform/tunnel/common/tunnel';
import { IWorkbenchLayoutService, Parts, positionFromString, Position } from 'vs/workbench/services/layout/browser/layoutService';
import { IWorkingCopyService } from 'vs/workbench/services/workingCopy/common/workingCopyService';
import { WorkingCopyCapabilities } from 'vs/workbench/services/workingCopy/common/workingCopy';
import { AutoSaveMode, IFilesConfigurationService } from 'vs/workbench/services/filesConfiguration/common/filesConfigurationService';
import { Event } from 'vs/base/common/event';
import { IRemoteAuthorityResolverService } from 'vs/platform/remote/common/remoteAuthorityResolver';
import { IAddressProvider, IAddress } from 'vs/platform/remote/common/remoteAgentConnection';
import { IEditorGroupsService } from 'vs/workbench/services/editor/common/editorGroupsService';
import { IDialogService } from 'vs/platform/dialogs/common/dialogs';
import { AuthInfo } from 'vs/base/parts/sandbox/electron-sandbox/electronTypes';
import { ILogService } from 'vs/platform/log/common/log';
import { IInstantiationService, ServicesAccessor } from 'vs/platform/instantiation/common/instantiation';
import { whenEditorClosed } from 'vs/workbench/browser/editor';
import { ISharedProcessService } from 'vs/platform/ipc/electron-sandbox/services';
import { IProgressService, ProgressLocation } from 'vs/platform/progress/common/progress';
import { toErrorMessage } from 'vs/base/common/errorMessage';
import { registerWindowDriver } from 'vs/platform/driver/electron-sandbox/driver';
import { ILabelService } from 'vs/platform/label/common/label';
import { dirname } from 'vs/base/common/resources';
export class NativeWindow extends Disposable {
private static REMEMBER_PROXY_CREDENTIALS_KEY = 'window.rememberProxyCredentials';
private touchBarMenu: IMenu | undefined;
private readonly touchBarDisposables = this._register(new DisposableStore());
private lastInstalledTouchedBar: ICommandAction[][] | undefined;
private readonly customTitleContextMenuDisposable = this._register(new DisposableStore());
private previousConfiguredZoomLevel: number | undefined;
private readonly addFoldersScheduler = this._register(new RunOnceScheduler(() => this.doAddFolders(), 100));
private pendingFoldersToAdd: URI[] = [];
private readonly closeEmptyWindowScheduler = this._register(new RunOnceScheduler(() => this.onDidAllEditorsClose(), 50));
private isDocumentedEdited = false;
constructor(
@IEditorService private readonly editorService: IEditorService,
@IEditorGroupsService private readonly editorGroupService: IEditorGroupsService,
@IConfigurationService private readonly configurationService: IConfigurationService,
@ITitleService private readonly titleService: ITitleService,
@IWorkbenchThemeService protected themeService: IWorkbenchThemeService,
@INotificationService private readonly notificationService: INotificationService,
@ICommandService private readonly commandService: ICommandService,
@IKeybindingService private readonly keybindingService: IKeybindingService,
@ITelemetryService private readonly telemetryService: ITelemetryService,
@IWorkspaceEditingService private readonly workspaceEditingService: IWorkspaceEditingService,
@IFileService private readonly fileService: IFileService,
@IMenuService private readonly menuService: IMenuService,
@ILifecycleService private readonly lifecycleService: ILifecycleService,
@IIntegrityService private readonly integrityService: IIntegrityService,
@INativeWorkbenchEnvironmentService private readonly environmentService: INativeWorkbenchEnvironmentService,
@IAccessibilityService private readonly accessibilityService: IAccessibilityService,
@IWorkspaceContextService private readonly contextService: IWorkspaceContextService,
@IOpenerService private readonly openerService: IOpenerService,
@INativeHostService private readonly nativeHostService: INativeHostService,
@ITunnelService private readonly tunnelService: ITunnelService,
@IWorkbenchLayoutService private readonly layoutService: IWorkbenchLayoutService,
@IWorkingCopyService private readonly workingCopyService: IWorkingCopyService,
@IFilesConfigurationService private readonly filesConfigurationService: IFilesConfigurationService,
@IProductService private readonly productService: IProductService,
@IRemoteAuthorityResolverService private readonly remoteAuthorityResolverService: IRemoteAuthorityResolverService,
@IDialogService private readonly dialogService: IDialogService,
@IStorageService private readonly storageService: IStorageService,
@ILogService private readonly logService: ILogService,
@IInstantiationService private readonly instantiationService: IInstantiationService,
@ISharedProcessService private readonly sharedProcessService: ISharedProcessService,
@IProgressService private readonly progressService: IProgressService,
@ILabelService private readonly labelService: ILabelService
) {
super();
this.registerListeners();
this.create();
}
private registerListeners(): void {
// Layout
this._register(addDisposableListener(window, EventType.RESIZE, e => this.onWindowResize(e, true)));
// React to editor input changes
this._register(this.editorService.onDidActiveEditorChange(() => this.updateTouchbarMenu()));
// prevent opening a real URL inside the window
for (const event of [EventType.DRAG_OVER, EventType.DROP]) {
window.document.body.addEventListener(event, (e: DragEvent) => {
EventHelper.stop(e);
});
}
// Support runAction event
ipcRenderer.on('vscode:runAction', async (event: unknown, request: INativeRunActionInWindowRequest) => {
const args: unknown[] = request.args || [];
// If we run an action from the touchbar, we fill in the currently active resource
// as payload because the touch bar items are context aware depending on the editor
if (request.from === 'touchbar') {
const activeEditor = this.editorService.activeEditor;
if (activeEditor) {
const resource = EditorResourceAccessor.getOriginalUri(activeEditor, { supportSideBySide: SideBySideEditor.PRIMARY });
if (resource) {
args.push(resource);
}
}
} else {
args.push({ from: request.from });
}
try {
await this.commandService.executeCommand(request.id, ...args);
this.telemetryService.publicLog2<WorkbenchActionExecutedEvent, WorkbenchActionExecutedClassification>('workbenchActionExecuted', { id: request.id, from: request.from });
} catch (error) {
this.notificationService.error(error);
}
});
// Support runKeybinding event
ipcRenderer.on('vscode:runKeybinding', (event: unknown, request: INativeRunKeybindingInWindowRequest) => {
if (document.activeElement) {
this.keybindingService.dispatchByUserSettingsLabel(request.userSettingsLabel, document.activeElement);
}
});
// Error reporting from main
ipcRenderer.on('vscode:reportError', (event: unknown, error: string) => {
if (error) {
onUnexpectedError(JSON.parse(error));
}
});
// Support openFiles event for existing and new files
ipcRenderer.on('vscode:openFiles', (event: unknown, request: IOpenFileRequest) => this.onOpenFiles(request));
// Support addFolders event if we have a workspace opened
ipcRenderer.on('vscode:addFolders', (event: unknown, request: IAddFoldersRequest) => this.onAddFoldersRequest(request));
// Message support
ipcRenderer.on('vscode:showInfoMessage', (event: unknown, message: string) => this.notificationService.info(message));
// Shell Environment Issue Notifications
ipcRenderer.on('vscode:showResolveShellEnvError', (event: unknown, message: string) => this.notificationService.prompt(
Severity.Error,
message,
[{
label: localize('learnMore', "Learn More"),
run: () => this.openerService.open('https://go.microsoft.com/fwlink/?linkid=2149667')
}]
));
ipcRenderer.on('vscode:showCredentialsError', (event: unknown, message: string) => this.notificationService.prompt(
Severity.Error,
localize('keychainWriteError', "Writing login information to the keychain failed with error '{0}'.", message),
[{
label: localize('troubleshooting', "Troubleshooting Guide"),
run: () => this.openerService.open('https://go.microsoft.com/fwlink/?linkid=2190713')
}]
));
// Fullscreen Events
ipcRenderer.on('vscode:enterFullScreen', async () => setFullscreen(true));
ipcRenderer.on('vscode:leaveFullScreen', async () => setFullscreen(false));
// Proxy Login Dialog
ipcRenderer.on('vscode:openProxyAuthenticationDialog', async (event: unknown, payload: { authInfo: AuthInfo; username?: string; password?: string; replyChannel: string }) => {
const rememberCredentials = this.storageService.getBoolean(NativeWindow.REMEMBER_PROXY_CREDENTIALS_KEY, StorageScope.GLOBAL);
const result = await this.dialogService.input(Severity.Warning, localize('proxyAuthRequired', "Proxy Authentication Required"),
[
localize({ key: 'loginButton', comment: ['&& denotes a mnemonic'] }, "&&Log In"),
localize({ key: 'cancelButton', comment: ['&& denotes a mnemonic'] }, "&&Cancel")
],
[
{ placeholder: localize('username', "Username"), value: payload.username },
{ placeholder: localize('password', "Password"), type: 'password', value: payload.password }
],
{
cancelId: 1,
detail: localize('proxyDetail', "The proxy {0} requires a username and password.", `${payload.authInfo.host}:${payload.authInfo.port}`),
checkbox: {
label: localize('rememberCredentials', "Remember my credentials"),
checked: rememberCredentials
}
});
// Reply back to the channel without result to indicate
// that the login dialog was cancelled
if (result.choice !== 0 || !result.values) {
ipcRenderer.send(payload.replyChannel);
}
// Other reply back with the picked credentials
else {
// Update state based on checkbox
if (result.checkboxChecked) {
this.storageService.store(NativeWindow.REMEMBER_PROXY_CREDENTIALS_KEY, true, StorageScope.GLOBAL, StorageTarget.MACHINE);
} else {
this.storageService.remove(NativeWindow.REMEMBER_PROXY_CREDENTIALS_KEY, StorageScope.GLOBAL);
}
// Reply back to main side with credentials
const [username, password] = result.values;
ipcRenderer.send(payload.replyChannel, { username, password, remember: !!result.checkboxChecked });
}
});
// Accessibility support changed event
ipcRenderer.on('vscode:accessibilitySupportChanged', (event: unknown, accessibilitySupportEnabled: boolean) => {
this.accessibilityService.setAccessibilitySupport(accessibilitySupportEnabled ? AccessibilitySupport.Enabled : AccessibilitySupport.Disabled);
});
// Zoom level changes
this.updateWindowZoomLevel();
this._register(this.configurationService.onDidChangeConfiguration(e => {
if (e.affectsConfiguration('window.zoomLevel')) {
this.updateWindowZoomLevel();
} else if (e.affectsConfiguration('keyboard.touchbar.enabled') || e.affectsConfiguration('keyboard.touchbar.ignored')) {
this.updateTouchbarMenu();
}
}));
// Listen to visible editor changes
this._register(this.editorService.onDidVisibleEditorsChange(() => this.onDidChangeVisibleEditors()));
// Listen to editor closing (if we run with --wait)
const filesToWait = this.environmentService.filesToWait;
if (filesToWait) {
this.trackClosedWaitFiles(filesToWait.waitMarkerFileUri, coalesce(filesToWait.paths.map(path => path.fileUri)));
}
// macOS OS integration
if (isMacintosh) {
this._register(this.editorService.onDidActiveEditorChange(() => {
const file = EditorResourceAccessor.getOriginalUri(this.editorService.activeEditor, { supportSideBySide: SideBySideEditor.PRIMARY, filterByScheme: Schemas.file });
// Represented Filename
this.updateRepresentedFilename(file?.fsPath);
// Custom title menu
this.provideCustomTitleContextMenu(file?.fsPath);
}));
}
// Maximize/Restore on doubleclick (for macOS custom title)
if (isMacintosh && getTitleBarStyle(this.configurationService) === 'custom') {
const titlePart = assertIsDefined(this.layoutService.getContainer(Parts.TITLEBAR_PART));
this._register(addDisposableListener(titlePart, EventType.DBLCLICK, e => {
EventHelper.stop(e);
this.nativeHostService.handleTitleDoubleClick();
}));
}
// Document edited: indicate for dirty working copies
this._register(this.workingCopyService.onDidChangeDirty(workingCopy => {
const gotDirty = workingCopy.isDirty();
if (gotDirty && !(workingCopy.capabilities & WorkingCopyCapabilities.Untitled) && this.filesConfigurationService.getAutoSaveMode() === AutoSaveMode.AFTER_SHORT_DELAY) {
return; // do not indicate dirty of working copies that are auto saved after short delay
}
this.updateDocumentEdited(gotDirty);
}));
this.updateDocumentEdited();
// Detect minimize / maximize
this._register(Event.any(
Event.map(Event.filter(this.nativeHostService.onDidMaximizeWindow, id => id === this.nativeHostService.windowId), () => true),
Event.map(Event.filter(this.nativeHostService.onDidUnmaximizeWindow, id => id === this.nativeHostService.windowId), () => false)
)(e => this.onDidChangeWindowMaximized(e)));
this.onDidChangeWindowMaximized(this.environmentService.window.maximized ?? false);
// Detect panel position to determine minimum width
this._register(this.layoutService.onDidChangePanelPosition(pos => this.onDidChangePanelPosition(positionFromString(pos))));
this.onDidChangePanelPosition(this.layoutService.getPanelPosition());
// Lifecycle
this._register(this.lifecycleService.onBeforeShutdown(e => this.onBeforeShutdown(e)));
this._register(this.lifecycleService.onBeforeShutdownError(e => this.onBeforeShutdownError(e)));
this._register(this.lifecycleService.onWillShutdown(e => this.onWillShutdown(e)));
}
private onBeforeShutdown({ veto, reason }: BeforeShutdownEvent): void {
if (reason === ShutdownReason.CLOSE) {
const confirmBeforeCloseSetting = this.configurationService.getValue<'always' | 'never' | 'keyboardOnly'>('window.confirmBeforeClose');
const confirmBeforeClose = confirmBeforeCloseSetting === 'always' || (confirmBeforeCloseSetting === 'keyboardOnly' && ModifierKeyEmitter.getInstance().isModifierPressed);
if (confirmBeforeClose) {
// When we need to confirm on close or quit, veto the shutdown
// with a long running promise to figure out whether shutdown
// can proceed or not.
return veto((async () => {
let actualReason: ShutdownReason = reason;
if (reason === ShutdownReason.CLOSE && !isMacintosh) {
const windowCount = await this.nativeHostService.getWindowCount();
if (windowCount === 1) {
actualReason = ShutdownReason.QUIT; // Windows/Linux: closing last window means to QUIT
}
}
let confirmed = true;
if (confirmBeforeClose) {
confirmed = await this.instantiationService.invokeFunction(accessor => NativeWindow.confirmOnShutdown(accessor, actualReason));
}
// Progress for long running shutdown
if (confirmed) {
this.progressOnBeforeShutdown(reason);
}
return !confirmed;
})(), 'veto.confirmBeforeClose');
}
}
// Progress for long running shutdown
this.progressOnBeforeShutdown(reason);
}
private progressOnBeforeShutdown(reason: ShutdownReason): void {
this.progressService.withProgress({
location: ProgressLocation.Window, // use window progress to not be too annoying about this operation
delay: 800, // delay so that it only appears when operation takes a long time
title: this.toShutdownLabel(reason, false),
}, () => {
return Event.toPromise(Event.any(
this.lifecycleService.onWillShutdown, // dismiss this dialog when we shutdown
this.lifecycleService.onShutdownVeto, // or when shutdown was vetoed
this.dialogService.onWillShowDialog // or when a dialog asks for input
));
});
}
static async confirmOnShutdown(accessor: ServicesAccessor, reason: ShutdownReason): Promise<boolean> {
const dialogService = accessor.get(IDialogService);
const configurationService = accessor.get(IConfigurationService);
const message = reason === ShutdownReason.QUIT ?
(isMacintosh ? localize('quitMessageMac', "Are you sure you want to quit?") : localize('quitMessage', "Are you sure you want to exit?")) :
localize('closeWindowMessage', "Are you sure you want to close the window?");
const primaryButton = reason === ShutdownReason.QUIT ?
(isMacintosh ? localize({ key: 'quitButtonLabel', comment: ['&& denotes a mnemonic'] }, "&&Quit") : localize({ key: 'exitButtonLabel', comment: ['&& denotes a mnemonic'] }, "&&Exit")) :
localize({ key: 'closeWindowButtonLabel', comment: ['&& denotes a mnemonic'] }, "&&Close Window");
const res = await dialogService.confirm({
type: 'question',
message,
primaryButton,
checkbox: {
label: localize('doNotAskAgain', "Do not ask me again")
}
});
// Update setting if checkbox checked
if (res.checkboxChecked) {
await configurationService.updateValue('window.confirmBeforeClose', 'never');
}
return res.confirmed;
}
private onBeforeShutdownError({ error, reason }: BeforeShutdownErrorEvent): void {
this.dialogService.show(Severity.Error, this.toShutdownLabel(reason, true), undefined, {
detail: localize('shutdownErrorDetail', "Error: {0}", toErrorMessage(error))
});
}
private onWillShutdown({ reason, force, joiners }: WillShutdownEvent): void {
// Delay so that the dialog only appears after timeout
const shutdownDialogScheduler = new RunOnceScheduler(() => {
const pendingJoiners = joiners();
this.progressService.withProgress({
location: ProgressLocation.Dialog, // use a dialog to prevent the user from making any more interactions now
buttons: [this.toForceShutdownLabel(reason)], // allow to force shutdown anyway
cancellable: false, // do not allow to cancel
sticky: true, // do not allow to dismiss
title: this.toShutdownLabel(reason, false),
detail: pendingJoiners.length > 0 ? localize('willShutdownDetail', "The following operations are still running: \n{0}", pendingJoiners.map(joiner => `- ${joiner.label}`).join('\n')) : undefined
}, () => {
return Event.toPromise(this.lifecycleService.onDidShutdown); // dismiss this dialog when we actually shutdown
}, () => {
force();
});
}, 1200);
shutdownDialogScheduler.schedule();
// Dispose scheduler when we actually shutdown
Event.once(this.lifecycleService.onDidShutdown)(() => shutdownDialogScheduler.dispose());
}
private toShutdownLabel(reason: ShutdownReason, isError: boolean): string {
if (isError) {
switch (reason) {
case ShutdownReason.CLOSE:
return localize('shutdownErrorClose', "An unexpected error prevented the window to close");
case ShutdownReason.QUIT:
return localize('shutdownErrorQuit', "An unexpected error prevented the application to quit");
case ShutdownReason.RELOAD:
return localize('shutdownErrorReload', "An unexpected error prevented the window to reload");
case ShutdownReason.LOAD:
return localize('shutdownErrorLoad', "An unexpected error prevented to change the workspace");
}
}
switch (reason) {
case ShutdownReason.CLOSE:
return localize('shutdownTitleClose', "Closing the window is taking a bit longer...");
case ShutdownReason.QUIT:
return localize('shutdownTitleQuit', "Quitting the application is taking a bit longer...");
case ShutdownReason.RELOAD:
return localize('shutdownTitleReload', "Reloading the window is taking a bit longer...");
case ShutdownReason.LOAD:
return localize('shutdownTitleLoad', "Changing the workspace is taking a bit longer...");
}
}
private toForceShutdownLabel(reason: ShutdownReason): string {
switch (reason) {
case ShutdownReason.CLOSE:
return localize('shutdownForceClose', "Close Anyway");
case ShutdownReason.QUIT:
return localize('shutdownForceQuit', "Quit Anyway");
case ShutdownReason.RELOAD:
return localize('shutdownForceReload', "Reload Anyway");
case ShutdownReason.LOAD:
return localize('shutdownForceLoad', "Change Anyway");
}
}
private onWindowResize(e: UIEvent, retry: boolean): void {
if (e.target === window) {
if (window.document && window.document.body && window.document.body.clientWidth === 0) {
// TODO@electron this is an electron issue on macOS when simple fullscreen is enabled
// where for some reason the window clientWidth is reported as 0 when switching
// between simple fullscreen and normal screen. In that case we schedule the layout
// call at the next animation frame once, in the hope that the dimensions are
// proper then.
if (retry) {
scheduleAtNextAnimationFrame(() => this.onWindowResize(e, false));
}
return;
}
this.layoutService.layout();
}
}
private updateDocumentEdited(isDirty = this.workingCopyService.hasDirty): void {
if ((!this.isDocumentedEdited && isDirty) || (this.isDocumentedEdited && !isDirty)) {
this.isDocumentedEdited = isDirty;
this.nativeHostService.setDocumentEdited(isDirty);
}
}
private onDidChangeWindowMaximized(maximized: boolean): void {
this.layoutService.updateWindowMaximizedState(maximized);
}
private getWindowMinimumWidth(panelPosition: Position = this.layoutService.getPanelPosition()): number {
// if panel is on the side, then return the larger minwidth
const panelOnSide = panelPosition === Position.LEFT || panelPosition === Position.RIGHT;
if (panelOnSide) {
return WindowMinimumSize.WIDTH_WITH_VERTICAL_PANEL;
}
return WindowMinimumSize.WIDTH;
}
private onDidChangePanelPosition(pos: Position): void {
const minWidth = this.getWindowMinimumWidth(pos);
this.nativeHostService.setMinimumSize(minWidth, undefined);
}
private onDidChangeVisibleEditors(): void {
// Close when empty: check if we should close the window based on the setting
// Overruled by: window has a workspace opened or this window is for extension development
// or setting is disabled. Also enabled when running with --wait from the command line.
const visibleEditorPanes = this.editorService.visibleEditorPanes;
if (visibleEditorPanes.length === 0 && this.contextService.getWorkbenchState() === WorkbenchState.EMPTY && !this.environmentService.isExtensionDevelopment) {
const closeWhenEmpty = this.configurationService.getValue('window.closeWhenEmpty');
if (closeWhenEmpty || this.environmentService.args.wait) {
this.closeEmptyWindowScheduler.schedule();
}
}
}
private onDidAllEditorsClose(): void {
const visibleEditorPanes = this.editorService.visibleEditorPanes.length;
if (visibleEditorPanes === 0) {
this.nativeHostService.closeWindow();
}
}
private updateWindowZoomLevel(): void {
const windowConfig = this.configurationService.getValue<IWindowsConfiguration>();
let configuredZoomLevel = 0;
if (windowConfig.window && typeof windowConfig.window.zoomLevel === 'number') {
configuredZoomLevel = windowConfig.window.zoomLevel;
// Leave early if the configured zoom level did not change (https://github.com/microsoft/vscode/issues/1536)
if (this.previousConfiguredZoomLevel === configuredZoomLevel) {
return;
}
this.previousConfiguredZoomLevel = configuredZoomLevel;
}
if (getZoomLevel() !== configuredZoomLevel) {
applyZoom(configuredZoomLevel);
}
}
private updateRepresentedFilename(filePath: string | undefined): void {
this.nativeHostService.setRepresentedFilename(filePath ? filePath : '');
}
private provideCustomTitleContextMenu(filePath: string | undefined): void {
// Clear old menu
this.customTitleContextMenuDisposable.clear();
// Provide new menu if a file is opened and we are on a custom title
if (!filePath || getTitleBarStyle(this.configurationService) !== 'custom') {
return;
}
// Split up filepath into segments
const segments = filePath.split(posix.sep);
for (let i = segments.length; i > 0; i--) {
const isFile = (i === segments.length);
let pathOffset = i;
if (!isFile) {
pathOffset++; // for segments which are not the file name we want to open the folder
}
const path = URI.file(segments.slice(0, pathOffset).join(posix.sep));
let label: string;
if (!isFile) {
label = this.labelService.getUriBasenameLabel(dirname(path));
} else {
label = this.labelService.getUriBasenameLabel(path);
}
const commandId = `workbench.action.revealPathInFinder${i}`;
this.customTitleContextMenuDisposable.add(CommandsRegistry.registerCommand(commandId, () => this.nativeHostService.showItemInFolder(path.fsPath)));
this.customTitleContextMenuDisposable.add(MenuRegistry.appendMenuItem(MenuId.TitleBarContext, { command: { id: commandId, title: label || posix.sep }, order: -i }));
}
}
private create(): void {
// Handle open calls
this.setupOpenHandlers();
// Notify some services about lifecycle phases
this.lifecycleService.when(LifecyclePhase.Ready).then(() => this.nativeHostService.notifyReady());
this.lifecycleService.when(LifecyclePhase.Restored).then(() => this.sharedProcessService.notifyRestored());
// Integrity warning
this.integrityService.isPure().then(({ isPure }) => this.titleService.updateProperties({ isPure }));
// Root warning
this.lifecycleService.when(LifecyclePhase.Restored).then(async () => {
const isAdmin = await this.nativeHostService.isAdmin();
// Update title
this.titleService.updateProperties({ isAdmin });
// Show warning message (unix only)
if (isAdmin && !isWindows) {
this.notificationService.warn(localize('runningAsRoot', "It is not recommended to run {0} as root user.", this.productService.nameShort));
}
});
// Touchbar menu (if enabled)
this.updateTouchbarMenu();
// Check for cyclic dependencies
if (require.hasDependencyCycle()) {
if (isCI) {
this.logService.error('Error: There is a dependency cycle in the AMD modules that needs to be resolved!');
this.nativeHostService.exit(37); // running on a build machine, just exit without showing a dialog
} else {
this.dialogService.show(Severity.Error, localize('loaderCycle', "There is a dependency cycle in the AMD modules that needs to be resolved!"));
this.nativeHostService.openDevTools();
}
}
// Smoke Test Driver
if (this.environmentService.enableSmokeTestDriver) {
this.setupDriver();
}
}
private setupDriver(): void {
const that = this;
registerWindowDriver({
async exitApplication(): Promise<void> {
return that.nativeHostService.quit();
}
});
}
private setupOpenHandlers(): void {
// Block window.open() calls
window.open = function (): Window | null {
throw new Error('Prevented call to window.open(). Use IOpenerService instead!');
};
// Handle external open() calls
this.openerService.setDefaultExternalOpener({
openExternal: async (href: string) => {
const success = await this.nativeHostService.openExternal(href);
if (!success) {
const fileCandidate = URI.parse(href);
if (fileCandidate.scheme === Schemas.file) {
// if opening failed, and this is a file, we can still try to reveal it
await this.nativeHostService.showItemInFolder(fileCandidate.fsPath);
}
}
return true;
}
});
// Register external URI resolver
this.openerService.registerExternalUriResolver({
resolveExternalUri: async (uri: URI, options?: OpenOptions) => {
if (options?.allowTunneling) {
const portMappingRequest = extractLocalHostUriMetaDataForPortMapping(uri);
if (portMappingRequest) {
const remoteAuthority = this.environmentService.remoteAuthority;
const addressProvider: IAddressProvider | undefined = remoteAuthority ? {
getAddress: async (): Promise<IAddress> => {
return (await this.remoteAuthorityResolverService.resolveAuthority(remoteAuthority)).authority;
}
} : undefined;
const tunnel = await this.tunnelService.openTunnel(addressProvider, portMappingRequest.address, portMappingRequest.port);
if (tunnel) {
const addressAsUri = URI.parse(tunnel.localAddress);
const resolved = addressAsUri.scheme.startsWith(uri.scheme) ? addressAsUri : uri.with({ authority: tunnel.localAddress });
return {
resolved,
dispose: () => tunnel.dispose(),
};
}
}
}
if (!options?.openExternal) {
const canHandleResource = await this.fileService.canHandleResource(uri);
if (canHandleResource) {
return {
resolved: URI.from({
scheme: this.productService.urlProtocol,
path: 'workspace',
query: uri.toString()
}),
dispose() { }
};
}
}
return undefined;
}
});
}
private updateTouchbarMenu(): void {
if (!isMacintosh) {
return; // macOS only
}
// Dispose old
this.touchBarDisposables.clear();
this.touchBarMenu = undefined;
// Create new (delayed)
const scheduler: RunOnceScheduler = this.touchBarDisposables.add(new RunOnceScheduler(() => this.doUpdateTouchbarMenu(scheduler), 300));
scheduler.schedule();
}
private doUpdateTouchbarMenu(scheduler: RunOnceScheduler): void {
if (!this.touchBarMenu) {
const scopedContextKeyService = this.editorService.activeEditorPane?.scopedContextKeyService || this.editorGroupService.activeGroup.scopedContextKeyService;
this.touchBarMenu = this.menuService.createMenu(MenuId.TouchBarContext, scopedContextKeyService);
this.touchBarDisposables.add(this.touchBarMenu);
this.touchBarDisposables.add(this.touchBarMenu.onDidChange(() => scheduler.schedule()));
}
const actions: Array<MenuItemAction | Separator> = [];
const disabled = this.configurationService.getValue('keyboard.touchbar.enabled') === false;
const touchbarIgnored = this.configurationService.getValue('keyboard.touchbar.ignored');
const ignoredItems = isArray(touchbarIgnored) ? touchbarIgnored : [];
// Fill actions into groups respecting order
this.touchBarDisposables.add(createAndFillInActionBarActions(this.touchBarMenu, undefined, actions));
// Convert into command action multi array
const items: ICommandAction[][] = [];
let group: ICommandAction[] = [];
if (!disabled) {
for (const action of actions) {
// Command
if (action instanceof MenuItemAction) {
if (ignoredItems.indexOf(action.item.id) >= 0) {
continue; // ignored
}
group.push(action.item);
}
// Separator
else if (action instanceof Separator) {
if (group.length) {
items.push(group);
}
group = [];
}
}
if (group.length) {
items.push(group);
}
}
// Only update if the actions have changed
if (!equals(this.lastInstalledTouchedBar, items)) {
this.lastInstalledTouchedBar = items;
this.nativeHostService.updateTouchBar(items);
}
}
private onAddFoldersRequest(request: IAddFoldersRequest): void {
// Buffer all pending requests
this.pendingFoldersToAdd.push(...request.foldersToAdd.map(folder => URI.revive(folder)));
// Delay the adding of folders a bit to buffer in case more requests are coming
if (!this.addFoldersScheduler.isScheduled()) {
this.addFoldersScheduler.schedule();
}
}
private doAddFolders(): void {
const foldersToAdd: IWorkspaceFolderCreationData[] = [];
for (const folder of this.pendingFoldersToAdd) {
foldersToAdd.push(({ uri: folder }));
}
this.pendingFoldersToAdd = [];
this.workspaceEditingService.addFolders(foldersToAdd);
}
private async onOpenFiles(request: INativeOpenFileRequest): Promise<void> {
const inputs: Array<IResourceEditorInput | IUntitledTextResourceEditorInput> = [];
const diffMode = !!(request.filesToDiff && (request.filesToDiff.length === 2));
if (!diffMode && request.filesToOpenOrCreate) {
inputs.push(...(await pathsToEditors(request.filesToOpenOrCreate, this.fileService)));
}
if (diffMode && request.filesToDiff) {
inputs.push(...(await pathsToEditors(request.filesToDiff, this.fileService)));
}
if (inputs.length) {
this.openResources(inputs, diffMode);
}
if (request.filesToWait && inputs.length) {
// In wait mode, listen to changes to the editors and wait until the files
// are closed that the user wants to wait for. When this happens we delete
// the wait marker file to signal to the outside that editing is done.
this.trackClosedWaitFiles(URI.revive(request.filesToWait.waitMarkerFileUri), coalesce(request.filesToWait.paths.map(path => URI.revive(path.fileUri))));
}
}
private async trackClosedWaitFiles(waitMarkerFile: URI, resourcesToWaitFor: URI[]): Promise<void> {
// Wait for the resources to be closed in the text editor...
await this.instantiationService.invokeFunction(accessor => whenEditorClosed(accessor, resourcesToWaitFor));
// ...before deleting the wait marker file
await this.fileService.del(waitMarkerFile);
}
private async openResources(resources: Array<IResourceEditorInput | IUntitledTextResourceEditorInput>, diffMode: boolean): Promise<unknown> {
const editors: IUntypedEditorInput[] = [];
// In diffMode we open 2 resources as diff
if (diffMode && resources.length === 2 && resources[0].resource && resources[1].resource) {
const diffEditor: IResourceDiffEditorInput = {
original: { resource: resources[0].resource },
modified: { resource: resources[1].resource },
options: { pinned: true }
};
editors.push(diffEditor);
} else {
editors.push(...resources);
}
// Open as editors
return this.editorService.openEditors(editors, undefined, { validateTrust: true });
}
}
|