Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit b0f7b8a

Browse files
authored
Merge pull request #14930 from joelfrederico/macos-common-icon
Set Dock icon on the macosx backend
2 parents 7cc7858 + 8ba946b commit b0f7b8a

File tree

2 files changed

+45
-0
lines changed

2 files changed

+45
-0
lines changed

lib/matplotlib/backends/backend_macosx.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,8 @@ class FigureManagerMac(_macosx.FigureManager, FigureManagerBase):
6767
"""
6868
def __init__(self, canvas, num):
6969
_macosx.FigureManager.__init__(self, canvas)
70+
icon_path = str(cbook._get_data_path('images/matplotlib.pdf'))
71+
_macosx.FigureManager.set_icon(icon_path)
7072
FigureManagerBase.__init__(self, canvas, num)
7173
if mpl.rcParams['toolbar'] == 'toolbar2':
7274
self.toolbar = NavigationToolbar2Mac(canvas)

src/_macosx.m

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -697,6 +697,45 @@ static CGFloat _get_device_scale(CGContextRef cr)
697697
Py_RETURN_NONE;
698698
}
699699

700+
static PyObject*
701+
FigureManager_set_icon(PyObject* null, PyObject* args) {
702+
PyObject* icon_path;
703+
if (!PyArg_ParseTuple(args, "O&", &PyUnicode_FSDecoder, &icon_path)) {
704+
return NULL;
705+
}
706+
const char* icon_path_ptr = PyUnicode_AsUTF8(icon_path);
707+
if (!icon_path_ptr) {
708+
Py_DECREF(icon_path);
709+
return NULL;
710+
}
711+
@autoreleasepool {
712+
NSString* ns_icon_path = [NSString stringWithUTF8String: icon_path_ptr];
713+
Py_DECREF(icon_path);
714+
if (!ns_icon_path) {
715+
PyErr_SetString(PyExc_RuntimeError, "Could not convert to NSString*");
716+
return NULL;
717+
}
718+
NSImage* image = [[[NSImage alloc] initByReferencingFile: ns_icon_path] autorelease];
719+
if (!image) {
720+
PyErr_SetString(PyExc_RuntimeError, "Could not create NSImage*");
721+
return NULL;
722+
}
723+
if (!image.valid) {
724+
PyErr_SetString(PyExc_RuntimeError, "Image is not valid");
725+
return NULL;
726+
}
727+
@try {
728+
NSApplication* app = [NSApplication sharedApplication];
729+
app.applicationIconImage = image;
730+
}
731+
@catch (NSException* exception) {
732+
PyErr_SetString(PyExc_RuntimeError, exception.reason.UTF8String);
733+
return NULL;
734+
}
735+
}
736+
Py_RETURN_NONE;
737+
}
738+
700739
static PyObject*
701740
FigureManager_set_window_title(FigureManager* self,
702741
PyObject *args, PyObject *kwds)
@@ -769,6 +808,10 @@ static CGFloat _get_device_scale(CGContextRef cr)
769808
{"destroy",
770809
(PyCFunction)FigureManager_destroy,
771810
METH_NOARGS},
811+
{"set_icon",
812+
(PyCFunction)FigureManager_set_icon,
813+
METH_STATIC | METH_VARARGS,
814+
"Set application icon"},
772815
{"set_window_title",
773816
(PyCFunction)FigureManager_set_window_title,
774817
METH_VARARGS},

0 commit comments

Comments
 (0)