1

I have implemented my own explorer view for my extension by implementing vscode.TreeDataProvider<> Now I want to customize color of tree view item/node which is my root node (or it can be other specific node as well). To color tree view item , I have tried to implement vscode.FileDecorationProvider interface and register this to vscode.window.registerFileDecorationProvider(); But I am not able to provide custom color for tree view item. Is there any sample code for this ? or any pointer to achieve this?

ajayg
  • 27
  • 4
  • Custom colors are currently not possible. – Mike Lischke Nov 18 '22 at 07:52
  • you have to write your own explorer view tree in a WebviewView, then you have **full** control, most likely there is a HTML/JavaScript framework that has a tree widget – rioV8 Nov 18 '22 at 09:41
  • See also your answered question at https://stackoverflow.com/questions/74449432/how-to-add-and-select-color-for-nodes-tree-view-items-in-explorer-view-in-my-vsc/74483366#74483366 about using a FileDecorationProvider to apply themeColors to TreeItem labels. – Mark Nov 18 '22 at 17:48
  • @MikeLischke An extension can define its own theme colors which can then be used in a FileDecorationProvider. – Mark Nov 18 '22 at 17:50
  • To me this question sounds like it asks for general treeviews, not just for the file tree. – Mike Lischke Nov 19 '22 at 09:11
  • FileDecoration providers also decorate treeViews. – Mark Dec 16 '22 at 05:04

1 Answers1

0

From themeable colors in extensions, you can add a colors section to your package.json:

"contributes": {

    "colors": [
        {
            "id": "editorManager.treeItemTextForeground",
            "description": "Color for a TreeIteem label",
            "defaults": {
                "dark": "#00ccff",   // your own defined colors
                "light": "#00ccff",
                "highContrast": "errorForeground"  // you can also use existing color theme references here
            }
        }
    ]
}

You can create as many colors aas you need. The id is just a name you can make up. It can then be used in your FileDecorationProvider like so:

  async provideFileDecoration(uri: Uri): Promise<FileDecoration | undefined> {
    const activeEditor = window.activeTextEditor.document.uri;
    if (uri.fsPath === activeEditor.fsPath) {
      return {
        badge: "⇐",
        // color: new ThemeColor("charts.red"), 
        color: new ThemeColor("editorManager.treeItemTextForeground"), // *****
        // color: new vscode.ThemeColor("tab.activeBackground"), 
        // tooltip: ""
      };
    }
    else return null;  // to get rid of the custom fileDecoration
  }

That color theme id will also be available to users of the extension to change in their settings.json colorCustomizations like so:

"workbench.colorCustomizations": {

  "editorManager.treeItemTextForeground": "#f00"
}

So you can create an initial default themeColor that can be applied to a TreeItem label via your FileDecorationProvider, but you don't know what color theme users might be using so they will and should have the ability to change your default colors if necessary in their own settings.

Mark
  • 143,421
  • 24
  • 428
  • 436
  • This is not working for treeview item instead it seems to be for the file open in the editor. – ajayg Nov 24 '22 at 16:39
  • It does work to color TreeItem labels, I tested it. you can use whatever you want to test against to change the color or nothing at all. – Mark Dec 06 '22 at 22:58