welcomePage.ts 6.0 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';
C
Christof Marti 已提交
10
import { WalkThroughInput } from 'vs/workbench/parts/walkThrough/node/walkThroughInput';
C
Christof Marti 已提交
11 12 13 14 15 16 17 18 19 20 21 22 23
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 已提交
24
import { ITelemetryService } from 'vs/platform/telemetry/common/telemetry';
25
import { UntitledEditorInput } from 'vs/workbench/common/editor/untitledEditorInput';
26
import { WALK_THROUGH_SCHEME } from 'vs/workbench/parts/walkThrough/node/walkThroughContentProvider';
C
Christof Marti 已提交
27 28 29 30 31 32 33 34 35

const enabledKey = 'workbench.welcome.enabled';

export class WelcomePageContribution implements IWorkbenchContribution {

	constructor(
		@IPartService partService: IPartService,
		@IInstantiationService instantiationService: IInstantiationService,
		@IConfigurationService configurationService: IConfigurationService,
36
		@IWorkbenchEditorService editorService: IWorkbenchEditorService,
C
Christof Marti 已提交
37
		@ITelemetryService telemetryService: ITelemetryService
C
Christof Marti 已提交
38
	) {
C
Christof Marti 已提交
39 40
		const configured = configurationService.lookup<boolean>(enabledKey).value;
		const enabled = typeof configured === 'boolean' ? configured : telemetryService.getExperiments().enableWelcomePage;
C
Christof Marti 已提交
41 42
		if (enabled) {
			partService.joinCreation().then(() => {
43 44 45 46
				const activeInput = editorService.getActiveEditorInput();
				if (!activeInput || activeInput instanceof UntitledEditorInput) {
					instantiationService.createInstance(WelcomePage);
				}
C
Christof Marti 已提交
47 48 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 79 80 81 82 83
			}).then(null, onUnexpectedError);
		}
	}

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

export class WelcomePageAction extends Action {

	public static ID = 'workbench.action.welcomePage';
	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 已提交
84 85
		@IConfigurationEditingService private configurationEditingService: IConfigurationEditingService,
		@ITelemetryService private telemetryService: ITelemetryService
C
Christof Marti 已提交
86 87 88 89 90 91
	) {
		this.create();
	}

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

	private onReady(container: HTMLElement, recentlyOpened: TPromise<{ files: string[]; folders: string[]; }>): void {
C
Christof Marti 已提交
100 101
		const configured = this.configurationService.lookup<boolean>(enabledKey).value;
		const enabled = typeof configured === 'boolean' ? configured : this.telemetryService.getExperiments().enableWelcomePage;
C
Christof Marti 已提交
102 103 104 105 106 107 108 109 110 111
		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 已提交
112 113 114 115 116
			if (!folders.length) {
				const recent = container.querySelector('.recent') as HTMLElement;
				recent.classList.add('empty');
				return;
			}
C
Christof Marti 已提交
117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132
			const ul = container.querySelector('.recent ul');
			if (this.contextService.hasWorkspace()) {
				const current = this.contextService.getWorkspace().resource.fsPath;
				folders = folders.filter(folder => folder !== current);
			}
			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 已提交
133
					e.stopPropagation();
C
Christof Marti 已提交
134 135 136 137 138 139 140 141 142 143 144 145 146 147 148
				});
				li.appendChild(a);

				const span = document.createElement('span');
				span.classList.add('path');
				const parentFolder = path.dirname(folder);
				span.innerText = parentFolder;
				span.title = folder;
				li.appendChild(span);

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