@richard s solution seems nice and clean. Some time ago I implemented it myself here. Maybe it’s still usefull.