Popup fit after resize

I have made a change to let the popup keep in view even after an image resize, my usecase is using a map full-screen on Android and a popup containing a FORM: when I press any text input the (sometimes huge) keyboard pops up and the screen real estate is 40% the size it was before and the popup is generally hidden by it. With this patch the popup keeps on screen (using the same logic it currently already does on appearing, only done also on resize).
I'm not sure this is a feature you'd want in general, but it was essential to me and, just in case, I'm opening this request to ease the import.
The second and third commit are because the solution used in the first lead to "bouncing" when the popup doesn't fit on screen.