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

134 lines
3.4 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,
} from './../../helpers/settings';
import {getDefaultValues} from './../../helpers/fs';
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
},
'settings.modifiedItemForeground': {
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): Thenable<string> =>
vscode.workspace.getConfiguration().update('workbench.colorCustomizations', config, true)
.then(() => updateAccent(accentSelected),
reason => vscode.window.showErrorMessage(reason));
2018-04-20 20:07:36 +02:00
/**
* VSCode command
*/
export default async (): Promise<boolean> => {
const themeConfigCommon = getDefaultValues();
const purgeColourKey: string = 'Remove accents';
const options: string[] = Object.keys(themeConfigCommon.accents).concat(purgeColourKey);
2018-04-20 20:07:36 +02:00
// shows the quick pick dropdown and wait response
const accentSelected = await vscode.window.showQuickPick(options);
2018-04-20 20:07:36 +02:00
if (accentSelected === null || accentSelected === undefined) {
Promise.resolve(null);
}
2018-04-20 20:07:36 +02:00
const config: any = vscode.workspace.getConfiguration().get('workbench.colorCustomizations');
switch (accentSelected) {
case purgeColourKey:
assignColorCustomizations(undefined, config);
await setWorkbenchOptions(undefined, config);
return Promise.resolve(true);
default:
assignColorCustomizations(themeConfigCommon.accents[accentSelected], config);
await setWorkbenchOptions(accentSelected, config);
return Promise.resolve(true);
}
2018-04-20 20:07:36 +02:00
};