Skip to content

Commit 89a168f

Browse files
amartya4256HeikoKlare
authored andcommitted
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 18eaec6 commit 89a168f

File tree

5 files changed

+47
-45
lines changed

5 files changed

+47
-45
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: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1724,6 +1724,28 @@ public Point getCursorLocation () {
17241724
return coordinateSystemMapper.getCursorLocation();
17251725
}
17261726

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

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -271,4 +271,11 @@ 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 ? monitorAwarePoint.getMonitor()
277+
: getContainingMonitorForPoints(point.x, point.y);
278+
return getMonitorClientAreaInPixels(monitor);
279+
}
280+
274281
}

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: 3 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -8175,12 +8175,9 @@ 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);
8178+
8179+
Rectangle adjustedTooltipBounds = getDisplay().fitRectangleBoundsIntoMonitorWithCursor(toolRect);
8180+
if(adjustedTooltipBounds != null) {
81848181
OS.SetWindowPos (hdr.hwndFrom, 0, adjustedTooltipBounds.x, adjustedTooltipBounds.y, adjustedTooltipBounds.width, adjustedTooltipBounds.height, flags);
81858182
result = LRESULT.ONE;
81868183
}
@@ -8196,45 +8193,6 @@ private LRESULT positionTooltip(NMHDR hdr, long wParam, long lParam, boolean man
81968193
return result;
81978194
}
81988195

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-
82388196
LRESULT wmNotifyToolTip (NMTTCUSTOMDRAW nmcd, long lParam) {
82398197
switch (nmcd.dwDrawStage) {
82408198
case OS.CDDS_PREPAINT: {

0 commit comments

Comments
 (0)