By cobbling together bits and pieces of various other answers from SO and elsewhere I have something that almost does what I want, but not quite: