windowService.ts 4.8 KB
Newer Older
J
Joao Moreno 已提交
1 2 3 4 5 6 7
/*---------------------------------------------------------------------------------------------
 *  Copyright (c) Microsoft Corporation. All rights reserved.
 *  Licensed under the MIT License. See License.txt in the project root for license information.
 *--------------------------------------------------------------------------------------------*/

'use strict';

8
import Event, { filterEvent, mapEvent, any } from 'vs/base/common/event';
J
Joao Moreno 已提交
9
import { TPromise } from 'vs/base/common/winjs.base';
B
Benjamin Pasero 已提交
10
import { IWindowService, IWindowsService, INativeOpenDialogOptions } from 'vs/platform/windows/common/windows';
11
import { remote } from 'electron';
B
Benjamin Pasero 已提交
12
import { IRecentlyOpened } from 'vs/platform/history/common/history';
13
import { ICommandAction } from 'vs/platform/actions/common/actions';
J
Joao Moreno 已提交
14 15 16

export class WindowService implements IWindowService {

J
Joao Moreno 已提交
17
	readonly onDidChangeFocus: Event<boolean>;
18

J
Joao Moreno 已提交
19 20 21 22 23
	_serviceBrand: any;

	constructor(
		private windowId: number,
		@IWindowsService private windowsService: IWindowsService
24 25 26
	) {
		const onThisWindowFocus = mapEvent(filterEvent(windowsService.onWindowFocus, id => id === windowId), _ => true);
		const onThisWindowBlur = mapEvent(filterEvent(windowsService.onWindowBlur, id => id === windowId), _ => false);
J
Joao Moreno 已提交
27
		this.onDidChangeFocus = any(onThisWindowFocus, onThisWindowBlur);
28
	}
J
Joao Moreno 已提交
29

J
Joao Moreno 已提交
30 31 32 33
	getCurrentWindowId(): number {
		return this.windowId;
	}

B
Benjamin Pasero 已提交
34 35 36 37
	pickFileFolderAndOpen(options: INativeOpenDialogOptions): TPromise<void> {
		options.windowId = this.windowId;

		return this.windowsService.pickFileFolderAndOpen(options);
J
Joao Moreno 已提交
38 39
	}

B
Benjamin Pasero 已提交
40 41 42 43
	pickFileAndOpen(options: INativeOpenDialogOptions): TPromise<void> {
		options.windowId = this.windowId;

		return this.windowsService.pickFileAndOpen(options);
J
Joao Moreno 已提交
44 45
	}

B
Benjamin Pasero 已提交
46 47 48 49
	pickFolderAndOpen(options: INativeOpenDialogOptions): TPromise<void> {
		options.windowId = this.windowId;

		return this.windowsService.pickFolderAndOpen(options);
J
Joao Moreno 已提交
50
	}
51 52 53 54

	reloadWindow(): TPromise<void> {
		return this.windowsService.reloadWindow(this.windowId);
	}
J
Joao Moreno 已提交
55

J
Joao Moreno 已提交
56 57 58 59
	openDevTools(): TPromise<void> {
		return this.windowsService.openDevTools(this.windowId);
	}

J
Joao Moreno 已提交
60 61 62
	toggleDevTools(): TPromise<void> {
		return this.windowsService.toggleDevTools(this.windowId);
	}
J
Joao Moreno 已提交
63

64 65
	closeWorkspace(): TPromise<void> {
		return this.windowsService.closeWorkspace(this.windowId);
66 67 68 69
	}

	openWorkspace(): TPromise<void> {
		return this.windowsService.openWorkspace(this.windowId);
J
Joao Moreno 已提交
70
	}
J
Joao Moreno 已提交
71

72 73 74 75
	createAndOpenWorkspace(folders?: string[], path?: string): TPromise<void> {
		return this.windowsService.createAndOpenWorkspace(this.windowId, folders, path);
	}

76 77 78 79
	saveAndOpenWorkspace(path: string): TPromise<void> {
		return this.windowsService.saveAndOpenWorkspace(this.windowId, path);
	}

80 81 82 83
	closeWindow(): TPromise<void> {
		return this.windowsService.closeWindow(this.windowId);
	}

J
Joao Moreno 已提交
84 85 86
	toggleFullScreen(): TPromise<void> {
		return this.windowsService.toggleFullScreen(this.windowId);
	}
87 88 89 90

	setRepresentedFilename(fileName: string): TPromise<void> {
		return this.windowsService.setRepresentedFilename(this.windowId, fileName);
	}
J
Joao Moreno 已提交
91

92
	getRecentlyOpened(): TPromise<IRecentlyOpened> {
B
Benjamin Pasero 已提交
93
		return this.windowsService.getRecentlyOpened(this.windowId);
J
Joao Moreno 已提交
94
	}
J
Joao Moreno 已提交
95 96 97 98

	focusWindow(): TPromise<void> {
		return this.windowsService.focusWindow(this.windowId);
	}
J
Joao Moreno 已提交
99

100 101 102 103
	isFocused(): TPromise<boolean> {
		return this.windowsService.isFocused(this.windowId);
	}

104 105 106 107 108 109 110 111 112 113 114 115
	isMaximized(): TPromise<boolean> {
		return this.windowsService.isMaximized(this.windowId);
	}

	maximizeWindow(): TPromise<void> {
		return this.windowsService.maximizeWindow(this.windowId);
	}

	unmaximizeWindow(): TPromise<void> {
		return this.windowsService.unmaximizeWindow(this.windowId);
	}

116 117 118 119
	onWindowTitleDoubleClick(): TPromise<void> {
		return this.windowsService.onWindowTitleDoubleClick(this.windowId);
	}

J
Joao Moreno 已提交
120 121 122
	setDocumentEdited(flag: boolean): TPromise<void> {
		return this.windowsService.setDocumentEdited(this.windowId, flag);
	}
J
Joao Moreno 已提交
123

J
Joao 已提交
124 125 126 127
	show(): TPromise<void> {
		return this.windowsService.showWindow(this.windowId);
	}

128
	showMessageBox(options: Electron.MessageBoxOptions): number {
129 130 131 132 133 134 135 136 137 138
		return remote.dialog.showMessageBox(remote.getCurrentWindow(), options);
	}

	showSaveDialog(options: Electron.SaveDialogOptions, callback?: (fileName: string) => void): string {
		if (callback) {
			return remote.dialog.showSaveDialog(remote.getCurrentWindow(), options, callback);
		}

		return remote.dialog.showSaveDialog(remote.getCurrentWindow(), options); // https://github.com/electron/electron/issues/4936
	}
139 140 141 142 143 144 145 146

	showOpenDialog(options: Electron.OpenDialogOptions, callback?: (fileNames: string[]) => void): string[] {
		if (callback) {
			return remote.dialog.showOpenDialog(remote.getCurrentWindow(), options, callback);
		}

		return remote.dialog.showOpenDialog(remote.getCurrentWindow(), options); // https://github.com/electron/electron/issues/4936
	}
147 148 149 150

	updateTouchBar(items: ICommandAction[][]): TPromise<void> {
		return this.windowsService.updateTouchBar(this.windowId, items);
	}
151
}