Skip to content

Commit

Permalink
Add support for opening SceneGraph Inspector in panel (#561)
Browse files Browse the repository at this point in the history
* add support for opening scenegraph inspector in panel

* switch back to private for setupViewMessageObserver

* Change icon to link-external

---------

Co-authored-by: Brian Leighty <[email protected]>
Co-authored-by: Bronley Plumb <[email protected]>
  • Loading branch information
3 people authored Apr 10, 2024
1 parent 7671c86 commit abe1813
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 9 deletions.
18 changes: 11 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -384,9 +384,13 @@
"group": "navigation@1"
},
{
"command": "extension.brightscript.openSceneGraphInspectorInPanel",
"when": "view == sceneGraphInspectorView",
"group": "navigation@1"
}, {
"command": "extension.brightscript.disconnectFromDevice",
"when": "view == sceneGraphInspectorView && brightscript.isOnDeviceComponentAvailable",
"group": "navigation@1"
"group": "navigation@2"
}
],
"webview/context": [
Expand Down Expand Up @@ -3034,17 +3038,17 @@
"category": "BrighterScript",
"icon": "$(clippy)"
},
{
"command": "extension.brightscript.sceneGraphInspectorView.refreshNodeTree",
"title": "Refresh Nodetree",
"category": "BrighterScript",
"icon": "$(refresh)"
},
{
"command": "extension.brightscript.disconnectFromDevice",
"title": "Disconnect From Roku Device",
"category": "BrighterScript",
"icon": "$(debug-disconnect)"
},
{
"command": "extension.brightscript.openSceneGraphInspectorInPanel",
"title": "Open SceneGraph Inspector In New Window",
"category": "BrighterScript",
"icon": "$(link-external)"
}
],
"keybindings": [
Expand Down
3 changes: 2 additions & 1 deletion src/commands/VscodeCommand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,6 @@ export enum VscodeCommand {
rokuAppOverlaysViewAddNewOverlay = 'extension.brightscript.rokuAppOverlaysView.addNewOverlay',
rokuAppOverlaysViewRemoveAllOverlays = 'extension.brightscript.rokuAppOverlaysView.removeAllOverlays',
rokuFileSystemViewRefresh = 'extension.brightscript.rokuFileSystemView.refresh',
disconnectFromDevice = 'extension.brightscript.disconnectFromDevice'
disconnectFromDevice = 'extension.brightscript.disconnectFromDevice',
openSceneGraphInspectorInPanel = 'extension.brightscript.openSceneGraphInspectorInPanel'
}
55 changes: 55 additions & 0 deletions src/viewProviders/BaseWebviewViewProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ export abstract class BaseWebviewViewProvider implements vscode.WebviewViewProvi
*/
public readonly abstract id: string;

protected panel?: vscode.WebviewPanel;
protected view?: vscode.WebviewView;
protected webviewBasePath: string;
private outDirWatcher: AsyncSubscription;
Expand Down Expand Up @@ -87,6 +88,10 @@ export abstract class BaseWebviewViewProvider implements vscode.WebviewViewProvi
this.view?.webview.postMessage(message).then(null, (reason) => {
console.log('postMessage failed: ', reason);
});

this.panel?.webview.postMessage(message).then(null, (reason) => {
console.log('postMessage failed: ', reason);
});
}

private postQueuedMessages() {
Expand Down Expand Up @@ -258,4 +263,54 @@ export abstract class BaseWebviewViewProvider implements vscode.WebviewViewProvi
};
webview.html = await this.getHtmlForWebview();
}

protected async createOrRevealWebviewPanel() {
// See if we need to make the panel or not
let createPanel = false;
if (!this.panel) {
createPanel = true;
} else {
try {
if (!this.panel.active) {
// If we still exist and aren't active then reveal the panel
this.panel.reveal();
}
} catch (e) {
createPanel = true;
}
}

if (createPanel) {
this.panel = vscode.window.createWebviewPanel(
this.id,
await this.getViewNameById(this.id),
vscode.ViewColumn.Active,
{
// Enable javascript in the webview
enableScripts: true,
localResourceRoots: [
vscode.Uri.file(this.webviewBasePath)
]
}
);

this.setupViewMessageObserver(this.panel.webview);

const html = await this.getHtmlForWebview();
this.panel.webview.html = html;
}
}

private async getViewNameById(viewId) {
const packageJsonPath = path.join(this.extensionContext.extensionPath, 'package.json');
const packageJson = JSON.parse(await fsExtra.readFile(packageJsonPath, 'utf8'));

for (const view of [...packageJson.contributes.views.debug, ...packageJson.contributes.views['vscode-brightscript-language']]) {
if (view.id === viewId) {
return view.name;
}
}

return null;
}
}
5 changes: 4 additions & 1 deletion src/viewProviders/SceneGraphInspectorViewProvider.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
import type * as vscode from 'vscode';
import { BaseRdbViewProvider } from './BaseRdbViewProvider';
import { ViewProviderId } from './ViewProviderId';
import { VscodeCommand } from '../commands/VscodeCommand';

export class SceneGraphInspectorViewProvider extends BaseRdbViewProvider {
public readonly id = ViewProviderId.sceneGraphInspectorView;

constructor(context: vscode.ExtensionContext, dependencies) {
super(context, dependencies);

this.registerCommandWithWebViewNotifier(context, 'extension.brightscript.sceneGraphInspectorView.refreshNodeTree');
this.registerCommand(context, VscodeCommand.openSceneGraphInspectorInPanel, async () => {
await this.createOrRevealWebviewPanel();
});
}
}

0 comments on commit abe1813

Please sign in to comment.