From 1af7cf94e362210d1a0f64d43a40b334809fbd3b Mon Sep 17 00:00:00 2001 From: Alessio Occhipinti Date: Mon, 30 Apr 2018 09:55:47 +0200 Subject: [PATCH] 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 --- extensions/helpers/settings.ts | 30 +++---------- extensions/material.theme.config.ts | 66 +++++++++++------------------ package.json | 38 ++++++++++++----- 3 files changed, 58 insertions(+), 76 deletions(-) diff --git a/extensions/helpers/settings.ts b/extensions/helpers/settings.ts index 5a4b0af..e4e569c 100644 --- a/extensions/helpers/settings.ts +++ b/extensions/helpers/settings.ts @@ -19,7 +19,7 @@ export function getAccent(): string | undefined { * @returns {*} */ export function getCustomSettings(): IThemeCustomProperties { - return vscode.workspace.getConfiguration().get('materialTheme.cache.workbench.settings', {}); + return vscode.workspace.getConfiguration().get('materialTheme', {}); } /** @@ -58,27 +58,11 @@ export function isMaterialThemeIcons(themeIconsName: string): boolean { /** * Sets a custom property in custom settings * @export - * @param {string} settingname + * @param {string} settingName * @param {*} value */ -export function setCustomSetting(settingname: string, value: any): Thenable { - let settings: any = getCustomSettings(); - 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} - */ -export function setCustomSettings(settingsObject: IThemeCustomProperties): Thenable { - 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); +export function setCustomSetting(settingName: string, value: any): Thenable { + return vscode.workspace.getConfiguration().update(`materialTheme.${settingName}`, value, true); } /** @@ -87,7 +71,7 @@ export function setCustomSettings(settingsObject: IThemeCustomProperties): Thena * @param {string} accentName */ export function updateAccent(accentName: string): Thenable { - const prevaccent = getAccent(); - return setCustomSetting('accentPrevious', prevaccent) - .then(() => setCustomSetting('accent', accentName)); + const prevAccent = getAccent(); + return setCustomSetting('accentPrevious', prevAccent) + .then(() => setCustomSetting('accent', accentName)) } \ No newline at end of file diff --git a/extensions/material.theme.config.ts b/extensions/material.theme.config.ts index 7b0a69b..4382632 100644 --- a/extensions/material.theme.config.ts +++ b/extensions/material.theme.config.ts @@ -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_ICONS } from "./commands/theme-icons/index"; import { shouldShowChangelog, showChangelog } from './helpers/changelog'; import { reloadWindow, getCurrentThemeID, setIconsID } from "./helpers/vscode"; -enum Commands { - ACCENTS, - CHANGELOG, - THEME_ICONS -} - -const OPTIONS: IGenericObject = { - '🎨 Change accent color': Commands.ACCENTS, - '🛠 Fix file icons': Commands.THEME_ICONS, - '🚧 Show changelog': Commands.CHANGELOG -} - const isMaterialTheme = (currentTheme: string): boolean => currentTheme.includes('Material Theme'); -export function activate(context: vscode.ExtensionContext) { - if (vscode.workspace.getConfiguration().has('materialTheme.cache.workbench.accent')) { - vscode.workspace.getConfiguration().update('materialTheme.cache.workbench.accent', undefined, true); - } +export function activate() { + const config = Workspace.getConfiguration(); - 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 currentTheme = getCurrentThemeID(); if (isColorTheme && isMaterialTheme(currentTheme)) { - await setIconsID('eq-material-theme-icons'); - await THEME_ICONS().catch(error => console.trace(error)); - reloadWindow(); + setIconsID('eq-material-theme-icons') + .then(() => THEME_ICONS().catch(error => console.trace(error))) + .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()) { showChangelog(); } - // registering the command - let command = vscode.commands.registerCommand('material.theme.config', () => { - // the user is going to choose what aspect of theme to config - vscode.window.showQuickPick(Object.keys(OPTIONS)).then(response => { - // switching selected option - switch(OPTIONS[response]) { - case Commands.ACCENTS: - THEME_ACCENTS_SETTER(); - break; - case Commands.CHANGELOG: - showChangelog(); - break; - case Commands.THEME_ICONS: - THEME_ICONS().then(() => reloadWindow()).catch(error => console.trace(error)) - break; - } - }); - }); - - context.subscriptions.push(command); + // Registering commands + Commands.registerCommand('materialTheme.setAccent', () => THEME_ACCENTS_SETTER()); + Commands.registerCommand('materialTheme.fixIcons', () => + THEME_ICONS() + .then(() => reloadWindow()) + .catch(err => console.trace(err)) + ); + Commands.registerCommand('materialTheme.showChangelog', () => showChangelog()); } diff --git a/package.json b/package.json index deb506a..a8eb83e 100644 --- a/package.json +++ b/package.json @@ -45,24 +45,40 @@ "Other" ], "activationEvents": [ - "*" + "onCommand:materialTheme.setAccent", + "onCommand:materialTheme.fixIcons", + "onCommand:materialTheme.showChangelog" ], "main": "./extensions/material.theme.config.js", "contributes": { "commands": [ - { - "category": "🎨 Material Theme", - "command": "material.theme.config", - "title": "Configuration" - } + { + "command": "materialTheme.setAccent", + "title": "Set accent color for Material Theme", + "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": { + "type": "object", + "title": "Material Theme configuration", "properties": { - "materialTheme.cache.workbench.settings": { - "default": {}, - "type": "object", - "title": "Custom material theme settings", - "description": "Material theme settings object." + "materialTheme.accent": { + "type": "string", + "description": "Current accent color selected" + }, + "materialTheme.accentPrevious": { + "type": "string", + "description": "Previous accent color selected" } } },