themes.contribution.ts 3.5 KB
Newer Older
E
Erich Gamma 已提交
1 2 3 4 5 6 7 8 9 10 11 12
/*---------------------------------------------------------------------------------------------
 *  Copyright (c) Microsoft Corporation. All rights reserved.
 *  Licensed under the MIT License. See License.txt in the project root for license information.
 *--------------------------------------------------------------------------------------------*/
'use strict';

import nls = require('vs/nls');
import winjs = require('vs/base/common/winjs.base');
import actions = require('vs/base/common/actions');
import {SyncActionDescriptor} from 'vs/platform/actions/common/actions';
import {IMessageService, Severity} from 'vs/platform/message/common/message';
import platform = require('vs/platform/platform');
13
import workbenchActionRegistry = require('vs/workbench/common/actionRegistry');
14
import {IQuickOpenService, IPickOpenEntry} from 'vs/workbench/services/quickopen/common/quickOpenService';
E
Erich Gamma 已提交
15
import {IWorkspaceContextService} from 'vs/platform/workspace/common/workspace';
M
Martin Aeschlimann 已提交
16
import {IThemeService} from 'vs/workbench/services/themes/common/themeService';
17
import {RunOnceScheduler} from 'vs/base/common/async';
E
Erich Gamma 已提交
18 19 20 21

class SelectThemeAction extends actions.Action {

	public static ID = 'workbench.action.selectTheme';
22
	public static LABEL = nls.localize('selectTheme.label', "Color Theme");
E
Erich Gamma 已提交
23 24 25 26 27 28 29 30 31 32 33 34

	constructor(
		id: string,
		label: string,
		@IWorkspaceContextService private contextService: IWorkspaceContextService,
		@IQuickOpenService private quickOpenService: IQuickOpenService,
		@IMessageService private messageService: IMessageService,
		@IThemeService private themeService: IThemeService
	) {
		super(id, label);
	}

35
	public run(): winjs.TPromise<void> {
E
Erich Gamma 已提交
36 37

		return this.themeService.getThemes().then(contributedThemes => {
M
Martin Aeschlimann 已提交
38
			let currentTheme = this.themeService.getTheme();
E
Erich Gamma 已提交
39 40 41 42 43 44 45 46 47 48

			let picks: IPickOpenEntry[] = [];

			contributedThemes.forEach(theme => {
				picks.push({ id: theme.id, label: theme.label, description: theme.description });
				contributedThemes[theme.id] = theme;
			});

			picks = picks.sort((t1, t2) => t1.label.localeCompare(t2.label));

49
			let selectedPickIndex: number;
E
Erich Gamma 已提交
50 51 52 53 54 55
			picks.forEach((p, index) => {
				if (p.id === currentTheme) {
					selectedPickIndex = index;
				}
			});

M
Martin Aeschlimann 已提交
56
			let selectTheme = (pick, broadcast) => {
E
Erich Gamma 已提交
57 58
				if (pick) {
					let themeId = pick.id;
M
Martin Aeschlimann 已提交
59 60 61
					this.themeService.setTheme(themeId, broadcast).then(null, error => {
						this.messageService.show(Severity.Info, nls.localize('problemChangingTheme', "Problem loading theme: {0}", error.message));
					});
E
Erich Gamma 已提交
62
				} else {
M
Martin Aeschlimann 已提交
63
					this.themeService.setTheme(currentTheme, broadcast);
E
Erich Gamma 已提交
64 65 66
				}
			};

67 68
			let themeToPreview : IPickOpenEntry = null;
			let previewThemeScheduler = new RunOnceScheduler(() => {
M
Martin Aeschlimann 已提交
69
				selectTheme(themeToPreview, false);
M
Martin Aeschlimann 已提交
70
			}, 100);
71 72 73 74 75 76
			let previewTheme = pick => {
				themeToPreview = pick;
				previewThemeScheduler.schedule();
			};
			let pickTheme = pick => {
				previewThemeScheduler.dispose();
M
Martin Aeschlimann 已提交
77
				selectTheme(pick, true);
78 79
			};
			return this.quickOpenService.pick(picks, { placeHolder: nls.localize('themes.selectTheme', "Select Color Theme"), autoFocus: { autoFocusIndex: selectedPickIndex } }).then(pickTheme, null, previewTheme);
E
Erich Gamma 已提交
80 81 82 83 84
		});
	}
}

const category = nls.localize('preferences', "Preferences");
85
let workbenchActionsRegistry = <workbenchActionRegistry.IWorkbenchActionRegistry>platform.Registry.as(workbenchActionRegistry.Extensions.WorkbenchActions);
86
workbenchActionsRegistry.registerWorkbenchAction(new SyncActionDescriptor(SelectThemeAction, SelectThemeAction.ID, SelectThemeAction.LABEL), 'Preferences: Color Theme', category);