Skip to content
This repository has been archived by the owner on Jan 21, 2023. It is now read-only.

Commit

Permalink
Merge pull request #250 from alex-courtis/quantized-height-width-wind…
Browse files Browse the repository at this point in the history
…ow-improvements

Quantized height width window improvements
  • Loading branch information
eczarny committed Oct 20, 2014
2 parents e668017 + 7c21240 commit 7773c14
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 80 deletions.
4 changes: 0 additions & 4 deletions SpectacleConstants.h
Expand Up @@ -64,10 +64,6 @@

#pragma mark -

#define SpectacleWindowCalculationFudgeFactor 8

#pragma mark -

#define SpectacleStatusItemIcon @"Spectacle Status Item"
#define SpectacleAlternateStatusItemIcon @"Spectacle Alternate Status Item"

Expand Down
3 changes: 2 additions & 1 deletion SpectacleUtilities.h
Expand Up @@ -10,7 +10,8 @@

#pragma mark -

#define CGRectEqualToRectWithFudge(a, b) ((abs(a.size.width - b.size.width) < (SpectacleWindowCalculationFudgeFactor * 2)) && (abs(a.size.height - b.size.height) < (SpectacleWindowCalculationFudgeFactor * 2)) && (abs(a.origin.x - b.origin.x) < (SpectacleWindowCalculationFudgeFactor * 2)) && (abs(a.origin.y - b.origin.y) < (SpectacleWindowCalculationFudgeFactor * 2)))
// true if b is contained and centred within a
#define CGRectCentredWithin(a, b) CGRectContainsRect(a, b) && fabs(CGRectGetMidX(b) - CGRectGetMidX(a)) <= 1.0f && fabs(CGRectGetMidY(b) - CGRectGetMidY(a)) <= 1.0f

#pragma mark -

Expand Down
94 changes: 38 additions & 56 deletions SpectacleWindowPositionCalculator.m
Expand Up @@ -9,11 +9,6 @@

#pragma mark -

#define AlreadyTwoThirdsOfDisplay(a, b) (abs(a.size.width - floor((b.size.width * 2.0f) / 3.0f)) < SpectacleWindowCalculationFudgeFactor) && (abs(a.size.height - b.size.height) < (SpectacleWindowCalculationFudgeFactor * 2))
#define AlreadyOneHalfOfDisplay(a, b) (abs(a.size.width - (b.size.width / 2.0f)) < SpectacleWindowCalculationFudgeFactor) && (abs(a.size.height - b.size.height) < (SpectacleWindowCalculationFudgeFactor * 2))

#pragma mark -

@implementation SpectacleWindowPositionCalculator

+ (CGRect)calculateWindowRect: (CGRect)windowRect visibleFrameOfScreen: (CGRect)visibleFrameOfScreen action: (SpectacleWindowAction)action {
Expand All @@ -36,19 +31,7 @@ + (CGRect)calculateWindowRect: (CGRect)windowRect visibleFrameOfScreen: (CGRect)
}

if ((action == SpectacleWindowActionLeftHalf) || (action == SpectacleWindowActionRightHalf)) {
if ([SpectacleWindowPositionCalculator halfToTwoThirds: windowRect visibleFrameOfScreen: visibleFrameOfScreen withAction: action]) {
calculatedWindowRect.size.width = floor((visibleFrameOfScreen.size.width * 2.0f) / 3.0f);
} else if ([SpectacleWindowPositionCalculator halfToOneThird: windowRect visibleFrameOfScreen: visibleFrameOfScreen withAction: action]) {
calculatedWindowRect.size.width = floor(visibleFrameOfScreen.size.width / 3.0f);
} else {
calculatedWindowRect.size.width = floor(visibleFrameOfScreen.size.width / 2.0f);
}

if (action == SpectacleWindowActionRightHalf) {
calculatedWindowRect.origin.x = visibleFrameOfScreen.origin.x + visibleFrameOfScreen.size.width - calculatedWindowRect.size.width;
}

calculatedWindowRect.size.height = visibleFrameOfScreen.size.height;
calculatedWindowRect = [SpectacleWindowPositionCalculator calculateLeftRightHalfRect:windowRect visibleFrameOfScreen:visibleFrameOfScreen withAction:action];
} else if ((action == SpectacleWindowActionTopHalf) || (action == SpectacleWindowActionBottomHalf)) {
calculatedWindowRect.size.width = visibleFrameOfScreen.size.width;
calculatedWindowRect.size.height = floor(visibleFrameOfScreen.size.height / 2.0f);
Expand All @@ -64,21 +47,6 @@ + (CGRect)calculateWindowRect: (CGRect)windowRect visibleFrameOfScreen: (CGRect)
calculatedWindowRect = [SpectacleWindowPositionCalculator findThirdForWindowRect: calculatedWindowRect visibleFrameOfScreen: visibleFrameOfScreen withAction: action];
}

