8000 Set the app icon in the MacOsX manager · matplotlib/matplotlib@c5a1fcf · GitHub
[go: up one dir, main page]

Skip to content

Commit c5a1fcf

Browse files
committed
Set the app icon in the MacOsX manager
1 parent 9d73164 commit c5a1fcf

File tree

2 files changed

+41
-0
lines changed

2 files changed

+41
-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: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -697,6 +697,41 @@ static CGFloat _get_device_scale(CGContextRef cr)
697697
Py_RETURN_NONE;
698698
}
699699

700+
static PyObject*
701+
FigureManager_set_icon(PyObject* null, PyObject* args, PyObject* kwds) {
702+
PyObject* icon_path;
703+
static char* kwlist[3] = { "icon_path", NULL };
704+
if (!PyArg_ParseTupleAndKeywords(args, kwds, "O&", kwlist,
705+
&PyUnicode_FSDecoder, &icon_path)) {
706+
return NULL;
707+
}
708+
const char* icon_path_ptr = PyUnicode_AsUTF8(icon_path);
709+
if (!icon_path_ptr) {
710+
Py_DECREF(icon_path);
711+
return NULL;
712+
}
713+
@autoreleasepool {
714+
NSString* ns_icon_path = [NSString stringWithUTF8String: icon_path_ptr];
715+
Py_DECREF(icon_path);
716+
if (!ns_icon_path) {
717+
PyErr_SetString(PyExc_RuntimeError, "Could not convert to NSString*");
718+
return NULL;
719+
}
720+
NSImage* image = [[[NSImage alloc] initByReferencingFile: ns_icon_path] autorelease];
721+
if (!image) {
722+
PyErr_SetString(PyExc_RuntimeError, "Could not create NSImage*");
723+
return NULL;
724+
}
725+
if (!image.valid) {
726+
PyErr_SetString(PyExc_RuntimeError, "Image is not valid");
727+
return NULL;
728+
}
729+
NSApplication* app = [NSApplication sharedApplication];
730+
app.applicationIconImage = image;
731+
}
732+
Py_RETURN_NONE;
733+
}
734+
700735
static PyObject*
701736
FigureManager_set_window_title(FigureManager* self,
702737
PyObject *args, PyObject *kwds)
@@ -769,6 +804,10 @@ static CGFloat _get_device_scale(CGContextRef cr)
769804
{"destroy",
770805
(PyCFunction)FigureManager_destroy,
771806
METH_NOARGS},
807+
{"set_icon",
808+
(PyCFunction)FigureManager_set_icon,
809+
METH_STATIC | METH_VARARGS | METH_KEYWORDS,
810+
"Set application icon"},
772811
{"set_window_title",
773812
(PyCFunction)FigureManager_set_window_title,
774813
METH_VARARGS},

0 commit comments

Comments
 (0)
0