welcomePage.ts 6.2 KB
Newer Older
C
Christof Marti 已提交
1 2 3 4 5 6
/*---------------------------------------------------------------------------------------------
 *  Copyright (c) Microsoft Corporation. All rights reserved.
 *  Licensed under the MIT License. See License.txt in the project root for license information.
 *--------------------------------------------------------------------------------------------*/
'use strict';

7
import 'vs/css!./welcomePage';
C
Christof Marti 已提交
8 9
import URI from 'vs/base/common/uri';
import * as path from 'path';
10 11
import * as platform from 'vs/base/common/platform';
import * as strings from 'vs/base/common/strings';
C
Christof Marti 已提交
12
import { WalkThroughInput } from 'vs/workbench/parts/walkThrough/node/walkThroughInput';
C
Christof Marti 已提交
13 14 15 16 17 18 19 20 21 22 23 24 25
import { IWorkbenchContribution } from 'vs/workbench/common/contributions';
import { IPartService } from 'vs/workbench/services/part/common/partService';
import { IInstantiationService } from 'vs/platform/instantiation/common/instantiation';
import { IWorkbenchEditorService } from 'vs/workbench/services/editor/common/editorService';
import { Position } from 'vs/platform/editor/common/editor';
import { onUnexpectedError } from 'vs/base/common/errors';
import { IWindowService, IWindowsService } from 'vs/platform/windows/common/windows';
import { TPromise } from 'vs/base/common/winjs.base';
import { IWorkspaceContextService } from 'vs/platform/workspace/common/workspace';
import { IConfigurationService } from 'vs/platform/configuration/common/configuration';
import { IConfigurationEditingService, ConfigurationTarget } from 'vs/workbench/services/configuration/common/configurationEditing';
import { localize } from 'vs/nls';
import { Action } from 'vs/base/common/actions';
C
Christof Marti 已提交
26
import { ITelemetryService } from 'vs/platform/telemetry/common/telemetry';
27
import { UntitledEditorInput } from 'vs/workbench/common/editor/untitledEditorInput';
28
import { IEnvironmentService } from 'vs/platform/environment/common/environment';
29
import { Schemas } from 'vs/base/common/network';
C
Christof Marti 已提交
30 31 32 33 34 35 36 37 38

const enabledKey = 'workbench.welcome.enabled';

export class WelcomePageContribution implements IWorkbenchContribution {

	constructor(
		@IPartService partService: IPartService,
		@IInstantiationService instantiationService: IInstantiationService,
		@IConfigurationService configurationService: IConfigurationService,
39
		@IWorkbenchEditorService editorService: IWorkbenchEditorService,
C
Christof Marti 已提交
40
		@ITelemetryService telemetryService: ITelemetryService
C
Christof Marti 已提交
41
	) {
42
		const enabled = configurationService.lookup<boolean>(enabledKey).value;
C
Christof Marti 已提交
43 44
		if (enabled) {
			partService.joinCreation().then(() => {
45 46 47 48
				const activeInput = editorService.getActiveEditorInput();
				if (!activeInput || activeInput instanceof UntitledEditorInput) {
					instantiationService.createInstance(WelcomePage);
				}
C
Christof Marti 已提交
49 50 51 52 53 54 55 56 57 58 59
			}).then(null, onUnexpectedError);
		}
	}

	public getId() {
		return 'vs.welcomePage';
	}
}

export class WelcomePageAction extends Action {

C
Christof Marti 已提交
60
	public static ID = 'workbench.action.showWelcomePage';
C
Christof Marti 已提交
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85
	public static LABEL = localize('welcomePage', "Welcome");

	constructor(
		id: string,
		label: string,
		@IInstantiationService private instantiationService: IInstantiationService
	) {
		super(id, label);
	}

	public run(): TPromise<void> {
		this.instantiationService.createInstance(WelcomePage);
		return null;
	}
}

class WelcomePage {

