@@ -81,7 +81,8 @@ public WebViewEnvironment(ICoreWebView2Environment environment) {
81
81
static boolean inCallback ;
82
82
boolean inNewWindow ;
83
83
HashMap <Long , LocationEvent > navigations = new HashMap <>();
84
- private boolean ignoreFocus ;
84
+ private boolean ignoreGotFocus ;
85
+ private boolean ignoreFocusIn ;
85
86
private String lastCustomText ;
86
87
87
88
private static record CursorPosition (Point location , boolean isInsideBrowser ) {};
@@ -829,8 +830,17 @@ void browserDispose(Event event) {
829
830
}
830
831
831
832
void browserFocusIn (Event event ) {
832
- if (ignoreFocus ) return ;
833
+ if (ignoreFocusIn ) return ;
833
834
// 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
+
834
844
controller .MoveFocus (COM .COREWEBVIEW2_MOVE_FOCUS_REASON_PROGRAMMATIC );
835
845
}
836
846
@@ -1350,16 +1360,21 @@ int handleNewWindowRequested(long pView, long pArgs) {
1350
1360
}
1351
1361
1352
1362
int handleGotFocus (long pView , long pArg ) {
1363
+ if (ignoreGotFocus ) {
1364
+ ignoreGotFocus = false ;
1365
+ return COM .S_OK ;
1366
+ }
1367
+ // https://github.com/eclipse-platform/eclipse.platform.swt/issues/1139
1353
1368
// browser.forceFocus() does not result in
1354
1369
// Shell#setActiveControl(Control)
1355
1370
// being called and therefore no SWT.FocusIn event being dispatched,
1356
1371
// causing active part, etc. not to be updated correctly.
1357
1372
// The solution is to explicitly send a WM_SETFOCUS
1358
1373
// to the browser, and, while doing so, ignoring any recursive
1359
1374
// calls in #browserFocusIn(Event).
1360
- ignoreFocus = true ;
1375
+ ignoreFocusIn = true ;
1361
1376
OS .SendMessage (browser .handle , OS .WM_SETFOCUS , 0 , 0 );
1362
- ignoreFocus = false ;
1377
+ ignoreFocusIn = false ;
1363
1378
return COM .S_OK ;
1364
1379
}
1365
1380
0 commit comments