-
-
Notifications
You must be signed in to change notification settings - Fork 603
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
20 changed files
with
173 additions
and
142 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,65 +1,56 @@ | ||
import * as helpers from './index'; | ||
import * as vscode from 'vscode'; | ||
import * as i18n from './../i18n'; | ||
import { createIconFile, getDefaultIconOptions } from '../icons/index'; | ||
import { checkFolderIconsStatus } from '../commands/folders'; | ||
import { IconPack, IconJsonOptions, IconAssociations } from '../models/index'; | ||
import * as merge from 'lodash.merge'; | ||
import { createIconFile } from '../icons/index'; | ||
import { getObjectPropertyValue, setObjectPropertyValue } from './objects'; | ||
import { getExtensionConfiguration, promptToReload, getMaterialIconsJSON, getThemeConfig } from '.'; | ||
|
||
/** Watch for changes in the configurations to update the icons theme. */ | ||
export const watchForConfigChanges = () => { | ||
vscode.workspace.onDidChangeConfiguration(detectConfigChanges); | ||
}; | ||
|
||
/** | ||
* Compare the workspace and the user configurations | ||
* with the current setup of the icons. | ||
*/ | ||
/** Compare the workspace and the user configurations with the current setup of the icons. */ | ||
export const detectConfigChanges = () => { | ||
return helpers.getMaterialIconsJSON() | ||
.then((json) => { | ||
compareConfig<string>('activeIconPack', json.options.activatedPack); | ||
compareConfig<string>('folders.theme', json.options.folderTheme); | ||
compareConfig<string>('folders.color', json.options.folderColor); | ||
compareConfig<boolean>('hidesExplorerArrows', json.options.hidesExplorerArrows); | ||
compareConfig<IconAssociations>('files.associations', json.options.fileAssociations); | ||
compareConfig<IconAssociations>('folders.associations', json.options.folderAssociations); | ||
compareConfig<IconAssociations>('languages.associations', json.options.languageAssociations); | ||
const configs = Object.keys(getExtensionConfiguration()) | ||
.map(c => c.split('.').slice(1).join('.')); | ||
|
||
return compareConfigs(configs).then(changes => { | ||
// if there's nothing to update | ||
if (Object.keys(changes).length === 0) return; | ||
|
||
// update icon json file with new options | ||
return createIconFile(changes).then(() => { | ||
promptToReload(); | ||
}).catch(err => { | ||
console.error(err); | ||
}); | ||
}; | ||
|
||
const compareConfig = <T>(config: string, currentState: T): Promise<void> => { | ||
const configValue = <T>helpers.getThemeConfig(config).globalValue; | ||
|
||
return helpers.getMaterialIconsJSON().then(result => { | ||
if (configValue !== undefined && JSON.stringify(configValue) !== JSON.stringify(currentState)) { | ||
updateIconJson(); | ||
} | ||
}); | ||
}; | ||
|
||
const updateIconJson = () => { | ||
const defaultOptions = getDefaultIconOptions(); | ||
|
||
// adjust options | ||
const options: IconJsonOptions = { | ||
folderTheme: getCurrentConfig<string>('folders.theme', defaultOptions.folderTheme), | ||
folderColor: getCurrentConfig<string>('folders.color', defaultOptions.folderColor), | ||
activatedPack: getCurrentConfig<string>('activeIconPack', defaultOptions.activatedPack), | ||
hidesExplorerArrows: getCurrentConfig<boolean>('hidesExplorerArrows', defaultOptions.hidesExplorerArrows), | ||
fileAssociations: getCurrentConfig<IconAssociations>('files.associations', defaultOptions.fileAssociations), | ||
folderAssociations: getCurrentConfig<IconAssociations>('folders.associations', defaultOptions.folderAssociations), | ||
languageAssociations: getCurrentConfig<IconAssociations>('languages.associations', defaultOptions.languageAssociations), | ||
}; | ||
/** | ||
* Compares a specific configuration in the settings with a current configuration state. | ||
* The current configuration state is read from the icons json file. | ||
* @param configs List of configuration names | ||
* @returns List of configurations that needs to be updated. | ||
*/ | ||
const compareConfigs = (configs: string[]): Promise<{ [name: string]: any }> => { | ||
// object with the config properties that must be updated | ||
const updateRequired: { [name: string]: any } = {}; | ||
|
||
return getMaterialIconsJSON().then((json) => { | ||
configs.forEach(configName => { | ||
const configValue = getThemeConfig(configName).globalValue; | ||
const currentState = getObjectPropertyValue(json.options, configName); | ||
|
||
if (configValue !== undefined && JSON.stringify(configValue) !== JSON.stringify(currentState)) { | ||
setObjectPropertyValue(updateRequired, configName, configValue); | ||
} | ||
}); | ||
|
||
// update icon json file with new options | ||
return createIconFile(options).then(() => { | ||
helpers.promptToReload(); | ||
}).catch(err => { | ||
console.error(err); | ||
if (Object.keys(updateRequired).length > 0) { | ||
return merge({}, json.options, updateRequired); | ||
} else { | ||
return {}; | ||
} | ||
}); | ||
}; | ||
|
||
const getCurrentConfig = <T>(config: string, defaultValue: T): T => { | ||
const result = <T>helpers.getThemeConfig(config).globalValue; | ||
return result !== undefined ? result : defaultValue; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
/** | ||
* Get the nested properties of an object | ||
* This solution is lighter than the lodash get-version and works fine for the translations. | ||
* Source: http://stackoverflow.com/a/6491621/6942210 | ||
*/ | ||
export const getObjectPropertyValue = (obj: Object, path: string) => { | ||
// convert indexes to properties | ||
path = path.replace(/\[(\w+)\]/g, '.$1'); | ||
|
||
// strip a leading dot | ||
path = path.replace(/^\./, ''); | ||
|
||
// separate paths in array | ||
let pathArray = path.split('.'); | ||
|
||
/** Avoid errors in the getValue function. */ | ||
const isObject = (object) => { | ||
return object === Object(object); | ||
}; | ||
|
||
for (let i = 0; i < pathArray.length; ++i) { | ||
let k = pathArray[i]; | ||
if (isObject(obj) && k in obj) { | ||
obj = obj[k]; | ||
} else { | ||
return; | ||
} | ||
} | ||
return obj; | ||
}; | ||
|
||
/** | ||
* Set a value for a nested object property. | ||
* @param obj Object | ||
* @param path Properties as string e.g. `'a.b.c'` | ||
* @param value Value to be set for the given property | ||
* Source: https://stackoverflow.com/a/13719799/6942210 | ||
*/ | ||
export const setObjectPropertyValue = (obj: Object, path, value) => { | ||
if (typeof path === 'string') { | ||
path = path.split('.'); | ||
} | ||
|
||
if (path.length > 1) { | ||
let e = path.shift(); | ||
setObjectPropertyValue(obj[e] = | ||
Object.prototype.toString.call(obj[e]) === '[object Object]' | ||
? obj[e] | ||
: {}, | ||
path, | ||
value); | ||
} else { | ||
obj[path[0]] = value; | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,24 +1,24 @@ | ||
/** | ||
* Path where the icons are located. | ||
*/ | ||
export const iconFolderPath = './../../icons/'; | ||
export const iconFolderPath: string = './../../icons/'; | ||
|
||
/** | ||
* File name of the JSON file that will be generated to the out folder. | ||
*/ | ||
export const iconJsonName = 'material-icons.json'; | ||
export const iconJsonName: string = 'material-icons.json'; | ||
|
||
/** | ||
* File ending for opened folders. | ||
*/ | ||
export const openedFolder = '-open'; | ||
export const openedFolder: string = '-open'; | ||
|
||
/** | ||
* FIle ending for light icons. | ||
*/ | ||
export const lightVersion = '_light'; | ||
export const lightVersion: string = '_light'; | ||
|
||
/** | ||
* File ending for high contrast icons. | ||
*/ | ||
export const highContrastVersion = '_highContrast'; | ||
export const highContrastVersion: string = '_highContrast'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.