relauncher.contribution.ts 9.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.
 *--------------------------------------------------------------------------------------------*/

B
Benjamin Pasero 已提交
6
import { IDisposable, dispose, Disposable } from 'vs/base/common/lifecycle';
7
import { IWorkbenchContributionsRegistry, IWorkbenchContribution, Extensions as WorkbenchExtensions } from 'vs/workbench/common/contributions';
8
import { Registry } from 'vs/platform/registry/common/platform';
9
import { IWindowsService, IWindowService, IWindowsConfiguration } from 'vs/platform/windows/common/windows';
10 11 12
import { IConfigurationService } from 'vs/platform/configuration/common/configuration';
import { localize } from 'vs/nls';
import { IEnvironmentService } from 'vs/platform/environment/common/environment';
13
import { IWorkspaceContextService, WorkbenchState } from 'vs/platform/workspace/common/workspace';
14
import { IExtensionService } from 'vs/workbench/services/extensions/common/extensions';
15
import { RunOnceScheduler } from 'vs/base/common/async';
16
import { URI } from 'vs/base/common/uri';
B
Benjamin Pasero 已提交
17
import { isEqual } from 'vs/base/common/resources';
18
import { isLinux, isMacintosh } from 'vs/base/common/platform';
19
import { LifecyclePhase } from 'vs/platform/lifecycle/common/lifecycle';
20
import { IDialogService } from 'vs/platform/dialogs/common/dialogs';
B
Benjamin Pasero 已提交
21
import { equals } from 'vs/base/common/objects';
22

23
interface IConfiguration extends IWindowsConfiguration {
24 25
	update: { channel: string; };
	telemetry: { enableCrashReporter: boolean };
26
	keyboard: { touchbar: { enabled: boolean } };
27
	workbench: { tree: { horizontalScrolling: boolean }, enableLegacyStorage: boolean };
B
Benjamin Pasero 已提交
28
	files: { useExperimentalFileWatcher: boolean, watcherExclude: object };
29 30
}

B
Benjamin Pasero 已提交
31
export class SettingsChangeRelauncher extends Disposable implements IWorkbenchContribution {
32 33

	private titleBarStyle: 'native' | 'custom';
34
	private nativeTabs: boolean;
B
Benjamin Pasero 已提交
35
	private nativeFullScreen: boolean;
36
	private clickThroughInactive: boolean;
37 38
	private updateChannel: string;
	private enableCrashReporter: boolean;
39
	private touchbarEnabled: boolean;
40
	private treeHorizontalScrolling: boolean;
41
	private experimentalFileWatcher: boolean;
B
Benjamin Pasero 已提交
42
	private fileWatcherExclude: object;
43
	private legacyStorage: boolean;
44

B
Benjamin Pasero 已提交
45
	private firstFolderResource: URI;
46
	private extensionHostRestarter: RunOnceScheduler;
47

48 49
	private onDidChangeWorkspaceFoldersUnbind: IDisposable;

50 51 52 53 54
	constructor(
		@IWindowsService private windowsService: IWindowsService,
		@IWindowService private windowService: IWindowService,
		@IConfigurationService private configurationService: IConfigurationService,
		@IEnvironmentService private envService: IEnvironmentService,
55
		@IDialogService private dialogService: IDialogService,
56 57
		@IWorkspaceContextService private contextService: IWorkspaceContextService,
		@IExtensionService private extensionService: IExtensionService
58
	) {
B
Benjamin Pasero 已提交
59 60
		super();

61
		const workspace = this.contextService.getWorkspace();
B
Benjamin Pasero 已提交
62
		this.firstFolderResource = workspace.folders.length > 0 ? workspace.folders[0].uri : void 0;
63
		this.extensionHostRestarter = new RunOnceScheduler(() => this.extensionService.restartExtensionHost(), 10);
64

65
		this.onConfigurationChange(configurationService.getValue<IConfiguration>(), false);
66
		this.handleWorkbenchState();
67 68 69 70 71

		this.registerListeners();
	}

	private registerListeners(): void {
B
Benjamin Pasero 已提交
72 73
		this._register(this.configurationService.onDidChangeConfiguration(e => this.onConfigurationChange(this.configurationService.getValue<IConfiguration>(), true)));
		this._register(this.contextService.onDidChangeWorkbenchState(() => setTimeout(() => this.handleWorkbenchState())));
74 75 76 77 78
	}

