Skip to content

Commit

Permalink
Edge: Ignore gotFocus if we were the one who set focus to WebView2
Browse files Browse the repository at this point in the history
Fixes #1848.
  • Loading branch information
sratz committed Feb 20, 2025
1 parent b85d9d2 commit 1b70210
Showing 1 changed file with 18 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,8 @@ public WebViewEnvironment(ICoreWebView2Environment environment) {
static boolean inCallback;
boolean inNewWindow;
HashMap<Long, LocationEvent> navigations = new HashMap<>();
private boolean ignoreFocus;
private boolean ignoreGotFocus;
private boolean ignoreFocusIn;
private String lastCustomText;

private static record CursorPosition(Point location, boolean isInsideBrowser) {};
Expand Down Expand Up @@ -829,8 +830,17 @@ void browserDispose(Event event) {
}

void browserFocusIn(Event event) {
if (ignoreFocus) return;
if (ignoreFocusIn) return;
// TODO: directional traversals

// https://github.com/eclipse-platform/eclipse.platform.swt/issues/1848
// When we call ICoreWebView2Controller.MoveFocus(int) here,
// WebView2 will call us back in handleGotFocus() asynchronously.
// We need to ignore that next event, as in the meantime the user might
// have moved focus to some other control and reacting on that event
// would bring us back to the Browser.
ignoreGotFocus = true;

controller.MoveFocus(COM.COREWEBVIEW2_MOVE_FOCUS_REASON_PROGRAMMATIC);
}

Expand Down Expand Up @@ -1350,16 +1360,20 @@ int handleNewWindowRequested(long pView, long pArgs) {
}

int handleGotFocus(long pView, long pArg) {
if (ignoreGotFocus) {
ignoreGotFocus = false;
return COM.S_OK;
}
// browser.forceFocus() does not result in
// Shell#setActiveControl(Control)
// being called and therefore no SWT.FocusIn event being dispatched,
// causing active part, etc. not to be updated correctly.
// The solution is to explicitly send a WM_SETFOCUS
// to the browser, and, while doing so, ignoring any recursive
// calls in #browserFocusIn(Event).
ignoreFocus = true;
ignoreFocusIn = true;
OS.SendMessage (browser.handle, OS.WM_SETFOCUS, 0, 0);
ignoreFocus = false;
ignoreFocusIn = false;
return COM.S_OK;
}

Expand Down

0 comments on commit 1b70210

Please sign in to comment.