@@ -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,94 +93,112 @@ 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+ return translateLocationInPointsToPixels (point .x , point .y , point . monitor );
102102 }
103103
104104 @ Override
105105 public Rectangle translateFromDisplayCoordinates (Rectangle rect , int zoom ) {
106- return translateRectangleInPixelsFromDisplayCoordinateSystemByContainment (rect .x , rect .y , rect .width ,
107- rect .height );
106+ return translateRectangleInPixelsToPoints (rect .x , rect .y , rect .width , rect .height , rect .monitor );
108107 }
109108
110109 @ Override
111110 public Rectangle translateToDisplayCoordinates (Rectangle rect , int zoom ) {
112- return translateRectangleInPointsToDisplayCoordinateSystemByContainment (rect .x , rect .y , rect .width ,
113- rect .height );
111+ return translateRectangleInPointsToPixels (rect .x , rect .y , rect .width , rect .height , rect .monitor );
114112 }
115113
116114 @ Override
117115 public Point getCursorLocation () {
118116 Point cursorLocationInPixels = display .getCursorLocationInPixels ();
119- return translateLocationInPixelsFromDisplayCoordinateSystem (cursorLocationInPixels .x , cursorLocationInPixels .y );
117+ return translateLocationInPixelsToPoints (cursorLocationInPixels .x , cursorLocationInPixels .y );
120118 }
121119
122120 @ Override
123- public void setCursorLocation (int x , int y ) {
124- Point cursorLocationInPixels = translateLocationInPointsToDisplayCoordinateSystem (x , y );
125- display .setCursorLocationInPixels (cursorLocationInPixels .x , cursorLocationInPixels .y );
121+ public void setCursorLocation (int x , int y ) {
122+ Point cursorLocationInPixels = translateLocationInPointsToPixels (x , y , null );
123+ display .setCursorLocationInPixels (cursorLocationInPixels .x , cursorLocationInPixels .y );
126124 }
127125
128- private Point translateLocationInPointsToDisplayCoordinateSystem (int x , int y ) {
129- Monitor monitor = getContainingMonitor (x , y );
126+ private Point translateLocationInPointsToPixels (int x , int y , Monitor monitor ) {
127+ monitor = getValidMonitorIfApplicable (x , y , monitor );
130128 return getPixelsFromPoint (monitor , x , y );
131129 }
132130
133- private Point translateLocationInPixelsFromDisplayCoordinateSystem (int x , int y ) {
134- Monitor monitor = getContainingMonitorInPixelsCoordinate (x , y );
131+ private Point translateLocationInPixelsToPoints (int x , int y ) {
132+ Monitor monitor = getContainingMonitorForPixels (x , y );
135133 return getPointFromPixels (monitor , x , y );
136134 }
137135
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 );
136+ private Rectangle translateRectangleInPointsToPixels (int x , int y , int width , int height , Monitor monitor ) {
137+ monitor = getValidMonitorIfApplicable (x , y , width , height , monitor );
138+ Point topLeft = getPixelsFromPoint (monitor , x , y );
139+ int zoom = getApplicableMonitorZoom (monitor );
155140 int widthInPixels = DPIUtil .scaleUp (width , zoom );
156141 int heightInPixels = DPIUtil .scaleUp (height , zoom );
157142 return new Rectangle (topLeft .x , topLeft .y , widthInPixels , heightInPixels );
158143 }
159144
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 );
145+ private Rectangle translateRectangleInPixelsToPoints (int x , int y , int widthInPixels , int heightInPixels , Monitor monitor ) {
146+ if (monitor == null )
147+ monitor = getContainingMonitorForPixels (x , y , widthInPixels , heightInPixels );
148+ int zoom = getApplicableMonitorZoom (monitor );
149+ Point topLeft = getPointFromPixels (monitor , x , y );
150+ int width = DPIUtil .scaleDown (widthInPixels , zoom );
151+ int height = DPIUtil .scaleDown (heightInPixels , zoom );
152+ Rectangle rect = new Rectangle (topLeft .x , topLeft .y , width , height );
153+ rect .monitor = monitor ;
154+ return rect ;
166155 }
167156
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 );
157+ private Monitor getValidMonitorIfApplicable (int x , int y , int width , int height , Monitor monitor ) {
158+ if (monitor != null ) {
159+ if (monitor .getClientArea ().intersects (x , y , width , height )) {
160+ return monitor ;
161+ } else {
162+ Monitor containingMonitor = getContainingMonitorForPoints (x , y , width , height );
163+ if (containingMonitor != null ) {
164+ return containingMonitor ;
165+ } else {
166+ return monitor ;
167+ }
168+ }
169+ } else {
170+ Monitor containingMonitor = getContainingMonitorForPoints (x , y , width , height );
171+ if (containingMonitor != null ) {
172+ return containingMonitor ;
173+ } else {
174+ return monitorSupplier .get ()[0 ];
175+ }
176+ }
172177 }
173178
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 );
179+ private Monitor getValidMonitorIfApplicable (int x , int y , Monitor monitor ) {
180+ if (monitor != null ) {
181+ if (monitor .getClientArea ().contains (x , y )) {
182+ return monitor ;
183+ } else {
184+ Monitor containingMonitor = getContainingMonitorForPoints (x , y );
185+ if (containingMonitor != null ) {
186+ return containingMonitor ;
187+ } else {
188+ return monitor ;
189+ }
190+ }
191+ } else {
192+ Monitor containingMonitor = getContainingMonitorForPoints (x , y );
193+ if (containingMonitor != null ) {
194+ return containingMonitor ;
195+ } else {
196+ return monitorSupplier .get ()[0 ];
197+ }
198+ }
181199 }
182200
183- private Monitor getContainingMonitor (int x , int y ) {
201+ private Monitor getContainingMonitorForPoints (int x , int y ) {
184202 Monitor [] monitors = monitorSupplier .get ();
185203 for (Monitor currentMonitor : monitors ) {
186204 Rectangle clientArea = currentMonitor .getClientArea ();
@@ -191,24 +209,27 @@ private Monitor getContainingMonitor(int x, int y) {
191209 return monitors [0 ];
192210 }
193211
194- private Monitor getContainingMonitor (int x , int y , int width , int height ) {
212+ private Monitor getContainingMonitorForPoints (int x , int y , int width , int height ) {
195213 Rectangle rectangle = new Rectangle (x , y , width , height );
196214 Monitor [] monitors = monitorSupplier .get ();
197215 Monitor selectedMonitor = monitors [0 ];
198- int highestArea = 0 ;
216+ int highestIntersectionRatio = 0 ;
199217 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 ) {
218+ Rectangle clientArea = getMonitorClientAreaInPixels (currentMonitor );
219+ Rectangle boundsInPixel = DPIUtil .scaleUp (rectangle , currentMonitor .zoom );
220+ Rectangle intersection = clientArea .intersection (boundsInPixel );
221+ int intersectionArea = intersection .width * intersection .height ;
222+ int boundsArea = boundsInPixel .width * boundsInPixel .height ;
223+ int intersectionRatio = (intersectionArea * 100 ) / boundsArea ;
224+ if (intersectionRatio > highestIntersectionRatio ) {
204225 selectedMonitor = currentMonitor ;
205- highestArea = area ;
226+ highestIntersectionRatio = intersectionRatio ;
206227 }
207228 }
208229 return selectedMonitor ;
209230 }
210231
211- private Monitor getContainingMonitorInPixelsCoordinate (int xInPixels , int yInPixels ) {
232+ private Monitor getContainingMonitorForPixels (int xInPixels , int yInPixels ) {
212233 Monitor [] monitors = monitorSupplier .get ();
213234 for (Monitor current : monitors ) {
214235 Rectangle clientArea = getMonitorClientAreaInPixels (current );
@@ -219,7 +240,7 @@ private Monitor getContainingMonitorInPixelsCoordinate(int xInPixels, int yInPix
219240 return monitors [0 ];
220241 }
221242
222- private Monitor getContainingMonitorInPixelsCoordinate (int xInPixels , int yInPixels , int widthInPixels ,
243+ private Monitor getContainingMonitorForPixels (int xInPixels , int yInPixels , int widthInPixels ,
223244 int heightInPixels ) {
224245 Rectangle rectangle = new Rectangle (xInPixels , yInPixels , widthInPixels , heightInPixels );
225246 Monitor [] monitors = monitorSupplier .get ();
@@ -255,7 +276,9 @@ private Point getPointFromPixels(Monitor monitor, int x, int y) {
255276 int zoom = getApplicableMonitorZoom (monitor );
256277 int mappedX = DPIUtil .scaleDown (x - monitor .clientX , zoom ) + monitor .clientX ;
257278 int mappedY = DPIUtil .scaleDown (y - monitor .clientY , zoom ) + monitor .clientY ;
258- return new Point (mappedX , mappedY );
279+ Point pt = new Point (mappedX , mappedY );
280+ pt .monitor = monitor ;
281+ return pt ;
259282 }
260283
261284 private int getApplicableMonitorZoom (Monitor monitor ) {
0 commit comments