Skip to content

Commit 79be27d

Browse files
committed
Fix scaling of mouse events
1 parent 742d698 commit 79be27d

File tree

1 file changed

+38
-26
lines changed

1 file changed

+38
-26
lines changed

src/_macosx.m

Lines changed: 38 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,7 @@ @interface View : NSView
231231
NSRect rubberband;
232232
BOOL inside;
233233
NSTrackingRectTag tracking;
234+
@public double device_scale;
234235
}
235236
- (void)dealloc;
236237
- (void)drawRect:(NSRect)rect;
@@ -407,6 +408,12 @@ static CGFloat _get_device_scale(CGContextRef cr)
407408
return NULL;
408409
}
409410
if(!PyArg_ParseTuple(args, "iiii", &x0, &y0, &x1, &y1)) return NULL;
411+
412+
x0 /= view->device_scale;
413+
x1 /= view->device_scale;
414+
y0 /= view->device_scale;
415+
y1 /= view->device_scale;
416+
410417
if (x1 > x0)
411418
{
412419
rubberband.origin.x = x0;
@@ -427,6 +434,7 @@ static CGFloat _get_device_scale(CGContextRef cr)
427434
rubberband.origin.y = y1;
428435
rubberband.size.height = y0 - y1;
429436
}
437+
430438
[view setRubberband: rubberband];
431439
Py_INCREF(Py_None);
432440
return Py_None;
@@ -2085,6 +2093,7 @@ - (View*)initWithFrame:(NSRect)rect
20852093
rubberband = NSZeroRect;
20862094
inside = false;
20872095
tracking = 0;
2096+
device_scale = 1;
20882097
return self;
20892098
}
20902099

@@ -2181,11 +2190,14 @@ -(void)drawRect:(NSRect)rect
21812190

21822191
CGContextRef cr = [[NSGraphicsContext currentContext] graphicsPort];
21832192

2184-
double device_scale = _get_device_scale(cr);
2193+
double new_device_scale = _get_device_scale(cr);
21852194

