@@ -77,6 +77,9 @@ public WebViewEnvironment(ICoreWebView2Environment environment) {
7777 private boolean ignoreFocus ;
7878 private String lastCustomText ;
7979
80+ private CursorPosition cursorPosition = CursorPosition .OUTSIDE ;
81+ private Point previousCursorLocation ;
82+
8083 static {
8184 NativeClearSessions = () -> {
8285 ICoreWebView2CookieManager manager = getCookieManager ();
@@ -645,6 +648,7 @@ void setupBrowser(int hr, long pv) {
645648 browser .addListener (SWT .FocusIn , this ::browserFocusIn );
646649 browser .addListener (SWT .Resize , this ::browserResize );
647650 browser .addListener (SWT .Move , this ::browserMove );
651+ handleBrowserHover ();
648652
649653 containingEnvironment .instances ().add (this );
650654 // Sometimes when the shell of the browser is opened before the browser is
@@ -700,6 +704,60 @@ void browserResize(Event event) {
700704 controller .put_IsVisible (true );
701705}
702706
707+ private void handleBrowserHover () {
708+ browser .getDisplay ().timerExec (100 , () -> {
709+ if (browser .isDisposed ()) {
710+ return ;
711+ }
712+ if (!browser .isVisible () || !hasDisplayFocus ()) {
713+ handleBrowserHover ();
714+ return ;
715+ }
716+
717+ final Point cursorLocation = browser .getDisplay ().getCursorLocation ();
718+ Point cursorLocationInControlCoordinate = browser .toControl (cursorLocation );
719+ boolean isCursorInsideBrowser = browser .getBounds ().contains (cursorLocationInControlCoordinate );
720+ boolean isCursorLocationChanged = !cursorLocation .equals (previousCursorLocation );
721+ previousCursorLocation = cursorLocation ;
722+
723+ switch (cursorPosition ) {
724+ case INSIDE :
725+ if (isCursorInsideBrowser && isCursorLocationChanged ) {
726+ sendMouseEvent (cursorLocationInControlCoordinate , SWT .MouseMove );
727+ } else if (!isCursorInsideBrowser ) {
728+ cursorPosition = CursorPosition .OUTSIDE ;
729+ sendMouseEvent (cursorLocationInControlCoordinate , SWT .MouseExit );
730+ }
731+ break ;
732+ case OUTSIDE :
733+ if (isCursorInsideBrowser ) {
734+ cursorPosition = CursorPosition .INSIDE ;
735+ sendMouseEvent (cursorLocationInControlCoordinate , SWT .MouseEnter );
736+ }
737+ break ;
738+ }
739+ handleBrowserHover ();
740+ });
741+ }
742+
743+ private enum CursorPosition {
744+ INSIDE , OUTSIDE
745+ }
746+
747+ private void sendMouseEvent (Point cursorLocationInControlCoordinate , int mouseEvent ) {
748+ Event newEvent = new Event ();
749+ newEvent .widget = browser ;
750+ Point position = cursorLocationInControlCoordinate ;
751+ newEvent .x = position .x ;
752+ newEvent .y = position .y ;
753+ newEvent .type = mouseEvent ;
754+ browser .notifyListeners (newEvent .type , newEvent );
755+ }
756+
757+ private boolean hasDisplayFocus () {
758+ return browser .getDisplay ().getFocusControl () != null ;
759+ }
760+
703761@ Override
704762public Object evaluate (String script ) throws SWTException {
705763 // Feature in WebView2. ExecuteScript works regardless of IsScriptEnabled setting.
0 commit comments