if (MovingToTopRegionOfDisplay(action)) {
if (((visibleFrameOfScreen.size.height / 2.0f) - calculatedWindowRect.size.height) > 0.0f) {
calculatedWindowRect.origin.y = calculatedWindowRect.origin.y + 1.0f;
} else {
calculatedWindowRect.origin.y = calculatedWindowRect.origin.y + 1.0f;
calculatedWindowRect.size.height = calculatedWindowRect.size.height - 1.0f;
}

calculatedWindowRect.origin.y = calculatedWindowRect.origin.y + 1.0f;
}

if ((action >= SpectacleWindowActionLeftHalf) && (action <= SpectacleWindowActionLowerLeft)) {
calculatedWindowRect.size.width = calculatedWindowRect.size.width - 1.0f;
}

return calculatedWindowRect;
}

Expand Down Expand Up @@ -160,11 +128,6 @@ + (NSArray *)thirdsFromVisibleFrameOfScreen: (CGRect)visibleFrameOfScreen {
thirdOfScreen.origin.y = visibleFrameOfScreen.origin.y + visibleFrameOfScreen.size.height - (floor(visibleFrameOfScreen.size.height / 3.0f) * (i + 1));
thirdOfScreen.size.height = floor(visibleFrameOfScreen.size.height / 3.0f);

if ((i == 2) && (fmodf(visibleFrameOfScreen.size.height, 3.0f) != 0.0f)) {
thirdOfScreen.origin.y = thirdOfScreen.origin.y - 1.0f;
thirdOfScreen.size.height = thirdOfScreen.size.height + 1.0f;
}

[result addObject: [SpectacleHistoryItem historyItemFromAccessibilityElement: nil windowRect: thirdOfScreen]];
}

