Skip to content

[Windows] Keep regular windows in place when another is activated#188016

Merged
knopp merged 9 commits into
flutter:masterfrom
9AZX:fix_window_focus_win32
Jul 2, 2026
Merged

[Windows] Keep regular windows in place when another is activated#188016
knopp merged 9 commits into
flutter:masterfrom
9AZX:fix_window_focus_win32

Conversation

@9AZX

@9AZX 9AZX commented Jun 15, 2026

Copy link
Copy Markdown
Contributor

fix #187436

On Windows with multi-window enabled, activating a regular window pulled other regular windows forward in the z-order (and could steal focus back), even though they are independent. The causes span the engine and the framework:

  • Engine: the base WM_ACTIVATE handler refocused a window's own view on deactivation, reactivating it. The activation logic is now shared with the dialog handler and skips refocusing on WA_INACTIVE
  • Framework: when a view is rendered inside another (via ViewAnchor), its FocusScope nested under the parent view's scope, so the parent reported hasFocus whenever a child view was focused and asked the engine to focus its window. Each view now attaches its focus subtree to the root scope (new FocusTraversalGroup.parentNode), so each view is an independent focus root. This also fixes moving focus from a child view back to a parent view.
  • Framework: on desktop, the focus suspended when the app goes inactive was restored on resume even if another window had been activated meanwhile, stealing focus back. It now restores only when nothing else has claimed focus.

Tested manually on Windows 11 with examples/multiple_windows

Screen.Recording.2026-06-15.at.14.26.39.mov

Pre-launch Checklist

If you need help, consider asking for advice on the #hackers-new channel on Discord.

If this change needs to override an active code freeze, provide a comment explaining why. The code freeze workflow can be overridden by code reviewers. See pinned issues for any active code freezes with guidance.

Note: The Flutter team is currently trialing the use of Gemini Code Assist for GitHub. Comments from the gemini-code-assist bot should not be taken as authoritative feedback from the Flutter team. If you find its comments useful you can update your code accordingly, but if you are unsure or disagree with the feedback, please feel free to wait for a Flutter team member's review for guidance on which automated comments should be addressed.

@9AZX
9AZX requested a review from a team as a code owner June 15, 2026 12:35
@github-actions github-actions Bot added engine flutter/engine related. See also e: labels. platform-windows Building on or for Windows specifically a: desktop Running on desktop team-windows Owned by the Windows platform team labels Jun 15, 2026

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request modifies window focus and activation handling on Windows to prevent unexpected window reactivation and z-order changes. It updates FlutterWindow::Focus to only apply focus if the top-level window is already active, ignores unfocus requests in FlutterWindowsEngine::OnViewFocusChangeRequest, and consolidates window activation logic into HostWindow::HandleWindowActivation to prevent deactivated windows from reactivating themselves. Unit tests are added to verify these behaviors. No review comments were provided, and there is no feedback to address.

@mattkae mattkae left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One question!

Comment thread engine/src/flutter/shell/platform/windows/host_window.h Outdated
Comment thread engine/src/flutter/shell/platform/windows/flutter_window.cc Outdated
mattkae
mattkae previously approved these changes Jun 15, 2026

@mattkae mattkae left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice fix! This makes sense to me. @loic-sharma do you care to chime in for a Googler review?

@mattkae
mattkae requested a review from loic-sharma June 15, 2026 17:39
Comment thread engine/src/flutter/shell/platform/windows/flutter_windows_engine.cc Outdated
@github-actions github-actions Bot added the framework flutter/packages/flutter repository. See also f: labels. label Jun 16, 2026
@9AZX
9AZX requested a review from loic-sharma June 16, 2026 14:15
Comment thread packages/flutter/lib/src/widgets/view.dart Outdated
Comment thread packages/flutter/lib/src/widgets/view.dart
@9AZX
9AZX requested a review from loic-sharma June 17, 2026 16:10
@github-actions github-actions Bot added the f: focus Focus traversal, gaining or losing focus label Jun 17, 2026
@9AZX
9AZX force-pushed the fix_window_focus_win32 branch from e6fe62b to 6f4d065 Compare June 22, 2026 15:41
@mattkae
mattkae requested a review from knopp June 22, 2026 18:00
Comment thread packages/flutter/test/widgets/focus_manager_test.dart Outdated
Comment thread packages/flutter/lib/src/widgets/focus_manager.dart Outdated
@loic-sharma

