Skip to content

Commit 1fd399e

Browse files
author
Benjamin Pasero
committed
debt - move some electron specific listeners
1 parent 3946e32 commit 1fd399e

4 files changed

Lines changed: 77 additions & 76 deletions

File tree

src/tsconfig.strictNullChecks.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -311,7 +311,7 @@
311311
"./vs/workbench/contrib/themes/test/electron-browser/themes.test.contribution.ts",
312312
"./vs/workbench/contrib/url/common/url.contribution.ts",
313313
"./vs/workbench/contrib/webview/electron-browser/webviewProtocols.ts",
314-
"./vs/workbench/electron-browser/window.ts",
314+
// "./vs/workbench/electron-browser/window.ts", TODO@matt https://github.com/Microsoft/vscode/issues/69198
315315
"./vs/workbench/services/activity/common/activity.ts",
316316
"./vs/workbench/services/backup/common/backup.ts",
317317
"./vs/workbench/services/backup/node/backupFileService.ts",

src/vs/workbench/electron-browser/main.ts

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,21 +4,21 @@
44
*--------------------------------------------------------------------------------------------*/
55

66
import * as fs from 'fs';
7+
import * as gracefulFs from 'graceful-fs';
78
import { createHash } from 'crypto';
8-
import * as perf from 'vs/base/common/performance';
9+
import { importEntries, mark } from 'vs/base/common/performance';
910
import { Workbench } from 'vs/workbench/electron-browser/workbench';
1011
import { ElectronWindow } from 'vs/workbench/electron-browser/window';
11-
import * as browser from 'vs/base/browser/browser';
12+
import { setZoomLevel, setZoomFactor, setFullscreen } from 'vs/base/browser/browser';
1213
import { domContentLoaded, addDisposableListener, EventType, scheduleAtNextAnimationFrame } from 'vs/base/browser/dom';
1314
import { onUnexpectedError } from 'vs/base/common/errors';
14-
import * as platform from 'vs/base/common/platform';
15+
import { isLinux, isMacintosh, isWindows } from 'vs/base/common/platform';
1516
import { URI as uri } from 'vs/base/common/uri';
1617
import { WorkspaceService } from 'vs/workbench/services/configuration/node/configurationService';
1718
import { SyncDescriptor } from 'vs/platform/instantiation/common/descriptors';
1819
import { ServiceCollection } from 'vs/platform/instantiation/common/serviceCollection';
1920
import { stat } from 'vs/base/node/pfs';
2021
import { EnvironmentService } from 'vs/platform/environment/node/environmentService';
21-
import * as gracefulFs from 'graceful-fs';
2222
import { KeyboardMapperFactory } from 'vs/workbench/services/keybinding/electron-browser/keybindingService';
2323
import { IWindowConfiguration, IWindowsService } from 'vs/platform/windows/common/windows';
2424
import { WindowsChannelClient } from 'vs/platform/windows/node/windowsIpc';
@@ -51,7 +51,7 @@ import { InstantiationService } from 'vs/platform/instantiation/common/instantia
5151
import { Disposable } from 'vs/base/common/lifecycle';
5252
import { registerWindowDriver } from 'vs/platform/driver/electron-browser/driver';
5353

