electronMainService.ts 8.2 KB
Newer Older
1 2 3 4 5
/*---------------------------------------------------------------------------------------------
 *  Copyright (c) Microsoft Corporation. All rights reserved.
 *  Licensed under the MIT License. See License.txt in the project root for license information.
 *--------------------------------------------------------------------------------------------*/

6
import { IWindowsMainService } from 'vs/platform/windows/electron-main/windows';
7
import { MessageBoxOptions, MessageBoxReturnValue, shell, OpenDevToolsOptions, SaveDialogOptions, SaveDialogReturnValue, OpenDialogOptions, OpenDialogReturnValue, CrashReporterStartOptions, crashReporter, Menu } from 'electron';
8
import { ILifecycleMainService } from 'vs/platform/lifecycle/electron-main/lifecycleMainService';
9
import { OpenContext, INativeOpenDialogOptions } from 'vs/platform/windows/common/windows';
10
import { isMacintosh } from 'vs/base/common/platform';
11
import { IElectronService } from 'vs/platform/electron/node/electron';
12
import { ISerializableCommandAction } from 'vs/platform/actions/common/actions';
B
Benjamin Pasero 已提交
13
import { AddContextToFunctions } from 'vs/platform/ipc/node/simpleIpcProxy';
14

B
Benjamin Pasero 已提交
15
export class ElectronMainService implements AddContextToFunctions<IElectronService, number> {
16 17 18 19

	_serviceBrand: undefined;

	constructor(
20 21
		@IWindowsMainService private readonly windowsMainService: IWindowsMainService,
		@ILifecycleMainService private readonly lifecycleMainService: ILifecycleMainService
22 23 24
	) {
	}

25 26
	//#region Window

27
	async windowCount(windowId: number): Promise<number> {
28 29 30
		return this.windowsMainService.getWindowCount();
	}

31
	async openEmptyWindow(windowId: number, options?: { reuse?: boolean, remoteAuthority?: string }): Promise<void> {
32 33 34
		this.windowsMainService.openEmptyWindow(OpenContext.API, options);
	}

35 36
	async toggleFullScreen(windowId: number): Promise<void> {
		const window = this.windowsMainService.getWindowById(windowId);
37 38 39 40 41
		if (window) {
			window.toggleFullScreen();
		}
	}

42 43 44 45 46 47 48
	async handleTitleDoubleClick(windowId: number): Promise<void> {
		const window = this.windowsMainService.getWindowById(windowId);
		if (window) {
			window.handleTitleDoubleClick();
		}
	}

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
	async isMaximized(windowId: number): Promise<boolean> {
		const window = this.windowsMainService.getWindowById(windowId);
		if (window) {
			return window.win.isMaximized();
		}

		return false;
	}

	async maximizeWindow(windowId: number): Promise<void> {
		const window = this.windowsMainService.getWindowById(windowId);
		if (window) {
			window.win.maximize();
		}
	}

	async unmaximizeWindow(windowId: number): Promise<void> {
		const window = this.windowsMainService.getWindowById(windowId);
		if (window) {
			window.win.unmaximize();
		}
	}

	async minimizeWindow(windowId: number): Promise<void> {
		const window = this.windowsMainService.getWindowById(windowId);
		if (window) {
			window.win.minimize();
		}
	}

79 80 81 82 83 84 85 86 87 88 89
	async focusWindow(windowId: number): Promise<void> {
		const window = this.windowsMainService.getWindowById(windowId);
		if (window) {
			if (isMacintosh) {
				window.win.show();
			} else {
				window.win.focus();
			}
		}
	}

90 91
	//#endregion

92 93
	//#region Dialog

94 95
	async showMessageBox(windowId: number, options: MessageBoxOptions): Promise<MessageBoxReturnValue> {
		return this.windowsMainService.showMessageBox(options, this.windowsMainService.getWindowById(windowId));
96 97
	}

98 99
	async showSaveDialog(windowId: number, options: SaveDialogOptions): Promise<SaveDialogReturnValue> {
		return this.windowsMainService.showSaveDialog(options, this.windowsMainService.getWindowById(windowId));
100
	}
101

102 103
	async showOpenDialog(windowId: number, options: OpenDialogOptions): Promise<OpenDialogReturnValue> {
		return this.windowsMainService.showOpenDialog(options, this.windowsMainService.getWindowById(windowId));
104
	}
105

106
	async pickFileFolderAndOpen(windowId: number, options: INativeOpenDialogOptions): Promise<void> {
B
Benjamin Pasero 已提交
107
		return this.windowsMainService.pickFileFolderAndOpen(options, this.windowsMainService.getWindowById(windowId));
108 109
	}

110
	async pickFileAndOpen(windowId: number, options: INativeOpenDialogOptions): Promise<void> {
B
Benjamin Pasero 已提交
111
		return this.windowsMainService.pickFileAndOpen(options, this.windowsMainService.getWindowById(windowId));
112 113
	}

114
	async pickFolderAndOpen(windowId: number, options: INativeOpenDialogOptions): Promise<void> {
B
Benjamin Pasero 已提交
115
		return this.windowsMainService.pickFolderAndOpen(options, this.windowsMainService.getWindowById(windowId));
116 117
	}

118
	async pickWorkspaceAndOpen(windowId: number, options: INativeOpenDialogOptions): Promise<void> {
B
Benjamin Pasero 已提交
119
		return this.windowsMainService.pickWorkspaceAndOpen(options, this.windowsMainService.getWindowById(windowId));
120 121
	}

122 123
	//#endregion

124 125
	//#region OS

126
	async showItemInFolder(windowId: number, path: string): Promise<void> {
127 128
		shell.showItemInFolder(path);
	}
129

130 131
	async setRepresentedFilename(windowId: number, path: string): Promise<void> {
		const window = this.windowsMainService.getWindowById(windowId);
132 133 134 135 136
		if (window) {
			window.setRepresentedFilename(path);
		}
	}

137 138
	async setDocumentEdited(windowId: number, edited: boolean): Promise<void> {
		const window = this.windowsMainService.getWindowById(windowId);
139 140 141 142 143
		if (window) {
			window.win.setDocumentEdited(edited);
		}
	}

144 145 146 147
	async openExternal(windowId: number, url: string): Promise<boolean> {
		return this.windowsMainService.openExternal(url);
	}

148 149 150 151 152 153 154
	async updateTouchBar(windowId: number, items: ISerializableCommandAction[][]): Promise<void> {
		const window = this.windowsMainService.getWindowById(windowId);
		if (window) {
			window.updateTouchBar(items);
		}
	}

155 156
	//#endregion

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
	//#region macOS Touchbar