2186-
if (!PyObject_CallMethod(canvas, "_set_device_scale", "d", device_scale, NULL)) {
2187-
PyErr_Print();
2188-
goto exit;
2195+
if (device_scale != new_device_scale) {
2196+
device_scale = new_device_scale;
2197+
if (!PyObject_CallMethod(canvas, "_set_device_scale", "d", device_scale, NULL)) {
2198+
PyErr_Print();
2199+
goto exit;
2200+
}
21892201
}
21902202

21912203
renderer = PyObject_CallMethod(canvas, "_draw", "", NULL);
@@ -2330,8 +2342,8 @@ - (void)mouseDown:(NSEvent *)event
23302342
PyGILState_STATE gstate;
23312343
NSPoint location = [event locationInWindow];
23322344
location = [self convertPoint: location fromView: nil];
2333-
x = location.x;
2334-
y = location.y;
2345+
x = location.x * device_scale;
2346+
y = location.y * device_scale;
23352347
switch ([event type])
23362348
{ case NSLeftMouseDown:
23372349
{ unsigned int modifier = [event modifierFlags];
@@ -2374,8 +2386,8 @@ - (void)mouseUp:(NSEvent *)event
23742386
PyGILState_STATE gstate;
23752387
NSPoint location = [event locationInWindow];
23762388
location = [self convertPoint: location fromView: nil];
2377-
x = location.x;
2378-
y = location.y;
2389+
x = location.x * device_scale;
2390+
y = location.y * device_scale;
23792391
switch ([event type])
23802392
{ case NSLeftMouseUp:
23812393
num = 1;
@@ -2401,8 +2413,8 @@ - (void)mouseMoved:(NSEvent *)event
24012413
int x, y;
24022414
NSPoint location = [event locationInWindow];
24032415
location = [self convertPoint: location fromView: nil];
2404-
x = location.x;
2405-
y = location.y;
2416+
x = location.x * device_scale;
2417+
y = location.y * device_scale;
24062418
PyGILState_STATE gstate = PyGILState_Ensure();
24072419
PyObject* result = PyObject_CallMethod(canvas, "motion_notify_event", "ii", x, y);
24082420
if(result)
@@ -2418,8 +2430,8 @@ - (void)mouseDragged:(NSEvent *)event
24182430
int x, y;
24192431
NSPoint location = [event locationInWindow];
24202432
location = [self convertPoint: location fromView: nil];
2421-
x = location.x;
2422-
y = location.y;
2433+
x = location.x * device_scale;
2434+
y = location.y * device_scale;
24232435
PyGILState_STATE gstate = PyGILState_Ensure();
24242436
PyObject* result = PyObject_CallMethod(canvas, "motion_notify_event", "ii", x, y);
24252437
if(result)
@@ -2439,8 +2451,8 @@ - (void)rightMouseDown:(NSEvent *)event
24392451
PyGILState_STATE gstate;
24402452
NSPoint location = [event locationInWindow];
24412453
location = [self convertPoint: location fromView: nil];
2442-
x = location.x;
2443-
y = location.y;
2454+
x = location.x * device_scale;
2455+
y = location.y * device_scale;
24442456
gstate = PyGILState_Ensure();
24452457
if ([event clickCount] == 2) {
24462458
dblclick = 1;
@@ -2462,8 +2474,8 @@ - (void)rightMouseUp:(NSEvent *)event
24622474
PyGILState_STATE gstate;
24632475
NSPoint location = [event locationInWindow];
24642476
location = [self convertPoint: location fromView: nil];
2465-
x = location.x;
2466-
y = location.y;
2477+
x = location.x * device_scale;
2478+
y = location.y * device_scale;
24672479
gstate = PyGILState_Ensure();
24682480
result = PyObject_CallMethod(canvas, "button_release_event", "iii", x, y, num);
24692481
if(result)
@@ -2479,8 +2491,8 @@ - (void)rightMouseDragged:(NSEvent *)event
24792491
int x, y;
24802492
NSPoint location = [event locationInWindow];
24812493
location = [self convertPoint: location fromView: nil];
2482-
x = location.x;
2483-
y = location.y;
2494+
x = location.x * device_scale;
2495+
y = location.y * device_scale;
24842496
PyGILState_STATE gstate = PyGILState_Ensure();
24852497
PyObject* result = PyObject_CallMethod(canvas, "motion_notify_event", "ii", x, y);
24862498
if(result)
@@ -2500,8 +2512,8 @@ - (void)otherMouseDown:(NSEvent *)event
25002512
PyGILState_STATE gstate;
25012513
NSPoint location = [event locationInWindow];
25022514
location = [self convertPoint: location fromView: nil];
2503-
x = location.x;
2504-
y = location.y;
2515+
x = location.x * device_scale;
2516+
y = location.y * device_scale;
25052517
gstate = PyGILState_Ensure();
25062518
if ([event clickCount] == 2) {
25072519
dblclick = 1;
@@ -2523,8 +2535,8 @@ - (void)otherMouseUp:(NSEvent *)event
25232535
PyGILState_STATE gstate;
25242536
NSPoint location = [event locationInWindow];
25252537
location = [self convertPoint: location fromView: nil];
2526-
x = location.x;
2527-
y = location.y;
2538+
x = location.x * device_scale;
2539+
y = location.y * device_scale;
25282540
gstate = PyGILState_Ensure();
25292541
result = PyObject_CallMethod(canvas, "button_release_event", "iii", x, y, num);
25302542
if(result)
@@ -2540,8 +2552,8 @@ - (void)otherMouseDragged:(NSEvent *)event
25402552
int x, y;
25412553
NSPoint location = [event locationInWindow];
25422554
location = [self convertPoint: location fromView: nil];
2543-
x = location.x;
2544-
y = location.y;
2555+
x = location.x * device_scale;
2556+
y = location.y * device_scale;
25452557
PyGILState_STATE gstate = PyGILState_Ensure();
25462558
PyObject* result = PyObject_CallMethod(canvas, "motion_notify_event", "ii", x, y);
25472559
if(result)
@@ -2680,8 +2692,8 @@ - (void)scrollWheel:(NSEvent*)event
26802692
else return;
26812693
NSPoint location = [event locationInWindow];
26822694
NSPoint point = [self convertPoint: location fromView: nil];
2683-
int x = (int)round(point.x);
2684-
int y = (int)round(point.y - 1);
2695+
int x = (int)round(point.x * device_scale);
2696+
int y = (int)round(point.y * device_scale - 1);
26852697

26862698
PyObject* result;
26872699
PyGILState_STATE gstate = PyGILState_Ensure();

0 commit comments

Comments
 (0)