[3.17] 476588: on a small screen, the comment navigation buttons open the
comment popup with a scrollbar

Change-Id: Ifa2d3f00239b52b69247a5ea569f5b9192905356
Task-Url: https://bugs.eclipse.org/bugs/show_bug.cgi?id=476588
Signed-off-by: chris.poon <chris.poon@tasktop.com>
diff --git a/org.eclipse.mylyn.reviews.ui.tests/src/org/eclipse/mylyn/internal/reviews/ui/annotations/CommentPopupDialogTest.java b/org.eclipse.mylyn.reviews.ui.tests/src/org/eclipse/mylyn/internal/reviews/ui/annotations/CommentPopupDialogTest.java
index a6b924f..09d82e5 100644
--- a/org.eclipse.mylyn.reviews.ui.tests/src/org/eclipse/mylyn/internal/reviews/ui/annotations/CommentPopupDialogTest.java
+++ b/org.eclipse.mylyn.reviews.ui.tests/src/org/eclipse/mylyn/internal/reviews/ui/annotations/CommentPopupDialogTest.java
@@ -49,6 +49,7 @@
 import org.eclipse.mylyn.tasks.ui.TasksUi;
 import org.eclipse.swt.SWT;
 import org.eclipse.swt.custom.StyledText;
+import org.eclipse.swt.graphics.Rectangle;
 import org.eclipse.swt.widgets.Button;
 import org.eclipse.swt.widgets.Composite;
 import org.eclipse.swt.widgets.Control;
@@ -67,6 +68,8 @@
 
 public class CommentPopupDialogTest extends TestCase {
 
+	private static final int MAX_WIDTH = 500;
+
 	private final static String USER_ID = "1";
 
 	private final static String USER_NAME = "Test User";
@@ -434,6 +437,44 @@
 	}
 
 	/**
+	 * Tests that setting the height based on the y-coordinate of the mouse works for one or more monitors
+	 */
+	@Test
+	public void testSetHeightBasedOnMouse() {
+		commentPopupDialog = spy(createPopupWithXComments(100, true));
+
+		// Monitor is the primary display
+		Rectangle monitorArea = new Rectangle(0, 0, 1680, 1050);
+		doReturn(monitorArea).when(commentPopupDialog).getMonitorArea();
+		commentPopupDialog.setHeightBasedOnMouse(500);
+		assertEquals(534, commentPopupDialog.getShell().getSize().y);
+
+		// Monitor to the left and below the top of primary display
+		monitorArea = new Rectangle(-1920, 480, 1920, 1080);
+		doReturn(monitorArea).when(commentPopupDialog).getMonitorArea();
+		commentPopupDialog.setHeightBasedOnMouse(500);
+		assertEquals(1044, commentPopupDialog.getShell().getSize().y);
+
+		// Monitor to the right and below the top of primary display
+		monitorArea = new Rectangle(1680, 480, 1920, 1080);
+		doReturn(monitorArea).when(commentPopupDialog).getMonitorArea();
+		commentPopupDialog.setHeightBasedOnMouse(500);
+		assertEquals(1044, commentPopupDialog.getShell().getSize().y);
+
+		// Monitor to the left above the top of primary display
+		monitorArea = new Rectangle(-1920, -420, 1920, 1080);
+		doReturn(monitorArea).when(commentPopupDialog).getMonitorArea();
+		commentPopupDialog.setHeightBasedOnMouse(500);
+		assertEquals(144, commentPopupDialog.getShell().getSize().y);
+
+		// Monitor to the right above the top of primary display
+		monitorArea = new Rectangle(1680, -420, 1920, 1080);
+		doReturn(monitorArea).when(commentPopupDialog).getMonitorArea();
+		commentPopupDialog.setHeightBasedOnMouse(500);
+		assertEquals(144, commentPopupDialog.getShell().getSize().y);
+	}
+
+	/**
 	 * Returns a comment from the dialog depending on the ordering from the top of the comment list
 	 * 
 	 * @param commentNumber
diff --git a/org.eclipse.mylyn.reviews.ui/src/org/eclipse/mylyn/internal/reviews/ui/annotations/CommentPopupDialog.java b/org.eclipse.mylyn.reviews.ui/src/org/eclipse/mylyn/internal/reviews/ui/annotations/CommentPopupDialog.java
index f319409..d6332f9 100644
--- a/org.eclipse.mylyn.reviews.ui/src/org/eclipse/mylyn/internal/reviews/ui/annotations/CommentPopupDialog.java
+++ b/org.eclipse.mylyn.reviews.ui/src/org/eclipse/mylyn/internal/reviews/ui/annotations/CommentPopupDialog.java
@@ -283,6 +283,10 @@
 		return getShell().getBounds();
 	}
 
+	public Rectangle getMonitorArea() {
+		return getShell().getMonitor().getClientArea();
+	}
+
 	public Rectangle computeTrim() {
 		return getShell().computeTrim(0, 0, 0, 0);
 	}
@@ -299,7 +303,7 @@
 	 */
 	public void setLocation(Point location) {
 		Rectangle bounds = getShell().getBounds();
-		Rectangle monitorBounds = getShell().getMonitor().getClientArea();
+		Rectangle monitorBounds = getMonitorArea();
 		// ensure the popup fits on the shell's monitor
 		bounds.x = constrain(location.x, monitorBounds.x, monitorBounds.x + monitorBounds.width - bounds.width);
 		bounds.y = constrain(location.y, monitorBounds.y, monitorBounds.y + monitorBounds.height - bounds.height);
@@ -319,7 +323,7 @@
 		Point size = composite.computeSize(SWT.DEFAULT, SWT.DEFAULT);
 
 		scrolledComposite.setMinSize(size);
-		int height = constrain(size.y, MIN_HEIGHT, getShell().getMonitor().getClientArea().height);
+		int height = constrain(size.y, MIN_HEIGHT, getMonitorArea().height);
 		getShell().setSize(size.x, height);
 		scrolledComposite.setSize(size.x, height);
 		setLocation(new Point(bounds.x, bounds.y));
@@ -340,7 +344,7 @@
 	}
 
 	public void setHeightBasedOnMouse(int mouseY) {
-		int mouseYFromBottom = getShell().getMonitor().getClientArea().height - mouseY;
+		int mouseYFromBottom = getMonitorArea().height + getMonitorArea().y - mouseY; // Coordinates are based on the primary monitor
 		recomputeSize();
 		setSize(MAX_WIDTH, constrain(mouseYFromBottom - ICON_BUFFER, MIN_HEIGHT, getShell().getSize().y));
 	}