Skip to content

Changes icon when using macosx backend #14927

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 3 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: 9 additions & 0 deletions lib/matplotlib/backends/backend_macos_common.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import sys

if sys.platform != "darwin":
def set_mac_icon(path):
pass
else:
from . import _macosx
def set_mac_icon(path):
_macosx.set_icon(path)
6 changes: 6 additions & 0 deletions lib/matplotlib/backends/backend_qt5.py
Original file line number Diff line number Diff line change
Expand Up @@ -540,6 +540,12 @@ def __init__(self, canvas, num):

self.window.setWindowTitle("Figure %d" % num)
image = str(cbook._get_data_path('images/matplotlib.svg'))

if sys.platform == "darwin":
from . import backend_macos_common
image = str(cbook._get_data_path('images/matplotlib.pdf'))
backend_macos_common.set_mac_icon(image)

self.window.setWindowIcon(QtGui.QIcon(image))

# Give the keyboard focus to the figure instead of the
Expand Down
140 changes: 139 additions & 1 deletion src/_macosx.m
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,132 @@ static int wait_for_stdin(void)
return 1;
}

PyObject* _get_pdf_icon_path(void)
{
PyObject* mpl = PyImport_ImportModule("matplotlib");
if (!mpl)
{
return NULL;
}

PyObject* get_data_path = PyObject_GetAttrString(mpl, "get_data_path");
if (!get_data_path)
{
Py_DECREF(mpl);
return NULL;
}

PyObject* arg = PyTuple_New(0);
if (!arg)
{
Py_DECREF(mpl);
Py_DECREF(get_data_path);
return NULL;
}

PyObject* path = PyObject_Call(get_data_path, arg, NULL);
if (!path)
{
Py_DECREF(mpl);
Py_DECREF(get_data_path);
Py_DECREF(arg);
return NULL;
}

PyObject* pdf_rel = PyUnicode_FromString("/images/matplotlib.pdf");
if (!pdf_rel)
{
Py_DECREF(mpl);
Py_DECREF(get_data_path);
Py_DECREF(arg);
Py_DECREF(path);
return NULL;
}

PyObject* pdf_absolute = PyUnicode_Concat(path, pdf_rel);

Py_DECREF(mpl);
Py_DECREF(get_data_path);
Py_DECREF(arg);
Py_DECREF(path);
Py_DECREF(pdf_rel);

return pdf_absolute;
}

static PyObject* set_icon(PyObject* ignored, PyObject* path)
{
if (!path)
{
PyErr_SetString(PyExc_RuntimeError, "No path passed");
return NULL;
}

Py_ssize_t size;
// API indicates Python owns path_string; we don't need to free it
const char* path_string = PyUnicode_AsUTF8AndSize(path, &size);
if (!path_string)
{
PyErr_SetString(PyExc_TypeError, "bad argument type, must be UTF-8 "
"path to icon");
return NULL;
}

NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];

NSImage* image;
@try
{
NSString* path_nsstring =
[[NSString alloc] initWithUTF8String: path_string];

image =
[[NSImage alloc] initByReferencingFile: path_nsstring];
}
@catch (NSException *e)
{

PyErr_SetString(PyExc_RuntimeError, "could not load icon");
[pool drain];
return NULL;
}

@try
{
NSApplication* app = [NSApplication sharedApplication];
app.applicationIconImage = image;
}
@catch (NSException *e)
{
PyErr_SetString(PyExc_RuntimeError, "could not set icon");
[pool drain];
return NULL;
}

[pool drain];
Py_RETURN_NONE;
}

static int _set_icon(void)
{
PyObject* pdf_path = _get_pdf_icon_path();
if (!pdf_path)
{
return -1;
}

PyObject* result = set_icon(NULL, pdf_path);
if (!result)
{
Py_DECREF(pdf_path);
return -1;
}

Py_DECREF(pdf_path);
Py_DECREF(result);
return 0;
}

/* ---------------------------- Cocoa classes ---------------------------- */

@interface WindowServerConnectionManager : NSObject
Expand Down Expand Up @@ -705,7 +831,7 @@ static CGFloat _get_device_scale(CGContextRef cr)
View* view;
const char* title;
PyObject* size;
int width, height;
int width, height, result;
PyObject* obj;
FigureCanvas* canvas;

Expand Down Expand Up @@ -739,6 +865,12 @@ static CGFloat _get_device_scale(CGContextRef cr)
rect.size.width = width;

NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];

if (_set_icon() != 0)
{
return -1;
}

self->window = [self->window initWithContentRect: rect
styleMask: NSTitledWindowMask
| NSClosableWindowMask
Expand All @@ -756,6 +888,7 @@ static CGFloat _get_device_scale(CGContextRef cr)
[window makeFirstResponder: view];
[[window contentView] addSubview: view];


[pool release];
return 0;
}
Expand Down Expand Up @@ -2625,6 +2758,11 @@ static bool verify_framework(void)
METH_VARARGS,
"Sets the active cursor."
},
{"set_icon",
set_icon,
METH_O,
"Sets the Dock icon in macOS"
},
{NULL, NULL, 0, NULL} /* sentinel */
};

Expand Down