	constructor(
		@IWorkbenchEditorService private editorService: IWorkbenchEditorService,
		@IInstantiationService private instantiationService: IInstantiationService,
		@IWindowService private windowService: IWindowService,
		@IWindowsService private windowsService: IWindowsService,
		@IWorkspaceContextService private contextService: IWorkspaceContextService,
		@IConfigurationService private configurationService: IConfigurationService,
C
Christof Marti 已提交
86
		@IConfigurationEditingService private configurationEditingService: IConfigurationEditingService,
87
		@IEnvironmentService private environmentService: IEnvironmentService,
C
Christof Marti 已提交
88
		@ITelemetryService private telemetryService: ITelemetryService
C
Christof Marti 已提交
89 90 91 92 93 94
	) {
		this.create();
	}

	private create() {
		const recentlyOpened = this.windowService.getRecentlyOpen();
C
Christof Marti 已提交
95
		const uri = URI.parse(require.toUrl('./welcomePage.html'))
96
			.with({ scheme: Schemas.walkThrough });
C
Christof Marti 已提交
97
		const input = this.instantiationService.createInstance(WalkThroughInput, localize('welcome.title', "Welcome"), '', uri, 'welcomePage', container => this.onReady(container, recentlyOpened));
C
Christof Marti 已提交
98 99 100 101 102
		this.editorService.openEditor(input, { pinned: true }, Position.ONE)
			.then(null, onUnexpectedError);
	}

	private onReady(container: HTMLElement, recentlyOpened: TPromise<{ files: string[]; folders: string[]; }>): void {
103
		const enabled = this.configurationService.lookup<boolean>(enabledKey).value;
C
Christof Marti 已提交
104 105 106 107 108 109 110 111 112 113
		const showOnStartup = <HTMLInputElement>container.querySelector('#showOnStartup');
		if (enabled) {
			showOnStartup.setAttribute('checked', 'checked');
		}
		showOnStartup.addEventListener('click', e => {
			this.configurationEditingService.writeConfiguration(ConfigurationTarget.USER, { key: enabledKey, value: showOnStartup.checked })
				.then(null, onUnexpectedError);
		});

		recentlyOpened.then(({folders}) => {
C
Christof Marti 已提交
114 115 116 117
			if (this.contextService.hasWorkspace()) {
				const current = this.contextService.getWorkspace().resource.fsPath;
				folders = folders.filter(folder => folder !== current);
			}
C
Christof Marti 已提交
118
			if (!folders.length) {
119 120
				const recent = container.querySelector('.welcomePage') as HTMLElement;
				recent.classList.add('emptyRecent');
C
Christof Marti 已提交
121 122
				return;
			}
C
Christof Marti 已提交
123 124 125 126 127 128 129 130 131 132 133 134
			const ul = container.querySelector('.recent ul');
			folders.slice(0, 5).forEach(folder => {
				const li = document.createElement('li');

				const a = document.createElement('a');
				const name = path.basename(folder);
				a.innerText = name;
				a.title = folder;
				a.href = 'javascript:void(0)';
				a.addEventListener('click', e => {
					this.windowsService.openWindow([folder]);
					e.preventDefault();
C
Christof Marti 已提交
135
					e.stopPropagation();
C
Christof Marti 已提交
136 137 138 139 140
				});
				li.appendChild(a);

				const span = document.createElement('span');
				span.classList.add('path');
141 142 143 144
				let parentFolder = path.dirname(folder);
				if ((platform.isMacintosh || platform.isLinux) && strings.startsWith(parentFolder, this.environmentService.userHome)) {
					parentFolder = `~${parentFolder.substr(this.environmentService.userHome.length)}`;
				}
C
Christof Marti 已提交
145 146 147 148 149 150 151 152 153
				span.innerText = parentFolder;
				span.title = folder;
				li.appendChild(span);

				ul.appendChild(li);
			});
		}).then(null, onUnexpectedError);
	}
}