vsc-material-theme/extensions/commands/accents-setter/index.ts

148 lines
3.7 KiB
TypeScript
Raw Normal View History

2018-04-20 20:07:36 +02:00
import * as vscode from 'vscode';
import {IAccentCustomProperty} from './../../interfaces/iaccent-custom-property';
import {IGenericObject} from './../../interfaces/igeneric-object';
import {
updateAccent,
isMaterialTheme,
isMaterialThemeIcons
} from './../../helpers/settings';
import {
getCurrentThemeID,
getCurrentThemeIconsID,
reloadWindow
} from './../../helpers/vscode';
import {getDefaultValues} from './../../helpers/fs';
import {THEME_ICONS} from './../theme-icons/index';
2018-04-20 20:07:36 +02:00
const REGEXP_HEX: RegExp = /^#([0-9A-F]{6}|[0-9A-F]{8})$/i;
const accentsProperties: IGenericObject <IAccentCustomProperty> = {
'activityBarBadge.background': {
2018-04-20 20:07:36 +02:00
alpha: 100,
value: undefined
},
'list.activeSelectionForeground': {
2018-04-20 20:07:36 +02:00
alpha: 100,
value: undefined
},
'list.inactiveSelectionForeground': {
2018-04-20 20:07:36 +02:00
alpha: 100,
value: undefined
},
'list.highlightForeground': {
2018-04-20 20:07:36 +02:00
alpha: 100,
value: undefined
},
'scrollbarSlider.activeBackground': {
2018-04-20 20:07:36 +02:00
alpha: 50,
value: undefined
},
'editorSuggestWidget.highlightForeground': {
2018-04-20 20:07:36 +02:00
alpha: 100,
value: undefined
},
'textLink.foreground': {
2018-04-20 20:07:36 +02:00
alpha: 100,
value: undefined
},
'progressBar.background': {
2018-04-20 20:07:36 +02:00
alpha: 100,
value: undefined
},
'pickerGroup.foreground': {
2018-04-20 20:07:36 +02:00
alpha: 100,
value: undefined
},
'tab.activeBorder': {
2018-04-20 20:07:36 +02:00
alpha: 100,
value: undefined
},
'notificationLink.foreground': {
2018-04-20 20:07:36 +02:00
alpha: 100,
value: undefined
},
"editor.findWidgetResizeBorder": {
alpha: 100,
value: undefined
},
"editorWidget.border": {
alpha: 100,
value: undefined
2018-04-20 20:07:36 +02:00
}
};
2018-04-20 20:07:36 +02:00
/**
* Assigns colours
*/
const assignColorCustomizations = (colour: string, config: any): void => {
const newColour = isValidColour(colour) ? colour : undefined;
2018-04-20 20:07:36 +02:00
Object.keys(accentsProperties).forEach(propertyName => {
const accent: IAccentCustomProperty = accentsProperties[propertyName];
let colorProp = newColour;
2018-04-20 20:07:36 +02:00
if (colour && accent.alpha < 100) {
colorProp = `${ colour }${ accent.alpha > 10 ? accent.alpha : `0${ accent.alpha }` }`;
2018-04-20 20:07:36 +02:00
}
if (accent) {
config[propertyName] = colorProp;
2018-04-20 20:07:36 +02:00
}
});
};
2018-04-20 20:07:36 +02:00
/**
* Determines if a string is a valid colour
*/
const isValidColour = (colour: string | null | undefined): boolean =>
typeof colour === 'string' && REGEXP_HEX.test(colour);
2018-04-20 20:07:36 +02:00
/**
* Sets workbench options
*/
const setWorkbenchOptions = (accentSelected: string | undefined, config: any): void => {
2018-04-20 20:07:36 +02:00
vscode.workspace.getConfiguration().update('workbench.colorCustomizations', config, true).then(() => {
const themeID = getCurrentThemeID();
const themeIconsID = getCurrentThemeIconsID();
2018-04-20 20:07:36 +02:00
updateAccent(accentSelected).then(() => {
if (isMaterialTheme(themeID) && isMaterialThemeIcons(themeIconsID)) {
THEME_ICONS().then(() => reloadWindow());
}
});
}, reason => {
vscode.window.showErrorMessage(reason);
});
};
2018-04-20 20:07:36 +02:00
/**
* VSCode command
*/
export const THEME_ACCENTS_SETTER = () => {
const themeConfigCommon = getDefaultValues();
2018-04-20 20:07:36 +02:00
// shows the quick pick dropdown
const options: string[] = Object.keys(themeConfigCommon.accents);
const purgeColourKey: string = 'Remove accents';
2018-04-20 20:07:36 +02:00
options.push(purgeColourKey);
vscode.window.showQuickPick(options).then(accentSelected => {
if (accentSelected === null || accentSelected === undefined) {
return;
}
2018-04-20 20:07:36 +02:00
const config: any = vscode.workspace.getConfiguration().get('workbench.colorCustomizations');
2018-04-20 20:07:36 +02:00
switch (accentSelected) {
2018-04-20 20:07:36 +02:00
case purgeColourKey:
assignColorCustomizations(undefined, config);
setWorkbenchOptions(undefined, config);
break;
2018-04-20 20:07:36 +02:00
default:
assignColorCustomizations(themeConfigCommon.accents[accentSelected], config);
setWorkbenchOptions(accentSelected, config);
}
});
};