themes.contribution.ts 10.9 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

J
Johannes Rieken 已提交
6 7 8
import { localize } from 'vs/nls';
import { Action } from 'vs/base/common/actions';
import { firstIndex } from 'vs/base/common/arrays';
C
Christof Marti 已提交
9
import { KeyMod, KeyChord, KeyCode } from 'vs/base/common/keyCodes';
I
isidor 已提交
10
import { SyncActionDescriptor, MenuRegistry, MenuId } from 'vs/platform/actions/common/actions';
11
import { Registry } from 'vs/platform/registry/common/platform';
B
Benjamin Pasero 已提交
12
import { IWorkbenchActionRegistry, Extensions } from 'vs/workbench/common/actions';
13
import { IWorkbenchThemeService, COLOR_THEME_SETTING, ICON_THEME_SETTING, IColorTheme, IFileIconTheme } from 'vs/workbench/services/themes/common/workbenchThemeService';
14
import { VIEWLET_ID, IExtensionsViewlet } from 'vs/workbench/contrib/extensions/common/extensions';
J
Johannes Rieken 已提交
15
import { IExtensionGalleryService } from 'vs/platform/extensionManagement/common/extensionManagement';
B
Benjamin Pasero 已提交
16
import { IViewletService } from 'vs/workbench/services/viewlet/browser/viewlet';
J
Johannes Rieken 已提交
17
import { Delayer } from 'vs/base/common/async';
18
import { IColorRegistry, Extensions as ColorRegistryExtensions } from 'vs/platform/theme/common/colorRegistry';
19
import { IEditorService } from 'vs/workbench/services/editor/common/editorService';
J
Joao Moreno 已提交
20
import { Color } from 'vs/base/common/color';
21
import { ConfigurationTarget, IConfigurationService } from 'vs/platform/configuration/common/configuration';
22
import { LIGHT, DARK, HIGH_CONTRAST } from 'vs/platform/theme/common/themeService';
23
import { colorThemeSchemaId } from 'vs/workbench/services/themes/common/colorThemeSchema';
24
import { onUnexpectedError } from 'vs/base/common/errors';
C
Christof Marti 已提交
25
import { IQuickInputService, IQuickPickItem, QuickPickInput } from 'vs/platform/quickinput/common/quickInput';
E
Erich Gamma 已提交
26

C
Christof Marti 已提交
27
export class SelectColorThemeAction extends Action {
E
Erich Gamma 已提交
28

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

	constructor(
		id: string,
		label: string,
35 36 37 38
		@IQuickInputService private readonly quickInputService: IQuickInputService,
		@IWorkbenchThemeService private readonly themeService: IWorkbenchThemeService,
		@IExtensionGalleryService private readonly extensionGalleryService: IExtensionGalleryService,
		@IViewletService private readonly viewletService: IViewletService,
39
		@IConfigurationService private readonly configurationService: IConfigurationService
E
Erich Gamma 已提交
40 41 42 43
	) {
		super(id, label);
	}

J
Johannes Rieken 已提交
44
	run(): Promise<void> {
M
Martin Aeschlimann 已提交
45
		return this.themeService.getColorThemes().then(themes => {
M
Martin Aeschlimann 已提交
46
			const currentTheme = this.themeService.getColorTheme();
E
Erich Gamma 已提交
47

48
			const picks: QuickPickInput[] = ([] as QuickPickInput[]).concat(
49
				toEntries(themes.filter(t => t.type === LIGHT), localize('themes.category.light', "light themes")),
C
Christof Marti 已提交
50 51
				toEntries(themes.filter(t => t.type === DARK), localize('themes.category.dark', "dark themes")),
				toEntries(themes.filter(t => t.type === HIGH_CONTRAST), localize('themes.category.hc', "high contrast themes")),
C
Christof Marti 已提交
52
				configurationEntries(this.extensionGalleryService, localize('installColorThemes', "Install Additional Color Themes..."))
53
			);
E
Erich Gamma 已提交
54

55
			const selectTheme = (theme, applyTheme: boolean) => {
56
				if (typeof theme.id === 'undefined') { // 'pick in marketplace' entry
C
Christof Marti 已提交
57
					if (applyTheme) {
A
al 已提交
58
						openExtensionViewlet(this.viewletService, 'category:themes ');
C
Christof Marti 已提交
59
					}
60 61
					theme = currentTheme;
				}
62
				let target: ConfigurationTarget | undefined = undefined;
M
Martin Aeschlimann 已提交
63
				if (applyTheme) {
64
					let confValue = this.configurationService.inspect(COLOR_THEME_SETTING);
M
Martin Aeschlimann 已提交
65 66 67
					target = typeof confValue.workspace !== 'undefined' ? ConfigurationTarget.WORKSPACE : ConfigurationTarget.USER;
				}

R
Rob Lourens 已提交
68
				this.themeService.setColorTheme(theme.id, target).then(undefined,
69
					err => {
70
						onUnexpectedError(err);
71
						this.themeService.setColorTheme(currentTheme.id, undefined);
72 73
					}
				);
J
Joao Moreno 已提交
74
			};
E
Erich Gamma 已提交
75

76
			const placeHolder = localize('themes.selectTheme', "Select Color Theme (Up/Down Keys to Preview)");
C
Christof Marti 已提交
77
			const autoFocusIndex = firstIndex(picks, p => p.type !== 'separator' && p.id === currentTheme.id);
J
Joao Moreno 已提交
78
			const delayer = new Delayer<void>(100);
79 80
			const chooseTheme = theme => delayer.trigger(() => selectTheme(theme || currentTheme, true), 0);
			const tryTheme = theme => delayer.trigger(() => selectTheme(theme, false));
E
Erich Gamma 已提交
81

C
Christof Marti 已提交
82
			return this.quickInputService.pick(picks, { placeHolder, activeItem: picks[autoFocusIndex], onDidFocus: tryTheme })
83
				.then(chooseTheme);
E
Erich Gamma 已提交
84 85 86 87
		});
	}
}

