Skip to content

Commit 7d86f5c

Browse files
committed
Edge: Ignore gotFocus if we were the one who set focus to WebView2
Fixes #1848.
1 parent f393ad8 commit 7d86f5c

File tree

1 file changed

+19
-4
lines changed
  • bundles/org.eclipse.swt/Eclipse SWT Browser/win32/org/eclipse/swt/browser

1 file changed

+19
-4
lines changed

bundles/org.eclipse.swt/Eclipse SWT Browser/win32/org/eclipse/swt/browser/Edge.java

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -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

831832
void 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,21 @@ int handleNewWindowRequested(long pView, long pArgs) {
13501360
}
13511361

13521362
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
13531368
// browser.forceFocus() does not result in
13541369
// Shell#setActiveControl(Control)
13551370
// being called and therefore no SWT.FocusIn event being dispatched,
13561371
// causing active part, etc. not to be updated correctly.
13571372
// The solution is to explicitly send a WM_SETFOCUS
13581373
// to the browser, and, while doing so, ignoring any recursive
13591374
// calls in #browserFocusIn(Event).
1360-
ignoreFocus = true;
1375+
ignoreFocusIn = true;
13611376
OS.SendMessage (browser.handle, OS.WM_SETFOCUS, 0, 0);
1362-
ignoreFocus = false;
1377+
ignoreFocusIn = false;
13631378
return COM.S_OK;
13641379
}
13651380

0 commit comments

Comments
 (0)