Copy link
Copy Markdown
Member

This looks good to me, but I'd like to get reviews from other folks as well.

@9AZX Could you update this PR's description to describe the latest implementation? This will make it easier to understand these changes when in the future when we're looking back at the history :)

Comment thread packages/flutter/lib/src/widgets/focus_traversal.dart
@loic-sharma
loic-sharma requested a review from justinmc June 24, 2026 00:00
@9AZX
9AZX force-pushed the fix_window_focus_win32 branch from 0c3d207 to 10686a9 Compare June 24, 2026 08:25
@9AZX
9AZX requested a review from loic-sharma June 24, 2026 20:29
loic-sharma
loic-sharma previously approved these changes Jun 24, 2026

@loic-sharma loic-sharma left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks good to me, excellent work!

@loic-sharma loic-sharma added the CICD Run CI/CD label Jun 26, 2026
mattkae
mattkae previously approved these changes Jun 29, 2026

@mattkae mattkae left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks great to me! This should meaningfully solve some other focus problems that we have. @knopp may also want to take a look at this, but this looks correct to me.

// an independent focus root: nesting would otherwise make an ancestor
// view report hasFocus when a descendant view is focused, causing it
// to request native focus and pull its window forward.
parentNode: FocusManager.instance.rootScope,

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am not sure how I feel about this. In some cases, with independent windows, you want to have separate focus tree. But in other cases, like popovers you don't. I'm wondering if the root of the issue is that the view is anchored at the wrong place in widget hierarchy. I think the WindowManager hosted by the application right now is not a good solution - it causes other issues as well, i.e. widget inspector not working in other than the first window.

I think we might want to take a step back and figure out if it wouldn't be better to have an Material/WidgetsApp per window, which would also take care of the focus issue.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree that this may be a sign that the whole widget tree structure has a problem, not just the focus tree.

Maybe instead of a WidgetsApp per window we could split out the window-specific stuff into a new widget (WidgetsWindow...?). There is a bunch of stuff inside of WidgetsApp that probably makes sense to be defined at the app-level (keyboard shortcuts, localizations, state restoration, etc.) while focus makes more sense at the widgets-level. I haven't looked into the details of whether this is possible but just an idea.

@knopp knopp Jul 1, 2026

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have a WIP PR for moving window manager outside of WidgetsApp. Yes, ideally we might want to share some things for all windows, but I think in the meanwhile having shortcuts etc defined per window is less of a problem that things weirdly failing (like focus issue here or turning on widget inspector literally breaking updating of secondary windows, or even mere the existence of "secondary" windows...)

auto-merge was automatically disabled July 2, 2026 07:33

Head branch was pushed to by a user without write access

@9AZX
9AZX force-pushed the fix_window_focus_win32 branch from b9af7c8 to b34f7ea Compare July 2, 2026 07:33
@flutter-dashboard flutter-dashboard Bot removed the CICD Run CI/CD label Jul 2, 2026
@knopp knopp added the CICD Run CI/CD label Jul 2, 2026
@knopp
knopp enabled auto-merge July 2, 2026 12:00
@knopp
knopp added this pull request to the merge queue Jul 2, 2026
Merged via the queue into flutter:master with commit 48d9105 Jul 2, 2026
209 checks passed
This was referenced Jul 3, 2026
auto-submit Bot pushed a commit to flutter/packages that referenced this pull request Jul 8, 2026
…12135)

Manual roll Flutter from ca9f874f5284 to 6995038d96ef (44 revisions)

Manual roll requested by [email protected]

flutter/flutter@ca9f874...6995038