88 89
class SelectIconThemeAction extends Action {

90
	static readonly ID = 'workbench.action.selectIconTheme';
91 92 93 94 95
	static LABEL = localize('selectIconTheme.label', "File Icon Theme");

	constructor(
		id: string,
		label: string,
96 97 98 99
		@IQuickInputService private readonly quickInputService: IQuickInputService,
		@IWorkbenchThemeService private readonly themeService: IWorkbenchThemeService,
		@IExtensionGalleryService private readonly extensionGalleryService: IExtensionGalleryService,
		@IViewletService private readonly viewletService: IViewletService,
100
		@IConfigurationService private readonly configurationService: IConfigurationService
M
Martin Aeschlimann 已提交
101

102 103 104 105
	) {
		super(id, label);
	}

J
Johannes Rieken 已提交
106
	run(): Promise<void> {
107
		return this.themeService.getFileIconThemes().then(themes => {
M
Martin Aeschlimann 已提交
108
			const currentTheme = this.themeService.getFileIconTheme();
109

C
Christof Marti 已提交
110
			let picks: QuickPickInput[] = [{ id: '', label: localize('noIconThemeLabel', 'None'), description: localize('noIconThemeDesc', 'Disable file icons') }];
111 112
			picks = picks.concat(
				toEntries(themes),
C
Christof Marti 已提交
113
				configurationEntries(this.extensionGalleryService, localize('installIconThemes', "Install Additional File Icon Themes..."))
114
			);
115

116
			const selectTheme = (theme, applyTheme: boolean) => {
117
				if (typeof theme.id === 'undefined') { // 'pick in marketplace' entry
C
Christof Marti 已提交
118
					if (applyTheme) {
A
al 已提交
119
						openExtensionViewlet(this.viewletService, 'tag:icon-theme ');
C
Christof Marti 已提交
120
					}
121 122
					theme = currentTheme;
				}
123
				let target: ConfigurationTarget | undefined = undefined;
M
Martin Aeschlimann 已提交
124
				if (applyTheme) {
125
					let confValue = this.configurationService.inspect(ICON_THEME_SETTING);
M
Martin Aeschlimann 已提交
126 127
					target = typeof confValue.workspace !== 'undefined' ? ConfigurationTarget.WORKSPACE : ConfigurationTarget.USER;
				}
R
Rob Lourens 已提交
128
				this.themeService.setFileIconTheme(theme && theme.id, target).then(undefined,
129
					err => {
130
						onUnexpectedError(err);
131
						this.themeService.setFileIconTheme(currentTheme.id, undefined);
132 133
					}
				);
134 135 136
			};

			const placeHolder = localize('themes.selectIconTheme', "Select File Icon Theme");
C
Christof Marti 已提交
137
			const autoFocusIndex = firstIndex(picks, p => p.type !== 'separator' && p.id === currentTheme.id);
138
			const delayer = new Delayer<void>(100);
139 140
			const chooseTheme = theme => delayer.trigger(() => selectTheme(theme || currentTheme, true), 0);
			const tryTheme = theme => delayer.trigger(() => selectTheme(theme, false));
141

C
Christof Marti 已提交
142
			return this.quickInputService.pick(picks, { placeHolder, activeItem: picks[autoFocusIndex], onDidFocus: tryTheme })
143
				.then(chooseTheme);
144 145 146 147
		});
	}
}

C
Christof Marti 已提交
148
function configurationEntries(extensionGalleryService: IExtensionGalleryService, label: string): QuickPickInput[] {
149
	if (extensionGalleryService.isEnabled()) {
C
Christof Marti 已提交
150 151
		return [
			{
C
Christof Marti 已提交
152
				type: 'separator'
C
Christof Marti 已提交
153 154
			},
			{
R
Rob Lourens 已提交
155
				id: undefined,
C
Christof Marti 已提交
156 157 158 159
				label: label,
				alwaysShow: true,
			}
		];
160 161 162 163
	}
	return [];
}

