@@ -66,13 +66,13 @@ public Rectangle map(Control from, Control to, int x, int y, int width, int heig
6666 Rectangle mappedRectangleInPoints ;
6767 if (from == null ) {
6868 Rectangle mappedRectangleInPixels = display .mapInPixels (from , to ,
69- translateRectangleInPixelsInDisplayCoordinateSystem (x , y , width , height ,
69+ translateRectangleInPointsToPixels (x , y , width , height ,
7070 to .getShell ().getMonitor ()));
7171 mappedRectangleInPoints = DPIUtil .scaleDown (mappedRectangleInPixels , to .getZoom ());
7272 } else if (to == null ) {
7373 Rectangle mappedRectangleInPixels = display .mapInPixels (from , to ,
7474 DPIUtil .scaleUp (new Rectangle (x , y , width , height ), from .getZoom ()));
75- mappedRectangleInPoints = translateRectangleInPointsInDisplayCoordinateSystem (mappedRectangleInPixels .x ,
75+ mappedRectangleInPoints = translateRectangleInPixelsToPoints (mappedRectangleInPixels .x ,
7676 mappedRectangleInPixels .y , mappedRectangleInPixels .width , mappedRectangleInPixels .height ,
7777 from .getShell ().getMonitor ());
7878 } else {
@@ -93,122 +93,145 @@ public Rectangle mapMonitorBounds(Rectangle rect, int zoom) {
9393
9494 @ Override
9595 public Point translateFromDisplayCoordinates (Point point , int zoom ) {
96- return translateLocationInPixelsFromDisplayCoordinateSystem (point .x , point .y );
96+ return translateLocationInPixelsToPoints (point .x , point .y );
9797 }
9898
9999 @ Override
100100 public Point translateToDisplayCoordinates (Point point , int zoom ) {
101- return translateLocationInPointsToDisplayCoordinateSystem (point .x , point .y );
101+ Monitor monitor = point instanceof MonitorAwarePoint monitorAwarePoint ? monitorAwarePoint .monitor : null ;
102+ return translateLocationInPointsToPixels (point .x , point .y , monitor );
102103 }
103104
104105 @ Override
105106 public Rectangle translateFromDisplayCoordinates (Rectangle rect , int zoom ) {
106- return translateRectangleInPixelsFromDisplayCoordinateSystemByContainment ( rect . x , rect . y , rect . width ,
107- rect .height );
107+ Monitor monitor = rect instanceof MonitorAwareRectangle monitorAwareRect ? monitorAwareRect . monitor : null ;
108+ return translateRectangleInPixelsToPoints ( rect .x , rect . y , rect . width , rect . height , monitor );
108109 }
109110
110111 @ Override
111112 public Rectangle translateToDisplayCoordinates (Rectangle rect , int zoom ) {
112- return translateRectangleInPointsToDisplayCoordinateSystemByContainment ( rect . x , rect . y , rect . width ,
113- rect .height );
113+ Monitor monitor = rect instanceof MonitorAwareRectangle monitorAwareRect ? monitorAwareRect . monitor : null ;
114+ return translateRectangleInPointsToPixels ( rect .x , rect . y , rect . width , rect . height , monitor );
114115 }
115116
116117 @ Override
117118 public Point getCursorLocation () {
118119 Point cursorLocationInPixels = display .getCursorLocationInPixels ();
119- return translateLocationInPixelsFromDisplayCoordinateSystem (cursorLocationInPixels .x , cursorLocationInPixels .y );
120+ return translateLocationInPixelsToPoints (cursorLocationInPixels .x , cursorLocationInPixels .y );
120121 }
121122
122123 @ Override
123- public void setCursorLocation (int x , int y ) {
124- Point cursorLocationInPixels = translateLocationInPointsToDisplayCoordinateSystem (x , y );
125- display .setCursorLocationInPixels (cursorLocationInPixels .x , cursorLocationInPixels .y );
124+ public void setCursorLocation (int x , int y ) {
125+ Point cursorLocationInPixels = translateLocationInPointsToPixels (x , y , null );
126+ display .setCursorLocationInPixels (cursorLocationInPixels .x , cursorLocationInPixels .y );
126127 }
127128
128- private Point translateLocationInPointsToDisplayCoordinateSystem (int x , int y ) {
129- Monitor monitor = getContainingMonitor (x , y );
129+ private Point translateLocationInPointsToPixels (int x , int y , Monitor monitor ) {
130+ monitor = getValidMonitorIfApplicable (x , y , monitor );
130131 return getPixelsFromPoint (monitor , x , y );
131132 }
132133
133- private Point translateLocationInPixelsFromDisplayCoordinateSystem (int x , int y ) {
134- Monitor monitor = getContainingMonitorInPixelsCoordinate (x , y );
134+ private Point translateLocationInPixelsToPoints (int x , int y ) {
135+ Monitor monitor = getContainingMonitorForPixels (x , y );
135136 return getPointFromPixels (monitor , x , y );
136137 }
137138
138- private Rectangle translateRectangleInPointsToDisplayCoordinateSystemByContainment (int x , int y , int width ,
139- int height ) {
140- Monitor monitorByLocation = getContainingMonitor (x , y );
141- Monitor monitorByContainment = getContainingMonitor (x , y , width , height );
142- return translateRectangleInPixelsInDisplayCoordinateSystem (x , y , width , height , monitorByLocation ,
143- monitorByContainment );
144- }
145-
146- private Rectangle translateRectangleInPixelsInDisplayCoordinateSystem (int x , int y , int width , int height ,
147- Monitor monitor ) {
148- return translateRectangleInPixelsInDisplayCoordinateSystem (x , y , width , height , monitor , monitor );
149- }
150-
151- private Rectangle translateRectangleInPixelsInDisplayCoordinateSystem (int x , int y , int width , int height ,
152- Monitor monitorOfLocation , Monitor monitorOfArea ) {
153- Point topLeft = getPixelsFromPoint (monitorOfLocation , x , y );
154- int zoom = getApplicableMonitorZoom (monitorOfArea );
139+ private Rectangle translateRectangleInPointsToPixels (int x , int y , int width , int height , Monitor monitor ) {
140+ monitor = getValidMonitorIfApplicable (x , y , width , height , monitor );
141+ Point topLeft = getPixelsFromPoint (monitor , x , y );
142+ int zoom = getApplicableMonitorZoom (monitor );
155143 int widthInPixels = DPIUtil .scaleUp (width , zoom );
156144 int heightInPixels = DPIUtil .scaleUp (height , zoom );
157145 return new Rectangle (topLeft .x , topLeft .y , widthInPixels , heightInPixels );
158146 }
159147
160- private Rectangle translateRectangleInPixelsFromDisplayCoordinateSystemByContainment (int x , int y ,
161- int widthInPixels , int heightInPixels ) {
162- Monitor monitorByLocation = getContainingMonitor (x , y );
163- Monitor monitorByContainment = getContainingMonitorInPixelsCoordinate (x , y , widthInPixels , heightInPixels );
164- return translateRectangleInPointsInDisplayCoordinateSystem (x , y , widthInPixels , heightInPixels ,
165- monitorByLocation , monitorByContainment );
148+ private Rectangle translateRectangleInPixelsToPoints (int x , int y , int widthInPixels , int heightInPixels , Monitor monitor ) {
149+ if (monitor == null )
150+ monitor = getContainingMonitorForPixels (x , y , widthInPixels , heightInPixels );
151+ int zoom = getApplicableMonitorZoom (monitor );
152+ Point topLeft = getPointFromPixels (monitor , x , y );
153+ int width = DPIUtil .scaleDown (widthInPixels , zoom );
154+ int height = DPIUtil .scaleDown (heightInPixels , zoom );
155+ MonitorAwareRectangle rect = new MonitorAwareRectangle (topLeft .x , topLeft .y , width , height , monitor );
156+ return rect ;
166157 }
167158
168- private Rectangle translateRectangleInPointsInDisplayCoordinateSystem (int x , int y , int widthInPixels ,
169- int heightInPixels , Monitor monitor ) {
170- return translateRectangleInPointsInDisplayCoordinateSystem (x , y , widthInPixels , heightInPixels , monitor ,
171- monitor );
159+ private Monitor getValidMonitorIfApplicable (int x , int y , int width , int height , Monitor monitor ) {
160+ if (monitor != null ) {
161+ if (monitor .getClientArea ().intersects (x , y , width , height )) {
162+ return monitor ;
163+ } else {
164+ Monitor containingMonitor = getContainingMonitorForPoints (x , y , width , height );
165+ if (containingMonitor != null ) {
166+ return containingMonitor ;
167+ } else {
168+ return monitor ;
169+ }
170+ }
171+ } else {
172+ Monitor containingMonitor = getContainingMonitorForPoints (x , y , width , height );
173+ if (containingMonitor != null ) {
174+ return containingMonitor ;
175+ } else {
176+ return monitorSupplier .get ()[0 ];
177+ }
178+ }
172179 }
173180
174- private Rectangle translateRectangleInPointsInDisplayCoordinateSystem (int x , int y , int widthInPixels ,
175- int heightInPixels , Monitor monitorOfLocation , Monitor monitorOfArea ) {
176- Point topLeft = getPointFromPixels (monitorOfLocation , x , y );
177- int zoom = getApplicableMonitorZoom (monitorOfArea );
178- int width = DPIUtil .scaleDown (widthInPixels , zoom );
179- int height = DPIUtil .scaleDown (heightInPixels , zoom );
180- return new Rectangle (topLeft .x , topLeft .y , width , height );
181+ private Monitor getValidMonitorIfApplicable (int x , int y , Monitor monitor ) {
182+ if (monitor != null ) {
183+ if (monitor .getClientArea ().contains (x , y )) {
184+ return monitor ;
185+ } else {
186+ Monitor containingMonitor = getContainingMonitorForPoints (x , y );
187+ if (containingMonitor != null ) {
188+ return containingMonitor ;
189+ } else {
190+ return monitor ;
191+ }
192+ }
193+ } else {
194+ Monitor containingMonitor = getContainingMonitorForPoints (x , y );
195+ if (containingMonitor != null ) {
196+ return containingMonitor ;
197+ } else {
198+ return monitorSupplier .get ()[0 ];
199+ }
200+ }
181201 }
182202
183- private Monitor getContainingMonitor (int x , int y ) {
203+ private Monitor getContainingMonitorForPoints (int x , int y ) {
184204 Monitor [] monitors = monitorSupplier .get ();
185205 for (Monitor currentMonitor : monitors ) {
186206 Rectangle clientArea = currentMonitor .getClientArea ();
187207 if (clientArea .contains (x , y )) {
188208 return currentMonitor ;
189209 }
190210 }
191- return monitors [ 0 ] ;
211+ return null ;
192212 }
193213
194- private Monitor getContainingMonitor (int x , int y , int width , int height ) {
214+ private Monitor getContainingMonitorForPoints (int x , int y , int width , int height ) {
195215 Rectangle rectangle = new Rectangle (x , y , width , height );
196216 Monitor [] monitors = monitorSupplier .get ();
197- Monitor selectedMonitor = monitors [ 0 ] ;
198- int highestArea = 0 ;
217+ Monitor selectedMonitor = null ;
218+ int highestIntersectionRatio = 0 ;
199219 for (Monitor currentMonitor : monitors ) {
200- Rectangle clientArea = currentMonitor .getClientArea ();
201- Rectangle intersection = clientArea .intersection (rectangle );
202- int area = intersection .width * intersection .height ;
203- if (area > highestArea ) {
220+ Rectangle clientArea = getMonitorClientAreaInPixels (currentMonitor );
221+ Rectangle boundsInPixel = DPIUtil .scaleUp (rectangle , currentMonitor .zoom );
222+ Rectangle intersection = clientArea .intersection (boundsInPixel );
223+ int intersectionArea = intersection .width * intersection .height ;
224+ int boundsArea = boundsInPixel .width * boundsInPixel .height ;
225+ int intersectionRatio = (intersectionArea * 100 ) / boundsArea ;
226+ if (intersectionRatio > highestIntersectionRatio ) {
204227 selectedMonitor = currentMonitor ;
205- highestArea = area ;
228+ highestIntersectionRatio = intersectionRatio ;
206229 }
207230 }
208231 return selectedMonitor ;
209232 }
210233
211- private Monitor getContainingMonitorInPixelsCoordinate (int xInPixels , int yInPixels ) {
234+ private Monitor getContainingMonitorForPixels (int xInPixels , int yInPixels ) {
212235 Monitor [] monitors = monitorSupplier .get ();
213236 for (Monitor current : monitors ) {
214237 Rectangle clientArea = getMonitorClientAreaInPixels (current );
@@ -219,7 +242,7 @@ private Monitor getContainingMonitorInPixelsCoordinate(int xInPixels, int yInPix
219242 return monitors [0 ];
220243 }
221244
222- private Monitor getContainingMonitorInPixelsCoordinate (int xInPixels , int yInPixels , int widthInPixels ,
245+ private Monitor getContainingMonitorForPixels (int xInPixels , int yInPixels , int widthInPixels ,
223246 int heightInPixels ) {
224247 Rectangle rectangle = new Rectangle (xInPixels , yInPixels , widthInPixels , heightInPixels );
225248 Monitor [] monitors = monitorSupplier .get ();
@@ -255,7 +278,8 @@ private Point getPointFromPixels(Monitor monitor, int x, int y) {
255278 int zoom = getApplicableMonitorZoom (monitor );
256279 int mappedX = DPIUtil .scaleDown (x - monitor .clientX , zoom ) + monitor .clientX ;
257280 int mappedY = DPIUtil .scaleDown (y - monitor .clientY , zoom ) + monitor .clientY ;
258- return new Point (mappedX , mappedY );
281+ Point pt = new MonitorAwarePoint (mappedX , mappedY , monitor );
282+ return pt ;
259283 }
260284
261285 private int getApplicableMonitorZoom (Monitor monitor ) {
0 commit comments