Skip to content

Commit

Permalink
add config codeui.preferredScope & related functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
ryanraposo committed Aug 30, 2019
1 parent 45ec8ba commit 6356767
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 22 deletions.
10 changes: 8 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
"codeui.showNotifications": {
"type": "boolean",
"default": true,
"description": "Default is true. Controls the display of various notifications by CodeUI."
"description": "Controls the display of various notifications by CodeUI. Default is true."
},
"codeui.favoriteColors": {
"type": "object",
Expand All @@ -52,8 +52,14 @@
"general"
],
"default": "themeSpecific",
"description": "Default is themeSpecific. Controls target of customizations applied by CodeUI.",
"description": "Controls context of customizations applied by CodeUI. Default is 'themeSpecific'",
"scope": "application"
},
"codeui.preferredScope" : {
"description": "Controls scoping behaviour when a workspace/folder is open. Default is 'alwaysAsk'",
"type": "string",
"enum": ["alwaysAsk", "global", "workspace"],
"default": "alwaysAsk"
}
}
},
Expand Down
15 changes: 13 additions & 2 deletions src/configuration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,11 +113,11 @@ export function getWorkspaceRootFolder() : vscode.WorkspaceFolder | undefined{
}


export function getTargetingMode() : any {
export function getTargetingMode() : string {

const configuration = getConfiguration();

const themeSpecificCustomizations = configuration.get("codeui.targetingMode");
const themeSpecificCustomizations = <string> configuration.get("codeui.targetingMode");

return themeSpecificCustomizations;

Expand All @@ -136,3 +136,14 @@ export function setTargetingMode(targetingMode : string) : void {
}


export function getPreferredScope() : string {

const configuration = getConfiguration();

const preferredScope = <string> configuration.get("codeui.preferredScope");

return preferredScope;

}


49 changes: 31 additions & 18 deletions src/elementProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { chooseScope, showNotification, getInfoProvider } from './extension';

import * as theme from './theme';
import * as configuration from './configuration';
import { resolve } from 'url';


interface ColorConfig {
Expand Down Expand Up @@ -513,19 +514,9 @@ export class ElementProvider implements vscode.TreeDataProvider<any>{

static async updateWorkbenchColors(customizations: WorkbenchCustomizations){

let target : any;
const workspaceRootFolder = configuration.getWorkspaceRootFolder();
const currentThemeProp = "[" + configuration.getEffectiveColorThemeName() + "]";

if(workspaceRootFolder){ // If in a workspace, give the option to target Workspace Settings
target = await chooseScope(workspaceRootFolder);
if(!target){
return;
}
}else{
target = vscode.ConfigurationTarget.Global; // If no workspace, target Global Settings
}


const target = await resolveTarget();
let scopedCustomizations : any = {};

if(target === vscode.ConfigurationTarget.Global){
Expand All @@ -537,9 +528,6 @@ export class ElementProvider implements vscode.TreeDataProvider<any>{

for(let element in customizations){ // for key(element name) in supplied array
let value = customizations[element]; // value = color value
// if(value === undefined){// handles calls from clear() eg. ["activityBar.foreground" : undefined, ...]
// scopedCustomizations[currentThemeProp][element] = undefined;
// }else{ // handles calls from customize() eg. ["activityBar.forground" : "#ffffff", ...]
if(await isHexidecimal(value) || value === undefined){
const targetingMode = configuration.getTargetingMode();
if(targetingMode === 'themeSpecific'){
Expand All @@ -556,13 +544,11 @@ export class ElementProvider implements vscode.TreeDataProvider<any>{
await showNotification(value + "is not a valid hex color!");
return;
}
}

}

await vscode.workspace.getConfiguration().update("workbench.colorCustomizations", scopedCustomizations, target);

}


}

Expand Down Expand Up @@ -745,6 +731,33 @@ function getEffectiveColor(colorConfig:ColorConfig) : string | undefined {
}


function resolveTarget() : any {

const workspaceRootFolder = configuration.getWorkspaceRootFolder();
const preferredScope = configuration.getPreferredScope();

let target : any;

if(workspaceRootFolder){
if(preferredScope === "alwaysAsk"){
target = chooseScope(workspaceRootFolder);
if(!target){
return;
}
} else if(preferredScope === "global"){
target = vscode.ConfigurationTarget.Global;
} else if(preferredScope === "workspace"){
target = target = vscode.ConfigurationTarget.Workspace;
}
}else if(!workspaceRootFolder){
target = vscode.ConfigurationTarget.Global;
}

return target;

}


function isHexidecimal(str: string) {

let regexp = /^#[0-9a-fA-F]+$/;
Expand Down

0 comments on commit 6356767

Please sign in to comment.