chore: Sets alpha and checks if a colour is valid.
This commit is contained in:
parent
c714fb7f07
commit
2a09d41136
3 changed files with 67 additions and 10 deletions
|
@ -1,17 +1,41 @@
|
||||||
import * as vscode from 'vscode';
|
import * as vscode from 'vscode';
|
||||||
|
|
||||||
|
import { IAccentCustomProperty } from "../interfaces/iaccent-custom-property";
|
||||||
import { IGenericObject } from "../interfaces/igeneric-object";
|
import { IGenericObject } from "../interfaces/igeneric-object";
|
||||||
import { IThemeConfigCommons } from "../interfaces/icommons";
|
import { IThemeConfigCommons } from "../interfaces/icommons";
|
||||||
|
|
||||||
|
const REGEXP_HEX: RegExp = /^#([0-9A-F]{6}|[0-9A-F]{8})$/i;
|
||||||
|
|
||||||
let themeConfigCommon: IThemeConfigCommons = require('./commons.json');
|
let themeConfigCommon: IThemeConfigCommons = require('./commons.json');
|
||||||
let accentsProperties: IGenericObject<string> = {
|
let accentsProperties: IGenericObject<IAccentCustomProperty> = {
|
||||||
"activityBarBadge.background": undefined,
|
"activityBarBadge.background": {
|
||||||
"list.activeSelectionForeground": undefined,
|
alpha: 100,
|
||||||
"list.inactiveSelectionForeground": undefined,
|
value: undefined
|
||||||
"list.highlightForeground": undefined,
|
},
|
||||||
"scrollbarSlider.activeBackground": undefined,
|
"list.activeSelectionForeground": {
|
||||||
"editorSuggestWidget.highlightForeground": undefined,
|
alpha: 100,
|
||||||
"textLink.foreground": undefined,
|
value: undefined
|
||||||
|
},
|
||||||
|
"list.inactiveSelectionForeground": {
|
||||||
|
alpha: 100,
|
||||||
|
value: undefined
|
||||||
|
},
|
||||||
|
"list.highlightForeground": {
|
||||||
|
alpha: 100,
|
||||||
|
value: undefined
|
||||||
|
},
|
||||||
|
"scrollbarSlider.activeBackground": {
|
||||||
|
alpha: 100,
|
||||||
|
value: undefined
|
||||||
|
},
|
||||||
|
"editorSuggestWidget.highlightForeground": {
|
||||||
|
alpha: 100,
|
||||||
|
value: undefined
|
||||||
|
},
|
||||||
|
"textLink.foreground": {
|
||||||
|
alpha: 50,
|
||||||
|
value: undefined
|
||||||
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -21,10 +45,34 @@ let accentsProperties: IGenericObject<string> = {
|
||||||
*/
|
*/
|
||||||
function assignColorCustomizations(colour: string, config: any): void {
|
function assignColorCustomizations(colour: string, config: any): void {
|
||||||
Object.keys(accentsProperties).forEach(propertyName => {
|
Object.keys(accentsProperties).forEach(propertyName => {
|
||||||
|
let accent: IAccentCustomProperty = accentsProperties[propertyName];
|
||||||
|
|
||||||
|
if (!isValidColour(colour)) {
|
||||||
|
colour = undefined;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (colour && accent.alpha < 100) {
|
||||||
|
colour = `${ colour }${ accent.alpha > 10 ? accent.alpha : `0${ accent.alpha }` }`;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (accent) {
|
||||||
config[propertyName] = colour;
|
config[propertyName] = colour;
|
||||||
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 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;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Sets workbench options
|
* Sets workbench options
|
||||||
* @param {string} accentSelected
|
* @param {string} accentSelected
|
||||||
|
@ -60,6 +108,11 @@ export const THEME_ACCENTS_SETTER = () => {
|
||||||
vscode.window.showInputBox().then(colourCode => {
|
vscode.window.showInputBox().then(colourCode => {
|
||||||
if (colourCode === null || colourCode === undefined) return;
|
if (colourCode === null || colourCode === undefined) return;
|
||||||
|
|
||||||
|
if (colourCode && !isValidColour(colourCode)) {
|
||||||
|
vscode.window.showWarningMessage('Invalid colour set, aborting.');
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
assignColorCustomizations(colourCode, config);
|
assignColorCustomizations(colourCode, config);
|
||||||
setWorkbenchOptions(accentSelected, config);
|
setWorkbenchOptions(accentSelected, config);
|
||||||
});
|
});
|
||||||
|
|
4
extensions/interfaces/iaccent-custom-property.ts
Normal file
4
extensions/interfaces/iaccent-custom-property.ts
Normal file
|
@ -0,0 +1,4 @@
|
||||||
|
export interface IAccentCustomProperty {
|
||||||
|
alpha: number;
|
||||||
|
value: any;
|
||||||
|
}
|
File diff suppressed because one or more lines are too long
Loading…
Reference in a new issue