C
Christof Marti 已提交
164 165
function openExtensionViewlet(viewletService: IViewletService, query: string) {
	return viewletService.openViewlet(VIEWLET_ID, true).then(viewlet => {
166 167 168 169
		if (viewlet) {
			(viewlet as IExtensionsViewlet).search(query);
			viewlet.focus();
		}
C
Christof Marti 已提交
170 171 172
	});
}

173
function toEntries(themes: Array<IColorTheme | IFileIconTheme>, label?: string) {
C
Christof Marti 已提交
174 175 176
	const toEntry = theme => <IQuickPickItem>{ id: theme.id, label: theme.label, description: theme.description };
	const sorter = (t1: IQuickPickItem, t2: IQuickPickItem) => t1.label.localeCompare(t2.label);
	let entries: QuickPickInput[] = themes.map(toEntry).sort(sorter);
C
Christof Marti 已提交
177 178
	if (entries.length > 0 && label) {
		entries.unshift({ type: 'separator', label });
179 180
	}
	return entries;
181 182
}

183 184
class GenerateColorThemeAction extends Action {

185
	static readonly ID = 'workbench.action.generateColorTheme';
186 187 188 189 190
	static LABEL = localize('generateColorTheme.label', "Generate Color Theme From Current Settings");

	constructor(
		id: string,
		label: string,
191 192
		@IWorkbenchThemeService private readonly themeService: IWorkbenchThemeService,
		@IEditorService private readonly editorService: IEditorService,
193 194 195 196
	) {
		super(id, label);
	}

J
Johannes Rieken 已提交
197
	run(): Promise<any> {
198
		let theme = this.themeService.getColorTheme();
199 200
		let colors = Registry.as<IColorRegistry>(ColorRegistryExtensions.ColorContribution).getColors();
		let colorIds = colors.map(c => c.id).sort();
201
		let resultingColors = {};
M
Matt Bierner 已提交
202
		let inherited: string[] = [];
203
		for (let colorId of colorIds) {
M
Matt Bierner 已提交
204
			const color = theme.getColor(colorId, false);
205
			if (color) {
206 207 208
				resultingColors[colorId] = Color.Format.CSS.formatHexA(color, true);
			} else {
				inherited.push(colorId);
209
			}
210 211
		}
		for (let id of inherited) {
M
Matt Bierner 已提交
212
			const color = theme.getColor(id);
213 214 215 216
			if (color) {
				resultingColors['__' + id] = Color.Format.CSS.formatHexA(color, true);
			}
		}
217
		let contents = JSON.stringify({
218
			'$schema': colorThemeSchemaId,
219 220
			type: theme.type,
			colors: resultingColors,
221
			tokenColors: theme.tokenColors.filter(t => !!t.scope)
222
		}, null, '\t');
223 224
		contents = contents.replace(/\"__/g, '//"');

225
		return this.editorService.openEditor({ contents, language: 'jsonc' });
226 227 228
	}
}

J
Joao Moreno 已提交
229
const category = localize('preferences', "Preferences");
230

C
Christof Marti 已提交
231
const colorThemeDescriptor = new SyncActionDescriptor(SelectColorThemeAction, SelectColorThemeAction.ID, SelectColorThemeAction.LABEL, { primary: KeyChord(KeyMod.CtrlCmd | KeyCode.KEY_K, KeyMod.CtrlCmd | KeyCode.KEY_T) });
232 233 234 235
Registry.as<IWorkbenchActionRegistry>(Extensions.WorkbenchActions).registerWorkbenchAction(colorThemeDescriptor, 'Preferences: Color Theme', category);

const iconThemeDescriptor = new SyncActionDescriptor(SelectIconThemeAction, SelectIconThemeAction.ID, SelectIconThemeAction.LABEL);
Registry.as<IWorkbenchActionRegistry>(Extensions.WorkbenchActions).registerWorkbenchAction(iconThemeDescriptor, 'Preferences: File Icon Theme', category);
236 237 238 239 240 241


const developerCategory = localize('developer', "Developer");

const generateColorThemeDescriptor = new SyncActionDescriptor(GenerateColorThemeAction, GenerateColorThemeAction.ID, GenerateColorThemeAction.LABEL);
Registry.as<IWorkbenchActionRegistry>(Extensions.WorkbenchActions).registerWorkbenchAction(generateColorThemeDescriptor, 'Developer: Generate Color Theme From Current Settings', developerCategory);
I
isidor 已提交
242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259

MenuRegistry.appendMenuItem(MenuId.MenubarPreferencesMenu, {
	group: '4_themes',
	command: {
		id: SelectColorThemeAction.ID,
		title: localize({ key: 'miSelectColorTheme', comment: ['&& denotes a mnemonic'] }, "&&Color Theme")
	},
	order: 1
});

MenuRegistry.appendMenuItem(MenuId.MenubarPreferencesMenu, {
	group: '4_themes',
	command: {
		id: SelectIconThemeAction.ID,
		title: localize({ key: 'miSelectIconTheme', comment: ['&& denotes a mnemonic'] }, "File &&Icon Theme")
	},
	order: 2
});