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

174 lines
5.4 KiB
TypeScript
Raw Normal View History

2017-05-29 18:01:55 +02:00
import * as vscode from 'vscode';
import { IAccentCustomProperty } from "../interfaces/iaccent-custom-property";
2017-05-29 18:01:55 +02:00
import { IGenericObject } from "../interfaces/igeneric-object";
import { IThemeConfigCommons } from "../interfaces/icommons";
const REGEXP_HEX: RegExp = /^#([0-9A-F]{6}|[0-9A-F]{8})$/i;
2017-05-29 18:01:55 +02:00
let themeConfigCommon: IThemeConfigCommons = require('./commons.json');
let accentsProperties: IGenericObject<IAccentCustomProperty> = {
"activityBarBadge.background": {
alpha: 100,
value: undefined
},
"list.activeSelectionForeground": {
alpha: 100,
value: undefined
},
"list.inactiveSelectionForeground": {
alpha: 100,
value: undefined
},
"list.highlightForeground": {
alpha: 100,
value: undefined
},
"scrollbarSlider.activeBackground": {
2017-06-06 15:36:38 +02:00
alpha: 50,
value: undefined
},
"editorSuggestWidget.highlightForeground": {
alpha: 100,
value: undefined
},
"textLink.foreground": {
2017-06-06 15:36:38 +02:00
alpha: 100,
value: undefined
},
2017-05-29 18:01:55 +02:00
}
/**
* Assigns colours
* @param {string} colour
* @param {*} config
*/
function assignColorCustomizations(colour: string, config: any): void {
2017-06-08 10:08:46 +02:00
if (!isValidColour(colour)) {
colour = undefined;
}
2017-05-29 18:01:55 +02:00
Object.keys(accentsProperties).forEach(propertyName => {
let accent: IAccentCustomProperty = accentsProperties[propertyName];
2017-06-08 10:08:46 +02:00
let _colour = colour;
if (colour && accent.alpha < 100) {
2017-06-08 10:08:46 +02:00
_colour = `${ colour }${ accent.alpha > 10 ? accent.alpha : `0${ accent.alpha }` }`;
}
if (accent) {
2017-06-08 10:08:46 +02:00
config[propertyName] = _colour;
}
2017-05-29 18:01:55 +02:00
});
}
/**
* Gets the accented icon theme name
* @param accentName
*/
function accentedThemeName(accentName: string): string {
return `material-theme-icons-${ accentName.replace(/\s+/g, '-').toLowerCase() }`;
}
/**
* Assigns related icons theme name by accent name
* @param accentName
*/
function assignIconTheme(accentName: string | undefined): void {
let accentValue: string;
let cacheKey: string = 'materialTheme.cache.workbench.iconTheme';
let cache: any = vscode.workspace.getConfiguration().inspect(cacheKey);
let currentTheme: any = vscode.workspace.getConfiguration().inspect('workbench.colorTheme');
let materialThemeName: string = 'Material Theme';
if (!cache.globalValue && accentName !== undefined) {
vscode.workspace.getConfiguration().update(cacheKey, vscode.workspace.getConfiguration().get('workbench.iconTheme'), true).then(() => {}, reason => vscode.window.showErrorMessage(reason));
}
if (accentName === undefined && cache.globalValue) {
accentValue = vscode.workspace.getConfiguration().get<string>(cacheKey);
vscode.workspace.getConfiguration().update(cacheKey, undefined, true);
} else if (accentName !== undefined) {
accentValue = accentedThemeName(accentName);
if (!!currentTheme.globalValue && currentTheme.globalValue.indexOf(materialThemeName) >= 0) {
let variantName: string | undefined = currentTheme.globalValue.split(materialThemeName)[1];
accentValue = `${ accentValue }-${ !!variantName ? variantName.trim().toLowerCase() : 'default' }`;
}
}
vscode.workspace.getConfiguration().update('workbench.iconTheme', accentValue, true).then(() => {}, reason => {
vscode.window.showErrorMessage(reason);
});
}
/**
* Determines if a string is a valid colour
* @param {(string | null | undefined)} colour
* @returns {boolean}
*/
function isValidColour(colour: string | null | undefined): boolean {
if (typeof colour === 'string' && REGEXP_HEX.test(colour)) {
return true;
}
return false;
}
2017-05-29 18:01:55 +02:00
/**
* Sets workbench options
* @param {string} accentSelected
* @param {*} config
*/
function setWorkbenchOptions(accentSelected: string, config: any): void {
vscode.workspace.getConfiguration().update('workbench.colorCustomizations', config, true).then(() => {
vscode.window.showInformationMessage(`${ accentSelected } set`);
}, reason => {
vscode.window.showErrorMessage(reason);
});
}
/**
* VSCode command
*/
export const THEME_ACCENTS_SETTER = () => {
// shows the quick pick dropdown
let options: string[] = Object.keys(themeConfigCommon.accents);
2017-06-12 19:49:50 +02:00
// let customColourKey: string = 'Custom colour';
2017-05-29 18:01:55 +02:00
let purgeColourKey: string = 'Remove accents';
2017-06-12 19:49:50 +02:00
// options.push(customColourKey);
2017-05-29 18:01:55 +02:00
options.push(purgeColourKey);
vscode.window.showQuickPick(options).then(accentSelected => {
if (accentSelected === null || accentSelected === undefined) return;
let config: any = vscode.workspace.getConfiguration().get('workbench.colorCustomizations');
switch(accentSelected) {
2017-06-12 19:49:50 +02:00
// case customColourKey:
// vscode.window.showInputBox().then(colourCode => {
// if (colourCode === null || colourCode === undefined) return;
// if (colourCode && !isValidColour(colourCode)) {
// vscode.window.showWarningMessage('Invalid colour set, aborting.');
// return;
// }
// assignColorCustomizations(colourCode, config);
// setWorkbenchOptions(accentSelected, config);
// });
// break;
2017-05-29 18:01:55 +02:00
case purgeColourKey:
assignColorCustomizations(undefined, config);
setWorkbenchOptions(accentSelected, config);
assignIconTheme(undefined);
2017-05-29 18:01:55 +02:00
break;
default:
assignColorCustomizations(themeConfigCommon.accents[accentSelected], config);
setWorkbenchOptions(accentSelected, config);
assignIconTheme(accentSelected);
2017-05-29 18:01:55 +02:00
break;
}
});
}