@@ -237,6 +237,8 @@ @interface View : NSView <NSWindowDelegate>
237
237
}
238
238
- (void )dealloc ;
239
239
- (void )drawRect : (NSRect )rect ;
240
+ - (void )updateDevicePixelRatio : (double )scale ;
241
+ - (void )windowDidChangeBackingProperties : (NSNotification *)notification ;
240
242
- (void )windowDidResize : (NSNotification *)notification ;
241
243
- (View*)initWithFrame : (NSRect )rect ;
242
244
- (void )setCanvas : (PyObject*)newCanvas ;
@@ -706,6 +708,7 @@ static CGFloat _get_device_scale(CGContextRef cr)
706
708
[window setDelegate: view];
707
709
[window makeFirstResponder: view];
708
710
[[window contentView ] addSubview: view];
711
+ [view updateDevicePixelRatio: [window backingScaleFactor ]];
709
712
710
713
return 0 ;
711
714
}
@@ -801,6 +804,9 @@ static CGFloat _get_device_scale(CGContextRef cr)
801
804
Window* window = self->window ;
802
805
if (window)
803
806
{
807
+ CGFloat device_pixel_ratio = [window backingScaleFactor ];
808
+ width /= device_pixel_ratio;
809
+ height /= device_pixel_ratio;
804
810
// 36 comes from hard-coded size of toolbar later in code
805
811
[window setContentSize: NSMakeSize (width, height + 36 .)];
806
812
}
@@ -1654,15 +1660,6 @@ -(void)drawRect:(NSRect)rect
1654
1660
1655
1661
CGContextRef cr = [[NSGraphicsContext currentContext ] CGContext ];
1656
1662
1657
- double new_device_scale = _get_device_scale (cr);
1658
-
1659
- if (device_scale != new_device_scale) {
1660
- device_scale = new_device_scale;
1661
- if (!PyObject_CallMethod (canvas, " _set_device_scale" , " d" , device_scale, NULL )) {
1662
- PyErr_Print ();
1663
- goto exit ;
1664
- }
1665
- }
1666
1663
if (!(renderer = PyObject_CallMethod (canvas, " _draw" , " " , NULL ))
1667
1664
|| !(renderer_buffer = PyObject_GetAttrString (renderer, " _renderer" ))) {
1668
1665
PyErr_Print ();
@@ -1683,6 +1680,33 @@ -(void)drawRect:(NSRect)rect
1683
1680
PyGILState_Release (gstate);
1684
1681
}
1685
1682
1683
+ - (void )updateDevicePixelRatio : (double )scale
1684
+ {
1685
+ PyObject* change = NULL ;
1686
+ PyGILState_STATE gstate = PyGILState_Ensure ();
1687
+
1688
+ device_scale = scale;
1689
+ if (!(change = PyObject_CallMethod (canvas, " _set_device_pixel_ratio" , " d" , device_scale, NULL ))) {
1690
+ PyErr_Print ();
1691
+ goto exit ;
1692
+ }
1693
+ if (PyObject_IsTrue (change)) {
1694
+ [self setNeedsDisplay: YES ];
1695
+ }
1696
+
1697
+ exit :
1698
+ Py_XDECREF (change);
1699
+
1700
+ PyGILState_Release (gstate);
1701
+ }
1702
+
1703
+ - (void )windowDidChangeBackingProperties : (NSNotification *)notification
1704
+ {
1705
+ Window* window = [notification object ];
1706
+
1707
+ [self updateDevicePixelRatio: [window backingScaleFactor ]];
1708
+ }
1709
+
1686
1710
- (void )windowDidResize : (NSNotification *)notification
1687
1711
{
1688
1712
int width, height;
0 commit comments