Repository navigation
Description
Preflight Checklist
- I have read the Contributing Guidelines for this project.
- I agree to follow the Code of Conduct that this project adheres to.
- I have searched the issue tracker for a bug report that matches the one I want to file, without success.
Electron Version
45.5.0
What operating system(s) are you using?
Windows
Operating System Version
Windows 10.0.26300, x64
What arch are you using?
x64
Last Known Working Electron version
Don't know — never worked for me.
Does the issue also appear in Chromium / Google Chrome?
No
Chrome's own docked DevTools does not have this behaviour (its DevToolsWindow applies a dock change without tearing the frontend down, and its page layout is synchronous).
Expected Behavior
The inspected page keeps its scroll position. window.scrollX stays 0 while opening and closing docked DevTools.
Actual Behavior
Closing docked DevTools leaves the page horizontally scrolled, and it never scrolls back:
before { w: 1100, x: 0, active: 'BODY#' }
docked { w: 545, x: 0, active: 'BODY#' }
after { w: 1100, x: 430, active: 'A#target' } <-- expected x: 0
The user-visible symptom: on pages that declare html { overflow-x: hidden } — which is most real sites — no horizontal scrollbar is rendered, so the user cannot scroll back manually. The content is cut off halfway across the window with the right part blank, and the page is only recoverable by navigating or reloading.
Note the active field in the log: focus ends up on an element inside the page, which is what makes Blink restore it and scroll it into view.
Testcase Gist URL
Self-contained: two data: URLs, no network, no third-party site. Run with <path-to>/electron.exe repro.js.
Three conditions are needed, and any one of them alone is not enough:
- when the dock happens, focus must be outside the page (e.g. on a toolbar
WebContents) — otherwise hiding the focused DevTools view has no focus to hand over; - the page must hold an element that Blink remembers but that is not currently focused — the gist produces this with
el.focus(); el.blur(), which mirrors a user who clicked an ad/input earlier and then scrolled back; - that element must sit outside the narrowed viewport, on a page with
html{overflow-x:hidden}.
Reproduced 3/3 on 45.5.0 and 3/3 on 39.8.10, identical numbers. Change mode to 'detach' and it never reproduces (see Additional Information) — side docking is required.
Additional Information
Only tested on Windows x64 — I have not tried macOS or Linux. The same script also reproduces on Electron 39.8.10 (EOL) with an identical scrollX value (430), so this is not a regression from a recent version.
In-depth investigation
Two separate paths hand focus to the inspected page while DevTools is being torn down.
1. InspectableWebContents::CloseDevTools() unconditionally focuses the page
// shell/browser/ui/inspectable_web_contents.cc (still present on main)
embedder_message_dispatcher_.reset();
if (!is_guest())
web_contents_->Focus();This runs while the DevTools view is going away. Handing focus to the page makes Blink restore the previously focused element and scroll it into view. The focus is dropped again right after this call, so the hand-off achieves nothing except the scroll.
2. Hiding the focused DevTools view makes views transfer focus to the page
// shell/browser/ui/inspectable_web_contents_view.cc
devtools_web_view_->SetVisible(false); // this view currently holds focus
devtools_web_view_->SetWebContents(nullptr);
FlushPendingRootLayout(this); // DeprecatedLayoutImmediately() on 39.xviews moves focus to the next focusable view, i.e. contents_web_view_. Same outcome, second path.
Docking mode matters, and that pins the cause to focus, not painting:
openDevTools({ mode }) |
page viewport | scrollX after close |
|---|---|---|
'right' |
1100 → 545 | 398, 4/4 runs |
'bottom' |
1100 (unchanged) | 0 — but 1/16 runs still hit 398 |
'detach' |
1100 (unchanged) | 0, 4/4 runs |
detach never reproduces because a separate window never changes the page viewport, so the
whole "narrow the page → hand it focus → Blink scrolls to the remembered element" chain is absent.
bottom does not narrow the viewport either, yet it still fails occasionally — so the trigger is
the focus hand-off, not the resize or the painting. Side docking just makes it deterministic by
guaranteeing the remembered element ends up outside the narrower viewport.
A related variant (page scrolls while still docked): changing the dock side goes through
InspectableWebContentsView::SetIsDocked(), implemented as close-then-show:
void InspectableWebContentsView::SetIsDocked(bool docked, bool activate) {
CloseDevTools(); // Layout() then takes the "DevTools hidden" branch
...
ShowDevTools(activate);
}The layout flush inside CloseDevTools() makes Layout() call SetContentsViewBounds(GetContentsBounds()), so the page is briefly laid out at the full window width (measured innerWidth 545 → 1100 → 545 within ~180 ms). Combined with the focus hand-off, the page scrolls sideways while DevTools is still docked. Fixing the focus hand-off alone does not cover this variant.
Why InspectableWebContents is where it happens: the inspected page rect arrives asynchronously from the DevTools frontend (SetInspectedPageBounds → SetContentsResizingStrategy), which is what creates the window in which the page rect momentarily falls back to the full container. Chromium's own DevToolsWindow does not have this indirection.
Suggested fix — don't hand focus to the page during teardown, and don't let the page rect jump back to the full window width during a dock transition:
--- a/shell/browser/ui/inspectable_web_contents.cc
+++ b/shell/browser/ui/inspectable_web_contents.cc
@@ -453,8 +453,10 @@ void InspectableWebContents::CloseDevTools() {
managed_devtools_web_contents_.reset();
}
embedder_message_dispatcher_.reset();
- if (!is_guest())
- web_contents_->Focus();
+ // Focusing the page here makes Blink restore the last focused element and
+ // scroll it into view. The focus is dropped again right after anyway, so
+ // leave restoring focus to the embedder, which also knows whether the
+ // window is in the foreground.
}
}--- a/shell/browser/ui/inspectable_web_contents_view.cc
+++ b/shell/browser/ui/inspectable_web_contents_view.cc
@@ -164,9 +164,14 @@ void InspectableWebContentsView::CloseDevTools() {
devtools_window_delegate_ = nullptr;
} else {
+ // Hiding a focused view makes views transfer focus to the inspected page.
+ if (contents_web_view_)
+ contents_web_view_->SetFocusBehavior(views::View::FocusBehavior::NEVER);
devtools_web_view_->SetVisible(false);
devtools_web_view_->SetWebContents(nullptr);
FlushPendingRootLayout(this);
+ if (contents_web_view_ && !in_dock_transition_)
+ contents_web_view_->SetFocusBehavior(views::View::FocusBehavior::ALWAYS);
}
} void InspectableWebContentsView::SetIsDocked(bool docked, bool activate) {
+ const bool was_transitioning = in_dock_transition_;
+ in_dock_transition_ = true;
CloseDevTools();
...
ShowDevTools(activate);
+ in_dock_transition_ = was_transitioning;
+ if (contents_web_view_)
+ contents_web_view_->SetFocusBehavior(views::View::FocusBehavior::ALWAYS);
} void InspectableWebContentsView::Layout(PassKey) {
if (!devtools_web_view_->GetVisible()) {
+ // Don't stretch the page back to the full window width mid-transition.
+ if (in_dock_transition_) {
+ LayoutSuperclass<View>(this);
+ return;
+ }
SetContentsViewBounds(GetContentsBounds());plus bool in_dock_transition_ = false; in inspectable_web_contents_view.h. 46 lines across 3 files; I can open a PR if that is welcome.
Verification of the patch (same gist, only the binary differs, 3 runs each):
| Electron | before | docked | after |
|---|---|---|---|
| 45.5.0 stock | 0 | 0 | 430 |
| 39.8.10 stock | 0 | 0 | 430 |
| patched (39.8.10 base) | 0 | 0 | 0 |
Pixel-level check as well (webContents.capturePage() against a pre-dock baseline): 74.9% of pixels differ without the patch, 0.3% with it (0.3% is carousel noise).
Related but distinct: #35715 (WCO forces right → undocked), #50807 (DevTools re-attaches on open), #49847 (docked DevTools and draggable regions).
Activity
github-actions commentedon Nov 28, 2026
Hello @Sutanm. Thanks for taking the time to open an issue and helping to make Electron better!
This issue was automatically closed because its body does not follow any of this repository's issue templates. Maintainers rely on the sections in those templates to triage issues, and issues that are missing them usually cannot be acted upon.
To get this issue triaged, please either:
docs(issue): 已提交上游 electron/electron#56086