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

E
Erich Gamma 已提交
6 7
'use strict';

J
Joao Moreno 已提交
8 9 10 11
import {localize} from 'vs/nls';
import {TPromise} from 'vs/base/common/winjs.base';
import {Action} from 'vs/base/common/actions';
import {firstIndex} from 'vs/base/common/arrays';
E
Erich Gamma 已提交
12 13
import {SyncActionDescriptor} from 'vs/platform/actions/common/actions';
import {IMessageService, Severity} from 'vs/platform/message/common/message';
J
Joao Moreno 已提交
14 15
import {Registry} from 'vs/platform/platform';
import {IWorkbenchActionRegistry, Extensions} from 'vs/workbench/common/actionRegistry';
16
import {IQuickOpenService, IPickOpenEntry} from 'vs/workbench/services/quickopen/common/quickOpenService';
M
Martin Aeschlimann 已提交
17
import {IThemeService} from 'vs/workbench/services/themes/common/themeService';
J
Joao Moreno 已提交
18 19 20 21
import {VIEWLET_ID, IExtensionsViewlet} from 'vs/workbench/parts/extensions/electron-browser/extensions';
import {IExtensionGalleryService} from 'vs/platform/extensionManagement/common/extensionManagement';
import {IViewletService} from 'vs/workbench/services/viewlet/common/viewletService';
import {Delayer} from 'vs/base/common/async';
E
Erich Gamma 已提交
22

J
Joao Moreno 已提交
23
class SelectThemeAction extends Action {
E
Erich Gamma 已提交
24

J
Joao Moreno 已提交
25 26
	static ID = 'workbench.action.selectTheme';
	static LABEL = localize('selectTheme.label', "Color Theme");
E
Erich Gamma 已提交
27 28 29 30 31 32

	constructor(
		id: string,
		label: string,
		@IQuickOpenService private quickOpenService: IQuickOpenService,
		@IMessageService private messageService: IMessageService,
J
Joao Moreno 已提交
33 34 35
		@IThemeService private themeService: IThemeService,
		@IExtensionGalleryService private extensionGalleryService: IExtensionGalleryService,
		@IViewletService private viewletService: IViewletService
E
Erich Gamma 已提交
36 37 38 39
	) {
		super(id, label);
	}

J
Joao Moreno 已提交
40
	run(): TPromise<void> {
M
Martin Aeschlimann 已提交
41 42
		return this.themeService.getColorThemes().then(themes => {
			const currentThemeId = this.themeService.getColorTheme();
J
Joao Moreno 已提交
43
			const currentTheme = themes.filter(theme => theme.id === currentThemeId)[0];
E
Erich Gamma 已提交
44

J
Joao Moreno 已提交
45 46 47
			const picks: IPickOpenEntry[] = themes
				.map(theme => ({ id: theme.id, label: theme.label, description: theme.description }))
				.sort((t1, t2) => t1.label.localeCompare(t2.label));
E
Erich Gamma 已提交
48

J
Joao Moreno 已提交
49
			const selectTheme = (theme, broadcast) => {
M
Martin Aeschlimann 已提交
50
				this.themeService.setColorTheme(theme.id, broadcast)
J
Joao Moreno 已提交
51 52
					.done(null, err => this.messageService.show(Severity.Info, localize('problemChangingTheme', "Problem loading theme: {0}", err.message)));
			};
E
Erich Gamma 已提交
53

J
Joao Moreno 已提交
54 55 56
			const placeHolder = localize('themes.selectTheme', "Select Color Theme");
			const autoFocusIndex = firstIndex(picks, p => p.id === currentThemeId);
			const delayer = new Delayer<void>(100);
E
Erich Gamma 已提交
57

J
Joao Moreno 已提交
58 59 60 61 62
			if (this.extensionGalleryService.isEnabled()) {
				const run = () => {
					return this.viewletService.openViewlet(VIEWLET_ID, true)
						.then(viewlet => viewlet as IExtensionsViewlet)
						.then(viewlet => {
63
							viewlet.search('category:themes');
J
Joao Moreno 已提交
64 65 66
							viewlet.focus();
						});
				};
E
Erich Gamma 已提交
67

J
Joao Moreno 已提交
68 69 70 71
				picks.push({
					id: 'themes.findmore',
					label: localize('findMore', "Find more in the Marketplace..."),
					separator: { border: true },
72
					alwaysShow: true,
J
Joao Moreno 已提交
73 74 75
					run
				});
			}
E
Erich Gamma 已提交
76

J
Joao Moreno 已提交
77 78 79 80 81 82
			return this.quickOpenService.pick(picks, { placeHolder, autoFocus: { autoFocusIndex }})
				.then(
					theme => delayer.trigger(() => selectTheme(theme || currentTheme, true), 0),
					null,
					theme => delayer.trigger(() => selectTheme(theme, false))
				);
E
Erich Gamma 已提交
83 84 85 86
		});
	}
}

J
Joao Moreno 已提交
87 88 89
const category = localize('preferences', "Preferences");
const descriptor = new SyncActionDescriptor(SelectThemeAction, SelectThemeAction.ID, SelectThemeAction.LABEL);
Registry.as<IWorkbenchActionRegistry>(Extensions.WorkbenchActions).registerWorkbenchAction(descriptor, 'Preferences: Color Theme', category);