Add heuristic to set foreground window when current one is not visible
As Uebergame accidentally proved, it is possible to have a foreground window that is not visible, in which case our current heuristics won't replace that window when handling keyboard enter event. Add heuristic to take this into account.