	private onConfigurationChange(config: IConfiguration, notify: boolean): void {
		let changed = false;

79 80
		// Titlebar style
		if (config.window && config.window.titleBarStyle !== this.titleBarStyle && (config.window.titleBarStyle === 'native' || config.window.titleBarStyle === 'custom')) {
81 82 83 84
			this.titleBarStyle = config.window.titleBarStyle;
			changed = true;
		}

85 86
		// macOS: Native tabs
		if (isMacintosh && config.window && typeof config.window.nativeTabs === 'boolean' && config.window.nativeTabs !== this.nativeTabs) {
87 88 89 90
			this.nativeTabs = config.window.nativeTabs;
			changed = true;
		}

91
		// macOS: Native fullscreen
B
Benjamin Pasero 已提交
92 93
		if (isMacintosh && config.window && typeof config.window.nativeFullScreen === 'boolean' && config.window.nativeFullScreen !== this.nativeFullScreen) {
			this.nativeFullScreen = config.window.nativeFullScreen;
94 95 96
			changed = true;
		}

97 98 99 100 101 102
		// macOS: Click through (accept first mouse)
		if (isMacintosh && config.window && typeof config.window.clickThroughInactive === 'boolean' && config.window.clickThroughInactive !== this.clickThroughInactive) {
			this.clickThroughInactive = config.window.clickThroughInactive;
			changed = true;
		}

103
		// Update channel
104
		if (config.update && typeof config.update.channel === 'string' && config.update.channel !== this.updateChannel) {
105 106 107 108 109
			this.updateChannel = config.update.channel;
			changed = true;
		}

		// Crash reporter
110
		if (config.telemetry && typeof config.telemetry.enableCrashReporter === 'boolean' && config.telemetry.enableCrashReporter !== this.enableCrashReporter) {
111 112 113 114
			this.enableCrashReporter = config.telemetry.enableCrashReporter;
			changed = true;
		}

115 116 117 118 119 120
		// Experimental File Watcher
		if (config.files && typeof config.files.useExperimentalFileWatcher === 'boolean' && config.files.useExperimentalFileWatcher !== this.experimentalFileWatcher) {
			this.experimentalFileWatcher = config.files.useExperimentalFileWatcher;
			changed = true;
		}

B
Benjamin Pasero 已提交
121 122 123 124 125 126 127 128
		// File Watcher Excludes (only if in folder workspace mode)
		if (!this.experimentalFileWatcher && this.contextService.getWorkbenchState() === WorkbenchState.FOLDER) {
			if (config.files && typeof config.files.watcherExclude === 'object' && !equals(config.files.watcherExclude, this.fileWatcherExclude)) {
				this.fileWatcherExclude = config.files.watcherExclude;
				changed = true;
			}
		}

129 130
		// macOS: Touchbar config
		if (isMacintosh && config.keyboard && config.keyboard.touchbar && typeof config.keyboard.touchbar.enabled === 'boolean' && config.keyboard.touchbar.enabled !== this.touchbarEnabled) {
131 132 133 134
			this.touchbarEnabled = config.keyboard.touchbar.enabled;
			changed = true;
		}

135 136 137 138 139 140
		// Tree horizontal scrolling support
		if (config.workbench && config.workbench.tree && typeof config.workbench.tree.horizontalScrolling === 'boolean' && config.workbench.tree.horizontalScrolling !== this.treeHorizontalScrolling) {
			this.treeHorizontalScrolling = config.workbench.tree.horizontalScrolling;
			changed = true;
		}

141 142 143 144 145 146
		// Legacy Workspace Storage
		if (config.workbench && typeof config.workbench.enableLegacyStorage === 'boolean' && config.workbench.enableLegacyStorage !== this.legacyStorage) {
			this.legacyStorage = config.workbench.enableLegacyStorage;
			changed = true;
		}

147 148
		// Notify only when changed and we are the focused window (avoids notification spam across windows)
		if (notify && changed) {
149 150 151
			this.doConfirm(
				localize('relaunchSettingMessage', "A setting has changed that requires a restart to take effect."),
				localize('relaunchSettingDetail', "Press the restart button to restart {0} and enable the setting.", this.envService.appNameLong),
152
				localize('restart', "&&Restart"),
153 154 155 156 157
				() => this.windowsService.relaunch(Object.create(null))
			);
		}
	}

158 159 160 161
	private handleWorkbenchState(): void {

		// React to folder changes when we are in workspace state
		if (this.contextService.getWorkbenchState() === WorkbenchState.WORKSPACE) {
162 163 164

			// Update our known first folder path if we entered workspace
			const workspace = this.contextService.getWorkspace();
B
Benjamin Pasero 已提交
165
			this.firstFolderResource = workspace.folders.length > 0 ? workspace.folders[0].uri : void 0;
166 167

			// Install workspace folder listener
168
			if (!this.onDidChangeWorkspaceFoldersUnbind) {
169
				this.onDidChangeWorkspaceFoldersUnbind = this.contextService.onDidChangeWorkspaceFolders(() => this.onDidChangeWorkspaceFolders());
170 171 172 173 174 175 176 177 178
			}
		}

		// Ignore the workspace folder changes in EMPTY or FOLDER state
		else {
			this.onDidChangeWorkspaceFoldersUnbind = dispose(this.onDidChangeWorkspaceFoldersUnbind);
		}
	}

S
Sandeep Somavarapu 已提交
179
	private onDidChangeWorkspaceFolders(): void {
180 181
		const workspace = this.contextService.getWorkspace();

182
		// Restart extension host if first root folder changed (impact on deprecated workspace.rootPath API)
B
Benjamin Pasero 已提交
183 184 185
		const newFirstFolderResource = workspace.folders.length > 0 ? workspace.folders[0].uri : void 0;
		if (!isEqual(this.firstFolderResource, newFirstFolderResource, !isLinux)) {
			this.firstFolderResource = newFirstFolderResource;
186

187
			this.extensionHostRestarter.schedule(); // buffer calls to extension host restart
188
		}
189
	}
190 191 192 193

	private doConfirm(message: string, detail: string, primaryButton: string, confirmed: () => void): void {
		this.windowService.isFocused().then(focused => {
			if (focused) {
194
				return this.dialogService.confirm({
195 196 197 198
					type: 'info',
					message,
					detail,
					primaryButton
199 200
				}).then(res => {
					if (res.confirmed) {
201 202
						confirmed();
					}
203 204
				});
			}
205 206

			return void 0;
207
		});
208 209 210
	}
}

211
const workbenchRegistry = Registry.as<IWorkbenchContributionsRegistry>(WorkbenchExtensions.Workbench);
212
workbenchRegistry.registerWorkbenchContribution(SettingsChangeRelauncher, LifecyclePhase.Running);