Feat/split commands (#177)

* feat(split-commands): splitting commands in package.json and rewrite

* chore(cleanup): removed useless function

* chore(old-conf): delete old configuration
This commit is contained in:
Alessio Occhipinti 2018-04-30 09:55:47 +02:00 committed by Mattia Astorino
parent c4e1dbe106
commit 1af7cf94e3
3 changed files with 58 additions and 76 deletions

View file

@ -19,7 +19,7 @@ export function getAccent(): string | undefined {
* @returns {*} * @returns {*}
*/ */
export function getCustomSettings(): IThemeCustomProperties { export function getCustomSettings(): IThemeCustomProperties {
return vscode.workspace.getConfiguration().get<IThemeCustomProperties>('materialTheme.cache.workbench.settings', {}); return vscode.workspace.getConfiguration().get<IThemeCustomProperties>('materialTheme', {});
} }
/** /**
@ -58,27 +58,11 @@ export function isMaterialThemeIcons(themeIconsName: string): boolean {
/** /**
* Sets a custom property in custom settings * Sets a custom property in custom settings
* @export * @export
* @param {string} settingname * @param {string} settingName
* @param {*} value * @param {*} value
*/ */
export function setCustomSetting(settingname: string, value: any): Thenable<void> { export function setCustomSetting(settingName: string, value: any): Thenable<void> {
let settings: any = getCustomSettings(); return vscode.workspace.getConfiguration().update(`materialTheme.${settingName}`, value, true);
settings[settingname] = value;
return vscode.workspace.getConfiguration().update('materialTheme.cache.workbench.settings', settings, true);
}
/**
* Sets custom properties in custom settings
* @export
* @param {*} settingsObject
* @returns {Thenable<void>}
*/
export function setCustomSettings(settingsObject: IThemeCustomProperties): Thenable<void> {
let settings: any = getCustomSettings();
Object.keys(settingsObject).forEach(key => settings[key] = (settingsObject as any)[key]);
return vscode.workspace.getConfiguration().update('materialTheme.cache.workbench.settings', settings, true);
} }
/** /**
@ -87,7 +71,7 @@ export function setCustomSettings(settingsObject: IThemeCustomProperties): Thena
* @param {string} accentName * @param {string} accentName
*/ */
export function updateAccent(accentName: string): Thenable<void> { export function updateAccent(accentName: string): Thenable<void> {
const prevaccent = getAccent(); const prevAccent = getAccent();
return setCustomSetting('accentPrevious', prevaccent) return setCustomSetting('accentPrevious', prevAccent)
.then(() => setCustomSetting('accent', accentName)); .then(() => setCustomSetting('accent', accentName))
} }

View file

@ -1,63 +1,45 @@
import * as vscode from 'vscode'; import {
workspace as Workspace,
commands as Commands
} from 'vscode';
import { IGenericObject } from "./interfaces/igeneric-object";
import { THEME_ACCENTS_SETTER } from "./commands/accents-setter/index"; import { THEME_ACCENTS_SETTER } from "./commands/accents-setter/index";
import { THEME_ICONS } from "./commands/theme-icons/index"; import { THEME_ICONS } from "./commands/theme-icons/index";
import { shouldShowChangelog, showChangelog } from './helpers/changelog'; import { shouldShowChangelog, showChangelog } from './helpers/changelog';
import { reloadWindow, getCurrentThemeID, setIconsID } from "./helpers/vscode"; import { reloadWindow, getCurrentThemeID, setIconsID } from "./helpers/vscode";
enum Commands {
ACCENTS,
CHANGELOG,
THEME_ICONS
}
const OPTIONS: IGenericObject<number> = {
'🎨 Change accent color': Commands.ACCENTS,
'🛠 Fix file icons': Commands.THEME_ICONS,
'🚧 Show changelog': Commands.CHANGELOG
}
const isMaterialTheme = (currentTheme: string): boolean => const isMaterialTheme = (currentTheme: string): boolean =>
currentTheme.includes('Material Theme'); currentTheme.includes('Material Theme');
export function activate(context: vscode.ExtensionContext) { export function activate() {
if (vscode.workspace.getConfiguration().has('materialTheme.cache.workbench.accent')) { const config = Workspace.getConfiguration();
vscode.workspace.getConfiguration().update('materialTheme.cache.workbench.accent', undefined, true);
}
vscode.workspace.onDidChangeConfiguration(async event => { // Listen on set theme: when the theme is Material Theme, just adjust icon and accent.
Workspace.onDidChangeConfiguration(event => {
const isColorTheme = event.affectsConfiguration('workbench.colorTheme'); const isColorTheme = event.affectsConfiguration('workbench.colorTheme');
const currentTheme = getCurrentThemeID(); const currentTheme = getCurrentThemeID();
if (isColorTheme && isMaterialTheme(currentTheme)) { if (isColorTheme && isMaterialTheme(currentTheme)) {
await setIconsID('eq-material-theme-icons'); setIconsID('eq-material-theme-icons')
await THEME_ICONS().catch(error => console.trace(error)); .then(() => THEME_ICONS().catch(error => console.trace(error)))
reloadWindow(); .then(() => reloadWindow());
} }
}); });
// Delete old configuration, must remove with next major release
if (config.has('materialTheme.cache.workbench')) {
config.update('materialTheme.cache.workbench', undefined, true);
}
if (shouldShowChangelog()) { if (shouldShowChangelog()) {
showChangelog(); showChangelog();
} }
// registering the command // Registering commands
let command = vscode.commands.registerCommand('material.theme.config', () => { Commands.registerCommand('materialTheme.setAccent', () => THEME_ACCENTS_SETTER());
// the user is going to choose what aspect of theme to config Commands.registerCommand('materialTheme.fixIcons', () =>
vscode.window.showQuickPick(Object.keys(OPTIONS)).then(response => { THEME_ICONS()
// switching selected option .then(() => reloadWindow())
switch(OPTIONS[response]) { .catch(err => console.trace(err))
case Commands.ACCENTS: );
THEME_ACCENTS_SETTER(); Commands.registerCommand('materialTheme.showChangelog', () => showChangelog());
break;
case Commands.CHANGELOG:
showChangelog();
break;
case Commands.THEME_ICONS:
THEME_ICONS().then(() => reloadWindow()).catch(error => console.trace(error))
break;
}
});
});
context.subscriptions.push(command);
} }

View file

@ -45,24 +45,40 @@
"Other" "Other"
], ],
"activationEvents": [ "activationEvents": [
"*" "onCommand:materialTheme.setAccent",
"onCommand:materialTheme.fixIcons",
"onCommand:materialTheme.showChangelog"
], ],
"main": "./extensions/material.theme.config.js", "main": "./extensions/material.theme.config.js",
"contributes": { "contributes": {
"commands": [ "commands": [
{ {
"category": "🎨 Material Theme", "command": "materialTheme.setAccent",
"command": "material.theme.config", "title": "Set accent color for Material Theme",
"title": "Configuration" "category": "🎨 Material Theme"
},
{
"command": "materialTheme.fixIcons",
"title": "Fix icons color when needed",
"category": "🎨 Material Theme"
},
{
"command": "materialTheme.showChangelog",
"title": "Show changelog",
"category": "🎨 Material Theme"
} }
], ],
"configuration": { "configuration": {
"properties": {
"materialTheme.cache.workbench.settings": {
"default": {},
"type": "object", "type": "object",
"title": "Custom material theme settings", "title": "Material Theme configuration",
"description": "Material theme settings object." "properties": {
"materialTheme.accent": {
"type": "string",
"description": "Current accent color selected"
},
"materialTheme.accentPrevious": {
"type": "string",
"description": "Previous accent color selected"
} }
} }
}, },