Is there a better way to sense active windows?

Continuing the discussion from Quick Look Touch Bar waveform view:

Saw that post but this is a bit more than whats asked there.

I’ve been wanting to hide BTT for very specific window focus conditions, such as whenever theres a dialog window (and you’d get the Cancel/OK in the TouchBar), when quicklook is frontmost, when the user clicks on the desktop but not any finder window or desktop file, among other things.

Is there a way to do this @Andreas_Hegenberg?