@@ -78,6 +78,9 @@ public WebViewEnvironment(ICoreWebView2Environment environment) {
7878 private boolean ignoreFocus ;
7979 private String lastCustomText ;
8080
81+ private CursorPosition cursorPosition = CursorPosition .OUTSIDE ;
82+ private Point previousCursorLocation ;
83+
8184 static {
8285 NativeClearSessions = () -> {
8386 ICoreWebView2CookieManager manager = getCookieManager ();
@@ -654,6 +657,7 @@ void setupBrowser(int hr, long pv) {
654657 browser .addListener (SWT .FocusIn , this ::browserFocusIn );
655658 browser .addListener (SWT .Resize , this ::browserResize );
656659 browser .addListener (SWT .Move , this ::browserMove );
660+ handleBrowserHover ();
657661
658662 containingEnvironment .instances ().add (this );
659663 // Sometimes when the shell of the browser is opened before the browser is
@@ -709,6 +713,60 @@ void browserResize(Event event) {
709713 controller .put_IsVisible (true );
710714}
711715
716+ private void handleBrowserHover () {
717+ browser .getDisplay ().timerExec (100 , () -> {
718+ if (browser .isDisposed ()) {
719+ return ;
720+ }
721+ if (!browser .isVisible () || !hasDisplayFocus ()) {
722+ handleBrowserHover ();
723+ return ;
724+ }
725+
726+ final Point cursorLocation = browser .getDisplay ().getCursorLocation ();
727+ Point cursorLocationInControlCoordinate = browser .toControl (cursorLocation );
728+ boolean isCursorInsideBrowser = browser .getBounds ().contains (cursorLocationInControlCoordinate );
729+ boolean isCursorLocationChanged = !cursorLocation .equals (previousCursorLocation );
730+ previousCursorLocation = cursorLocation ;
731+
732+ switch (cursorPosition ) {
733+ case INSIDE :
734+ if (isCursorInsideBrowser && isCursorLocationChanged ) {
735+ sendMouseEvent (cursorLocationInControlCoordinate , SWT .MouseMove );
736+ } else if (!isCursorInsideBrowser ) {
737+ cursorPosition = CursorPosition .OUTSIDE ;
738+ sendMouseEvent (cursorLocationInControlCoordinate , SWT .MouseExit );
739+ }
740+ break ;
741+ case OUTSIDE :
742+ if (isCursorInsideBrowser ) {
743+ cursorPosition = CursorPosition .INSIDE ;
744+ sendMouseEvent (cursorLocationInControlCoordinate , SWT .MouseEnter );
745+ }
746+ break ;
747+ }
748+ handleBrowserHover ();
749+ });
750+ }
751+
752+ private enum CursorPosition {
753+ INSIDE , OUTSIDE
754+ }
755+
756+ private void sendMouseEvent (Point cursorLocationInControlCoordinate , int mouseEvent ) {
757+ Event newEvent = new Event ();
758+ newEvent .widget = browser ;
759+ Point position = cursorLocationInControlCoordinate ;
760+ newEvent .x = position .x ;
761+ newEvent .y = position .y ;
762+ newEvent .type = mouseEvent ;
763+ browser .notifyListeners (newEvent .type , newEvent );
764+ }
765+
766+ private boolean hasDisplayFocus () {
767+ return browser .getDisplay ().getFocusControl () != null ;
768+ }
769+
712770@ Override
713771public Object evaluate (String script ) throws SWTException {
714772 // Feature in WebView2. ExecuteScript works regardless of IsScriptEnabled setting.
0 commit comments