2026-07-03 [email protected] Roll Fuchsia Linux SDK from sx_eN0J_f2BV6jqjW... to Jr08vyeibMSv3Oxst... (flutter/flutter#188946)
2026-07-03 [email protected] Roll Packages from 420e135 to 2fbe873 (1 revision) (flutter/flutter#188945)
2026-07-03 [email protected] Roll Dart SDK from 786212a2ce0d to 1f9a08ce0638 (4 revisions) (flutter/flutter#188944)
2026-07-03 [email protected] Roll Skia from 5358ab75b840 to 919956953af6 (7 revisions) (flutter/flutter#188943)
2026-07-02 [email protected] [Windows] Keep regular windows in place when another is activated (flutter/flutter#188016)
2026-07-02 [email protected] [flutter_tools] Provide guided message when iOS/macOS build fails due to low minimum version (flutter/flutter#188812)
2026-07-02 [email protected] [Flutter GPU] Load a ShaderLibrary from shader bundle bytes (flutter/flutter#188596)
2026-07-02 [email protected] [tool] Enable record_use experiment by default on all channels (flutter/flutter#188887)
2026-07-02 [email protected] Roll Packages from e742106 to 420e135 (13 revisions) (flutter/flutter#188916)
2026-07-02 [email protected] Roll Fuchsia Linux SDK from I2h2eXk06RrA3pIG2... to sx_eN0J_f2BV6jqjW... (flutter/flutter#188915)
2026-07-02 [email protected] Clarify layout callback debug flag docs (flutter/flutter#186879)
2026-07-02 [email protected] Roll Skia from 0c4faca350cc to 5358ab75b840 (2 revisions) (flutter/flutter#188899)
2026-07-02 [email protected] Roll Dart SDK from e47361c7fe9a to 786212a2ce0d (2 revisions) (flutter/flutter#188898)
2026-07-01 [email protected] Roll Skia from 0fc8ba72e802 to 0c4faca350cc (2 revisions) (flutter/flutter#188886)
2026-07-01 [email protected] Hide draft PRs from the triage list (flutter/flutter#188885)
2026-07-01 [email protected] Stricten isSemantics and matchesSemantics children mismatch check (flutter/flutter#188827)
2026-07-01 [email protected] [AGP 9] Support Enabling Built-in Kotlin (flutter/flutter#188543)
2026-07-01 [email protected] Roll Skia from d19e557ac317 to 0fc8ba72e802 (4 revisions) (flutter/flutter#188879)
2026-07-01 [email protected] [ci] Increase test timeout for Mac_x64 build_tests shards (flutter/flutter#188804)
2026-07-01 [email protected] Roll Skia from bd4ae38ca3bb to d19e557ac317 (1 revision) (flutter/flutter#188865)
2026-07-01 [email protected] Roll Dart SDK from 26d723eb89af to e47361c7fe9a (5 revisions) (flutter/flutter#188864)
2026-07-01 [email protected] Update triage links for material_ui and cupertino_ui --> Design triage (flutter/flutter#188567)
2026-07-01 [email protected] [Impeller] Share a single ContextGLES among all PlaygroundImplGLES (flutter/flutter#188080)
2026-07-01 [email protected] Use null-aware elements in dev/devicelab/lib/integration_tests.dart (flutter/flutter#187852)
2026-07-01 [email protected] Roll Packages from 274ed3e to e742106 (18 revisions) (flutter/flutter#188863)
2026-07-01 [email protected] Add android 17 to embedding (flutter/flutter#187965)
2026-07-01 [email protected] Adds semantics role check to isSemantics and matchesSemantics (flutter/flutter#188825)
2026-07-01 [email protected] Roll Dart SDK from e1bdb9ce3327 to 26d723eb89af (3 revisions) (flutter/flutter#188795)
2026-07-01 [email protected] [web] Apply autocapitalize to text editing elements (flutter/flutter#188351)
2026-07-01 [email protected] Roll Fuchsia Linux SDK from RymJjIj7dd5vQ3Cnh... to I2h2eXk06RrA3pIG2... (flutter/flutter#188852)
2026-07-01 [email protected] [Impeller] Compute dispatch takes 3D workgroup counts and honors the shader local_size (flutter/flutter#188601)
2026-07-01 [email protected] Improve stylus support on linux (flutter/flutter#186831)
2026-07-01 [email protected] Resolve issue  Catch am start failures with 'Error type' and prevent hang (flutter/flutter#187196)
2026-07-01 [email protected] Roll Skia from ef178c9898af to bd4ae38ca3bb (3 revisions) (flutter/flutter#188834)
2026-07-01 [email protected] Roll Skia from 3ac99be47229 to ef178c9898af (3 revisions) (flutter/flutter#188831)
2026-07-01 [email protected] Add a macosArm64Only feature flag (flutter/flutter#188598)
2026-07-01 [email protected] Roll Skia from 15302f1625b2 to 3ac99be47229 (1 revision) (flutter/flutter#188819)
2026-07-01 [email protected] [flutter_tools] Track asset transformer dependencies for hot reload (Reland #187947) (flutter/flutter#188808)
2026-06-30 [email protected] Add TapRegion samples (flutter/flutter#188685)
2026-06-30 [email protected] Print a warning in `flutter doctor` when running on Intel Macs (flutter/flutter#188760)
2026-06-30 [email protected] [framework] Keep scrollable semantics role stable (flutter/flutter#187963)
2026-06-30 [email protected] feat(skills): Add shepherd-prs skill for managing approved external contributor PRs (flutter/flutter#188534)
2026-06-30 [email protected] Roll Skia from 71947c4110b0 to 15302f1625b2 (17 revisions) (flutter/flutter#188815)
2026-06-30 [email protected] [Tool] Run re-entrant upgrade in original CWD (flutter/flutter#188794)
...
kalyujniy pushed a commit to brickit-app/camera that referenced this pull request Jul 8, 2026
…lutter#12135)

Manual roll Flutter from ca9f874f5284 to 6995038d96ef (44 revisions)

Manual roll requested by [email protected]

flutter/flutter@ca9f874...6995038

2026-07-03 [email protected] Roll Fuchsia Linux SDK from sx_eN0J_f2BV6jqjW... to Jr08vyeibMSv3Oxst... (flutter/flutter#188946)
2026-07-03 [email protected] Roll Packages from 420e135 to 2fbe873 (1 revision) (flutter/flutter#188945)
2026-07-03 [email protected] Roll Dart SDK from 786212a2ce0d to 1f9a08ce0638 (4 revisions) (flutter/flutter#188944)
2026-07-03 [email protected] Roll Skia from 5358ab75b840 to 919956953af6 (7 revisions) (flutter/flutter#188943)
2026-07-02 [email protected] [Windows] Keep regular windows in place when another is activated (flutter/flutter#188016)
2026-07-02 [email protected] [flutter_tools] Provide guided message when iOS/macOS build fails due to low minimum version (flutter/flutter#188812)
2026-07-02 [email protected] [Flutter GPU] Load a ShaderLibrary from shader bundle bytes (flutter/flutter#188596)
2026-07-02 [email protected] [tool] Enable record_use experiment by default on all channels (flutter/flutter#188887)
2026-07-02 [email protected] Roll Packages from e742106 to 420e135 (13 revisions) (flutter/flutter#188916)
2026-07-02 [email protected] Roll Fuchsia Linux SDK from I2h2eXk06RrA3pIG2... to sx_eN0J_f2BV6jqjW... (flutter/flutter#188915)
2026-07-02 [email protected] Clarify layout callback debug flag docs (flutter/flutter#186879)
2026-07-02 [email protected] Roll Skia from 0c4faca350cc to 5358ab75b840 (2 revisions) (flutter/flutter#188899)
2026-07-02 [email protected] Roll Dart SDK from e47361c7fe9a to 786212a2ce0d (2 revisions) (flutter/flutter#188898)
2026-07-01 [email protected] Roll Skia from 0fc8ba72e802 to 0c4faca350cc (2 revisions) (flutter/flutter#188886)
2026-07-01 [email protected] Hide draft PRs from the triage list (flutter/flutter#188885)
2026-07-01 [email protected] Stricten isSemantics and matchesSemantics children mismatch check (flutter/flutter#188827)
2026-07-01 [email protected] [AGP 9] Support Enabling Built-in Kotlin (flutter/flutter#188543)
2026-07-01 [email protected] Roll Skia from d19e557ac317 to 0fc8ba72e802 (4 revisions) (flutter/flutter#188879)
2026-07-01 [email protected] [ci] Increase test timeout for Mac_x64 build_tests shards (flutter/flutter#188804)
2026-07-01 [email protected] Roll Skia from bd4ae38ca3bb to d19e557ac317 (1 revision) (flutter/flutter#188865)
2026-07-01 [email protected] Roll Dart SDK from 26d723eb89af to e47361c7fe9a (5 revisions) (flutter/flutter#188864)
2026-07-01 [email protected] Update triage links for material_ui and cupertino_ui --> Design triage (flutter/flutter#188567)
2026-07-01 [email protected] [Impeller] Share a single ContextGLES among all PlaygroundImplGLES (flutter/flutter#188080)
2026-07-01 [email protected] Use null-aware elements in dev/devicelab/lib/integration_tests.dart (flutter/flutter#187852)
2026-07-01 [email protected] Roll Packages from 274ed3e to e742106 (18 revisions) (flutter/flutter#188863)
2026-07-01 [email protected] Add android 17 to embedding (flutter/flutter#187965)
2026-07-01 [email protected] Adds semantics role check to isSemantics and matchesSemantics (flutter/flutter#188825)
2026-07-01 [email protected] Roll Dart SDK from e1bdb9ce3327 to 26d723eb89af (3 revisions) (flutter/flutter#188795)
2026-07-01 [email protected] [web] Apply autocapitalize to text editing elements (flutter/flutter#188351)
2026-07-01 [email protected] Roll Fuchsia Linux SDK from RymJjIj7dd5vQ3Cnh... to I2h2eXk06RrA3pIG2... (flutter/flutter#188852)
2026-07-01 [email protected] [Impeller] Compute dispatch takes 3D workgroup counts and honors the shader local_size (flutter/flutter#188601)
2026-07-01 [email protected] Improve stylus support on linux (flutter/flutter#186831)
2026-07-01 [email protected] Resolve issue  Catch am start failures with 'Error type' and prevent hang (flutter/flutter#187196)
2026-07-01 [email protected] Roll Skia from ef178c9898af to bd4ae38ca3bb (3 revisions) (flutter/flutter#188834)
2026-07-01 [email protected] Roll Skia from 3ac99be47229 to ef178c9898af (3 revisions) (flutter/flutter#188831)
2026-07-01 [email protected] Add a macosArm64Only feature flag (flutter/flutter#188598)
2026-07-01 [email protected] Roll Skia from 15302f1625b2 to 3ac99be47229 (1 revision) (flutter/flutter#188819)
2026-07-01 [email protected] [flutter_tools] Track asset transformer dependencies for hot reload (Reland #187947) (flutter/flutter#188808)
2026-06-30 [email protected] Add TapRegion samples (flutter/flutter#188685)
2026-06-30 [email protected] Print a warning in `flutter doctor` when running on Intel Macs (flutter/flutter#188760)
2026-06-30 [email protected] [framework] Keep scrollable semantics role stable (flutter/flutter#187963)
2026-06-30 [email protected] feat(skills): Add shepherd-prs skill for managing approved external contributor PRs (flutter/flutter#188534)
2026-06-30 [email protected] Roll Skia from 71947c4110b0 to 15302f1625b2 (17 revisions) (flutter/flutter#188815)
2026-06-30 [email protected] [Tool] Run re-entrant upgrade in original CWD (flutter/flutter#188794)
...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

a: desktop Running on desktop CICD Run CI/CD engine flutter/engine related. See also e: labels. f: focus Focus traversal, gaining or losing focus framework flutter/packages/flutter repository. See also f: labels. platform-windows Building on or for Windows specifically team-windows Owned by the Windows platform team

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Windows][windowing] Activating a child RegularWindow from the taskbar briefly focuses it, then the main window is brought to front

5 participants