@@ -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+
700739static PyObject*
701740FigureManager_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