Skip to content

Commit 9d6bea9

Browse files
[Win32] GetLocation as Point.WithMonitor to ensure correct monitor association
Previously, dialog positioning could be incorrect if the monitor was misidentified when restoring location. By returning the shell's location as a Point.WithMonitor (including monitor reference), we ensure dialogs and secondary shells open on the intended monitor, even in multi-monitor setups. This fixes issues where dialogs could appear on the wrong monitor after moving the parent shell.
1 parent 43f54cc commit 9d6bea9

File tree

3 files changed

+29
-6
lines changed

3 files changed

+29
-6
lines changed

bundles/org.eclipse.swt/Eclipse SWT Tests/win32/org/eclipse/swt/widgets/CoordinateSystemMapperTests.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,26 @@ void translateRectangleInPixelsForZeroSize(CoordinateSystemMapper mapper) {
218218
assertEquals(rectInPts, mapper.translateFromDisplayCoordinates(rectInPxs));
219219
}
220220

221+
@ParameterizedTest
222+
@MethodSource("provideCoordinateSystemMappers")
223+
void translateShellBoundsToSecondMonitor(CoordinateSystemMapper mapper) {
224+
setupMonitors(mapper);
225+
Rectangle currentPosition = new Rectangle.WithMonitor(1901, 0, 100, 100, monitors[1]);
226+
Rectangle result = mapper.translateFromDisplayCoordinates(currentPosition);
227+
assertTrue(monitors[1].getClientArea().intersects(result));
228+
}
229+
230+
@ParameterizedTest
231+
@MethodSource("provideCoordinateSystemMappers")
232+
void translateShellPointsToSecondMonitor(CoordinateSystemMapper mapper) {
233+
setupMonitors(mapper);
234+
Point currentPosition = new Point.WithMonitor(1901, 0, monitors[1]);
235+
Point size = new Point(100, 100);
236+
Point result = mapper.translateFromDisplayCoordinates(currentPosition);
237+
Rectangle resultRect = new Rectangle(result.x, result.y, size.x, size.y);
238+
assertTrue(monitors[1].getClientArea().intersects(resultRect));
239+
}
240+
221241
private Point createExpectedPoint(CoordinateSystemMapper mapper, int x, int y, Monitor monitor) {
222242
if (mapper instanceof SingleZoomCoordinateSystemMapper) {
223243
return new Point(x, y);

bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/widgets/MultiZoomCoordinateSystemMapper.java

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,8 @@ public Rectangle mapMonitorBounds(Rectangle rect, int zoom) {
9393

9494
@Override
9595
public Point translateFromDisplayCoordinates(Point point) {
96-
return translateLocationInPixelsToPoints(point.x, point.y);
96+
Monitor monitor = point instanceof Point.WithMonitor pointWithMonitor ? pointWithMonitor.getMonitor() : null;
97+
return translateLocationInPixelsToPoints(point.x, point.y, monitor);
9798
}
9899

99100
@Override
@@ -117,7 +118,7 @@ public Rectangle translateToDisplayCoordinates(Rectangle rect) {
117118
@Override
118119
public Point getCursorLocation() {
119120
Point cursorLocationInPixels = display.getCursorLocationInPixels();
120-
return translateLocationInPixelsToPoints(cursorLocationInPixels.x, cursorLocationInPixels.y);
121+
return translateLocationInPixelsToPoints(cursorLocationInPixels.x, cursorLocationInPixels.y, null);
121122
}
122123

123124
@Override
@@ -131,8 +132,10 @@ private Point translateLocationInPointsToPixels(int x, int y, Monitor monitor) {
131132
return getPixelsFromPoint(monitor, x, y);
132133
}
133134

134-
private Point translateLocationInPixelsToPoints(int x, int y) {
135-
Monitor monitor = getContainingMonitorForPixels(x, y);
135+
private Point translateLocationInPixelsToPoints(int x, int y, Monitor monitor) {
136+
if (monitor == null) {
137+
monitor = getContainingMonitorForPixels(x, y);
138+
}
136139
return getPointFromPixels(monitor, x, y);
137140
}
138141

bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/widgets/Shell.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -924,7 +924,7 @@ public int getAlpha () {
924924
OS.GetWindowRect (handle, rect);
925925
int width = rect.right - rect.left;
926926
int height = rect.bottom - rect.top;
927-
return new Rectangle (rect.left, rect.top, width, height);
927+
return new Rectangle.WithMonitor (rect.left, rect.top, width, height, getMonitor());
928928
}
929929

930930
ToolTip getCurrentToolTip () {
@@ -1017,7 +1017,7 @@ public int getImeInputMode () {
10171017
if (OS.IsIconic (handle)) return super.getLocationInPixels ();
10181018
RECT rect = new RECT ();
10191019
OS.GetWindowRect (handle, rect);
1020-
return new Point (rect.left, rect.top);
1020+
return new Point.WithMonitor (rect.left, rect.top, getMonitor());
10211021
}
10221022

10231023
@Override

0 commit comments

Comments
 (0)