Skip to content

Commit 919a303

Browse files
committed
Switch to using WebviewView
1 parent 22c8923 commit 919a303

File tree

2 files changed

+63
-59
lines changed

2 files changed

+63
-59
lines changed

package.json

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,12 +42,20 @@
4242
"color": "#252526"
4343
},
4444
"activationEvents": [
45-
"onDebug",
46-
"onCommand:python-resource-monitor.rsmInterval",
47-
"onCommand:python-resource-monitor.rsmLength"
45+
"onDebug"
4846
],
4947
"main": "./out/extension.js",
5048
"contributes": {
49+
"views": {
50+
"debug": [
51+
{
52+
"id": "python-resource-monitor",
53+
"name": "Python Resource Monitor",
54+
"when": "debugType == 'debugpy' || debugType == 'python'",
55+
"type": "webview"
56+
}
57+
]
58+
},
5159
"commands": [
5260
{
5361
"command": "python-resource-monitor.rsmInterval",

src/extension.ts

Lines changed: 52 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -3,63 +3,56 @@ import * as ps from "node-ps-data";
33
import { join } from "path";
44
import * as vscode from "vscode";
55

6-
var panel: vscode.WebviewPanel;
6+
var webview: vscode.Webview;
77
var pollingInterval = 100;
88
var rsmLength = 10000;
99

1010
var pidMonitors = new Map();
11-
var shouldResetPanel = false;
1211

1312
function nop() {}
1413

15-
/**
16-
* Creates and starts a new Webview resource monitor.
17-
* @param context The VS Code Extension Context from which to launch the Webview.
18-
* @param pid The process ID to track with the resource monitor.
19-
*/
20-
async function launchWebview(context: vscode.ExtensionContext, pid: number) {
21-
// We want to reuse the panel for new processes in an existing debug session.
22-
if (panel) {
23-
if (!shouldResetPanel) {
24-
return;
25-
}
26-
27-
// Recreate the panel when starting a new debug session.
28-
shouldResetPanel = false;
29-
try {
30-
panel.dispose();
31-
} catch {}
14+
class PyRSMWebviewProvider implements vscode.WebviewViewProvider {
15+
context: vscode.ExtensionContext;
16+
constructor(context: vscode.ExtensionContext) {
17+
this.context = context;
3218
}
33-
// Create the webview
34-
panel = vscode.window.createWebviewPanel(
35-
"resourceMonitor",
36-
"Resource Monitor",
37-
vscode.ViewColumn.Beside,
38-
{ enableScripts: true }
39-
);
4019

41-
// When removing the panel, stop all monitoring
42-
panel.onDidDispose(() => {
43-
stopMonitoring();
44-
pidMonitors.clear();
45-
});
20+
resolveWebviewView(
21+
webviewView: vscode.WebviewView,
22+
context: vscode.WebviewViewResolveContext<unknown>,
23+
token: vscode.CancellationToken
24+
): void | Thenable<void> {
25+
webviewView.webview.options = {
26+
enableScripts: true,
27+
};
4628

47-
// Set page
48-
let paneljs = panel.webview.asWebviewUri(
49-
vscode.Uri.file(join(context.extensionPath, "webview", "panel.js"))
50-
);
29+
webviewView.onDidDispose(() => {
30+
stopMonitoring();
31+
pidMonitors.clear();
32+
});
5133

52-
var htmlText = fs
53-
.readFileSync(join(context.extensionPath, "webview", "panel.html"))
54-
.toString();
55-
htmlText = htmlText.replace("${paneljs}", paneljs.toString());
56-
panel.webview.html = htmlText;
34+
// Set page
35+
let paneljs = webviewView.webview.asWebviewUri(
36+
vscode.Uri.file(
37+
join(this.context.extensionPath, "webview", "panel.js")
38+
)
39+
);
5740

58-
panel.webview.postMessage({ type: "length", value: rsmLength });
41+
var htmlText = fs
42+
.readFileSync(
43+
join(this.context.extensionPath, "webview", "panel.html")
44+
)
45+
.toString();
46+
htmlText = htmlText.replace("${paneljs}", paneljs.toString());
47+
webviewView.webview.html = htmlText;
5948

60-
// Start updates
61-
startMonitor(pid);
62-
console.log(`Starting resource monitor for process ID ${pid}.`);
49+
webviewView.webview.postMessage({ type: "length", value: rsmLength });
50+
51+
webview = webviewView.webview;
52+
// Start updates
53+
//startMonitor(pid);
54+
//console.log(`Starting resource monitor for process ID ${pid}.`);
55+
}
6356
}
6457

6558
class PyDebugAdapterTracker implements vscode.DebugAdapterTracker {
@@ -72,9 +65,13 @@ class PyDebugAdapterTracker implements vscode.DebugAdapterTracker {
7265
// onDidTerminateDebugSession.
7366
onDidSendMessage(message: vscode.DebugProtocolMessage | any): void {
7467
if (message.type === "event" && message.event === "process") {
68+
// https://microsoft.github.io/debug-adapter-protocol//specification.html#Events_Process
7569
// New process spawned, start monitoring pid and open/reuse webview
70+
if (pidMonitors.size === 0) {
71+
webview.postMessage({ type: "reset" });
72+
}
7673
const pid = message.body.systemProcessId;
77-
launchWebview(this.context, pid);
74+
startMonitor(pid);
7875
} else if (message.type === "event" && message.event === "stopped") {
7976
// Debugging is paused or breakpoint is reached, pause monitoring of all pids
8077
stopMonitoring();
@@ -106,6 +103,14 @@ class PyDebugAdapterTrackerFactory
106103

107104
export function activate(context: vscode.ExtensionContext) {
108105
console.log("Extension Python Resource Monitor activated.");
106+
context.subscriptions.push(
107+
vscode.window.registerWebviewViewProvider(
108+
"python-resource-monitor",
109+
new PyRSMWebviewProvider(context),
110+
{ webviewOptions: { retainContextWhenHidden: true } }
111+
)
112+
);
113+
109114
// Commands
110115
// Polling interval change
111116
context.subscriptions.push(
@@ -180,7 +185,7 @@ export function activate(context: vscode.ExtensionContext) {
180185
let rsmLengthRepr =
181186
rsmLength === 0 ? "unlimited" : `${rsmLength}ms`;
182187
try {
183-
await panel.webview.postMessage({
188+
await webview.postMessage({
184189
type: "length",
185190
value: num,
186191
});
@@ -225,7 +230,6 @@ export function activate(context: vscode.ExtensionContext) {
225230
console.log("Parent process stopped!");
226231
stopMonitoring();
227232
pidMonitors.clear();
228-
shouldResetPanel = true;
229233
}
230234
});
231235
}
@@ -246,7 +250,7 @@ async function postData(
246250
try {
247251
// Make sure to catch promise rejections (when the webview has been closed but a message is still posted) with
248252
// .then()
249-
await panel.webview
253+
await webview
250254
.postMessage({ pid: pid, type: key, time: time, value: value })
251255
.then(nop, nop);
252256
} catch {
@@ -296,11 +300,3 @@ function stopMonitoring() {
296300
console.log("Stopped monitoring pids", [...pidMonitors.keys()]);
297301
pidMonitors.forEach((updateInterval) => clearInterval(updateInterval));
298302
}
299-
300-
export function deactivate() {
301-
try {
302-
panel.dispose();
303-
} catch {
304-
// pass
305-
}
306-
}

0 commit comments

Comments
 (0)