54-
export class CodeWindow extends Disposable {
54+
class CodeRendererMain extends Disposable {
5555

5656
private workbench: Workbench;
5757

@@ -70,12 +70,12 @@ export class CodeWindow extends Disposable {
7070
this.reviveUris();
7171

7272
// Setup perf
73-
perf.importEntries(this.configuration.perfEntries);
73+
importEntries(this.configuration.perfEntries);
7474

7575
// Browser config
76-
browser.setZoomFactor(webFrame.getZoomFactor()); // Ensure others can listen to zoom level changes
77-
browser.setZoomLevel(webFrame.getZoomLevel(), true /* isTrusted */); // Can be trusted because we are not setting it ourselves (https://github.com/Microsoft/vscode/issues/26151)
78-
browser.setFullscreen(!!this.configuration.fullscreen);
76+
setZoomFactor(webFrame.getZoomFactor()); // Ensure others can listen to zoom level changes
77+
setZoomLevel(webFrame.getZoomLevel(), true /* isTrusted */); // Can be trusted because we are not setting it ourselves (https://github.com/Microsoft/vscode/issues/26151)
78+
setFullscreen(!!this.configuration.fullscreen);
7979

8080
// Keyboard support
8181
KeyboardMapperFactory.INSTANCE._onKeyboardLayoutChanged();
@@ -107,7 +107,7 @@ export class CodeWindow extends Disposable {
107107
return this.initServices(electronMainClient).then(services => {
108108

109109
return domContentLoaded().then(() => {
110-
perf.mark('willStartWorkbench');
110+
mark('willStartWorkbench');
111111

112112
const instantiationService = new InstantiationService(services, true);
113113

@@ -261,11 +261,11 @@ export class CodeWindow extends Disposable {
261261

262262
function computeLocalDiskFolderId(folder: uri, stat: fs.Stats): string {
263263
let ctime: number | undefined;
264-
if (platform.isLinux) {
264+
if (isLinux) {
265265
ctime = stat.ino; // Linux: birthtime is ctime, so we cannot use it! We use the ino instead!
266-
} else if (platform.isMacintosh) {
266+
} else if (isMacintosh) {
267267
ctime = stat.birthtime.getTime(); // macOS: birthtime is fine to use as is
268-
} else if (platform.isWindows) {
268+
} else if (isWindows) {
269269
if (typeof stat.birthtimeMs === 'number') {
270270
ctime = Math.floor(stat.birthtimeMs); // Windows: fix precision issue in node.js 8.x to get 7.x results (see https://github.com/nodejs/node/issues/19897)
271271
} else {
@@ -323,7 +323,7 @@ export class CodeWindow extends Disposable {
323323
}
324324

325325
export function main(configuration: IWindowConfiguration): Promise<void> {
326-
const window = new CodeWindow(configuration);
326+
const renderer = new CodeRendererMain(configuration);
327327

328-
return window.open();
328+
return renderer.open();
329329
}

src/vs/workbench/electron-browser/window.ts

Lines changed: 55 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,13 @@ import * as browser from 'vs/base/browser/browser';
2323
import { ICommandService } from 'vs/platform/commands/common/commands';
2424
import { IResourceInput } from 'vs/platform/editor/common/editor';
2525
import { KeyboardMapperFactory } from 'vs/workbench/services/keybinding/electron-browser/keybindingService';
26-
import { Themable } from 'vs/workbench/common/theme';
2726
import { ipcRenderer as ipc, webFrame, crashReporter } from 'electron';
2827
import { IWorkspaceEditingService } from 'vs/workbench/services/workspace/common/workspaceEditing';
2928
import { IMenuService, MenuId, IMenu, MenuItemAction, ICommandAction } from 'vs/platform/actions/common/actions';
3029
import { IContextKeyService } from 'vs/platform/contextkey/common/contextkey';
3130
import { fillInActionBarActions } from 'vs/platform/actions/browser/menuItemActionItem';
3231
import { RunOnceScheduler } from 'vs/base/common/async';
33-
import { IDisposable, dispose } from 'vs/base/common/lifecycle';
32+
import { IDisposable, dispose, Disposable } from 'vs/base/common/lifecycle';
3433
import { LifecyclePhase, ILifecycleService } from 'vs/platform/lifecycle/common/lifecycle';
3534
import { IWorkspaceFolderCreationData } from 'vs/platform/workspaces/common/workspaces';
3635
import { IIntegrityService } from 'vs/workbench/services/integrity/common/integrity';
@@ -42,6 +41,7 @@ import { EditorServiceImpl } from 'vs/workbench/browser/parts/editor/editor';
4241
import { IKeybindingService } from 'vs/platform/keybinding/common/keybinding';
4342
import { IEnvironmentService } from 'vs/platform/environment/common/environment';
4443
import { IAccessibilityService, AccessibilitySupport } from 'vs/platform/accessibility/common/accessibility';
44+
import { WorkbenchState, IWorkspaceContextService } from 'vs/platform/workspace/common/workspace';
4545

4646
const TextInputActions: IAction[] = [
4747
new Action('undo', nls.localize('undo', "Undo"), undefined, true, () => Promise.resolve(document.execCommand('undo'))),
@@ -54,7 +54,9 @@ const TextInputActions: IAction[] = [
5454
new Action('editor.action.selectAll', nls.localize('selectAll', "Select All"), undefined, true, () => Promise.resolve(document.execCommand('selectAll')))
5555
];
5656

57-
export class ElectronWindow extends Themable {
57+
export class ElectronWindow extends Disposable {
58+
59+
private static readonly closeWhenEmptyConfigurationKey = 'window.closeWhenEmpty';
5860

5961
private touchBarMenu?: IMenu;
6062
private touchBarUpdater: RunOnceScheduler;
@@ -66,6 +68,8 @@ export class ElectronWindow extends Themable {
6668
private addFoldersScheduler: RunOnceScheduler;
6769
private pendingFoldersToAdd: URI[];
6870

71+
private closeEmptyWindowScheduler: RunOnceScheduler = this._register(new RunOnceScheduler(() => this.onAllEditorsClosed(), 50));
72+
6973
constructor(
7074
@IEditorService private readonly editorService: EditorServiceImpl,
7175
@IWindowsService private readonly windowsService: IWindowsService,
@@ -84,9 +88,10 @@ export class ElectronWindow extends Themable {
8488
@ILifecycleService private readonly lifecycleService: ILifecycleService,
8589
@IIntegrityService private readonly integrityService: IIntegrityService,
8690
@IEnvironmentService private readonly environmentService: IEnvironmentService,
87-
@IAccessibilityService private readonly accessibilityService: IAccessibilityService
91+
@IAccessibilityService private readonly accessibilityService: IAccessibilityService,
92+
@IWorkspaceContextService private readonly contextService: IWorkspaceContextService
8893
) {
89-
super(themeService);
94+
super();
9095

9196
this.touchBarDisposables = [];
9297

@@ -219,6 +224,51 @@ export class ElectronWindow extends Themable {
219224

220225
// Context menu support in input/textarea
221226
window.document.addEventListener('contextmenu', e => this.onContextMenu(e));
227+
228+
// Listen to visible editor changes
229+
this._register(this.editorService.onDidVisibleEditorsChange(() => this.onDidVisibleEditorsChange()));
230+
231+
// Listen to editor closing (if we run with --wait)
232+
const filesToWait = this.windowService.getConfiguration().filesToWait;
233+
if (filesToWait) {
234+
const resourcesToWaitFor = filesToWait.paths.map(p => p.fileUri);
235+
const waitMarkerFile = URI.file(filesToWait.waitMarkerFilePath);
236+
const listenerDispose = this.editorService.onDidCloseEditor(() => this.onEditorClosed(listenerDispose, resourcesToWaitFor, waitMarkerFile));
237+
238+
this._register(listenerDispose);
239+
}
240+
}
241+
242+
private onDidVisibleEditorsChange(): void {
243+
244+
// Close when empty: check if we should close the window based on the setting
245+
// Overruled by: window has a workspace opened or this window is for extension development
246+
// or setting is disabled. Also enabled when running with --wait from the command line.
247+
const visibleEditors = this.editorService.visibleControls;
248+
if (visibleEditors.length === 0 && this.contextService.getWorkbenchState() === WorkbenchState.EMPTY && !this.environmentService.isExtensionDevelopment) {
249+
const closeWhenEmpty = this.configurationService.getValue<boolean>(ElectronWindow.closeWhenEmptyConfigurationKey);
250+
if (closeWhenEmpty || this.environmentService.args.wait) {
251+
this.closeEmptyWindowScheduler.schedule();
252+
}
253+
}
254+
}
255+
256+
private onAllEditorsClosed(): void {
257+
const visibleEditors = this.editorService.visibleControls.length;
258+
if (visibleEditors === 0) {
259+
this.windowService.closeWindow();
260+
}
261+
}
262+
263+
private onEditorClosed(listenerDispose: IDisposable, resourcesToWaitFor: URI[], waitMarkerFile: URI): void {
264+
265+
// In wait mode, listen to changes to the editors and wait until the files
266+
// are closed that the user wants to wait for. When this happens we delete
267+
// the wait marker file to signal to the outside that editing is done.
268+
if (resourcesToWaitFor.every(resource => !this.editorService.isOpen({ resource }))) {
269+
listenerDispose.dispose();
270+
this.fileService.del(waitMarkerFile);
271+
}
222272
}
223273

224274
private onContextMenu(e: MouseEvent): void {

src/vs/workbench/electron-browser/workbench.ts

Lines changed: 6 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import { setFileNameComparer } from 'vs/base/common/comparers';
1010
import { IDisposable, dispose, Disposable } from 'vs/base/common/lifecycle';
1111
import { Event, Emitter, setGlobalLeakWarningThreshold } from 'vs/base/common/event';
1212
import { EventType, addDisposableListener, addClasses, addClass, removeClass, isAncestor, getClientArea, position, size, removeClasses } from 'vs/base/browser/dom';
13-
import { RunOnceScheduler, runWhenIdle, IdleValue } from 'vs/base/common/async';
13+
import { runWhenIdle, IdleValue } from 'vs/base/common/async';
1414
import { getZoomLevel, onDidChangeFullscreen, isFullscreen, getZoomFactor } from 'vs/base/browser/browser';
1515
import { mark } from 'vs/base/common/performance';
1616
import { onUnexpectedError, setUnexpectedErrorHandler } from 'vs/base/common/errors';
@@ -71,7 +71,6 @@ import { IWorkspaceEditingService } from 'vs/workbench/services/workspace/common
7171
import { FileDecorationsService } from 'vs/workbench/services/decorations/browser/decorationsService';
7272
import { IDecorationsService } from 'vs/workbench/services/decorations/browser/decorations';
7373
import { ActivityService } from 'vs/workbench/services/activity/browser/activityService';
74-
import { URI } from 'vs/base/common/uri';
7574
import { IListService, ListService } from 'vs/platform/list/browser/listService';
7675
import { IViewsService } from 'vs/workbench/common/views';
7776
import { ViewsService } from 'vs/workbench/browser/parts/views/views';
@@ -241,7 +240,6 @@ export class Workbench extends Disposable implements IPartService {
241240
private static readonly sidebarPositionConfigurationKey = 'workbench.sideBar.location';
242241
private static readonly statusbarVisibleConfigurationKey = 'workbench.statusBar.visible';
243242
private static readonly activityBarVisibleConfigurationKey = 'workbench.activityBar.visible';
244-
private static readonly closeWhenEmptyConfigurationKey = 'window.closeWhenEmpty';
245243
private static readonly fontAliasingConfigurationKey = 'workbench.fontAliasing';
246244

247245
_serviceBrand: any;
@@ -316,8 +314,6 @@ export class Workbench extends Disposable implements IPartService {
316314
private inZenModeContext: IContextKey<boolean>;
317315
private sideBarVisibleContext: IContextKey<boolean>;
318316

319-
private closeEmptyWindowScheduler: RunOnceScheduler = this._register(new RunOnceScheduler(() => this.onAllEditorsClosed(), 50));
320-
321317
constructor(
322318
private container: HTMLElement,
323319
private configuration: IWindowConfiguration,
@@ -453,10 +449,7 @@ export class Workbench extends Disposable implements IPartService {
453449
this.layout();
454450

455451
// Handle case where workbench is not starting up properly
456-
const timeoutHandle = setTimeout(() => {
457-
this.logService.warn('Workbench did not finish loading in 10 seconds, that might be a problem that should be reported.');
458-
}, 10000);
459-
452+
const timeoutHandle = setTimeout(() => this.logService.warn('Workbench did not finish loading in 10 seconds, that might be a problem that should be reported.'), 10000);
460453
this.lifecycleService.when(LifecyclePhase.Restored).then(() => clearTimeout(timeoutHandle));
461454

462455
// Restore Parts
@@ -776,21 +769,9 @@ export class Workbench extends Disposable implements IPartService {
776769
// Storage
777770
this._register(this.storageService.onWillSaveState(e => this.saveState(e)));
778771

779-
// Listen to visible editor changes
780-
this._register(this.editorService.onDidVisibleEditorsChange(() => this.onDidVisibleEditorsChange()));
781-
782-
// Listen to editor group activations when editor is hidden
783-
this._register(this.editorPart.onDidActivateGroup(() => { if (this.editorHidden) { this.setEditorHidden(false); } }));
784-
785-
// Listen to editor closing (if we run with --wait)
786-
const filesToWait = this.configuration.filesToWait;
787-
if (filesToWait) {
788-
const resourcesToWaitFor = filesToWait.paths.map(p => p.fileUri);
789-
const waitMarkerFile = URI.file(filesToWait.waitMarkerFilePath);
790-
const listenerDispose = this.editorService.onDidCloseEditor(() => this.onEditorClosed(listenerDispose, resourcesToWaitFor, waitMarkerFile));
791-
792-
this._register(listenerDispose);
793-
}
772+
// Restore editor if hidden and it changes
773+
this._register(this.editorService.onDidVisibleEditorsChange(() => this.restoreHiddenEditor()));
774+
this._register(this.editorPart.onDidActivateGroup(() => this.restoreHiddenEditor()));
794775

795776
// Configuration changes
796777
this._register(this.configurationService.onDidChangeConfiguration(() => this.onDidUpdateConfiguration()));
@@ -837,42 +818,12 @@ export class Workbench extends Disposable implements IPartService {
837818
}
838819
}
839820

840-
private onEditorClosed(listenerDispose: IDisposable, resourcesToWaitFor: URI[], waitMarkerFile: URI): void {
841-
842-
// In wait mode, listen to changes to the editors and wait until the files
843-
// are closed that the user wants to wait for. When this happens we delete
844-
// the wait marker file to signal to the outside that editing is done.
845-
if (resourcesToWaitFor.every(resource => !this.editorService.isOpen({ resource }))) {
846-
listenerDispose.dispose();
847-
this.fileService.del(waitMarkerFile);
848-
}
849-
}
850-
851-
private onDidVisibleEditorsChange(): void {
852-
const visibleEditors = this.editorService.visibleControls;
853-
854-
// Close when empty: check if we should close the window based on the setting
855-
// Overruled by: window has a workspace opened or this window is for extension development
856-
// or setting is disabled. Also enabled when running with --wait from the command line.
857-
if (visibleEditors.length === 0 && this.contextService.getWorkbenchState() === WorkbenchState.EMPTY && !this.environmentService.isExtensionDevelopment) {
858-
const closeWhenEmpty = this.configurationService.getValue<boolean>(Workbench.closeWhenEmptyConfigurationKey);
859-
if (closeWhenEmpty || this.environmentService.args.wait) {
860-
this.closeEmptyWindowScheduler.schedule();
861-
}
862-
}
863-
821+
private restoreHiddenEditor(): void {
864822
if (this.editorHidden) {
865823
this.setEditorHidden(false);
866824
}
867825
}
868826

869-
private onAllEditorsClosed(): void {
870-
const visibleEditors = this.editorService.visibleControls.length;
871-
if (visibleEditors === 0) {
872-
this.windowService.closeWindow();
873-
}
874-
}
875-
876827
private onDidUpdateConfiguration(skipLayout?: boolean): void {
877828
const newSidebarPositionValue = this.configurationService.getValue<string>(Workbench.sidebarPositionConfigurationKey);
878829
const newSidebarPosition = (newSidebarPositionValue === 'right') ? Position.RIGHT : Position.LEFT;

0 commit comments

Comments
 (0)