Skip to content

Commit b4fe3e1

Browse files
committed
Improve initial setting of device_scale on macosx backend (Fixes matplotlib#18213)
1 parent 591b857 commit b4fe3e1

1 file changed

Lines changed: 13 additions & 7 deletions

File tree

‎src/_macosx.m‎

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,7 @@ @interface View : NSView <NSWindowDelegate>
207207
}
208208
- (void)dealloc;
209209
- (void)drawRect:(NSRect)rect;
210+
- (void)updateScale:(double)scale;
210211
- (void)windowDidResize:(NSNotification*)notification;
211212
- (View*)initWithFrame:(NSRect)rect;
212213
- (void)setCanvas: (PyObject*)newCanvas;
@@ -680,6 +681,7 @@ static CGFloat _get_device_scale(CGContextRef cr)
680681
[window setDelegate: view];
681682
[window makeFirstResponder: view];
682683
[[window contentView] addSubview: view];
684+
[view updateScale: [window backingScaleFactor]];
683685

684686
return 0;
685687
}
@@ -1617,6 +1619,16 @@ static int _copy_agg_buffer(CGContextRef cr, PyObject *renderer)
16171619
return 0;
16181620
}
16191621

1622+
- (void)updateScale:(double)scale
1623+
{
1624+
if (device_scale != scale) {
1625+
device_scale = scale;
1626+
if (!PyObject_CallMethod(canvas, "_set_device_scale", "d", device_scale, NULL)) {
1627+
PyErr_Print();
1628+
}
1629+
}
1630+
}
1631+
16201632
-(void)drawRect:(NSRect)rect
16211633
{
16221634
PyObject* renderer = NULL;
@@ -1627,14 +1639,8 @@ -(void)drawRect:(NSRect)rect
16271639
CGContextRef cr = [[NSGraphicsContext currentContext] graphicsPort];
16281640

16291641
double new_device_scale = _get_device_scale(cr);
1642+
[self updateScale: new_device_scale];
16301643

1631-
if (device_scale != new_device_scale) {
1632-
device_scale = new_device_scale;
1633-
if (!PyObject_CallMethod(canvas, "_set_device_scale", "d", device_scale, NULL)) {
1634-
PyErr_Print();
1635-
goto exit;
1636-
}
1637-
}
16381644
if (!(renderer = PyObject_CallMethod(canvas, "_draw", "", NULL))
16391645
|| !(renderer_buffer = PyObject_GetAttrString(renderer, "_renderer"))) {
16401646
PyErr_Print();

0 commit comments

Comments
 (0)