themes.contribution.ts 11.4 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';
25
import { IQuickInputService, 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 49 50 51 52 53
			const picks: QuickPickInput<ThemeItem>[] = [
				...toEntries(themes.filter(t => t.type === LIGHT), localize('themes.category.light', "light themes")),
				...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")),
				...configurationEntries(this.extensionGalleryService, localize('installColorThemes', "Install Additional Color Themes..."))
			];
E
Erich Gamma 已提交
54

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

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

77
			const placeHolder = localize('themes.selectTheme', "Select Color Theme (Up/Down Keys to Preview)");
78 79
			const autoFocusIndex = firstIndex(picks, p => isItem(p) && p.id === currentTheme.id);
			const activeItem: ThemeItem = picks[autoFocusIndex] as ThemeItem;
J
Joao Moreno 已提交
80
			const delayer = new Delayer<void>(100);
81 82
			const chooseTheme = (theme: ThemeItem) => delayer.trigger(() => selectTheme(theme || currentTheme, true), 0);
			const tryTheme = (theme: ThemeItem) => delayer.trigger(() => selectTheme(theme, false));
E
Erich Gamma 已提交
83

84
			return this.quickInputService.pick(picks, { placeHolder, activeItem, onDidFocus: tryTheme })
85
				.then(chooseTheme);
E
Erich Gamma 已提交
86 87 88 89
		});
	}
}

90 91
class SelectIconThemeAction extends Action {

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

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

104 105 106 107
	) {
		super(id, label);
	}

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

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

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

			const placeHolder = localize('themes.selectIconTheme', "Select File Icon Theme");
140 141
			const autoFocusIndex = firstIndex(picks, p => isItem(p) && p.id === currentTheme.id);
			const activeItem: ThemeItem = picks[autoFocusIndex] as ThemeItem;
142
			const delayer = new Delayer<void>(100);
143 144
			const chooseTheme = (theme: ThemeItem) => delayer.trigger(() => selectTheme(theme || currentTheme, true), 0);
			const tryTheme = (theme: ThemeItem) => delayer.trigger(() => selectTheme(theme, false));
145

146
			return this.quickInputService.pick(picks, { placeHolder, activeItem, onDidFocus: tryTheme })
147
				.then(chooseTheme);
148 149 150 151
		});
	}
}

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

C
Christof Marti 已提交
168 169
function openExtensionViewlet(viewletService: IViewletService, query: string) {
	return viewletService.openViewlet(VIEWLET_ID, true).then(viewlet => {
170 171 172 173
		if (viewlet) {
			(viewlet as IExtensionsViewlet).search(query);
			viewlet.focus();
		}
C
Christof Marti 已提交
174 175
	});
}
176 177 178 179 180 181 182 183 184 185
interface ThemeItem {
	id: string | undefined;
	label: string;
	description?: string;
	alwaysShow?: boolean;
}

function isItem(i: QuickPickInput<ThemeItem>): i is ThemeItem {
	return i['type'] !== 'separatpr';
}
C
Christof Marti 已提交
186

187 188 189 190
function toEntries(themes: Array<IColorTheme | IFileIconTheme>, label?: string): QuickPickInput<ThemeItem>[] {
	const toEntry = (theme: IColorTheme): ThemeItem => ({ id: theme.id, label: theme.label, description: theme.description });
	const sorter = (t1: ThemeItem, t2: ThemeItem) => t1.label.localeCompare(t2.label);
	let entries: QuickPickInput<ThemeItem>[] = themes.map(toEntry).sort(sorter);
C
Christof Marti 已提交
191 192
	if (entries.length > 0 && label) {
		entries.unshift({ type: 'separator', label });
193 194
	}
	return entries;
195 196
}

197 198
class GenerateColorThemeAction extends Action {

199
	static readonly ID = 'workbench.action.generateColorTheme';
200 201 202 203 204
	static LABEL = localize('generateColorTheme.label', "Generate Color Theme From Current Settings");

	constructor(
		id: string,
		label: string,
205 206
		@IWorkbenchThemeService private readonly themeService: IWorkbenchThemeService,
		@IEditorService private readonly editorService: IEditorService,
207 208 209 210
	) {
		super(id, label);
	}

J
Johannes Rieken 已提交
211
	run(): Promise<any> {
212
		let theme = this.themeService.getColorTheme();
213 214
		let colors = Registry.as<IColorRegistry>(ColorRegistryExtensions.ColorContribution).getColors();
		let colorIds = colors.map(c => c.id).sort();
215
		let resultingColors = {};
M
Matt Bierner 已提交
216
		let inherited: string[] = [];
217
		for (let colorId of colorIds) {
M
Matt Bierner 已提交
218
			const color = theme.getColor(colorId, false);
219
			if (color) {
220 221 222
				resultingColors[colorId] = Color.Format.CSS.formatHexA(color, true);
			} else {
				inherited.push(colorId);
223
			}
224 225
		}
		for (let id of inherited) {
M
Matt Bierner 已提交
226
			const color = theme.getColor(id);
227 228 229 230
			if (color) {
				resultingColors['__' + id] = Color.Format.CSS.formatHexA(color, true);
			}
		}
231
		let contents = JSON.stringify({
232
			'$schema': colorThemeSchemaId,
233 234
			type: theme.type,
			colors: resultingColors,
235
			tokenColors: theme.tokenColors.filter(t => !!t.scope)
236
		}, null, '\t');
237 238
		contents = contents.replace(/\"__/g, '//"');

239
		return this.editorService.openEditor({ contents, mode: 'jsonc' });
240 241 242
	}
}

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

C
Christof Marti 已提交
245
const colorThemeDescriptor = new SyncActionDescriptor(SelectColorThemeAction, SelectColorThemeAction.ID, SelectColorThemeAction.LABEL, { primary: KeyChord(KeyMod.CtrlCmd | KeyCode.KEY_K, KeyMod.CtrlCmd | KeyCode.KEY_T) });
246 247 248 249
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);
250 251 252 253 254 255


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 已提交
256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273

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
});