Bug 356742 - Implement nsCocoaWindow::ConstrainPosition(). r=mnyromyr
authorSteven Michaud <smichaud@pobox.com>
Wed, 21 Dec 2011 16:13:21 -0600
changeset 83215 163c2800f87f
parent 83214 ed120f1a9dea
child 83216 a376707b4efb
push id21742
push userbmo@edmorley.co.uk
push dateThu, 22 Dec 2011 11:29:57 +0000
treeherdermozilla-central@c5b90ea7e475 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersmnyromyr
bugs356742
milestone12.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 356742 - Implement nsCocoaWindow::ConstrainPosition(). r=mnyromyr
widget/src/cocoa/nsCocoaWindow.mm
--- a/widget/src/cocoa/nsCocoaWindow.mm
+++ b/widget/src/cocoa/nsCocoaWindow.mm
@@ -996,19 +996,54 @@ NS_IMETHODIMP nsCocoaWindow::Enable(bool
 
 NS_IMETHODIMP nsCocoaWindow::IsEnabled(bool *aState)
 {
   if (aState)
     *aState = true;
   return NS_OK;
 }
 
+#define kWindowPositionSlop 20
+
 NS_IMETHODIMP nsCocoaWindow::ConstrainPosition(bool aAllowSlop,
                                                PRInt32 *aX, PRInt32 *aY)
 {
+  if (!mWindow || ![mWindow screen]) {
+    return NS_OK;
+  }
+
+  nsIntRect screenBounds(
+    nsCocoaUtils::CocoaRectToGeckoRect([[mWindow screen] visibleFrame]));
+
+  if (aAllowSlop) {
+    if (*aX < screenBounds.x - mBounds.width + kWindowPositionSlop) {
+      *aX = screenBounds.x - mBounds.width + kWindowPositionSlop;
+    } else if (*aX >= screenBounds.x + screenBounds.width - kWindowPositionSlop) {
+      *aX = screenBounds.x + screenBounds.width - kWindowPositionSlop;
+    }
+
+    if (*aY < screenBounds.y - mBounds.height + kWindowPositionSlop) {
+      *aY = screenBounds.y - mBounds.height + kWindowPositionSlop;
+    } else if (*aY >= screenBounds.y + screenBounds.height - kWindowPositionSlop) {
+      *aY = screenBounds.y + screenBounds.height - kWindowPositionSlop;
+    }
+  } else {
+    if (*aX < screenBounds.x) {
+      *aX = screenBounds.x;
+    } else if (*aX >= screenBounds.x + screenBounds.width - mBounds.width) {
+      *aX = screenBounds.x + screenBounds.width - mBounds.width;
+    }
+
+    if (*aY < screenBounds.y) {
+      *aY = screenBounds.y;
+    } else if (*aY >= screenBounds.y + screenBounds.height - mBounds.height) {
+      *aY = screenBounds.y + screenBounds.height - mBounds.height;
+    }
+  }
+
   return NS_OK;
 }
 
 NS_IMETHODIMP nsCocoaWindow::Move(PRInt32 aX, PRInt32 aY)
 {
   if (!mWindow || (mBounds.x == aX && mBounds.y == aY))
     return NS_OK;