themes.contribution.ts 4.2 KB
Newer Older
E
Erich Gamma 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14
/*---------------------------------------------------------------------------------------------
 *  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 Constants = require('vs/workbench/common/constants');
import {SyncActionDescriptor} from 'vs/platform/actions/common/actions';
import {IMessageService, Severity} from 'vs/platform/message/common/message';
import {IStorageService, StorageScope} from 'vs/platform/storage/common/storage';
import platform = require('vs/platform/platform');
15
import workbenchActionRegistry = require('vs/workbench/common/actionRegistry');
16
import {IQuickOpenService, IPickOpenEntry} from 'vs/workbench/services/quickopen/common/quickOpenService';
E
Erich Gamma 已提交
17
import {IWorkspaceContextService} from 'vs/platform/workspace/common/workspace';
18
import {IThemeService, IThemeData, DEFAULT_THEME_ID} from 'vs/workbench/services/themes/common/themeService';
19
import {RunOnceScheduler} from 'vs/base/common/async';
E
Erich Gamma 已提交
20

B
Benjamin Pasero 已提交
21
import {ipcRenderer as ipc} from 'electron';
E
Erich Gamma 已提交
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39

class SelectThemeAction extends actions.Action {

	public static ID = 'workbench.action.selectTheme';
	public static LABEL = nls.localize('selectTheme.label', 'Color Theme');

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

40
	public run(): winjs.TPromise<void> {
E
Erich Gamma 已提交
41 42

		return this.themeService.getThemes().then(contributedThemes => {
43
			let currentTheme = this.storageService.get(Constants.Preferences.THEME, StorageScope.GLOBAL, DEFAULT_THEME_ID);
E
Erich Gamma 已提交
44 45 46

			let picks: IPickOpenEntry[] = [];

47
			let contributedThemesById: { [id: string]: IThemeData } = {};
E
Erich Gamma 已提交
48 49 50 51 52 53 54
			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));

55
			let selectedPickIndex: number;
E
Erich Gamma 已提交
56 57 58 59 60 61
			picks.forEach((p, index) => {
				if (p.id === currentTheme) {
					selectedPickIndex = index;
				}
			});

62
			let selectTheme = pick => {
E
Erich Gamma 已提交
63 64 65 66 67 68 69
				if (pick) {
					let themeId = pick.id;
					if (!contributedThemesById[themeId]) {
						// built-in theme
						ipc.send('vscode:changeTheme', themeId);
					} else {
						// before applying, check that it can be loaded
70
						return this.themeService.applyThemeCSS(themeId).then(_ => {
E
Erich Gamma 已提交
71 72 73 74 75 76 77 78 79 80 81 82 83
							ipc.send('vscode:changeTheme', themeId);
						}, error => {
							this.messageService.show(Severity.Info, nls.localize('problemChangingTheme', "Problem loading theme: {0}", error.message));
						});
					}
				} else {
					// undo changes
					if (this.storageService.get(Constants.Preferences.THEME, StorageScope.GLOBAL) !== currentTheme) {
						ipc.send('vscode:changeTheme', currentTheme);
					}
				}
			};

84 85 86 87 88 89 90 91 92 93 94 95 96
			let themeToPreview : IPickOpenEntry = null;
			let previewThemeScheduler = new RunOnceScheduler(() => {
				selectTheme(themeToPreview);
			}, 200);
			let previewTheme = pick => {
				themeToPreview = pick;
				previewThemeScheduler.schedule();
			};
			let pickTheme = pick => {
				previewThemeScheduler.dispose();
				selectTheme(pick);
			};
			return this.quickOpenService.pick(picks, { placeHolder: nls.localize('themes.selectTheme', "Select Color Theme"), autoFocus: { autoFocusIndex: selectedPickIndex } }).then(pickTheme, null, previewTheme);
E
Erich Gamma 已提交
97 98 99 100 101
		});
	}
}

const category = nls.localize('preferences', "Preferences");
102
let workbenchActionsRegistry = <workbenchActionRegistry.IWorkbenchActionRegistry>platform.Registry.as(workbenchActionRegistry.Extensions.WorkbenchActions);
E
Erich Gamma 已提交
103
workbenchActionsRegistry.registerWorkbenchAction(new SyncActionDescriptor(SelectThemeAction, SelectThemeAction.ID, SelectThemeAction.LABEL), category);