VSCode Extension - How to reuse Explorer window or other views? - visual-studio-code

I'm pretty new to VSCode development and looking through the docs I can't see much on how it would be possible to reuse the explorer view, say, in a new viewContainer
If I wanted to include the main explorer window in this new viewContainer is that even possible or is this an anti-pattern? It would be ideal to have ht main file explorer there for developers to use but have new windows underneath it that are more focused to the specific task.
I know you can just add new views into the root/main views section of an extensions config but I don't want to clutter the main view with a lot more views than is necessary.
So what would I put in here for example
{
// ... rest of package.json
"contributes": {
"views": {
"my-activitybar-container": [
{
"id": "<<MAIN_VSCODE_EXPLORER_VIEW>>",
"name": "Explorer"
},
// .. more views
]
}
}
// ... rest of file
}

Related

How do you focus on a panel view programmatically given an id

I'd like to be able to toggle open the panel (termainl, debug, output, etc.. area) and focus on a specific custom view given an id. I know this is possible since there are focus commands for terminal debug etc but searching them up in the vscode source code just gives string references and not publicly usable functions. Any help would be appreciated. (Please don't give shortcut answers like ctrl+j, I want to do this with code)
I tried copying the format of the focus view commands above but the focus of the custom view didn't work. Seems like its not an automatic thing.
I think you want this. Every custom TreeView or WebView gets some commands added automatically by vscode. For example, my Editor Manager ViewContainer, with this view:
"views": {
"editorManager": [
{
"id": "editor-groups",
"name": "Groups",
"size": 1,
"visibility": "visible"
}
]
}
gets these commands added automatically (they can be found in the Keyboard Shortcuts, at least when the extension is activated):
editor-groups.focus
workbench.actions.treeView.editor-groups.refresh
workbench.actions.treeView.editor-groups.collapseAll
workbench.view.extension.editorManager
workbench.view.extension.editorManager.resetViewContainerLocation
You probably want the first one to focus your view in the Panel.
vscode.commands.executeCommand("editor-groups.focus");

How to execute a particular command after clicking an extension's icon in VSCode's activity bar?

I am developing a VSCode extension and just finished adding the icon to the activity bar. What I want to do is, execute a command whenever the user clicks that icon. This is what I have tried:
Package.json
"viewsContainers": {
"activitybar": [
{
"id": "myID",
"title": "Testing",
"icon": "res/logo.svg"
}
]
},
"views": {
"myID": [
{
"id": "Explorer",
"name": "Browse contents",
"commands": "vscode-myorg.getContents"
}
]
}
However, the vscode-myorg.getContents command is not getting executed after clicking the icon. How can I achieve this?
In looking at Feature Request: Add an API event to indicate when the sidebar viewlet changes which was closed as waiting for another still open issue on getContext to be resolved. And using intellisense in package.json there is no other key besides id, title and icon. So you cannot put a command entry there for instance (I tried and nothing happened.)
There are a couple of other options that might work for you:
If your views are populated by a TreeView then you can detect when that treeViews' visibility changes:
myTree.onDidChangeVisibility(ev => {
console.log(ev); // ev.visibiltiy = true/false
})
So you can detect when the view becomes visible because the Activity Bar icon is clicked. That does work. However, this event is also triggered when the view is already opened and the user just collapses or expands that individual view. And I don't think there is really any way to distinguish between the two conditions.
If you are populating your views with a WebView there is also an onDidChangeVisibility event for that.
There is an activation event for a View becoming active:
"activationEvents": [
"onView:package-dependencies"
]
but that will only fire the first time a view is activated - and starts your extension's activate function so is probably not what you are looking for.
You have to define any new command of yours.
"commands": [
{
"command": "vscode-workat.getProblems",
"title": "Get Problems"
},
]
as well as register it in the extension.

Custom view decorations in VSCode extension

I'm building a VS Code extension and it uses a TreeDataProvider to create a list of items. The list has nested children and I would like to show a count for these in the parent.
I've looked for examples and tried to understand how the git extension does it but no luck. Perhaps someone can provide an example of how to do this.
Support for custom decorations in views appears to be a work-in-progress. There has been an API for it in the "proposed state" for a while, see:
Make decoration provider API public (#54938)
relevant section in vscode.proposed.d.ts
Source Control and Problem decorations already shown in custom views if TreeView.resourceUri is set.
Source Control decorations are managed via the Source Control API - each SourceControlResourceState instance can have decorations attached to it. That would be how the Git extension you mentioned does it.
Problem decorations are derived from the "problems" (errors, warnings...) associated with a URI. These are also shown in the Problems panel. Problems can be created using the Diagnostics API or with a problem matcher.
As of VS code version 1.52, the FileDecorationProvider is available to use, which is a way to add a text badge to a TreeItem. Related GitHub issue here for more context.
If you are using TreeItem then you will need to specify a resourceUri property which you then use to identify where to apply the badge.
To be clear the badge is limited to text and does not include the option to put it in a circle badge, like in the first picture of the question.
Here is a simple code snippet on how to use it:
class CountDecorationProvider {
constructor() {
this.disposables = [];
this.disposables.push(vscode.window.registerFileDecorationProvider(this));
}
provideFileDecoration(uri) {
const showCountFor = "/aUserHere";
if (uri.path === showCountFor) {
return {
badge: "10",
tooltip: "User count"
};
}
}
dispose() {
this.disposables.forEach((d) => d.dispose());
}
}

Controls in VSCode Custom Views

Now that VSCode has Custom Views, how can I implement one containing a simple textbox or anything but Tree?
The only example that I found is Azure Tools and it only registers tree items...
What I am after is something more like a Search panel. Is it possible to do it with VSCode Custom Views?
As of VS Code 1.29, you cannot add a search/input box to a tree view. This is tracked by this issue
You can add custom buttons to the top of a tree view using the menus -> view/title contribution point:
"menus": {
"view/title": [
{
"command": "myCommand",
"group": "navigation",
"when": "view == myTreeView.id"
}
]
LiveShare for example adds a join and start button:
You can also treat tree view items themselves as ad-hoc buttons. The Azure CosmosDB extension does this:
Please open a feature request if you are looking for more extensive customization

How to hide Open and Save document features in a Document Based application?

Like Safari app, in a macOS Swift project I would like to let users to open more than a window and possibly use tabbed browsing to switch from a window to another. The app starts with a blank window and don't need to save or open documents.
A Document Based application seems perfect to me for handle more than a window but I don't want users have to handle documents. How can I disable or hide, if possible, the Open and Save document features?
Edit: if possible, I would like to disable this popup also:
It is very simple - just remove (delete) the Open, Open Recent..., Save, Save as... menu items from the menu XIB. If you don't want a title bar, simply untick the "Title" checkbox for the window in the XIB, though that makes the window difficult to move.
If you have a title bar, to override "Untitled", you could
override var displayName: String! {
get {
return "Anything you like, even \"\""
}
set {
}
}
however that would still allow access to the save as menu through the chevron. To suppress that, you need an NSWindowDelegate Docs
window(_:shouldPopUpDocumentPathMenu:)
Asks the delegate whether the window displays the title pop-up menu in response to a Command-click or Control-click on its title.
Just add autosavesInPlace at true