Skip to content

Commit 6551c68

Browse files
committed
Working on strict null checking code/windows
1 parent e82d8bb commit 6551c68

3 files changed

Lines changed: 33 additions & 26 deletions

File tree

src/vs/code/electron-main/windows.ts

Lines changed: 22 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import { IPathWithLineAndColumn, parseLineAndColumnAware } from 'vs/code/node/pa
1818
import { ILifecycleService, UnloadReason, IWindowUnloadEvent, LifecycleService } from 'vs/platform/lifecycle/electron-main/lifecycleMain';
1919
import { IConfigurationService } from 'vs/platform/configuration/common/configuration';
2020
import { ILogService } from 'vs/platform/log/common/log';
21-
import { IWindowSettings, OpenContext, IPath, IWindowConfiguration, INativeOpenDialogOptions, IPathsToWaitFor, IEnterWorkspaceResult, IMessageBoxResult, INewWindowOptions } from 'vs/platform/windows/common/windows';
21+
import { IWindowSettings, OpenContext, IPath, IWindowConfiguration, INativeOpenDialogOptions, IPathsToWaitFor, IEnterWorkspaceResult, IMessageBoxResult, INewWindowOptions, OpenDialogOptions } from 'vs/platform/windows/common/windows';
2222
import { getLastActiveWindow, findBestWindowOrFolderForFile, findWindowOnWorkspace, findWindowOnExtensionDevelopmentPath, findWindowOnWorkspaceOrFolderUri } from 'vs/code/node/windowsFinder';
2323
import { Event as CommonEvent, Emitter } from 'vs/base/common/event';
2424
import product from 'vs/platform/node/product';
@@ -50,7 +50,7 @@ interface INewWindowState extends ISingleWindowState {
5050
interface IWindowState {
5151
workspace?: IWorkspaceIdentifier;
5252
folderUri?: URI;
53-
backupPath: string;
53+
backupPath?: string;
5454
remoteAuthority?: string;
5555
uiState: ISingleWindowState;
5656
}
@@ -131,7 +131,7 @@ export class WindowsManager implements IWindowsMainService {
131131
private initialUserEnv: IProcessEnvironment;
132132

133133
private windowsState: IWindowsState;
134-
private lastClosedWindowState: IWindowState;
134+
private lastClosedWindowState?: IWindowState;
135135

136136
private dialogs: Dialogs;
137137
private workspacesManager: WorkspacesManager;
@@ -188,8 +188,9 @@ export class WindowsManager implements IWindowsMainService {
188188
if (windowState.folderUri) {
189189
windowState.folderUri = URI.revive(windowState.folderUri);
190190
}
191-
if ((<IBackwardCompatibleWindowState>windowState).folderPath) {
192-
windowState.folderUri = URI.file((<IBackwardCompatibleWindowState>windowState).folderPath);
191+
const folderPath = (windowState as IBackwardCompatibleWindowState).folderPath;
192+
if (folderPath) {
193+
windowState.folderUri = URI.file(folderPath);
193194
}
194195
return windowState;
195196
}
@@ -403,12 +404,12 @@ export class WindowsManager implements IWindowsMainService {
403404
//
404405
// These are windows to open to show workspaces
405406
//
406-
const workspacesToOpen = arrays.distinct(pathsToOpen.filter(win => !!win.workspace).map(win => win.workspace), workspace => workspace.id); // prevent duplicates
407+
const workspacesToOpen = arrays.distinct(arrays.coalesce(pathsToOpen.map(win => win.workspace)), workspace => workspace.id); // prevent duplicates
407408

408409
//
409410
// These are windows to open to show either folders or files (including diffing files or creating them)
410411
//
411-
const foldersToOpen = arrays.distinct(pathsToOpen.filter(win => win.folderUri && !win.fileUri).map(win => win.folderUri), folder => getComparisonKey(folder)); // prevent duplicates
412+
const foldersToOpen = arrays.distinct(arrays.coalesce(pathsToOpen.filter(win => win.folderUri && !win.fileUri).map(win => win.folderUri)), folder => getComparisonKey(folder)); // prevent duplicates
412413

413414
//
414415
// These are windows to restore because of hot-exit or from previous session (only performed once on startup!)
@@ -423,7 +424,7 @@ export class WindowsManager implements IWindowsMainService {
423424
workspacesToRestore.push(...this.workspacesMainService.getUntitledWorkspacesSync()); // collect from previous window session
424425

425426
emptyToRestore = this.backupMainService.getEmptyWindowBackupPaths();
426-
emptyToRestore.push(...pathsToOpen.filter(w => !w.workspace && !w.folderUri && w.backupPath).map(w => ({ backupFolder: basename(w.backupPath), remoteAuthority: w.remoteAuthority }))); // add empty windows with backupPath
427+
emptyToRestore.push(...pathsToOpen.filter(w => !w.workspace && !w.folderUri && w.backupPath).map(w => ({ backupFolder: basename(w.backupPath!), remoteAuthority: w.remoteAuthority }))); // add empty windows with backupPath
427428
emptyToRestore = arrays.distinct(emptyToRestore, info => info.backupFolder); // prevent duplicates
428429
}
429430

@@ -444,9 +445,9 @@ export class WindowsManager implements IWindowsMainService {
444445

445446
// 1.) focus last active window if we are not instructed to open any paths
446447
if (focusLastActive) {
447-
const lastActiveWindw = usedWindows.filter(w => w.backupPath === this.windowsState.lastActiveWindow.backupPath);
448-
if (lastActiveWindw.length) {
449-
lastActiveWindw[0].focus();
448+
const lastActiveWindow = usedWindows.filter(w => w.backupPath === this.windowsState.lastActiveWindow!.backupPath);
449+
if (lastActiveWindow.length) {
450+
lastActiveWindow[0].focus();
450451
focusLastOpened = false;
451452
focusLastWindow = false;
452453
}
@@ -1593,7 +1594,7 @@ export class WindowsManager implements IWindowsMainService {
15931594
});
15941595
}
15951596

1596-
getFocusedWindow(): ICodeWindow {
1597+
getFocusedWindow(): ICodeWindow | null {
15971598
const win = BrowserWindow.getFocusedWindow();
15981599
if (win) {
15991600
return this.getWindowById(win.id);
@@ -1602,7 +1603,7 @@ export class WindowsManager implements IWindowsMainService {
16021603
return null;
16031604
}
16041605

1605-
getWindowById(windowId: number): ICodeWindow {
1606+
getWindowById(windowId: number): ICodeWindow | null {
16061607
const res = WindowsManager.WINDOWS.filter(w => w.id === windowId);
16071608
if (res && res.length === 1) {
16081609
return res[0];
@@ -1719,9 +1720,8 @@ export class WindowsManager implements IWindowsMainService {
17191720
internalOptions.pickFolders = pickFolders;
17201721
internalOptions.pickFiles = pickFiles;
17211722

1722-
if (!internalOptions.dialogOptions) {
1723-
internalOptions.dialogOptions = Object.create(null);
1724-
}
1723+
const dialogOptions: OpenDialogOptions = internalOptions.dialogOptions || Object.create(null);
1724+
internalOptions.dialogOptions = dialogOptions;
17251725

17261726
if (!internalOptions.dialogOptions.title) {
17271727
if (pickFolders && pickFiles) {
@@ -1952,13 +1952,12 @@ class Dialogs {
19521952
class WorkspacesManager {
19531953

19541954
constructor(
1955-
private workspacesMainService: IWorkspacesMainService,
1956-
private backupMainService: IBackupMainService,
1957-
private environmentService: IEnvironmentService,
1958-
private historyMainService: IHistoryMainService,
1959-
private windowsMainService: IWindowsMainService,
1960-
) {
1961-
}
1955+
private readonly workspacesMainService: IWorkspacesMainService,
1956+
private readonly backupMainService: IBackupMainService,
1957+
private readonly environmentService: IEnvironmentService,
1958+
private readonly historyMainService: IHistoryMainService,
1959+
private readonly windowsMainService: IWindowsMainService,
1960+
) { }
19621961

19631962
enterWorkspace(window: ICodeWindow, path: URI): Promise<IEnterWorkspaceResult | null> {
19641963
if (!window || !window.win || !window.isReady) {

src/vs/platform/driver/electron-main/driver.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,16 +57,21 @@ export class Driver implements IDriver, IWindowDriverRegistry {
5757
await this.whenUnfrozen(windowId);
5858

5959
const window = this.windowsService.getWindowById(windowId);
60+
if (!window) {
61+
throw new Error('Invalid window');
62+
}
6063
const webContents = window.win.webContents;
6164
const image = await new Promise<Electron.NativeImage>(c => webContents.capturePage(c));
62-
6365
return image.toPNG().toString('base64');
6466
}
6567

6668
async reloadWindow(windowId: number): Promise<void> {
6769
await this.whenUnfrozen(windowId);
6870

6971
const window = this.windowsService.getWindowById(windowId);
72+
if (!window) {
73+
throw new Error('Invalid window');
74+
}
7075
this.reloadingWindowIds.add(windowId);
7176
this.windowsService.reload(window);
7277
}
@@ -97,6 +102,9 @@ export class Driver implements IDriver, IWindowDriverRegistry {
97102
}
98103

99104
const window = this.windowsService.getWindowById(windowId);
105+
if (!window) {
106+
throw new Error('Invalid window');
107+
}
100108
const webContents = window.win.webContents;
101109
const noModifiedKeybinding = new SimpleKeybinding(false, false, false, false, keybinding.keyCode);
102110
const resolvedKeybinding = new USLayoutResolvedKeybinding(noModifiedKeybinding, OS);

src/vs/platform/windows/electron-main/windows.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -112,8 +112,8 @@ export interface IWindowsMainService {
112112
openNewTabbedWindow(context: OpenContext): ICodeWindow[];
113113
sendToFocused(channel: string, ...args: any[]): void;
114114
sendToAll(channel: string, payload: any, windowIdsToIgnore?: number[]): void;
115-
getFocusedWindow(): ICodeWindow;
116-
getWindowById(windowId: number): ICodeWindow;
115+
getFocusedWindow(): ICodeWindow | null;
116+
getWindowById(windowId: number): ICodeWindow | null;
117117
getWindows(): ICodeWindow[];
118118
getWindowCount(): number;
119119
quit(): void;

0 commit comments

Comments
 (0)