Skip to content

Commit

Permalink
Handle Edge Browser Hover
Browse files Browse the repository at this point in the history
This contribution captures the hover over edge browser and sends a mouse
move event to its listener.

contributes to eclipse-platform#1540
  • Loading branch information
amartya4256 authored and HeikoKlare committed Oct 28, 2024
1 parent 435fe7e commit 1d397ed
Showing 1 changed file with 58 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,9 @@ public WebViewEnvironment(ICoreWebView2Environment environment) {
private boolean ignoreFocus;
private String lastCustomText;

private CursorPosition cursorPosition = CursorPosition.OUTSIDE;
private Point previousCursorLocation;

static {
NativeClearSessions = () -> {
ICoreWebView2CookieManager manager = getCookieManager();
Expand Down Expand Up @@ -654,6 +657,7 @@ void setupBrowser(int hr, long pv) {
browser.addListener(SWT.FocusIn, this::browserFocusIn);
browser.addListener(SWT.Resize, this::browserResize);
browser.addListener(SWT.Move, this::browserMove);
handleBrowserHover();

containingEnvironment.instances().add(this);
// Sometimes when the shell of the browser is opened before the browser is
Expand Down Expand Up @@ -709,6 +713,60 @@ void browserResize(Event event) {
controller.put_IsVisible(true);
}

private void handleBrowserHover() {
browser.getDisplay().timerExec(100, () -> {
if (browser.isDisposed()) {
return;
}
if (!browser.isVisible() || !hasDisplayFocus()) {
handleBrowserHover();
return;
}

final Point cursorLocation = browser.getDisplay().getCursorLocation();
Point cursorLocationInControlCoordinate = browser.toControl(cursorLocation);
boolean isCursorInsideBrowser = browser.getBounds().contains(cursorLocationInControlCoordinate);
boolean isCursorLocationChanged = !cursorLocation.equals(previousCursorLocation);
previousCursorLocation = cursorLocation;

switch (cursorPosition) {
case INSIDE:
if (isCursorInsideBrowser && isCursorLocationChanged) {
sendMouseEvent(cursorLocationInControlCoordinate, SWT.MouseMove);
} else if (!isCursorInsideBrowser) {
cursorPosition = CursorPosition.OUTSIDE;
sendMouseEvent(cursorLocationInControlCoordinate, SWT.MouseExit);
}
break;
case OUTSIDE:
if (isCursorInsideBrowser) {
cursorPosition = CursorPosition.INSIDE;
sendMouseEvent(cursorLocationInControlCoordinate, SWT.MouseEnter);
}
break;
}
handleBrowserHover();
});
}

private enum CursorPosition {
INSIDE, OUTSIDE
}

private void sendMouseEvent(Point cursorLocationInControlCoordinate, int mouseEvent) {
Event newEvent = new Event();
newEvent.widget = browser;
Point position = cursorLocationInControlCoordinate;
newEvent.x = position.x;
newEvent.y = position.y;
newEvent.type = mouseEvent;
browser.notifyListeners(newEvent.type, newEvent);
}

private boolean hasDisplayFocus() {
return browser.getDisplay().getFocusControl() != null;
}

@Override
public Object evaluate(String script) throws SWTException {
// Feature in WebView2. ExecuteScript works regardless of IsScriptEnabled setting.
Expand Down

0 comments on commit 1d397ed

Please sign in to comment.