Repository navigation
don't bounce OS focus when surfacing the main window behind a satellite - #18642
Conversation
The bringMainFrameBehindActive desktop callback means "make the main window visible, but leave focus alone" -- GWT routes every cross-window event whose focus mode is MODE_AUXILIARY through it, including the SendToConsoleEvent fired when code is run from a popped-out source window with "focus console after executing code" turned off. The Electron implementation got there by focusing the main window and then handing focus back to the active one. Window managers with focus-stealing prevention refuse the second half of that bounce, so focus is left on the main window and the caret lands in the console -- exactly what the preference asks RStudio not to do. The Qt implementation this replaced used SetWindowPos with SWP_NOACTIVATE and was a deliberate no-op outside Windows. A main window that is already on screen now needs nothing done to it at all, and a hidden or minimized one is surfaced with showInactive() so focus is never touched. The minimized case that #11646 fixed is preserved. Addresses #18635
RStudio E2E - all platformsAll tests passed!
View / download the full reportView report online -- opens in the browser, no download needed. Available for 14 days. Download playwright-report.zip -- extract and open
All platforms - commit |
| // https://github.com/rstudio/rstudio/issues/18635 | ||
| if (mainWindow.isVisible() && !mainWindow.isMinimized()) { | ||
| return; | ||
| } |
There was a problem hiding this comment.
Returning early whenever the main window is visible drops the z-order raise this callback exists to perform.
AppCommand.java:179 documents the intent as "we still want the main window to be as visible as possible, so bring it up behind the current window", and the Qt implementation cited in the PR description did exactly that: SetWindowPos(hwndBottom, hwndTop, SWP_NOMOVE | SWP_NOSIZE | SWP_NOACTIVATE) restacks the main window directly beneath the active one without changing focus. It was a restack, not a no-op.
Concrete regression: the main window is on screen but buried under another application (browser, terminal), and the user runs a background command from the popped-out source window. isVisible() is true and isMinimized() false, so nothing happens and the main window stays buried — the user never sees the console output the raise was meant to reveal.
mainWindow.moveTop() followed by activeWindow.moveTop() restores the raise without touching focus (both are non-activating restacks), which matches the original Qt semantics.
There was a problem hiding this comment.
Agreed -- confirmed the Qt version restacked rather than no-op'd (cfe94e7: SetWindowPos(hwndBottom, hwndTop, SWP_NOMOVE | SWP_NOSIZE | SWP_NOACTIVATE)), and the early return dropped that. Fixed in 2396e89 with mainWindow.moveTop() followed by activeWindow.moveTop().
Checked moveTop() is genuinely non-activating in Electron 42 before adopting it: NativeWindowViews::MoveTop() is SetWindowPos(HWND_TOP, ..., SWP_NOACTIVATE) on Windows and _NET_RESTACK_WINDOW on X11; NativeWindowMac::MoveTop() is orderWindow:NSWindowAbove relativeTo:0. It is a no-op under native Wayland, which matches the Qt version being an explicit no-op outside Windows. A unit test now asserts both calls happen in that order, and that neither window is focused.
| } | ||
|
|
||
| // surface a hidden or minimized main window without activating it | ||
| mainWindow.showInactive(); |
There was a problem hiding this comment.
showInactive() is not documented to restore a minimized window, so the isMinimized() branch may be a no-op on macOS — which is the exact case #11646 fixed by switching focus() to show().
Electron implements showInactive() on macOS as [window_ orderFrontRegardless], and AppKit specifies that ordering methods have no effect on a miniaturized window. The previous show() went through makeKeyAndOrderFront:, which does de-miniaturize. So: main window minimized to the Dock, background command fires from the satellite, window stays in the Dock.
The unit tests assert showInactive was called on a fake, so they can't catch this, and the PR notes macOS wasn't manually verified. Worth checking on macOS before merge.
There was a problem hiding this comment.
Agreed. Electron 42's NativeWindowMac::ShowInactive() is widget()->ShowInactive() + [window_ orderFrontRegardless], and Apple documents orderFrontRegardless as pure ordering -- "without changing either the key window or the main window" -- with no de-miniaturize step. Restore() is [window_ deminiaturize:nil], the only path that de-miniaturizes.
Two more things point the same way: raiseAndActivateWindow() in utils.ts already calls restore() explicitly when isMinimized(), and #11646's own description says showInactive() was tried in this exact handler and rejected ("I tried showInactive() ... but it always focused the window for me on Mac").
Fixed in 2396e89 -- the minimized branch calls restore(), and showInactive() is reserved for a window that is hidden rather than minimized.
One caveat for the macOS pass: deminiaturize: is not documented either way on key-window status, so a minimized main window may still take focus on macOS when it is restored. The old code handed focus back with activeWindow.focus() after show(); I have deliberately not reintroduced that, since it is the bounce #18635 is about. If the macOS check shows focus moving, the narrow fix is to hand it back on the minimized path only.
gtritchie
left a comment
There was a problem hiding this comment.
A couple findings, not sure how critical they are.
The previous revision made the visible case a no-op, which dropped the z-order raise the callback exists to perform: AppCommand documents the intent as "we still want the main window to be as visible as possible, so bring it up behind the current window", and the Qt implementation restacked with SetWindowPos(..., SWP_NOACTIVATE). A main window buried under a browser or terminal would stay buried when a background command ran from a satellite. moveTop() on the main window followed by moveTop() on the active one restores that raise without touching focus. It is a non-activating restack on every platform Electron implements it for -- SetWindowPos(HWND_TOP, SWP_NOACTIVATE) on Windows, _NET_RESTACK_WINDOW on X11, orderWindow:NSWindowAbove on macOS -- and a no-op on Wayland, which matches the Qt version's platform coverage. showInactive() also does not de-miniaturize a window: on macOS it is orderFrontRegardless, which Apple documents as pure ordering, "without changing either the key window or the main window". The minimized case #11646 fixed now goes through restore() -- [window_ deminiaturize:nil] -- which is what raiseAndActivateWindow() in utils.ts already does. Addresses #18635
Resolved NEWS.md by hand. #18644 rewrote every Autumn Hawkbit entry into a one-line summary; git's auto-merge kept both that rewrite and this branch's pre-rewrite copy of the same entries, duplicating 30 of them. Took main's NEWS.md wholesale and re-added #18635 as a single entry in the new short style.
gtritchie
left a comment
There was a problem hiding this comment.
Re-reviewed after 2396e89. Both earlier findings are addressed: the visible case restacks again (moveTop pair, matching the Qt SetWindowPos(..., SWP_NOACTIVATE) semantics), and the minimized case no longer relies on showInactive() to de-miniaturize. Two things on the new revision.
NEWS.md entry for #18635 was lost in the merge. The merge commit message says it "re-added #18635 as a single entry in the new short style", but at the branch head:
$ git diff origin/main c25163cb62 -- NEWS.md # empty
$ git show c25163cb62:NEWS.md | grep -c 18635 # 0
Taking main's NEWS.md wholesale dropped the entry and it was never re-added, so the PR currently ships a user-facing fix with no release note. Something like:
- ([#18635](https://github.com/rstudio/rstudio/issues/18635)): Running code from a popped-out source window no longer moves focus to the console when "Focus console after executing code from the source pane" is off.
The inline comment covers the other point.
| // A minimized window can't be restacked, and ordering a window front does not | ||
| // de-miniaturize it, so bring it back to the screen first (#11646). | ||
| if (mainWindow.isMinimized()) { | ||
| mainWindow.restore(); |
There was a problem hiding this comment.
restore() is itself an activating call, so the minimized branch still moves OS focus to the main window — and unlike the old code, nothing hands it back.
On Windows it goes NativeWindowViews::Restore() → Widget::Restore() → HWNDMessageHandler::Restore() → ShowWindow(hwnd, SW_RESTORE), and Win32 documents SW_RESTORE as "Activates and displays the window". macOS deminiaturize: likewise makes the window key.
So: main window minimized, source window focused, user runs a line with the preference off. restore() activates the main window; the two moveTop() calls only restack, so the source window ends up visually on top without keyboard focus and keystrokes go to the console. The pre-PR code did return focus on Windows (show() then activeWindow.focus()), so this is a regression there for the minimized case.
showInactive() does de-miniaturize without activating on Windows (SW_SHOWNOACTIVATE, which is SW_SHOWNORMAL minus activation) and on X11 — macOS is the only platform where it doesn't. That suggests keeping the non-activating path where it works:
if (mainWindow.isMinimized() || !mainWindow.isVisible()) {
mainWindow.showInactive();
// macOS: orderFrontRegardless is pure ordering and won't de-miniaturize
if (mainWindow.isMinimized()) {
mainWindow.restore();
}
}Worth noting the never focuses either window test can't catch this — it asserts focus() wasn't called, and restore() activates natively without going through focus().
There was a problem hiding this comment.
Confirmed, and fixed in 32e808f. The mechanism is ExecuteSystemMenuCommand(SC_RESTORE) rather than ShowWindow(SW_RESTORE) (HWNDMessageHandler::Restore()), but it activates the same way, and deminiaturize: does on macOS too. Combined with the moveTop pair the satellite ended up visually on top with keystrokes going to the console.
I took the showInactive()-first half of your suggestion but not the isMinimized() re-query. Two reasons: HWNDMessageHandler::Show(kInactive) picks SW_SHOWNA rather than SW_SHOWNOACTIVATE when is_maximized_or_arranged is true, and IsZoomed() still reports true for a window minimized from maximized -- the common case for a main window -- so restore() runs anyway there. And on X11 IsMinimized() reads _NET_WM_STATE_HIDDEN out of window_properties_, which only updates on async PropertyNotify, so the re-query can't be trusted right after the call; it happens to be harmless only because X11Window::Show() early-returns on window_mapped_in_client_, making showInactive() a no-op for a minimized window there.
So restore() still runs on macOS and X11, and focus is handed back to the active window afterwards -- the pre-Electron behavior, scoped to the minimized branch. The #18635 path never reaches it. Two tests replace never focuses either window: one for the non-activating de-miniaturize, one for the handback; both fail against the previous revision.
One honest caveat: on macOS 13+ deminiaturize: no longer makes the window key synchronously, so the handback may race there. If the manual macOS pass shows focus landing on the main window anyway, that needs a delayed handback rather than an inline one.
Lost in the merge of main: #18644 rewrote the Autumn Hawkbit entries and resolving the conflict by taking main's NEWS.md wholesale dropped this branch's entry instead of re-adding it.
restore() is an activating call on every platform where it is the only way back onto the screen -- ExecuteSystemMenuCommand(SC_RESTORE) on Windows, deminiaturize: on macOS -- so the minimized branch moved OS focus to the main window and, unlike the pre-Electron-port code, never handed it back. Combined with the moveTop pair that follows, the satellite ended up visually on top while keystrokes went to the console. Try showInactive() first: on Windows it is SW_SHOWNOACTIVATE, which de-miniaturizes without activating. It is pure ordering on macOS and a no-op for an already-mapped window on X11, so restore() is still needed there, and focus is returned to the active window afterwards. Only the minimized path does this; the case #18635 is about never reaches it. Addresses #18635
gtritchie
left a comment
There was a problem hiding this comment.
Re-review say the NEWS.md entry was lost, and one other finding.
| // without activating, so try it first. It is pure ordering on macOS | ||
| // (orderFrontRegardless) and a no-op for an already-mapped window on X11, | ||
| // and on those platforms restore() is the only way back onto the screen. | ||
| mainWindow.showInactive(); |
There was a problem hiding this comment.
Worth verifying on Windows: SW_SHOWNOACTIVATE de-miniaturizes, but I don't think it restores the maximized state.
SW_SHOWNOACTIVATE is documented as "similar to SW_SHOWNORMAL, except that the window is not activated" — it doesn't honor WPF_RESTORETOMAXIMIZED the way SW_RESTORE / SC_RESTORE does. Electron's own show() works around exactly this by routing through GetRestoredState(), which returns SHOW_STATE_MAXIMIZED when the window was maximized; showInactive() always passes SHOW_STATE_INACTIVE.
Scenario: main window maximized, user minimizes it, runs a background command from the satellite → the main window comes back at its restored size instead of maximized. Both the pre-PR show() and the previous revision's restore() preserved it. It also can't be re-applied afterwards, since isMaximized() is false while the window is iconic (::IsZoomed returns false).
If that holds, the simplest fix is to skip the showInactive() attempt on Windows — or drop it entirely and let the restore() + activeWindow.focus() pair handle all three platforms. The minimized path already accepts that bounce on macOS and X11, and Windows is where the focus handoff actually works, so nothing is lost by using it there too.
Everything else looks right to me now. Verified separately that sinon.stub().onFirstCall().returns(true).returns(false) yields true-then-false as the test intends (behavior methods return the stub, so the trailing returns sets the default), so the Windows-path test exercises what it claims.
Problem
With the Source editor popped out into its own window on RStudio Desktop, running code moves the caret to the console even when Tools > Global Options > Code > Execution > "Focus console after executing code from the source pane" is unchecked. Docked editors are unaffected. Reported on Ubuntu 24.04; not reproducible on macOS.
Cause
The preference is read and propagated correctly. What moves focus is a window-level action that only runs when the editor is a satellite window:
EditingTargetCodeExecution.executeRange()firesSendToConsoleEventwithfocus = false(the preference).SendToConsoleEvent.focusMode()returnsMODE_FOCUSwhen focus is on andMODE_AUXILIARYwhen it is off, so turning the preference off is what routes execution intoEventBus's auxiliary branch:MODE_AUXILIARYmeans "make the window visible if possible but don't give it focus". The Electron implementation got there by bouncing OS focus:Window managers with focus-stealing prevention (GNOME on Ubuntu) refuse the second half, so focus stays on the main window -- where the caret lives in the console. macOS and Windows honor the bounce, which is why this looks platform-specific.
This is a regression from the Qt -> Electron port. The original implementation (cfe94e7) was
SetWindowPos(..., SWP_NOACTIVATE)-- no focus change at all, and an explicit no-op outside Windows. #11407 asked the Electron port to do "the same thing it did in Qt"; c4dd4c2 implemented the focus bounce instead, on every platform.Fix
Never touch focus. A main window that is already on screen needs nothing done to it, so the common case becomes a true no-op; a hidden or minimized one is surfaced with
showInactive(). The minimized-main-window case fixed by #11646 is preserved.Testing
Unit tests over the
desktop_bring_main_frame_behind_activehandler cover the visible, hidden, minimized, main-window-is-active, and nothing-focused cases. The three behavioral tests fail against the previous implementation and pass against this one.npm test,npm run lint, andnpm run typecheckall pass insrc/node/desktop(the one failure,Utils > findComponents, is a worktree artifact -- it needs a built rsession binary).Verification still needed
The user-visible symptom is window-manager dependent and could not be reproduced on macOS, so this needs a manual check on Ubuntu/GNOME: pop the Source editor out, uncheck the preference, run a line, and confirm the caret stays in the source window. Worth also confirming on Windows that a minimized main window still surfaces (#11646) and that a background command run from a satellite still brings the main window up behind it (#11407).
Addresses #18635.