Skip to content

Commit

Permalink
Only copy on drag if we've dragged more than 5 px away
Browse files Browse the repository at this point in the history
Prevent spurious copys on click while moving the mouse slightly
  • Loading branch information
josephlbarnett committed Mar 4, 2024
1 parent b4cc82d commit 0c481d9
Showing 1 changed file with 19 additions and 2 deletions.
21 changes: 19 additions & 2 deletions macpaste.m
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
char isDragging = 0;
long long prevClickTime = 0;
long long curClickTime = 0;
NSPoint initialLocation;

CGEventTapLocation tapA = kCGAnnotatedSessionEventTap;
CGEventTapLocation tapH = kCGHIDEventTap;
Expand Down Expand Up @@ -102,22 +103,38 @@ static CGEventRef mouseCallback (
button = CGEventGetIntegerValueField(event, kCGMouseEventButtonNumber);
NSCursor* cursor = [NSCursor currentSystemCursor];
NSCursor* ibeam = [NSCursor IBeamCursor];
if (*dontpaste == 0 && button == 2 && NSEqualPoints([cursor hotSpot] , [ibeam hotSpot] ))
if (*dontpaste == 0 && button == 2 && NSEqualPoints([cursor hotSpot] , [ibeam hotSpot] )) {
// NSLog(@"paste %@", NSStringFromPoint( [NSEvent mouseLocation]));
paste( event );
}
break;

case kCGEventLeftMouseDown:
//NSLog(@"down %@", NSStringFromPoint( [NSEvent mouseLocation]));
recordClickTime();
break;

case kCGEventLeftMouseUp:
if ( isDoubleClick() || isDragging ) {
//NSLog(@"up %@", NSStringFromPoint( [NSEvent mouseLocation]));
if (isDoubleClick()) {
//NSLog(@"copydblc %@", NSStringFromPoint( [NSEvent mouseLocation]));
copy();
}
if (isDragging) {
NSPoint clickLocation = [NSEvent mouseLocation];
int xdiff = fabs(initialLocation.x-clickLocation.x);
int ydiff = fabs(initialLocation.y-clickLocation.y);
if (xdiff > 5 || ydiff > 5) {
//NSLog(@"copydrag %@ %@", NSStringFromPoint(initialLocation), NSStringFromPoint( [NSEvent mouseLocation]));
copy();
}
}
isDragging = 0;
break;

case kCGEventLeftMouseDragged:
if (!isDragging)
initialLocation = [NSEvent mouseLocation];
isDragging = 1;
break;

Expand Down

0 comments on commit 0c481d9

Please sign in to comment.