@@ -641,6 +641,76 @@ void setupBrowser(int hr, long pv) {
641641 webView .AddHostObjectToScript ("swt\0 " .toCharArray (), hostObj );
642642 hostDisp .Release ();
643643
644+ class MouseNavigationFunction extends BrowserFunction {
645+ public MouseNavigationFunction (Browser browser , String name ) {
646+ super (browser , name );
647+ }
648+ @ Override
649+ public Object function (Object [] arguments ) {
650+ System .out .println (Arrays .toString (arguments ));
651+ Event newEvent = new Event ();
652+ newEvent .widget = browser ;
653+ String eventType = (String ) arguments [0 ];
654+ int x = (int ) Math .round ((Double ) arguments [1 ]);
655+ int y = (int ) Math .round ((Double ) arguments [2 ]);
656+ int button = (int ) Math .round ((Double ) arguments [3 ]);
657+
658+ if (OS .GetKeyState (OS .VK_MENU ) < 0 ) newEvent .stateMask |= SWT .ALT ;
659+ if (OS .GetKeyState (OS .VK_SHIFT ) < 0 ) newEvent .stateMask |= SWT .SHIFT ;
660+ if (OS .GetKeyState (OS .VK_CONTROL ) < 0 ) newEvent .stateMask |= SWT .CONTROL ;
661+
662+ x = DPIUtil .scaleUp (x , DPIUtil .getNativeDeviceZoom ());
663+ y = DPIUtil .scaleUp (y , DPIUtil .getNativeDeviceZoom ());
664+ x = DPIUtil .scaleDown (x , DPIUtil .getZoomForAutoscaleProperty (browser .getShell ().nativeZoom )); //
665+ y = DPIUtil .scaleDown (y , DPIUtil .getZoomForAutoscaleProperty (browser .getShell ().nativeZoom ));
666+ newEvent .x = x ;
667+ newEvent .y = y ;
668+
669+ switch (button ) {
670+ case 0 : button = 1 ; break ;
671+ case 1 : button = 3 ; break ;
672+ case 2 : button = 2 ;
673+ break ;
674+ }
675+ if (eventType .equals ("mousedown" )) {
676+ newEvent .type = SWT .MouseDown ;
677+ newEvent .button = button ;
678+ newEvent .count = 1 ;
679+ } else if (eventType .equals ("mouseup" )/* || eventType.equals(EVENT_DRAGEND)*/ ) {
680+ newEvent .type = SWT .MouseUp ;
681+ newEvent .button = button != 0 ? button : 1 ; /* button assumed to be 1 for dragends */
682+ newEvent .count = 1 ;
683+ switch (newEvent .button ) {
684+ case 1 : newEvent .stateMask |= SWT .BUTTON1 ; break ;
685+ case 2 : newEvent .stateMask |= SWT .BUTTON2 ; break ;
686+ case 3 : newEvent .stateMask |= SWT .BUTTON3 ; break ;
687+ case 4 : newEvent .stateMask |= SWT .BUTTON4 ; break ;
688+ case 5 : newEvent .stateMask |= SWT .BUTTON5 ; break ;
689+ }
690+ /*} else if (eventType.equals("mousewhell")) {
691+ newEvent.type = SWT.MouseWheel;
692+ rgdispid = newEvent.getIDsOfNames(new String[] { PROPERTY_WHEELDELTA });
693+ dispIdMember = rgdispid[0];
694+ pVarResult = newEvent.getProperty(dispIdMember);
695+ newEvent.count = pVarResult.getInt () / 120 * 3;
696+ pVarResult.dispose();*/
697+ } else if (eventType .equals ("mousemove" )) {
698+ newEvent .type = SWT .MouseMove ;
699+ } else if (eventType .equals ("mouseover" )) {
700+ newEvent .type = SWT .MouseEnter ;
701+ } else if (eventType .equals ("mouseout" )) {
702+ newEvent .type = SWT .MouseExit ;
703+ /*} else if (eventType.equals(EVENT_DRAGSTART)) {
704+ newEvent.type = SWT.DragDetect;
705+ newEvent.button = 1; // button assumed to be 1 for dragstarts
706+ newEvent.stateMask |= SWT.BUTTON1;*/
707+ }
708+ browser .notifyListeners (newEvent .type , newEvent );
709+ return null ;
710+ }
711+ }
712+ new MouseNavigationFunction (this .browser , "swtHandleMouse" );
713+
644714 browser .addListener (SWT .Dispose , this ::browserDispose );
645715 browser .addListener (SWT .FocusIn , this ::browserFocusIn );
646716 browser .addListener (SWT .Resize , this ::browserResize );
@@ -840,14 +910,6 @@ int handleNavigationStarting(long pView, long pArgs, boolean top) {
840910 if (event .doit ) {
841911 jsEnabled = jsEnabledOnNextPage ;
842912 settings .put_IsScriptEnabled (jsEnabled );
843- // Register browser functions in the new document.
844- if (!functions .isEmpty ()) {
845- StringBuilder sb = new StringBuilder ();
846- for (BrowserFunction function : functions .values ()) {
847- sb .append (function .functionString );
848- }
849- execute (sb .toString ());
850- }
851913 } else {
852914 args .put_Cancel (true );
853915 }
@@ -890,6 +952,7 @@ int handleDOMContentLoaded(long pView, long pArgs) {
890952 if (startEvent != null && startEvent .top ) {
891953 if (lastCustomText != null && getUrl ().equals (ABOUT_BLANK )) {
892954 IUnknown postExecute = newCallback ((long result , long json ) -> {
955+ registerBrowserFunctionsAndEventListenersInDocument ();
893956 sendProgressCompleted ();
894957 return COM .S_OK ;
895958 });
@@ -899,12 +962,31 @@ int handleDOMContentLoaded(long pView, long pArgs) {
899962 postExecute .Release ();
900963 this .lastCustomText = null ;
901964 } else {
965+ registerBrowserFunctionsAndEventListenersInDocument ();
902966 sendProgressCompleted ();
903967 }
904968 }
905969 return COM .S_OK ;
906970}
907971
972+ private void registerBrowserFunctionsAndEventListenersInDocument () {
973+ StringBuilder sb = new StringBuilder ();
974+ // Register browser functions in the new document.
975+ if (!functions .isEmpty ()) {
976+ for (BrowserFunction function : functions .values ()) {
977+ sb .append (function .functionString );
978+ }
979+ execute (sb .toString ());
980+ }
981+ sb .append ("""
982+ "mousedown mouseup mousewheel mouseover mouseout mousemove".split(" ").forEach(function(e){
983+ window.document.addEventListener(e, (event)=>swtHandleMouse(e, event.x, event.y, event.button))
984+ })""" );
985+ IUnknown newCallback = newCallback ((r , j ) -> COM .S_OK );
986+ webViewProvider .getWebView (true ).ExecuteScript (stringToWstr (sb .toString ()), newCallback );
987+ newCallback .Release ();
988+ }
989+
908990private static String escapeForSingleQuotedJSString (String str ) {
909991 return str .replace ("\\ " , "\\ \\ " ) //
910992 .replace ("'" , "\\ '" ) //
0 commit comments