cocoa: add constraints to prevent the window to exit the screen

Previously the window could be made to completly exit the screen with a
combination or moving it close to an edge and halving it's size (via cmd+0).

This commit address the problem in the most simple way possibile by
constraining the window to the closest edge in these edge cases.
This commit is contained in:
Stefano Pigozzi 2014-03-25 21:27:20 +01:00
parent 80ec0bac43
commit 2c08ab1c6a
1 changed files with 21 additions and 9 deletions

View File

@ -127,19 +127,31 @@
if ([self isInFullScreenMode])
return [super constrainFrameRect:nf toScreen:screen];
NSRect of = [self frame];
NSRect cf = [self frame];
NSRect vf = [[self screen] visibleFrame];
if (NSMaxY(nf) > NSMaxY(vf)) {
// If the new window is bigger than the visible frame, make sure it's
// titlebar is visible and at the top of the visible frame.
// Prevent the window's titlebar from exiting the screen on the top edge.
// This introduces a 'snap to top' behaviour.
if (NSMaxY(nf) > NSMaxY(vf))
nf.origin.y = NSMaxY(vf) - NSHeight(nf);
} else if (NSHeight(of) > NSHeight(vf)) {
// If the window is smaller than the visible frame, but it was bigger
// previously (so we ran the previous conditional branch), recenter
// the smaller window vertically.
// Prevent window from exiting the screen on the bottom edge
if (NSMaxY(nf) < NSMinY(vf))
nf.origin.y = NSMinY(vf);
// Prevent window from exiting the screen on the right edge
if (NSMinX(nf) > NSMaxX(vf))
nf.origin.x = NSMaxX(vf) - NSWidth(nf);
// Prevent window from exiting the screen on the left
if (NSMaxX(nf) < NSMinX(vf))
nf.origin.x = NSMinX(vf);
if (NSHeight(nf) < NSHeight(vf) && NSHeight(cf) > NSHeight(vf))
// If the window height is smaller than the visible frame, but it was
// bigger previously recenter the smaller window vertically. This is
// needed to counter the 'snap to top' behaviour.
nf.origin.y = (NSHeight(vf) - NSHeight(nf)) / 2;
}
return nf;
}