Don’t use actual modal components (big thing to avoid in plugins). See the popupmenu async stuff.
Also, have a look at FocusChangeListener class to watch for global focus changes. I think this should let you know when the DAW loses focus, etc.
Third, you should know this is a notoriously hard issue. DAWs can and do limit what mouse events get to the plugins, and can directly steal focus of course, etc:
