Bug 1447292: Handle constraining non popup windows properly in cocoa. r?mstange draft
authorEmilio Cobos Álvarez <emilio@crisal.io>
Thu, 01 Mar 2018 23:58:45 +0100
changeset 769950 4c9d792426949386608a714d6aa565b6e1f36acf
parent 769949 80fef6222bde8137025020fc86576e2eacaf3904
push id103266
push userbmo:emilio@crisal.io
push dateTue, 20 Mar 2018 13:41:58 +0000
reviewersmstange
bugs1447292
milestone61.0a1
Bug 1447292: Handle constraining non popup windows properly in cocoa. r?mstange MozReview-Commit-ID: LmbKi4YR3d3
widget/cocoa/nsCocoaWindow.mm
--- a/widget/cocoa/nsCocoaWindow.mm
+++ b/widget/cocoa/nsCocoaWindow.mm
@@ -1253,16 +1253,42 @@ void nsCocoaWindow::SetSizeConstraints(c
       FLT_MAX : nsCocoaUtils::DevPixelsToCocoaPoints(c.mMaxSize.width, scaleFactor),
     c.mMaxSize.height == NS_MAXSIZE ?
       FLT_MAX : nsCocoaUtils::DevPixelsToCocoaPoints(c.mMaxSize.height, scaleFactor)
   };
   [mWindow setMaxSize:maxSize];
 
   nsBaseWidget::SetSizeConstraints(c);
 
+  // Popups are constrained during layout, and we don't want to synchronously
+  // paint from reflow, so bail out... This is not great, but it's no worse than
+  // what we used to do.
+  //
+  // The right fix here is probably making constraint changes go through the
+  // view manager and such.
+  if (mWindowType == eWindowType_popup) {
+    return;
+  }
+
+  // Otherwise figure out if we need to actually apply the constraints.
+  NSRect frame = [mWindow frame];
+  NSRect clampedFrame = frame;
+  clampedFrame.size.height =
+    std::max(minSize.height, std::min(maxSize.height, frame.size.height));
+  clampedFrame.size.width =
+    std::max(minSize.width, std::min(maxSize.width, frame.size.width));
+
+  if (clampedFrame.size.width != frame.size.width ||
+      clampedFrame.size.height != frame.size.height) {
+    // The origin in the cocoa coordinate space is in the bottom left, so we
+    // need to do some math to avoid changing the origin Gecko cares about.
+    clampedFrame.origin.y += frame.size.height - clampedFrame.size.height;
+    [mWindow setFrame:clampedFrame display:YES];
+  }
+
   NS_OBJC_END_TRY_ABORT_BLOCK;
 }
 
 // Coordinates are desktop pixels
 void
 nsCocoaWindow::Move(double aX, double aY)
 {
   NS_OBJC_BEGIN_TRY_ABORT_BLOCK;