@@ -63,6 +63,7 @@ public WebViewEnvironment(ICoreWebView2Environment environment) {
6363 static boolean inCallback ;
6464 boolean inNewWindow ;
6565 HashMap <Long , LocationEvent > navigations = new HashMap <>();
66+ private boolean ignoreFocus ;
6667
6768 static {
6869 NativeClearSessions = () -> {
@@ -480,6 +481,7 @@ void browserDispose(Event event) {
480481}
481482
482483void browserFocusIn (Event event ) {
484+ if (ignoreFocus ) return ;
483485 // TODO: directional traversals
484486 controller .MoveFocus (COM .COREWEBVIEW2_MOVE_FOCUS_REASON_PROGRAMMATIC );
485487}
@@ -793,7 +795,16 @@ int handleNewWindowRequested(long pView, long pArgs) {
793795}
794796
795797int handleGotFocus (long pView , long pArg ) {
796- this .browser .forceFocus ();
798+ // browser.forceFocus() does not result in
799+ // Shell#setActiveControl(Control)
800+ // being called and therefore no SWT.FocusIn event being dispatched,
801+ // causing active part, etc. not to be updated correctly.
802+ // The solution is to explicitly send a WM_SETFOCUS
803+ // to the browser, and, while doing so, ignoring any recursive
804+ // calls in #browserFocusIn(Event).
805+ ignoreFocus = true ;
806+ OS .SendMessage (browser .handle , OS .WM_SETFOCUS , 0 , 0 );
807+ ignoreFocus = false ;
797808 return COM .S_OK ;
798809}
799810
0 commit comments