chore: Stores theme and icon changes, avoiding excessive window reload.

This commit is contained in:
OctoD 2017-06-20 14:39:45 +02:00
parent a3428ab812
commit 7ed1d45c65
7 changed files with 147 additions and 18 deletions

View file

@ -1,7 +1,8 @@
import * as vscode from 'vscode';
import { getCurrentThemeID, getCurrentThemeIconsID, reloadWindow } from "../../helpers/vscode";
import { getDefaultValues, getPackageJSON, getThemeIconsByContributeID, getThemeIconsContribute, writeFile } from "../../helpers/fs";
import { getCurrentThemeIconsID, reloadWindow } from "../../helpers/vscode";
import { getDefaultValues, getThemeIconsByContributeID, getThemeIconsContribute, writeFile } from "../../helpers/fs";
import { hasAccentChanged, isMaterialThemeIcons, updateAccent } from "../../helpers/settings";
import {IAccentCustomProperty} from '../../interfaces/iaccent-custom-property';
import {IGenericObject} from '../../interfaces/igeneric-object';
@ -75,12 +76,9 @@ function assignColorCustomizations(colour: string, config: any): void {
*/
export function assignIconTheme(accentName: string | undefined): void {
// let accentValue: string;
let cacheKey: string = 'materialTheme.cache.workbench.accent';
let themeIconsID: string = getCurrentThemeIconsID();
let themeID: string = getCurrentThemeID();
let packageJSON = getPackageJSON();
if (packageJSON.contributes.iconThemes.filter(contribute => contribute.id === themeIconsID).length > 0) {
if (isMaterialThemeIcons(themeIconsID)) {
let defaults = getDefaultValues();
let theme = getThemeIconsByContributeID(themeIconsID);
let themeContribute = getThemeIconsContribute(themeIconsID);
@ -96,17 +94,16 @@ export function assignIconTheme(accentName: string | undefined): void {
writeFile(themeContribute.path, JSON.stringify(theme));
vscode.workspace.getConfiguration().update(cacheKey, accentName, true);
// updateAccent(accentName);
vscode.workspace.getConfiguration().update('workbench.iconTheme', themeIconsID, true).then(() => {
// In order to load modified icons we will have to reload the whole window.
if (packageJSON.contributes.themes.filter(theme => theme.label === themeID).length > 0 && packageJSON.contributes.iconThemes.filter(theme => theme.id === themeIconsID).length > 0) {
if (hasAccentChanged(accentName)) {
reloadWindow();
}
});
} else {
vscode.workspace.getConfiguration().update(cacheKey, accentName, true);
// vscode.workspace.getConfiguration().update('workbench.iconTheme', themeIconsID, true).then(() => {
// In order to load modified icons we will have to reload the whole window.
// });
}
updateAccent(accentName);
}
/**

View file

@ -1,8 +1,9 @@
import * as fs from 'fs';
import * as vscode from 'vscode';
import { getAbsolutePath, getDefaultValues, getPackageJSON, getThemeIconsByContributeID, getThemeIconsContribute } from "../../helpers/fs";
import { getAbsolutePath, getDefaultValues, getThemeIconsByContributeID, getThemeIconsContribute } from "../../helpers/fs";
import { getCurrentThemeID, getCurrentThemeIconsID, reloadWindow } from "../../helpers/vscode";
import { shouldReloadWindow, updateSettingsTheme, updateSettingsThemeIcons } from "../../helpers/settings";
import { CHARSET } from "../../consts/files";
import { IPackageJSONThemeIcons } from "../../interfaces/ipackage.json";
@ -18,12 +19,12 @@ export const THEME_CHANGE_LISTENER = () => {
if (themeIconsID && /material-theme/i.test(themeIconsID)) {
let defaults = getDefaultValues();
let packageJSON = getPackageJSON();
let variantNames: string[] = themeID.split('Material Theme');
let variantName: string = variantNames[1] === undefined ? '' : variantNames[1].trim();
let themeContribute: IPackageJSONThemeIcons = getThemeIconsContribute(themeIconsID);
let theme: IThemeIcons = getThemeIconsByContributeID(themeIconsID);
let themepath: string = getAbsolutePath(themeContribute.path);
let shouldReload: boolean = shouldReloadWindow(themeID, themeIconsID);
theme.iconDefinitions._folder_dark.iconPath = defaults.icons.theme.iconDefinitions._folder_dark.iconPath.replace('.svg', `${ variantName }.svg`);
theme.iconDefinitions._file_folder.iconPath = defaults.icons.theme.iconDefinitions._file_folder.iconPath.replace('.svg', `${ variantName }.svg`);
@ -33,6 +34,9 @@ export const THEME_CHANGE_LISTENER = () => {
assignIconTheme(cacheKey);
}
updateSettingsTheme(themeID);
updateSettingsThemeIcons(themeIconsID);
console.log(
theme.iconDefinitions._folder_dark.iconPath
, theme.iconDefinitions._file_folder.iconPath
@ -41,7 +45,7 @@ export const THEME_CHANGE_LISTENER = () => {
fs.writeFileSync(themepath, JSON.stringify(theme), CHARSET);
vscode.workspace.getConfiguration().update('workbench.iconTheme', themeIconsID, true).then(() => {
if (packageJSON.contributes.themes.filter(theme => theme.label === themeID).length > 0 && packageJSON.contributes.iconThemes.filter(theme => theme.id ===themeIconsID).length > 0) {
if (shouldReload) {
reloadWindow();
}
});

View file

@ -0,0 +1,111 @@
import * as vscode from 'vscode';
import { IThemeCustomProperties } from "../interfaces/itheme-custom-properties";
import { getPackageJSON } from "./fs";
/**
* Gets saved accent
* @export
* @returns {(string | null)}
*/
export function getAccent(): string | null {
return vscode.workspace.getConfiguration().get<string>('materialTheme.cache.workbench.accent', null);
}
/**
* Gets custom settings
* @export
* @returns {*}
*/
export function getCustomSettings(): IThemeCustomProperties {
return vscode.workspace.getConfiguration().get<IThemeCustomProperties>('materialTheme.cache.workbench.settings', {});
}
/**
* Determines if the accent name has changed
* @export
* @param {string} accentName
* @returns {boolean}
*/
export function hasAccentChanged(accentName: string): boolean {
return accentName !== getAccent();
}
/**
* Determines if the passing theme label is a material theme
* @export
* @param {string} themeName
* @returns {boolean}
*/
export function isMaterialTheme(themeName: string): boolean {
let packageJSON = getPackageJSON();
return packageJSON.contributes.themes.filter(contrib => contrib.label === themeName).length > 0;
}
/**
* Determines if the passing icons theme is a material theme
* @export
* @param {string} themeIconsName
* @returns {boolean}
*/
export function isMaterialThemeIcons(themeIconsName: string): boolean {
let packageJSON = getPackageJSON();
return packageJSON.contributes.iconThemes.filter(contribute => contribute.id === themeIconsName).length > 0;
}
/**
* Sets custom properties in custom settings
* @export
* @param {string} settingname
* @param {*} value
*/
export function setCustomSetting(settingname: string, value: any): void {
let settings: any = getCustomSettings();
settings[settingname] = value;
vscode.workspace.getConfiguration().update('materialTheme.cache.workbench.settings', settings, true);
}
/**
* Determines if the window should reload
* @export
* @param {string} themeColour
* @param {string} themeIcons
* @returns {boolean}
*/
export function shouldReloadWindow(themeColour: string, themeIcons: string): boolean {
let isTheme: boolean = isMaterialTheme(themeColour);
let isThemeIcons: boolean = isMaterialThemeIcons(themeIcons);
if (!isTheme && !isThemeIcons) return false;
let customSettings = getCustomSettings();
return customSettings.themeColours !== themeColour || customSettings.themeIcons !== themeIcons;
}
/**
* Updates accent name
* @export
* @param {string} accentName
*/
export function updateAccent(accentName: string): void {
vscode.workspace.getConfiguration().update('materialTheme.cache.workbench.accent', accentName, true);
}
/**
* Updates theme name to custom settings
* @export
* @param {string} themeName
*/
export function updateSettingsTheme(themeName: string): void {
setCustomSetting('themeColours', themeName);
}
/**
* Updates icons theme name to custom settings
* @export
* @param {string} themeName
*/
export function updateSettingsThemeIcons(themeName: string): void {
setCustomSetting('themeIcons', themeName);
}

View file

@ -8,10 +8,17 @@ export interface IPackageJSONBadge {
export interface IPackageJSONContributes {
commands: IPackageJSONCommand[];
configuration: IPackageJSONConfiguration;
iconThemes: IPackageJSONThemeIcons[];
themes: IPackageJSONTheme[];
}
export interface IPackageJSONConfiguration {
properties: {
}
}
export interface IPackageJSONCommand {
category: string;
command: string;

View file

@ -0,0 +1,4 @@
export interface IThemeCustomProperties {
themeColours?: string;
themeIcons?: string;
}

View file

@ -65,6 +65,12 @@
"type": "object",
"title": "Previous color customizations",
"description": "Keeps in cache your previous color customizations."
},
"materialTheme.cache.workbench.settings": {
"default": {},
"type": "object",
"title": "Custom material theme settings",
"description": "Keeps in cache your previous color customizations."
}
}
},

File diff suppressed because one or more lines are too long