Files
vscode-extension-samples/tree-view-sample/USAGE.md

126 lines
4.5 KiB
Markdown
Raw Permalink Normal View History

2018-10-25 18:28:48 +02:00
# Contributing a View Container & View
2016-11-16 15:49:54 -08:00
- Contribute a view container using the [viewsContainers](https://code.visualstudio.com/api/references/contribution-points#contributes.viewsContainers) extension point.
- Contribute a view using the [views](https://code.visualstudio.com/api/references/contribution-points#contributes.views) extension point.
- Register a data provider for the view using the [TreeDataProvider](https://code.visualstudio.com/api/references/vscode-api#_TreeDataProvider) API.
- Contribute actions to the view using `view/title` and `view/item/context` locations in [menus](https://code.visualstudio.com/api/references/contribution-points#contributesmenus) extension point.
2016-11-16 15:49:54 -08:00
2018-10-25 18:28:48 +02:00
## contributes.viewsContainers extension point
2016-11-16 15:49:54 -08:00
2018-10-25 18:28:48 +02:00
As of Visual Studio Code v1.23.0, you can move custom views into your own view container which will show up in the activity bar.
To do such, extension writers can add a `viewsContainers` object in the contributes section. each object will require three things:
2018-10-25 18:28:48 +02:00
- `id`: The name of the new view you're creating
- `title`: The name which will show up at the top of the view
- `icon`: an image which will be displayed for the view container in the activity bar
## contributes.views extension point
2016-11-16 15:49:54 -08:00
You must specify an identifier and name for the view. You can contribute to following locations
2016-11-16 15:49:54 -08:00
- `explorer`: Explorer view in the Side bar
- `debug`: Debug view in the Side bar
2018-12-21 13:45:29 +01:00
- `scm`: Source Control Management view in the Side bar
2016-11-16 15:49:54 -08:00
When the user opens the view, VS Code will then emit an activationEvent `onView:${viewId}` (e.g. `onView:nodeDependencies` for the example below). You can also control the visibility of the view by providing the `when` context value.
2016-11-16 15:49:54 -08:00
Following, in the views object, you can then add a field with the same string as the `id` in the `viewsContainers`.
2018-10-25 18:28:48 +02:00
2016-11-16 15:49:54 -08:00
```json
"contributes": {
"viewsContainers": {
2018-10-25 18:28:48 +02:00
"activitybar": [
{
"id": "package-explorer",
"title": "Package Explorer",
"icon": "media/dep.svg"
2018-10-25 18:28:48 +02:00
}
]
},
"views": {
2018-10-25 18:28:48 +02:00
"tree-view": [
{
"id": "nodeDependencies",
"name": "Node Dependencies",
"when": "workspaceHasPackageJSON"
}
]
2016-11-16 15:49:54 -08:00
}
}
```
2018-10-25 18:28:48 +02:00
## View actions
2016-11-16 15:49:54 -08:00
2018-10-25 18:28:48 +02:00
You can contribute actions at following locations in the view
2018-10-25 18:28:48 +02:00
- `view/title`: Location to show actions in the view title. Primary or inline actions use `"group": "navigation"` and rest are secondary actions which are in `...` menu.
- `view/item/context`: Location to show actions for the tree item. Inline actions use `"group": "inline"` and rest are secondary actions which are in `...` menu.
You can control the visibility of these actions using the `when` property.
2018-10-25 18:28:48 +02:00
Examples:
```json
"contributes": {
2018-10-25 18:28:48 +02:00
"commands": [
{
"command": "nodeDependencies.refreshEntry",
"title": "Refresh",
"icon": {
"light": "resources/light/refresh.svg",
"dark": "resources/dark/refresh.svg"
}
2018-10-25 18:28:48 +02:00
}
],
"menus": {
"view/title": [
{
2018-10-25 18:28:48 +02:00
"command": "nodeDependencies.refreshEntry",
"when": "view == nodeDependencies",
"group": "navigation"
}
]
}
}
```
2018-10-25 18:28:48 +02:00
**Note:** If you want to show an action for specific items, you can do it by defining context of a tree item using `TreeItem.contextValue` and you can specify the context value for key `viewItem` in `when` expression.
Examples:
```json
"contributes": {
"menus": {
"view/item/context": [
{
"command": "nodeDependencies.deleteEntry",
"when": "view == nodeDependencies && viewItem == dependency"
}
2018-10-25 18:28:48 +02:00
]
}
}
```
## TreeDataProvider
Extension writers should register a [provider](https://code.visualstudio.com/api/references/vscode-api#TreeDataProvider) programmatically to populate data in the view.
2018-10-25 18:28:48 +02:00
```typescript
vscode.window.registerTreeDataProvider('nodeDependencies', new DepNodeProvider());
```
See [nodeDependencies.ts](src/nodeDependencies.ts) for the implementation.
## TreeView
2021-03-27 15:30:10 +03:00
If you would like to perform some UI operations on the view programmatically, you can use `window.createTreeView` instead of `window.registerDataProvider`. This will give access to the view which you can use for performing view operations.
2018-10-25 18:28:48 +02:00
```typescript
vscode.window.createTreeView('ftpExplorer', {
treeDataProvider: new FtpTreeDataProvider(),
});
2018-10-25 18:28:48 +02:00
```
See [ftpExplorer.ts](src/ftpExplorer.ts) for the implementation.