In engine, you can probably use
func _notification(what): if what == NOTIFICATION_WM_CLOSE_REQUEST: # Code to handle close request
or something like that.