Merge pull request #10661 from electron/browserview-multiple-screens

BrowserView: Allow dragging of window to screen above menubar
This commit is contained in:
Felix Rieseberg 2017-10-03 16:09:11 -07:00 committed by GitHub
commit 4e05d24d1b

View file

@ -43,6 +43,12 @@ const NSAutoresizingMaskOptions kDefaultAutoResizingMask =
- (void)mouseDown:(NSEvent *)event
{
if ([self.window respondsToSelector:@selector(performWindowDragWithEvent)]) {
// According to Google, using performWindowDragWithEvent:
// does not generate a NSWindowWillMoveNotification. Hence post one.
[[NSNotificationCenter defaultCenter]
postNotificationName:NSWindowWillMoveNotification
object:self];
[self.window performWindowDragWithEvent:event];
return;
}
@ -68,13 +74,45 @@ const NSAutoresizingMaskOptions kDefaultAutoResizingMask =
NSPoint newOrigin;
NSRect screenFrame = [[NSScreen mainScreen] frame];
NSSize screenSize = screenFrame.size;
NSRect windowFrame = [self.window frame];
NSSize windowSize = windowFrame.size;
newOrigin.x = currentLocation.x - self.initialLocation.x;
newOrigin.y = currentLocation.y - self.initialLocation.y;
BOOL inMenuBar = (newOrigin.y + windowSize.height) > (screenFrame.origin.y + screenSize.height);
BOOL screenAboveMainScreen = false;
if (inMenuBar) {
for (NSScreen *screen in [NSScreen screens]) {
NSRect currentScreenFrame = [screen frame];
BOOL isHigher = currentScreenFrame.origin.y > screenFrame.origin.y;
// If there's another screen that is generally above the current screen,
// we'll draw a new rectangle that is just above the current screen. If the
// "higher" screen intersects with this rectangle, we'll allow drawing above
// the menubar.
if (isHigher) {
NSRect aboveScreenRect = NSMakeRect(
screenFrame.origin.x,
screenFrame.origin.y + screenFrame.size.height - 10,
screenFrame.size.width,
200
);
BOOL screenAboveIntersects = NSIntersectsRect(currentScreenFrame, aboveScreenRect);
if (screenAboveIntersects) {
screenAboveMainScreen = true;
break;
}
}
}
}
// Don't let window get dragged up under the menu bar
if ((newOrigin.y + windowFrame.size.height) > (screenFrame.origin.y + screenFrame.size.height)) {
if (inMenuBar && !screenAboveMainScreen) {
newOrigin.y = screenFrame.origin.y + (screenFrame.size.height - windowFrame.size.height);
}