public
Last active

NSEvent+MouseClamped

  • Download Gist
gistfile1.txt
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130
//
// NSEvent+MouseClamped.h
// xScope
//
// Created by Craig Hockenberry on 7/25/12.
// Copyright (c) 2012 The Iconfactory. All rights reserved.
//
 
#import <Cocoa/Cocoa.h>
 
@interface NSEvent (MouseClamped)
 
+ (NSPoint)clampedMouseLocation;
+ (NSPoint)integralMouseLocation;
+ (NSPoint)clampedMouseLocationUsingBackingScaleFactor:(CGFloat)backingScaleFactor;
 
@end
 
//
// NSEvent+MouseClamped.m
// xScope
//
// Created by Craig Hockenberry on 7/25/12.
// Copyright (c) 2012 The Iconfactory. All rights reserved.
//
 
#import "NSEvent+MouseClamped.h"
 
 
@implementation NSEvent (MouseClamped)
 
+ (NSPoint)clampedMouseLocation
{
// BUG: http://www.openradar.me/11905408 - nasty hack to make sure mouse location is within screen bounds
 
NSPoint mouseLocation = [self mouseLocation];
 
// find the screen that contains the mouse location, or the screen that's closest to the location
float closestScreenDistance = MAXFLOAT;
NSUInteger closestScreenIndex = 0;
BOOL screenFound = NO;
NSUInteger screenIndex = 0;
for (NSScreen *screen in [NSScreen screens]) {
NSRect screenFrame = [screen frame];
if (NSPointInRect(mouseLocation, screenFrame)) {
screenFound = YES;
break;
}
float xDistance = 0.0;
if (mouseLocation.x < NSMinX(screenFrame)) {
xDistance = NSMinX(screenFrame) - mouseLocation.x;
}
else if (mouseLocation.x > NSMaxX(screenFrame)) {
xDistance = mouseLocation.x - NSMaxX(screenFrame);
}
float yDistance = 0.0;
if (mouseLocation.y < NSMinY(screenFrame)) {
yDistance = NSMinY(screenFrame) - mouseLocation.y;
}
else if (mouseLocation.y > NSMaxY(screenFrame)) {
yDistance = mouseLocation.y - NSMaxY(screenFrame);
}
float screenDistance = xDistance + yDistance;
if (screenDistance < closestScreenDistance) {
closestScreenDistance = screenDistance;
closestScreenIndex = screenIndex;
}
screenIndex += 1;
}
 
CGFloat backingScaleFactor = 1.0;
if (screenFound) {
// get the scaling factor from the screen that was found
NSScreen *screen = [[NSScreen screens] objectAtIndex:screenIndex];
backingScaleFactor = [screen backingScaleFactor];
}
else {
// get the scaling factor from the closest screen
NSScreen *screen = [[NSScreen screens] objectAtIndex:closestScreenIndex];
backingScaleFactor = [screen backingScaleFactor];
// clamp the mouse location to bounds of that closest screen
CGFloat inset = 1.0 / backingScaleFactor;
NSRect screenFrame = [screen frame];
if (mouseLocation.x > (NSMaxX(screenFrame) - inset)) {
mouseLocation.x = (NSMaxX(screenFrame) - inset);
}
else if (mouseLocation.x < NSMinX(screenFrame)) {
mouseLocation.x = NSMinX(screenFrame);
}
if (mouseLocation.y > (NSMaxY(screenFrame) - inset)) {
mouseLocation.y = (NSMaxY(screenFrame) - inset);
}
else if (mouseLocation.y < NSMinY(screenFrame)) {
mouseLocation.y = NSMinY(screenFrame);
}
}
 
// make sure the mouse location falls on a pixel boundary (a full screen point on non-Retina, a half-point on Retina)
mouseLocation.x = floor(mouseLocation.x * backingScaleFactor) / backingScaleFactor;
mouseLocation.y = floor(mouseLocation.y * backingScaleFactor) / backingScaleFactor;
 
return mouseLocation;
}
 
+ (NSPoint)integralMouseLocation
{
NSPoint mouseLocation = [self clampedMouseLocation];
 
mouseLocation.x = floor(mouseLocation.x);
mouseLocation.y = floor(mouseLocation.y);
return mouseLocation;
}
 
+ (NSPoint)clampedMouseLocationUsingBackingScaleFactor:(CGFloat)backingScaleFactor
{
// the mouse location is clamped to the screen where the mouse is located
NSPoint mouseLocation = [self clampedMouseLocation];
 
// there are cases where you want to clamp the mouse's location to another scale factor (for example, a window that spans multiple screens)
mouseLocation.x = floor(mouseLocation.x * backingScaleFactor) / backingScaleFactor;
mouseLocation.y = floor(mouseLocation.y * backingScaleFactor) / backingScaleFactor;
return mouseLocation;
}
 
@end

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.