Refactor browser element selection to be event-based#315362
Merged
Merged
Conversation
Contributor
There was a problem hiding this comment.
Pull request overview
Refactors Integrated Browser element selection from a request/cancellation-token flow into a model/service event-based flow, so element picks are delivered via events and selection is toggled explicitly.
Changes:
- Replace
getElementData/getFocusedElementDataRPCs withtoggleElementSelectionplusonDidSelectElement/onDidChangeElementSelectionActiveevents. - Wire the workbench chat integration to listen for element-selection events and attach picked elements to chat.
- Add main-process handling to pick the focused element via Ctrl/Cmd+Enter while element selection is active, and persist selection-active state in view state.
Show a summary per file
| File | Description |
|---|---|
| src/vs/workbench/contrib/browserView/electron-browser/features/browserEditorChatFeatures.ts | Switches chat attachment to react to element-selection events; toggles selection via model API. |
| src/vs/workbench/contrib/browserView/common/browserView.ts | Updates IBrowserViewModel/BrowserViewModel to expose selection state and selection events, and to toggle selection via the service. |
| src/vs/platform/browserView/electron-main/browserViewMainService.ts | Removes cancellable element-data RPCs and exposes dynamic events + toggleElementSelection to renderer via IPC. |
| src/vs/platform/browserView/electron-main/browserViewElementInspector.ts | Introduces selection-active state and emits events for selected elements and selection-active changes; adds focused-element pick API. |
| src/vs/platform/browserView/electron-main/browserView.ts | Intercepts Ctrl/Cmd+Enter during selection to pick the focused element; exposes inspector publicly and includes selection state in getState(). |
| src/vs/platform/browserView/common/browserView.ts | Extends service/state contracts with selection-active state, selection events, and toggleElementSelection. |
Copilot's findings
Comments suppressed due to low confidence (3)
src/vs/platform/browserView/electron-main/browserViewElementInspector.ts:149
toggleElementSelectionsets up_selectionStoreand anonEventlistener before callingOverlay.setInspectMode, but ifsendCommandthrows (e.g. CDP connection drops) the method will reject without disposing the store or resetting state. This can leak the event listener and leave_selectionStorepopulated whileisElementSelectionActiveremains false. Consider wrapping thesendCommand+ state flip in a try/catch that disposes_selectionStore(and resets it toundefined) on failure, or moving thesendCommandinto a try/finally cleanup path.
// Start selection
const connection = await this._connectionPromise;
// Clean up any prior selection state
this._selectionStore?.dispose();
const store = this._selectionStore = new DisposableStore();
store.add(connection.onEvent(async (event) => {
if (event.method !== 'Overlay.inspectNodeRequested') {
return;
}
const params = event.params as { backendNodeId: number };
if (!params?.backendNodeId) {
return;
}
try {
const nodeData = await extractNodeData(connection, { backendNodeId: params.backendNodeId });
this._onDidSelectElement.fire({
...nodeData,
url: this.browser.getURL()
});
await this._stopElementSelection();
} catch {
// Best effort - selection continues
}
}));
await connection.sendCommand('Overlay.setInspectMode', {
mode: 'searchForNode',
highlightConfig: inspectHighlightConfig,
});
this._elementSelectionActive = true;
this._onDidChangeElementSelectionActive.fire(true);
}
src/vs/platform/browserView/electron-main/browserViewElementInspector.ts:199
pickFocusedElementfiresonDidSelectElementbut does not disable element selection afterwards, unlike the click-pick path which calls_stopElementSelection(). This leaves the inspect overlay (and context key) active after Ctrl/Cmd+Enter, which is inconsistent with the “auto disabled after the first pick” behavior and the previous flow where accepting ended selection. Consider stopping selection after firing the event (and also ensuring errors from CDP calls don’t surface as unhandled rejections).
/**
* Fire a selection event for the currently focused element.
* Only effective when element selection is active.
*/
async pickFocusedElement(): Promise<void> {
if (!this._elementSelectionActive) {
return;
}
const connection = await this._connectionPromise;
await connection.sendCommand('Runtime.enable');
const { result } = await connection.sendCommand('Runtime.evaluate', {
expression: 'document.activeElement',
returnByValue: false,
}) as { result: { objectId?: string } };
if (!result?.objectId) {
return;
}
const nodeData = await extractNodeData(connection, { objectId: result.objectId });
this._onDidSelectElement.fire({
...nodeData,
url: this.browser.getURL()
});
}
src/vs/workbench/contrib/browserView/electron-browser/features/browserEditorChatFeatures.ts:418
- In
AddElementToChatAction.run,toggleElementSelectionis started withvoid ...which detaches it from the command execution. If the promise rejects, it will be unhandled and the command infrastructure won’t be able to report the error. Preferawait(or.catchwith logging) so failures are handled consistently.
async run(accessor: ServicesAccessor, browserEditor = accessor.get(IEditorService).activeEditorPane): Promise<void> {
if (browserEditor instanceof BrowserEditor) {
browserEditor.ensureBrowserFocus();
void browserEditor.model?.toggleElementSelection(undefined);
}
- Files reviewed: 6/6 changed files
- Comments generated: 3
Contributor
📬 CODENOTIFYThe following users are being notified based on files changed in this PR: @jrualesMatched files:
|
dmitrivMS
approved these changes
May 8, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
No description provided.