popup: improve visibility checking algorithm