	async newWindowTab(): Promise<void> {
		this.windowsMainService.openNewTabbedWindow(OpenContext.API);
	}

	async showPreviousWindowTab(): Promise<void> {
		Menu.sendActionToFirstResponder('selectPreviousTab:');
	}

	async showNextWindowTab(): Promise<void> {
		Menu.sendActionToFirstResponder('selectNextTab:');
	}

	async moveWindowTabToNewWindow(): Promise<void> {
		Menu.sendActionToFirstResponder('moveTabToNewWindow:');
	}

	async mergeAllWindowTabs(): Promise<void> {
		Menu.sendActionToFirstResponder('mergeAllWindows:');
	}

	async toggleWindowTabsBar(): Promise<void> {
		Menu.sendActionToFirstResponder('toggleTabBar:');
	}

	//#endregion

185 186
	//#region Lifecycle

187
	async relaunch(windowId: number, options?: { addArgs?: string[], removeArgs?: string[] }): Promise<void> {
188 189 190
		return this.lifecycleMainService.relaunch(options);
	}

191 192
	async reload(windowId: number): Promise<void> {
		const window = this.windowsMainService.getWindowById(windowId);
193 194 195 196 197
		if (window) {
			return this.windowsMainService.reload(window);
		}
	}

198 199 200 201 202 203 204
	async closeWorkpsace(windowId: number): Promise<void> {
		const window = this.windowsMainService.getWindowById(windowId);
		if (window) {
			return this.windowsMainService.closeWorkspace(window);
		}
	}

205 206 207 208 209 210 211
	async closeWindow(windowId: number): Promise<void> {
		const window = this.windowsMainService.getWindowById(windowId);
		if (window) {
			return window.win.close();
		}
	}

B
Benjamin Pasero 已提交
212 213 214 215
	async quit(windowId: number): Promise<void> {
		return this.windowsMainService.quit();
	}

216 217
	//#endregion

218 219 220 221 222 223 224 225 226 227 228 229 230 231 232
	//#region Connectivity

	async resolveProxy(windowId: number, url: string): Promise<string | undefined> {
		return new Promise(resolve => {
			const window = this.windowsMainService.getWindowById(windowId);
			if (window && window.win && window.win.webContents && window.win.webContents.session) {
				window.win.webContents.session.resolveProxy(url, proxy => resolve(proxy));
			} else {
				resolve();
			}
		});
	}

	//#endregion

233 234
	//#region Development

235 236
	async openDevTools(windowId: number, options?: OpenDevToolsOptions): Promise<void> {
		const window = this.windowsMainService.getWindowById(windowId);
237 238 239 240 241
		if (window) {
			window.win.webContents.openDevTools(options);
		}
	}

242 243
	async toggleDevTools(windowId: number): Promise<void> {
		const window = this.windowsMainService.getWindowById(windowId);
244 245 246 247 248 249 250 251 252
		if (window) {
			const contents = window.win.webContents;
			if (isMacintosh && window.hasHiddenTitleBarStyle() && !window.isFullScreen() && !contents.isDevToolsOpened()) {
				contents.openDevTools({ mode: 'undocked' }); // due to https://github.com/electron/electron/issues/3647
			} else {
				contents.toggleDevTools();
			}
		}
	}
253

254 255
	async startCrashReporter(windowId: number, options: CrashReporterStartOptions): Promise<void> {
		crashReporter.start(options);
256
	}
257 258

	//#endregion
259
}