Skip to content

Improve initial macosx device scale #18274

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 2 additions & 7 deletions lib/matplotlib/tests/test_backends_interactive.py
Original file line number Diff line number Diff line change
Expand Up @@ -154,13 +154,8 @@ def check_alt_backend(alt_backend):
@pytest.mark.parametrize("toolbar", ["toolbar2", "toolmanager"])
@pytest.mark.flaky(reruns=3)
def test_interactive_backend(backend, toolbar):
if backend == "macosx":
if toolbar == "toolmanager":
pytest.skip("toolmanager is not implemented for macosx.")
if toolbar == "toolbar2" and os.environ.get('TRAVIS'):
# See https://github.com/matplotlib/matplotlib/issues/18213
pytest.skip("toolbar2 for macosx is buggy on Travis.")

if backend == "macosx" and toolbar == "toolmanager":
pytest.skip("toolmanager is not implemented for macosx.")
proc = subprocess.run(
[sys.executable, "-c", _test_script,
json.dumps({"toolbar": toolbar})],
Expand Down
20 changes: 13 additions & 7 deletions src/_macosx.m
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,7 @@ @interface View : NSView <NSWindowDelegate>
}
- (void)dealloc;
- (void)drawRect:(NSRect)rect;
- (void)updateScale:(double)scale;
- (void)windowDidResize:(NSNotification*)notification;
- (View*)initWithFrame:(NSRect)rect;
- (void)setCanvas: (PyObject*)newCanvas;
Expand Down Expand Up @@ -680,6 +681,7 @@ static CGFloat _get_device_scale(CGContextRef cr)
[window setDelegate: view];
[window makeFirstResponder: view];
[[window contentView] addSubview: view];
[view updateScale: [window backingScaleFactor]];

return 0;
}
Expand Down Expand Up @@ -1617,6 +1619,16 @@ static int _copy_agg_buffer(CGContextRef cr, PyObject *renderer)
return 0;
}

- (void)updateScale:(double)scale
{
if (device_scale != scale) {
device_scale = scale;
if (!PyObject_CallMethod(canvas, "_set_device_scale", "d", device_scale, NULL)) {
PyErr_Print();
}
}
}

-(void)drawRect:(NSRect)rect
{
PyObject* renderer = NULL;
Expand All @@ -1627,14 +1639,8 @@ -(void)drawRect:(NSRect)rect
CGContextRef cr = [[NSGraphicsContext currentContext] graphicsPort];

double new_device_scale = _get_device_scale(cr);
[self updateScale: new_device_scale];

if (device_scale != new_device_scale) {
device_scale = new_device_scale;
if (!PyObject_CallMethod(canvas, "_set_device_scale", "d", device_scale, NULL)) {
PyErr_Print();
goto exit;
}
}
if (!(renderer = PyObject_CallMethod(canvas, "_draw", "", NULL))
|| !(renderer_buffer = PyObject_GetAttrString(renderer, "_renderer"))) {
PyErr_Print();
Expand Down