@@ -81,7 +81,8 @@ public WebViewEnvironment(ICoreWebView2Environment environment) {
8181 static boolean inCallback ;
8282 boolean inNewWindow ;
8383 HashMap <Long , LocationEvent > navigations = new HashMap <>();
84- private boolean ignoreFocus ;
84+ private boolean ignoreGotFocus ;
85+ private boolean ignoreFocusIn ;
8586 private String lastCustomText ;
8687
8788 private static record CursorPosition (Point location , boolean isInsideBrowser ) {};
@@ -829,8 +830,17 @@ void browserDispose(Event event) {
829830}
830831
831832void browserFocusIn (Event event ) {
832- if (ignoreFocus ) return ;
833+ if (ignoreFocusIn ) return ;
833834 // TODO: directional traversals
835+
836+ // https://github.com/eclipse-platform/eclipse.platform.swt/issues/1848
837+ // When we call ICoreWebView2Controller.MoveFocus(int) here,
838+ // WebView2 will call us back in handleGotFocus() asynchronously.
839+ // We need to ignore that next event, as in the meantime the user might
840+ // have moved focus to some other control and reacting on that event
841+ // would bring us back to the Browser.
842+ ignoreGotFocus = true ;
843+
834844 controller .MoveFocus (COM .COREWEBVIEW2_MOVE_FOCUS_REASON_PROGRAMMATIC );
835845}
836846
@@ -1350,16 +1360,20 @@ int handleNewWindowRequested(long pView, long pArgs) {
13501360}
13511361
13521362int handleGotFocus (long pView , long pArg ) {
1363+ if (ignoreGotFocus ) {
1364+ ignoreGotFocus = false ;
1365+ return COM .S_OK ;
1366+ }
13531367 // browser.forceFocus() does not result in
13541368 // Shell#setActiveControl(Control)
13551369 // being called and therefore no SWT.FocusIn event being dispatched,
13561370 // causing active part, etc. not to be updated correctly.
13571371 // The solution is to explicitly send a WM_SETFOCUS
13581372 // to the browser, and, while doing so, ignoring any recursive
13591373 // calls in #browserFocusIn(Event).
1360- ignoreFocus = true ;
1374+ ignoreFocusIn = true ;
13611375 OS .SendMessage (browser .handle , OS .WM_SETFOCUS , 0 , 0 );
1362- ignoreFocus = false ;
1376+ ignoreFocusIn = false ;
13631377 return COM .S_OK ;
13641378}
13651379
0 commit comments