Skip to content

Commit 64fb36e

Browse files
committed
Reorganized monitor-bound utils in Tree #62
This commit contributes to moving Tree:getContainingMonitorBoundsInPixels and Tree:fitRectangleBoundsIntoMonitor to CoordinateSystemMapper and Display, respectively for better clarity and encapsulation. contributes to #62 and #128
1 parent 7add671 commit 64fb36e

File tree

5 files changed

+46
-48
lines changed

5 files changed

+46
-48
lines changed

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ interface CoordinateSystemMapper {
3535

3636
Rectangle translateToDisplayCoordinates(Rectangle rect, int zoom);
3737

38+
Rectangle getContainingMonitorBoundsInPixels(Point point);
39+
3840
void setCursorLocation(int x, int y);
3941

4042
Point getCursorLocation();

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

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1724,6 +1724,25 @@ public Point getCursorLocation () {
17241724
return coordinateSystemMapper.getCursorLocation();
17251725
}
17261726

1727+
Rectangle fitRectangleBoundsIntoMonitorWithCursor(RECT rect) {
1728+
Rectangle monitorBounds = coordinateSystemMapper.getContainingMonitorBoundsInPixels(getCursorLocation());
1729+
int rectWidth = rect.right - rect.left;
1730+
int rectHeight = rect.bottom - rect.top;
1731+
if (rect.left < monitorBounds.x) {
1732+
rect.left = monitorBounds.x;
1733+
}
1734+
int monitorBoundsRightEnd = monitorBounds.x + monitorBounds.width;
1735+
if (rect.right > monitorBoundsRightEnd) {
1736+
if (rectWidth <= monitorBounds.width) {
1737+
rect.left = monitorBoundsRightEnd - rectWidth;
1738+
} else {
1739+
rect.left = monitorBounds.x;
1740+
}
1741+
rectWidth = monitorBoundsRightEnd - rect.left;
1742+
}
1743+
return new Rectangle(rect.left, rect.top, rectWidth, rectHeight);
1744+
}
1745+
17271746
Point getCursorLocationInPixels () {
17281747
POINT pt = new POINT ();
17291748
OS.GetCursorPos (pt);

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -271,4 +271,12 @@ private int getApplicableMonitorZoom(Monitor monitor) {
271271
return DPIUtil.getZoomForAutoscaleProperty(monitor.zoom);
272272
}
273273

274+
@Override
275+
public Rectangle getContainingMonitorBoundsInPixels(Point point) {
276+
Monitor monitor = point instanceof MonitorAwarePoint monitorAwarePoint
277+
? monitorAwarePoint.getMonitor()
278+
:getContainingMonitorForPoints(point.x, point.y);
279+
return getMonitorClientAreaInPixels(monitor);
280+
}
281+
274282
}

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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,4 +105,17 @@ public Point getCursorLocation() {
105105
public void setCursorLocation(int x, int y) {
106106
display.setCursorLocationInPixels(DPIUtil.autoScaleUp(x), DPIUtil.autoScaleUp(y));
107107
}
108+
109+
@Override
110+
public Rectangle getContainingMonitorBoundsInPixels(Point point) {
111+
int zoom = DPIUtil.getDeviceZoom();
112+
point = DPIUtil.scaleUp(point, zoom);
113+
for (Monitor monitor : display.getMonitors()) {
114+
Rectangle monitorBounds = DPIUtil.scaleUp(monitor.getBounds(), zoom);
115+
if (monitorBounds.contains(point)) {
116+
return monitorBounds;
117+
}
118+
}
119+
return null;
120+
}
108121
}

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

Lines changed: 4 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -8175,15 +8175,10 @@ private LRESULT positionTooltip(NMHDR hdr, long wParam, long lParam, boolean man
81758175
// triggers additional display messages to SWT, creating an infinite loop
81768176
// of positioning and re-scaling events.
81778177
// Refer: https://github.com/eclipse-platform/eclipse.platform.swt/issues/557
8178-
Point cursorLocation = display.getCursorLocation();
8179-
Rectangle monitorBounds = cursorLocation instanceof MonitorAwarePoint monitorAwarePoint
8180-
? getContainingMonitorBoundsInMultiZoomCoordinateSystem(monitorAwarePoint)
8181-
: getContainingMonitorBoundsInSingleZoomCoordinateSystem(cursorLocation);
8182-
if (monitorBounds != null) {
8183-
Rectangle adjustedTooltipBounds = fitTooltipBoundsIntoMonitor(toolRect, monitorBounds);
8184-
OS.SetWindowPos (hdr.hwndFrom, 0, adjustedTooltipBounds.x, adjustedTooltipBounds.y, adjustedTooltipBounds.width, adjustedTooltipBounds.height, flags);
8185-
result = LRESULT.ONE;
8186-
}
8178+
8179+
Rectangle adjustedTooltipBounds = getDisplay().fitRectangleBoundsIntoMonitorWithCursor(toolRect);
8180+
OS.SetWindowPos (hdr.hwndFrom, 0, adjustedTooltipBounds.x, adjustedTooltipBounds.y, adjustedTooltipBounds.width, adjustedTooltipBounds.height, flags);
8181+
result = LRESULT.ONE;
81878182
} else if (!managedTooltip) {
81888183
// If managedTooltip is false and the cursor is not over the valid part of the
81898184
// target cell, Windows may still try to display the default tooltip. Since we
@@ -8196,45 +8191,6 @@ private LRESULT positionTooltip(NMHDR hdr, long wParam, long lParam, boolean man
81968191
return result;
81978192
}
81988193

8199-
/**
8200-
* Adjust the tool tip to fit in a single monitor either by shifting its position or by adjusting it's width.
8201-
*/
8202-
private Rectangle fitTooltipBoundsIntoMonitor(RECT tooltipBounds, Rectangle monitorBounds) {
8203-
int tooltipWidth = tooltipBounds.right - tooltipBounds.left;
8204-
int tooltipHeight = tooltipBounds.bottom - tooltipBounds.top;
8205-
if (tooltipBounds.left < monitorBounds.x) {
8206-
tooltipBounds.left = monitorBounds.x;
8207-
}
8208-
int monitorBoundsRightEnd = monitorBounds.x + monitorBounds.width;
8209-
if (tooltipBounds.right > monitorBoundsRightEnd) {
8210-
if (tooltipWidth <= monitorBounds.width) {
8211-
tooltipBounds.left = monitorBoundsRightEnd - tooltipWidth;
8212-
} else {
8213-
tooltipBounds.left = monitorBounds.x;
8214-
}
8215-
tooltipWidth = monitorBoundsRightEnd - tooltipBounds.left;
8216-
}
8217-
return new Rectangle(tooltipBounds.left, tooltipBounds.top, tooltipWidth, tooltipHeight);
8218-
}
8219-
8220-
private Rectangle getContainingMonitorBoundsInSingleZoomCoordinateSystem(Point point) {
8221-
int zoom = getZoom();
8222-
point = DPIUtil.scaleUp(point, zoom);
8223-
for (Monitor monitor : display.getMonitors()) {
8224-
Rectangle monitorBounds = DPIUtil.scaleUp(monitor.getBounds(), zoom);
8225-
if (monitorBounds.contains(point)) {
8226-
return monitorBounds;
8227-
}
8228-
}
8229-
return null;
8230-
}
8231-
8232-
private Rectangle getContainingMonitorBoundsInMultiZoomCoordinateSystem(MonitorAwarePoint point) {
8233-
Monitor monitor = point.getMonitor();
8234-
return new Rectangle(monitor.x, monitor.y, DPIUtil.scaleUp(monitor.width, monitor.zoom),
8235-
DPIUtil.scaleUp(monitor.height, monitor.zoom));
8236-
}
8237-
82388194
LRESULT wmNotifyToolTip (NMTTCUSTOMDRAW nmcd, long lParam) {
82398195
switch (nmcd.dwDrawStage) {
82408196
case OS.CDDS_PREPAINT: {

0 commit comments

Comments
 (0)