fix: "Remove accents" command now will remove custom color conf (#289)

This commit is contained in:
Alessio Occhipinti 2018-11-25 15:05:10 +01:00 committed by Mattia Astorino
parent c8a23ef3d0
commit 85238745fe
5 changed files with 41 additions and 42 deletions

View file

@ -0,0 +1,3 @@
export default {
PURGE_KEY: 'Remove accents'
};

View file

@ -1,29 +1,27 @@
import * as vscode from 'vscode'; import * as vscode from 'vscode';
import {updateAccent} from './../../helpers/settings';
import {getDefaultValues, getAccentsProperties} from './../../helpers/fs'; import {getDefaultValues, getAccentsProperties} from './../../helpers/fs';
import consts from './consts';
const REGEXP_HEX: RegExp = /^#([0-9A-F]{6}|[0-9A-F]{8})$/i; const REGEXP_HEX: RegExp = /^#([0-9A-F]{6}|[0-9A-F]{8})$/i;
/** /**
* Assigns colours * Assigns colours
*/ */
const assignColorCustomizations = (colour: string, config: any): void => { const assignColorCustomizations = (colour: string): Object => {
const accentsProperties = getAccentsProperties(); const accentsProperties = getAccentsProperties();
const newColour = isValidColour(colour) ? colour : undefined; const newColour = isValidColour(colour) ? colour : undefined;
return Object.keys(accentsProperties).reduce((acc: any, propName) => {
Object.keys(accentsProperties).forEach(propertyName => { const accent = accentsProperties[propName];
const accent = accentsProperties[propertyName];
let colorProp = newColour; let colorProp = newColour;
if (colour && accent.alpha < 100) { if (colour && accent.alpha < 100) {
colorProp = `${ colour }${ accent.alpha > 10 ? accent.alpha : `0${ accent.alpha }` }`; colorProp = `${ colour }${ accent.alpha > 10 ? accent.alpha : `0${ accent.alpha }` }`;
} }
if (accent) { acc[propName] = colorProp;
config[propertyName] = colorProp; return acc;
} }, {});
});
}; };
/** /**
@ -38,43 +36,32 @@ const isValidColour = (colour: string | null | undefined): boolean =>
const setWorkbenchOptions = (config: any): Thenable<boolean> => const setWorkbenchOptions = (config: any): Thenable<boolean> =>
vscode.workspace.getConfiguration().update('workbench.colorCustomizations', config, true) vscode.workspace.getConfiguration().update('workbench.colorCustomizations', config, true)
.then(() => true, reason => vscode.window.showErrorMessage(reason)); .then(() => true, reason => vscode.window.showErrorMessage(reason));
/** /**
* VSCode command * VSCode command
*/ */
export default async (accent?: string): Promise<boolean> => { export default async (accent?: string): Promise<boolean> => {
const themeConfigCommon = getDefaultValues(); const themeConfigCommon = getDefaultValues();
const purgeColourKey: string = 'Remove accents';
const shouldUpdateAccent = Boolean(!accent);
let accentToSelect = accent;
// If called without accent shows the quick pick dropdown and wait response
if (!accentToSelect) {
const options: string[] = Object.keys(themeConfigCommon.accents).concat(purgeColourKey);
const accentSelected = await vscode.window.showQuickPick(options);
if (accentSelected === null || accentSelected === undefined) {
return Promise.resolve(null);
}
accentToSelect = accentSelected;
}
const config: any = vscode.workspace.getConfiguration().get('workbench.colorCustomizations'); const config: any = vscode.workspace.getConfiguration().get('workbench.colorCustomizations');
switch (accentToSelect) { switch (accent) {
case purgeColourKey: case consts.PURGE_KEY: {
assignColorCustomizations(undefined, config); const newConfig = {
return setWorkbenchOptions(config) ...config,
.then(() => updateAccent(undefined)) ...assignColorCustomizations(undefined)
.then(() => Promise.resolve(true)); };
default:
assignColorCustomizations(themeConfigCommon.accents[accentToSelect], config); return setWorkbenchOptions(newConfig)
return setWorkbenchOptions(config)
.then(() =>
shouldUpdateAccent ? updateAccent(accentToSelect) : Promise.resolve(accentToSelect)
)
.then(() => Promise.resolve(true)); .then(() => Promise.resolve(true));
}
default: {
const newConfig = {
...config,
...assignColorCustomizations(themeConfigCommon.accents[accent])
};
return setWorkbenchOptions(newConfig)
.then(() => Boolean(accent));
}
} }
}; };

View file

@ -0,0 +1,9 @@
import * as vscode from 'vscode';
import {getDefaultValues} from '../../helpers/fs';
import consts from './consts';
export default async () => {
const themeConfigCommon = getDefaultValues();
const options: string[] = Object.keys(themeConfigCommon.accents).concat(consts.PURGE_KEY);
return vscode.window.showQuickPick(options);
};

View file

@ -1,3 +1,4 @@
export {default as accentsSetter} from './accents-setter'; export {default as accentsSetter} from './accents-setter';
export {default as accentsQuickPick} from './accents-setter/quick-pick';
export {default as fixIcons} from './theme-icons'; export {default as fixIcons} from './theme-icons';
export {default as toggleApplyIcons} from './toggle-apply-icons'; export {default as toggleApplyIcons} from './toggle-apply-icons';

View file

@ -5,13 +5,12 @@ import {
} from 'vscode'; } from 'vscode';
import * as ThemeCommands from './commands'; import * as ThemeCommands from './commands';
import {setCustomSetting} from './helpers/settings'; import {setCustomSetting, updateAccent} from './helpers/settings';
import {onChangeConfiguration} from './helpers/configuration-change'; import {onChangeConfiguration} from './helpers/configuration-change';
import {changelogMessage, installationMessage} from './helpers/messages'; import {changelogMessage, installationMessage} from './helpers/messages';
import checkInstallation from './helpers/check-installation'; import checkInstallation from './helpers/check-installation';
import writeChangelog from './helpers/write-changelog'; import writeChangelog from './helpers/write-changelog';
import {ReleaseNotesWebview} from './webviews/ReleaseNotes'; import {ReleaseNotesWebview} from './webviews/ReleaseNotes';
import handleAutoapply from './helpers/handle-autoapply';
export async function activate(context: ExtensionContext) { export async function activate(context: ExtensionContext) {
const config = Workspace.getConfiguration(); const config = Workspace.getConfiguration();
@ -42,8 +41,8 @@ export async function activate(context: ExtensionContext) {
// Registering commands // Registering commands
Commands.registerCommand('materialTheme.setAccent', async () => { Commands.registerCommand('materialTheme.setAccent', async () => {
const wasSet = await ThemeCommands.accentsSetter(); const accentPicked = await ThemeCommands.accentsQuickPick();
handleAutoapply(wasSet); await updateAccent(accentPicked);
}); });
Commands.registerCommand('materialTheme.fixIcons', () => ThemeCommands.fixIcons()); Commands.registerCommand('materialTheme.fixIcons', () => ThemeCommands.fixIcons());
Commands.registerCommand('materialTheme.toggleApplyIcons', () => ThemeCommands.toggleApplyIcons()); Commands.registerCommand('materialTheme.toggleApplyIcons', () => ThemeCommands.toggleApplyIcons());