Expand All @@ -179,7 +142,8 @@ + (CGRect)findThirdForWindowRect: (CGRect)windowRect visibleFrameOfScreen: (CGRe
for (i = 0; i < thirds.count; i++) {
CGRect currentWindowRect = [thirds[i] windowRect];

if (CGRectEqualToRectWithFudge(currentWindowRect, windowRect)) {
// are we within this "third" and centred within it? advance to next
if (CGRectCentredWithin(currentWindowRect, windowRect)) {
NSInteger j = i;

if (action == SpectacleWindowActionNextThird) {
Expand Down Expand Up @@ -212,28 +176,46 @@ + (BOOL)isWindowRect: (CGRect)windowRect tooSmallRelativeToVisibleFrameOfScreen:

#pragma mark -

+ (BOOL)halfToOneThird: (CGRect)windowRect visibleFrameOfScreen: (CGRect)visibleFrameOfScreen withAction: (SpectacleWindowAction)action {
BOOL result = NO;

if (action == SpectacleWindowActionLeftHalf) {
result = AlreadyTwoThirdsOfDisplay(windowRect, visibleFrameOfScreen) && AgainstTheLeftEdgeOfScreen(windowRect, visibleFrameOfScreen);
} else if (action == SpectacleWindowActionRightHalf) {
result = AlreadyTwoThirdsOfDisplay(windowRect, visibleFrameOfScreen) && AgainstTheRightEdgeOfScreen(windowRect, visibleFrameOfScreen);
+ (CGRect)calculateLeftRightHalfRect: (CGRect)windowRect visibleFrameOfScreen: (CGRect)visibleFrameOfScreen withAction: (SpectacleWindowAction)action {

// calculate a left or right half visible rect
CGRect halfRect = visibleFrameOfScreen;
halfRect.size.width = floor(halfRect.size.width / 2.0f);
if (action == SpectacleWindowActionRightHalf) {
halfRect.origin.x += halfRect.size.width;
}

return result;
}
// are we already occuping full height?
if (fabs(CGRectGetMidY(windowRect) - CGRectGetMidY(halfRect)) <= 1.0f) {

// calculate a left or right one third visible rect
CGRect oneThirdRect = halfRect;
oneThirdRect.size.width = floor(visibleFrameOfScreen.size.width / 3.0f);
if (action == SpectacleWindowActionRightHalf) {
oneThirdRect.origin.x = visibleFrameOfScreen.origin.x + visibleFrameOfScreen.size.width - oneThirdRect.size.width;
}

+ (BOOL)halfToTwoThirds: (CGRect)windowRect visibleFrameOfScreen: (CGRect)visibleFrameOfScreen withAction: (SpectacleWindowAction)action {
BOOL result = NO;

if (action == SpectacleWindowActionLeftHalf) {
result = AlreadyOneHalfOfDisplay(windowRect, visibleFrameOfScreen) && AgainstTheLeftEdgeOfScreen(windowRect, visibleFrameOfScreen);
} else if (action == SpectacleWindowActionRightHalf) {
result = AlreadyOneHalfOfDisplay(windowRect, visibleFrameOfScreen) && AgainstTheRightEdgeOfScreen(windowRect, visibleFrameOfScreen);
// are we at half width already? shrink to one third
if (CGRectCentredWithin(halfRect, windowRect)) {
return oneThirdRect;
}

// are we at one third width already? grow to two thirds
if (CGRectCentredWithin(oneThirdRect, windowRect)) {

// calculate a left or right two thirds visible rect
CGRect twoThirdRects = halfRect;
twoThirdRects.size.width = floor(visibleFrameOfScreen.size.width * 2 / 3.0f);
if (action == SpectacleWindowActionRightHalf) {
twoThirdRects.origin.x = visibleFrameOfScreen.origin.x + visibleFrameOfScreen.size.width - twoThirdRects.size.width;
}
return twoThirdRects;
}

}

return result;
// default to the half
return halfRect;
}

@end
52 changes: 33 additions & 19 deletions SpectacleWindowPositionManager.m
Expand Up @@ -241,7 +241,7 @@ - (void)moveWindowRect: (CGRect)windowRect frameOfScreen: (CGRect)frameOfScreen

CGRect movedWindowRect = [self rectOfWindowWithAccessibilityElement: frontMostWindowElement];

if (MovingToThirdOfDisplay(action) && !CGRectEqualToRectWithFudge(movedWindowRect, windowRect)) {
if (MovingToThirdOfDisplay(action) && !CGRectCentredWithin(movedWindowRect, windowRect)) {
NSUserDefaults *userDefaults = NSUserDefaults.standardUserDefaults;

NSBeep();
Expand All @@ -255,26 +255,40 @@ - (void)moveWindowRect: (CGRect)windowRect frameOfScreen: (CGRect)frameOfScreen
return;
}

movedWindowRect.origin.y = FlipVerticalOriginOfRectInRect(movedWindowRect, frameOfScreen);

if (!CGRectContainsRect(visibleFrameOfScreen, movedWindowRect) && (action != SpectacleWindowActionUndo) && (action != SpectacleWindowActionRedo)) {
if (movedWindowRect.origin.x + movedWindowRect.size.width > visibleFrameOfScreen.origin.x + visibleFrameOfScreen.size.width) {
movedWindowRect.origin.x = (visibleFrameOfScreen.origin.x + visibleFrameOfScreen.size.width) - movedWindowRect.size.width;
} else if (movedWindowRect.origin.x < visibleFrameOfScreen.origin.x) {
movedWindowRect.origin.x = visibleFrameOfScreen.origin.x;
}

if (movedWindowRect.origin.y + movedWindowRect.size.height > visibleFrameOfScreen.origin.y + visibleFrameOfScreen.size.height) {
movedWindowRect.origin.y = (visibleFrameOfScreen.origin.y + visibleFrameOfScreen.size.height) - movedWindowRect.size.height;
} else if (movedWindowRect.origin.y < visibleFrameOfScreen.origin.y) {
movedWindowRect.origin.y = visibleFrameOfScreen.origin.y;
}

movedWindowRect.size = windowRect.size;
if ((action != SpectacleWindowActionUndo) && (action != SpectacleWindowActionRedo)) {

movedWindowRect.origin.y = FlipVerticalOriginOfRectInRect(movedWindowRect, frameOfScreen);
// attempt the move
[self moveWindowRect:windowRect frontMostWindowElement:frontMostWindowElement];
movedWindowRect = [self rectOfWindowWithAccessibilityElement: frontMostWindowElement];

[self moveWindowRect: movedWindowRect frontMostWindowElement: frontMostWindowElement];
// did we move exactly into the desired location?
if (!CGRectEqualToRect(movedWindowRect, windowRect)) {
CGRect adjustedWindowRect = windowRect;

// reduce size to fit
while (movedWindowRect.size.width > windowRect.size.width || movedWindowRect.size.height > windowRect.size.height) {
if (movedWindowRect.size.width > windowRect.size.width) {
adjustedWindowRect.size.width -= 1;
}
if (movedWindowRect.size.height > windowRect.size.height) {
adjustedWindowRect.size.height -= 1;
}

// give up if we're trying to shrink to half the desired as this resizing is just not working
if (adjustedWindowRect.size.width < windowRect.size.width / 2.0f || adjustedWindowRect.size.height < windowRect.size.height / 2.0f) {
break;
}

[self moveWindowRect:adjustedWindowRect frontMostWindowElement:frontMostWindowElement];
movedWindowRect = [self rectOfWindowWithAccessibilityElement: frontMostWindowElement];
}

// centre the resized window, taking into account any quantization adjustments
adjustedWindowRect.origin.x += floor((windowRect.size.width - movedWindowRect.size.width) / 2.0f);
adjustedWindowRect.origin.y += floor((windowRect.size.height - movedWindowRect.size.height) / 2.0f);

[self moveWindowRect:adjustedWindowRect frontMostWindowElement:frontMostWindowElement];
}
}
}

Expand Down

0 comments on commit 7773c14

Please sign in to comment.