Skip to content
Merged
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
2 changes: 2 additions & 0 deletions lib/matplotlib/backends/backend_macosx.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,8 @@ class FigureManagerMac(_macosx.FigureManager, FigureManagerBase):
"""
def __init__(self, canvas, num):
_macosx.FigureManager.__init__(self, canvas)
icon_path = str(cbook._get_data_path('images/matplotlib.pdf'))
_macosx.FigureManager.set_icon(icon_path)
FigureManagerBase.__init__(self, canvas, num)
if mpl.rcParams['toolbar'] == 'toolbar2':
self.toolbar = NavigationToolbar2Mac(canvas)
Expand Down
43 changes: 43 additions & 0 deletions src/_macosx.m
Original file line number Diff line number Diff line change
Expand Up @@ -697,6 +697,45 @@ static CGFloat _get_device_scale(CGContextRef cr)
Py_RETURN_NONE;
}

static PyObject*
FigureManager_set_icon(PyObject* null, PyObject* args) {
PyObject* icon_path;
if (!PyArg_ParseTuple(args, "O&", &PyUnicode_FSDecoder, &icon_path)) {
return NULL;
}
const char* icon_path_ptr = PyUnicode_AsUTF8(icon_path);
if (!icon_path_ptr) {
Py_DECREF(icon_path);
return NULL;
}
@autoreleasepool {
NSString* ns_icon_path = [NSString stringWithUTF8String: icon_path_ptr];
Py_DECREF(icon_path);
if (!ns_icon_path) {
PyErr_SetString(PyExc_RuntimeError, "Could not convert to NSString*");
return NULL;
}
NSImage* image = [[[NSImage alloc] initByReferencingFile: ns_icon_path] autorelease];
if (!image) {
PyErr_SetString(PyExc_RuntimeError, "Could not create NSImage*");
return NULL;
}
if (!image.valid) {
PyErr_SetString(PyExc_RuntimeError, "Image is not valid");
return NULL;
}
@try {
NSApplication* app = [NSApplication sharedApplication];
app.applicationIconImage = image;
}
@catch (NSException* exception) {
PyErr_SetString(PyExc_RuntimeError, exception.reason.UTF8String);
return NULL;
}
}
Py_RETURN_NONE;
}

static PyObject*
FigureManager_set_window_title(FigureManager* self,
PyObject *args, PyObject *kwds)
Expand Down Expand Up @@ -769,6 +808,10 @@ static CGFloat _get_device_scale(CGContextRef cr)
{"destroy",
(PyCFunction)FigureManager_destroy,
METH_NOARGS},
{"set_icon",
(PyCFunction)FigureManager_set_icon,
METH_STATIC | METH_VARARGS,
"Set application icon"},
{"set_window_title",
(PyCFunction)FigureManager_set_window_title,
METH_VARARGS},
Expand Down